Package: pretty-repr
pretty_reprpretty_repris a package that allows you to create clear and informative methods for your own classes in Python.PrerequisitesBefore you begin, ensure you have installed the latest version of Python.Installingpretty_reprTo installpretty_repr, follow these steps:Linux and macOS:pip3 install pretty-reprWindows:pip install pretty-reprUsingpretty_reprThere are will be examples of how to usepretty_repr.get_representationTheget_representationfunction 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)Run the following to exclude some parameters from the representation:>>> print(get_representation(example_1, excluded={'a', '_c'}))
A(b=2, d=0)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.]))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)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)RepresentableObjectTheRepresentableObjectclass is the class which__repr__method returnsget_representationfunction result:>>> from pretty_repr import RepresentableObject
>>> print(get_representation(RepresentableObject()))
RepresentableObject()
>>> print(repr(RepresentableObject()))
RepresentableObject()You can use class inherited fromRepresentableObjectinstead 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 propertyexcluded_attributes_for_reprto 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 topretty_reprTo contribute topretty_repr, follow these steps:Fork this repository.Create a branch:git checkout -b <branch_name>.Make your changes and commit them:git commit -m '<commit_message>'Push to the original branch:git push origin <project_name>/<location>Create the pull request.Alternatively see the GitHub documentation oncreating a pull request.Contributors@AlbertFarkhutdinovContactIf you want to contact me you can reach me atalbertfarhutdinov@gmail.com.LicenseThis project uses the following license:MIT License.