reprshed 1.0.6

Creator: railscoder56

Last updated:

Add to Cart

Description:

reprshed 1.0.6

A toolshed for writing great __repr__ methods quickly and easily.
Replace boilerplate like this:
@reprlib.recursive_repr('<...>')
def __repr__(self):
return f'{type(self).__name__}({self._foo!r}, bar={self._bar!r})'
with just this:
def __repr__(self):
return reprshed.pure(self, self._foo, bar=self._bar)

Versioning
This library’s version numbers follow the SemVer 2.0.0
specification.


Installation
pip install reprshed


Usage
Import:
import reprshed
For a “pure” class (a class whose exact state can be recreated purely
by passing the right constructor arguments), use reprshed.pure.
For an “impure” class (a class whose exact state is not reproducible),
use reprshed.impure.
Examples!
>>> class MyClass:
... def __init__(self, foo, bar, qux):
... self._foo = foo
... self._bar = bar
... self._qux = qux
... def __repr__(self):
... return reprshed.pure(self, self._foo, self._bar, self._qux)
The first argument to reprshed.pure must be self, because
reprshed uses that to get the class name. All other arguments
are formatted as arguments to that class constructor:
>>> MyClass('foo', 'bar', qux='qux')
MyClass('foo', 'bar', 'qux')
Formatting the repr as constructor keyword arguments is easy too:
>>> class MyClass:
... def __init__(self, foo, bar):
... self._foo = foo
... self._bar = bar
... def __repr__(self):
... return reprshed.pure(self, foo=self._foo, bar=self._bar)
...
>>> MyClass(1, bar='qux')
MyClass(foo=1, bar='qux')
You also get recursion protection automatically on all
common Python implementations:
>>> class MyClass:
... def __init__(self, number, foo):
... self.number = number
... self.foo = foo
... def __repr__(self):
... return reprshed.pure(self, self.number, foo=self.foo)
...
>>> my_instance = MyClass(1, 'whatever')
>>> my_instance
MyClass(1, foo='whatever')
>>> my_instance.foo = my_instance
>>> my_instance
MyClass(1, foo=<...>)
>>> my_instance.foo = MyClass(2, [None, my_instance, 3, '4'])
>>> my_instance
MyClass(1, foo=MyClass(2, foo=[None, <...>, 3, '4']))
Using reprshed.impure is the same, only the output format changes:
>>> class MyClass:
... def __repr__(self):
... return reprshed.impure(self, 1234, 'foo', bar=0, qux='qux')
...
>>> MyClass()
<MyClass 1234 'foo' bar=0 qux='qux'>
If you need to take more manual control, you can use reprshed.raw:
>>> class MyClass:
... def __repr__(self):
... return reprshed.impure(self, foo=5, bar=reprshed.raw('qux()'))
...
>>> MyClass()
<MyClass foo=5 bar=qux()>
By passing reprshed.raw as a positional argument, you can get
arbitrary formatting inside the repr if you really need to:
>>> class MyClass:
... def __repr__(self):
... return reprshed.impure(self, reprshed.raw('a b () c,{d,e}'))
...
>>> MyClass()
<MyClass a b () c,{d,e}>
You can even use reprshed.raw to override the class name:
>>> class MyClass:
... def __repr__(self):
... return reprshed.impure(reprshed.raw('fake name'))
...
>>> MyClass()
<fake name>

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.