API Reference

Module easyrepr

easyrepr.easyrepr(wrapped=None, **kwargs)[source]

Decorator for an automatic __repr__ method.

Parameters

wrapped – the function to wrap

See descriptor.EasyRepr for a full description of the accepted keyword parameters.

This decorator wraps a function (which is available as __wrapped__). The wrapped function should return a description of the attributes that should be included in the repr.

>>> class UseEasyRepr:
...     def __init__(self, foo, bar):
...         self.foo = foo
...         self.bar = bar
...     @easyrepr
...     def __repr__(self):
...         ...
...
>>> x = UseEasyRepr(1, 2)
>>> repr(x)
'UseEasyRepr(foo=1, bar=2)'

This function may be called with all arguments up-front (wrapped function and keyword arguments)

easyrepr(fn, style="<>")

or the wrapped function may be provided in a second call

easyrepr(style="<>")(fn)

to make it easier to use this function as a decorator.

Module easyrepr.descriptor

class easyrepr.descriptor.EasyRepr(wrapped, *, override=False, skip_private=True, style=None)[source]

Descriptor for an automatic __repr__ method.

Parameters
  • wrapped – the function to wrap

  • override – completely replace ancestor methods rather than concatenating to them. Default is False.

  • skip_private – skip private attributes — i.e., those whose names start with an underscore (“_”) — when finding attributes for None or Ellipsis. Default is True.

  • style – the style to use. Default is None.

Variables

__wrapped__ – the wrapped function

This descriptor wraps a function (which is available as __wrapped__). The wrapped function should return a description of the attributes that should be included in the repr.

Valid attribute descriptions include:

  • None — include all attributes of the instance (via vars)

    Note

    A function whose body is pass or Ellipsis (...) implicitly returns None.

  • An iterable containing zero or more of any of the following:

    • str — include the attribute with the given name

    • (key, value) — include a virtual attribute

    • (value,) — include a nameless virtual attribute

    • Ellipsis (...) — include all attributes of the instance (via vars())

The style of the repr string returned is determined by the style parameter, which may be one of:

  • None — inherit style from super class, or else default to “call” style. (This is the default.)

  • "()" — use the “call” style, defined by style.call_style():

    "Klass(foo=1, bar=2)"
    
  • "<>" — use the “angle” style, defined by style.angle_style():

    "<Klass foo=1 bar=2>"
    
  • Callable — use a user-defined style function, which should accept three parameters: the object instance, the computed class name, and an iterable of attributes, which may be either (key, value) or (value,), as described above.

Module easyrepr.style

easyrepr.style.angle_style(instance, class_name, attributes)[source]

Style function for an angular repr in the style of object.

Parameters
  • instance – the object whose repr is being formatted

  • klass_name – the class name that should be displayed

  • attributes – the sequence of attribute tuples

Returns

the styled repr string

>>> angle_style(obj, "Klass", [("foo", 1), ("bar", 2)])
'<Klass foo=1 bar=2>'
easyrepr.style.call_style(instance, class_name, attributes)[source]

Style function for an angular repr in the style of a constructor call.

Parameters
  • instance – the object whose repr is being formatted

  • klass_name – the class name that should be displayed

  • attributes – the sequence of attribute tuples

Returns

the styled repr string

>>> call_style(obj, "Klass", [("foo", 1), ("bar", 2)])
'Klass(foo=1, bar=2)'
easyrepr.style.format_attribute(attribute)[source]

Format a tuple describing an attribute.

Parameters

attribute – attribute tuple, which may be either (key, value) or (value,).

Returns

the formatted string

If given, key is either formatted as itself, if it’s a str, or else as repr(key), and is separated from value by an equal sign (“=”). value is always formatted as repr(value).