represent.helper#

Note

Names in this module should be imported from represent. They are in represent.helper for structural reasons.

class represent.helper.BaseReprHelper(other)[source]#
abstract positional_from_attr(attr_name)[source]#

Add positional argument by retrieving attribute attr_name

Parameters:

attr_name (str) – Attribute name such that getattr(self, attr_name) returns the correct value.

abstract positional_with_value(value, raw=False)[source]#

Add positional argument with value value

Parameters:
  • value – Value for positional argument.

  • raw (bool) – If false (default), repr(value) is used. Otherwise, the value is used as is.

abstract keyword_from_attr(name, attr_name=None)[source]#

Add keyword argument from attribute attr_name

Parameters:
  • name (str) – Keyword name. Also used as attribute name such that getattr(self, name) returns the correct value.

  • attr_name (str) – Attribute name, if different than name.

Changed in version 1.4: Method argument names swapped, didn’t make sense before.

abstract keyword_with_value(name, value, raw=False)[source]#

Add keyword argument name with value value.

Parameters:
  • name (str) – Keyword name.

  • value – Value for keyword argument.

  • raw (bool) – If false (default), repr(value) is used. Otherwise, the value is used as is.

class represent.helper.ReprHelper(other)[source]#

Bases: BaseReprHelper

Help manual construction of __repr__.

It should be used as follows:

def __repr__(self)
    r = ReprHelper(self)
    r.keyword_from_attr('name')
    return str(r)

Changed in version 1.4: parantheses property added. Must be set before str(r) is called:

def __repr__(self)
    r = ReprHelper(self)
    r.parantheses = ('<', '>')
    r.keyword_from_attr('name')
    return str(r)
positional_from_attr(attr_name)[source]#

Add positional argument by retrieving attribute attr_name

Parameters:

attr_name (str) – Attribute name such that getattr(self, attr_name) returns the correct value.

positional_with_value(value, raw=False)[source]#

Add positional argument with value value

Parameters:
  • value – Value for positional argument.

  • raw (bool) – If false (default), repr(value) is used. Otherwise, the value is used as is.

keyword_from_attr(name, attr_name=None)[source]#

Add keyword argument from attribute attr_name

Parameters:
  • name (str) – Keyword name. Also used as attribute name such that getattr(self, name) returns the correct value.

  • attr_name (str) – Attribute name, if different than name.

Changed in version 1.4: Method argument names swapped, didn’t make sense before.

keyword_with_value(name, value, raw=False)[source]#

Add keyword argument name with value value.

Parameters:
  • name (str) – Keyword name.

  • value – Value for keyword argument.

  • raw (bool) – If false (default), repr(value) is used. Otherwise, the value is used as is.

class represent.helper.PrettyReprHelper(other, p, cycle)[source]#

Bases: BaseReprHelper

Help manual construction of _repr_pretty_ for IPython.lib.pretty.

It should be used as follows:

def _repr_pretty_(self, p, cycle)
    with PrettyReprHelper(self, p, cycle) as r:
        r.keyword_from_attr('name')

Changed in version 1.4: parantheses property added. Must be set before PrettyReprHelper.open() is called (usually by context manager).

def _repr_pretty_(self, p, cycle)
    r = PrettyReprHelper(self, p, cycle)
    r.parantheses = ('<', '>')
    with r:
        r.keyword_from_attr('name')
positional_from_attr(attr_name)[source]#

Add positional argument by retrieving attribute attr_name

Parameters:

attr_name (str) – Attribute name such that getattr(self, attr_name) returns the correct value.

positional_with_value(value, raw=False)[source]#

Add positional argument with value value

Parameters:
  • value – Value for positional argument.

  • raw (bool) – If false (default), repr(value) is used. Otherwise, the value is used as is.

keyword_from_attr(name, attr_name=None)[source]#

Add keyword argument from attribute attr_name

Parameters:
  • name (str) – Keyword name. Also used as attribute name such that getattr(self, name) returns the correct value.

  • attr_name (str) – Attribute name, if different than name.

Changed in version 1.4: Method argument names swapped, didn’t make sense before.

keyword_with_value(name, value, raw=False)[source]#

Add keyword argument name with value value.

Parameters:
  • name (str) – Keyword name.

  • value – Value for keyword argument.

  • raw (bool) – If false (default), repr(value) is used. Otherwise, the value is used as is.

open()[source]#

Open group with class name.

This is normally called by using as a context manager.

close()[source]#

Close group with final bracket.

This is normally called by using as a context manager.

class represent.helper.RichReprHelper(other)[source]#

Bases: BaseReprHelper

Help manual construction of __rich_repr__ for rich.pretty.

It should be used as follows:

def __rich_repr__(self)
    r = RichReprHelper(self)
    r.keyword_from_attr('name')
    yield from r
positional_from_attr(attr_name)[source]#

Add positional argument by retrieving attribute attr_name

Parameters:

attr_name (str) – Attribute name such that getattr(self, attr_name) returns the correct value.

positional_with_value(value, raw=False)[source]#

Add positional argument with value value

Parameters:
  • value – Value for positional argument.

  • raw (bool) – If false (default), repr(value) is used. Otherwise, the value is used as is.

keyword_from_attr(name, attr_name=None)[source]#

Add keyword argument from attribute attr_name

Parameters:
  • name (str) – Keyword name. Also used as attribute name such that getattr(self, name) returns the correct value.

  • attr_name (str) – Attribute name, if different than name.

Changed in version 1.4: Method argument names swapped, didn’t make sense before.

keyword_with_value(name, value, raw=False)[source]#

Add keyword argument name with value value.

Parameters:
  • name (str) – Keyword name.

  • value – Value for keyword argument.

  • raw (bool) – If false (default), repr(value) is used. Otherwise, the value is used as is.