https://github.com/razerm/represent
Create __repr__ automatically or declaratively.
https://github.com/razerm/represent
Last synced: 6 months ago
JSON representation
Create __repr__ automatically or declaratively.
- Host: GitHub
- URL: https://github.com/razerm/represent
- Owner: RazerM
- License: other
- Created: 2015-03-24T01:36:29.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2024-11-14T19:58:30.000Z (7 months ago)
- Last Synced: 2024-12-10T08:42:26.694Z (6 months ago)
- Language: Python
- Homepage:
- Size: 152 KB
- Stars: 12
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
## Represent
[![PyPI Version][ppi]][ppl] [![Documentation][di]][dl] [![CI Status][gai]][gal] [![Coverage][cvi]][cvl] [![Python Version][pvi]][pvl] [![MIT License][mli]][mll]
[ppi]: https://img.shields.io/pypi/v/represent.svg?style=flat-square
[ppl]: https://pypi.python.org/pypi/represent/
[pvi]: https://img.shields.io/badge/python-2.7%2C%203-brightgreen.svg?style=flat-square
[pvl]: https://www.python.org/downloads/
[mli]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square
[mll]: https://raw.githubusercontent.com/RazerM/represent/master/LICENSE
[di]: https://img.shields.io/badge/docs-latest-brightgreen.svg?style=flat-square
[dl]: https://represent.readthedocs.io/en/latest/
[gai]: https://github.com/RazerM/represent/workflows/CI/badge.svg?branch=master
[gal]: https://github.com/RazerM/represent/actions?workflow=CI
[cvi]: https://img.shields.io/codecov/c/github/RazerM/represent/master.svg?style=flat-square
[cvl]: https://codecov.io/github/RazerM/represent?branch=master### Installation
```bash
$ pip install represent
```### Automatic Generation
```python
from represent import autorepr@autorepr
class Rectangle:
def __init__(self, name, color, width, height):
self.name = name
self.color = color
self.width = width
self.height = heightrect = Rectangle('Timothy', 'red', 15, 4.5)
print(rect)
``````
Rectangle(name='Timothy', color='red', width=15, height=4.5)
```### Declarative Generation
```python
from represent import ReprHelperMixinclass 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 * milesdef _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)
```