coexist.utilities.autorepr#

coexist.utilities.autorepr(_c=None, *, short={}, hide={})[source]#

Automatically create a __repr__ method for pretty-printing class attributes; they are discovered at runtime following some rules.

  1. Attribute names do not start with underscores and are not callable.

  2. The attributes given in short (set[str] | bool) are printed up to 80 characters. If short == True, then all attributes are shortened.

  3. The attributes given in hide (set[str]) are skipped.

  4. If the attribute representation is multiline (i.e. has newlines) then it is printed on a separate line and indented with 2 spaces.

Examples

>>> @autorepr
>>> class SomeClass:
>>>     def __init__(self):
>>>         self.x = "spam"
>>>         self.y = "eggs"
>>>
>>> print(SomeClass())
SomeClass
---------
x = spam
y = eggs
>>> @autorepr(hide = {"x"})
>>> class SomeClass:
>>>     def __init__(self):
>>>         self.x = "spam"
>>>         self.y = "eggs"
>>>
>>> print(SomeClass())
SomeClass
---------
y = eggs