https://github.com/furechan/lazyrepr
Python mixin class for concise object representation
https://github.com/furechan/lazyrepr
Last synced: 19 days ago
JSON representation
Python mixin class for concise object representation
- Host: GitHub
- URL: https://github.com/furechan/lazyrepr
- Owner: furechan
- License: mit
- Created: 2024-01-10T18:26:13.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-16T16:06:48.000Z (about 2 months ago)
- Last Synced: 2025-03-26T17:42:13.103Z (about 1 month ago)
- Language: Python
- Size: 71.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Python utilities for concise object representation
This library provides utilities to implement the `__repr__`
and ipython `_repr_pretty_` methods to represent objects in a concise manner.
The implementation is based on the class `__init__` method signature,
and will differentiate between positional and optional parameters
in order to make the representation as succinct as possible.These methods are intended to be used on simple classes that map all their constructor arguments to attributes. Also note that `dataclasses` already implement a similar `__repr__` method by default.
## Usage
To add a basic representation to a class you can overrides its `__repr__` method with `lazy_repr` as follows:
```python
from lazyrepr import lazy_reprclass MyClass:
__repr__ = lazy_repr
...
```To provide both `__repr__` and ipython `_repr_pretty_` for a class just inherit from `ReprMixin`
```python
from lazyrepr import ReprMixinclass MyList(ReprMixin):
def __init__(self, item=()):
self.items = list(items)lst = MyList(range(3))
print(lst) # >>> MyList([0, 1, 2])
```## Examples
Examples notebooks are in the `extras` folder.
## Installation
You can install this package with `pip`.
```console
pip install lazyrepr
```## Related Projects and Resources
- [easyrepr](https://github.com/chrisbouchard/easyrepr)
Python decorator to automatically generate repr strings