API Reference¶
Module easyrepr¶
- easyrepr.easyrepr(wrapped=None, **kwargs)[source]¶
Decorator for an automatic
__repr__method.- Parameters
wrapped – the function to wrap
See
descriptor.EasyReprfor 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
NoneorEllipsis. Default isTrue.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:
An iterable containing zero or more of any of the following:
The style of the repr string returned is determined by the
styleparameter, 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 bystyle.call_style():"Klass(foo=1, bar=2)""<>"— use the “angle” style, defined bystyle.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,
keyis either formatted as itself, if it’s astr, or else asrepr(key), and is separated fromvalueby an equal sign (“=”).valueis always formatted asrepr(value).