A toolshed for writing great ``__repr__`` methods quickly and easily.
Project description
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>
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file reprshed-1.0.6.tar.gz
.
File metadata
- Download URL: reprshed-1.0.6.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6381a081a268b7a958c35dd029c7d82447613af6c993f41095613ef2f93c01c2 |
|
MD5 | 072edf4abf1df39a20dcfbba92e7e2a1 |
|
BLAKE2b-256 | 01aca0106f7f0363ede6afb3e4ef1b01fdb70ff94b61411bf70042674793c4d6 |
File details
Details for the file reprshed-1.0.6-py38-none-any.whl
.
File metadata
- Download URL: reprshed-1.0.6-py38-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3.8
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8049e900c85c9695dc8ce8c7d3c57a65d4e512ed1b61d851967dd2041b13a24 |
|
MD5 | 681bedf09576cd996aadda713afab1e6 |
|
BLAKE2b-256 | 6cf07393e094166c0d83c6ab705612a414774f4d898721ad2d801481d8a2929d |
File details
Details for the file reprshed-1.0.6-py2.py30-none-any.whl
.
File metadata
- Download URL: reprshed-1.0.6-py2.py30-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 2, Python 3.0
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d6ec45c19366ead32eead81434326cebca576217fc112dc31cde5d1dfbd5e49 |
|
MD5 | acbb0f7858f4793bb355cf6a53e8954d |
|
BLAKE2b-256 | b1194633ca9f331a96fc07411d415892a808a8ae5d56a62abcf72beb494ccb06 |