Skip to main content

The package that allows you to create clear and informative methods for your own classes in Python.

Project description

pretty_repr

GitHub repo size PyPI version GitHub contributors GitHub stars GitHub forks GitHub licence

pretty_repr is a package that allows you to create clear and informative methods for your own classes in Python.

Prerequisites

Before you begin, ensure you have installed the latest version of Python.

Installing pretty_repr

To install pretty_repr, follow these steps:

Linux and macOS:

pip3 install pretty-repr

Windows:

pip install pretty-repr

Using pretty_repr

There are will be examples of how to use pretty_repr.

get_representation

  1. The get_representation function requires an instance of some class as parameter and returns its string representation:
>>> from pretty_repr import get_representation
>>> class A:
...     def __init__(self, a, b, c, d=0):
...         self.a = a
...         self.__b = b
...         self._c = c
...         self.d = d
...
>>> example_1 = A(1, 2, 3)
>>> print(get_representation(example_1))
A(a=1, b=2, _c=3, d=0)
  1. Run the following to exclude some parameters from the representation:
>>> print(get_representation(example_1, excluded={'a', '_c'}))
A(b=2, d=0)
  1. If a parameter is iterable is represented in accordance to its __repr__ method:
>>> import numpy as np
>>> example_2 = A((1, 2), [3, 4], {5, 6}, np.zeros(3))
>>> print(get_representation(example_2))
A(a=(1, 2), b=[3, 4], _c={5, 6}, d=array([0., 0., 0.]))
  1. Inheritor representation includes all parameters of the ancestor as well as its own parameters:
>>> from pretty_repr import get_representation
>>> class B(A):
...     def __init__(self, e, f, h=1, **kwargs):
...         super().__init__(**kwargs)
...         self.e = e
...         self.__f = f
...         self.h = h
...
>>> example_3 = B(a=1, b=2, c=3, e=4, f=5)
>>> print(get_representation(example_3))
B(a=1, b=2, _c=3, d=0, e=4, f=5, h=1)
  1. This representation includes private attributes of the ancestor. Run the following to exclude it from representation:
>>> example_3._A__b
2
>>> print(get_representation(example_3, ancestor_private_attributes=False))
B(a=1, _c=3, d=0, e=4, f=5, h=1)

RepresentableObject

The RepresentableObject class is the class which __repr__ method returns get_representation function result:

>>> from pretty_repr import RepresentableObject
>>> print(get_representation(RepresentableObject()))
RepresentableObject()
>>> print(repr(RepresentableObject()))
RepresentableObject()

You can use class inherited from RepresentableObject instead of __repr__ method implementation:

>>> class C(RepresentableObject):
...     def __init__(self, a, b, c, d=0):
...         self.a = a
...         self.__b = b
...         self._c = c
...         self.d = d
...
>>> example_1 = C(1, 2, 3)
>>> example_1
C(a=1, b=2, _c=3, d=0)

Define property excluded_attributes_for_repr to exclude some parameters from representation:

>>> class D(C):
...     def __init__(self, e, f, h=1, **kwargs):
...         super().__init__(**kwargs)
...         self.e = e
...         self.__f = f
...         self.h = h
...     @property
...     def excluded_attributes_for_repr(self):
...         return {'f', 'h'}
... 
>>> example_4 = D(a=1, b=2, c=3, e=4, f=5)
>>> example_4
D(a=1, b=2, _c=3, d=0, e=4)

Contributing to pretty_repr

To contribute to pretty_repr, follow these steps:

  1. Fork this repository.
  2. Create a branch: git checkout -b <branch_name>.
  3. Make your changes and commit them: git commit -m '<commit_message>'
  4. Push to the original branch: git push origin <project_name>/<location>
  5. Create the pull request.

Alternatively see the GitHub documentation on creating a pull request.

Contributors

Contact

If you want to contact me you can reach me at albertfarhutdinov@gmail.com.

License

This project uses the following license: MIT License.

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

pretty_repr-1.0.5.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

pretty_repr-1.0.5-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file pretty_repr-1.0.5.tar.gz.

File metadata

  • Download URL: pretty_repr-1.0.5.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for pretty_repr-1.0.5.tar.gz
Algorithm Hash digest
SHA256 9bfd8bca5fb0039755b9561b2b95bd43a2152dcc85b67eb737c1140ae0edbbf6
MD5 0be873017b5bbdc1801f5d870df18621
BLAKE2b-256 d964d633b8dfb02fd2d00dc14ebf6f8024dd96e8d4b23d3ce35b1958f196e0c3

See more details on using hashes here.

File details

Details for the file pretty_repr-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: pretty_repr-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for pretty_repr-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 79971605f43e881284a80428ef6ad3eec4253742e3f822a06c2f577016aade31
MD5 4396a221c5320dcd9430b687c6c428fd
BLAKE2b-256 35663858c99deb964709bf92dec0af3b2f4e96ee3da09ab65d2281f2b2a28a44

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page