Create __repr__ automatically or declaratively.
Project description
Installation
$ pip install represent
Automatic Generation
from represent import ReprMixin
class Rectangle(ReprMixin, object):
def __init__(self, name, color, width, height):
self.name = name
self.color = color
self.width = width
self.height = height
super(Rectangle, self).__init__()
rect = Rectangle('Timothy', 'red', 15, 4.5)
print(rect)
Rectangle(name='Timothy', color='red', width=15, height=4.5)
Declarative Generation
from represent import ReprHelperMixin
class ContrivedExample(ReprHelperMixin, object):
def __init__(self, description, radians, shape, color, miles):
self.description = description
self.degrees = radians * 180 / 3.141592654
self.shape = shape
self._color = color
self.km = 1.60934 * miles
def _repr_helper_(self, r):
r.positional_from_attr('description')
r.positional_with_value(self.degrees * 3.141592654 / 180)
r.keyword_from_attr('shape')
r.keyword_from_attr('color', '_color')
r.keyword_with_value('miles', self.km / 1.60934)
ce = ContrivedExample('does something', 0.345, 'square', 'red', 22)
print(ce)
from IPython.lib.pretty import pprint
pprint(ce)
ContrivedExample('does something', 0.345, shape='square', color='red', miles=22.0) ContrivedExample('does something', 0.345, shape='square', color='red', miles=22.0)
For pretty printer support and more information, visit the documentation!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Represent-1.4.1.tar.gz
(19.7 kB
view hashes)
Built Distribution
Close
Hashes for Represent-1.4.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 492baf937696ea3757202e1dd81f8c1c413a6e20a820e606065a025395b9d5ce |
|
MD5 | 41f73ad308a0511f43b932fcc0333911 |
|
BLAKE2b-256 | 10690f0d36cdd1128619b774ad56a418b255629b7690eb293af24ee40803916b |