Auto __repr__ method generation.
Project description
reprit
In what follows
pythonis an alias forpython3.5or any later version (python3.6and so on),pypyis an alias forpypy3.5or any later version (pypy3.6and so on).
Installation
Install the latest pip & setuptools packages versions:
- with
CPythonpython -m pip install --upgrade pip setuptools
- with
PyPypypy -m pip install --upgrade pip setuptools
User
Download and install the latest stable version from PyPI repository:
- with
CPythonpython -m pip install --upgrade reprit
- with
PyPypypy -m pip install --upgrade reprit
Developer
Download the latest version from GitHub repository
git clone https://github.com/lycantropos/reprit.git
cd reprit
Install:
- with
CPythonpython setup.py install
- with
PyPypypy setup.py install
Usage
Let's suppose we are defining a class and we want to have __repr__, that:
- Includes parameters involved in instance creation. For simple cases it should be possible to copy string & paste in some place (e.g. REPL session) and have similar object definition with as less work as possible. This helps a lot during debugging, logging, in failed test cases with randomly generated data, etc.
- In case of signature change method should handle this automatically for simple cases like renaming/removing/changing order of parameters.
This can be done like
>>> from reprit.base import generate_repr
>>> class DummyContainer:
... def __init__(self, positional, *variadic_positional, keyword_only, **variadic_keyword):
... self.positional = positional
... self.variadic_positional = variadic_positional
... self.keyword_only = keyword_only
... self.variadic_keyword = variadic_keyword
... __repr__ = generate_repr(__init__)
after that
>>> DummyContainer(range(10), 2, 3, keyword_only='some', a={'sample': 42})
DummyContainer(range(0, 10), 2, 3, keyword_only='some', a={'sample': 42})
or for a class with avoidance of built-in names clash & private'ish attributes & both
>>> from reprit import seekers
>>> from reprit.base import generate_repr
>>> class State:
... def __init__(self, id_, name, zip_):
... self.id = id_
... self._name = name
... self._zip = zip_
... __repr__ = generate_repr(__init__,
... field_seeker=seekers.complex_)
after that
>>> State(1, 'Alabama', 36016)
State(1, 'Alabama', 36016)
Note: this method doesn't automatically handle changes during runtime
(e.g. if someone deletes instance field
or replaces __init__/__new__ method implementation),
in this case user should update __repr__ method as well.
Development
Bumping version
Preparation
Install bump2version.
Pre-release
Choose which version number category to bump following semver specification.
Test bumping version
bump2version --dry-run --verbose $CATEGORY
where $CATEGORY is the target version number category name, possible
values are patch/minor/major.
Bump version
bump2version --verbose $CATEGORY
This will set version to major.minor.patch-alpha.
Release
Test bumping version
bump2version --dry-run --verbose release
Bump version
bump2version --verbose release
This will set version to major.minor.patch.
Running tests
Install dependencies:
- with
CPythonpython -m pip install --force-reinstall -r requirements-tests.txt
- with
PyPypypy -m pip install --force-reinstall -r requirements-tests.txt
Plain:
pytest
Inside Docker container:
- with
CPythondocker-compose --file docker-compose.cpython.yml up
- with
PyPydocker-compose --file docker-compose.pypy.yml up
Bash script (e.g. can be used in Git hooks):
-
with
CPython./run-tests.sh
or
./run-tests.sh cpython -
with
PyPy./run-tests.sh pypy
PowerShell script (e.g. can be used in Git hooks):
- with
CPython.\run-tests.ps1
or.\run-tests.ps1 cpython
- with
PyPy.\run-tests.ps1 pypy
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file reprit-0.2.0.tar.gz.
File metadata
- Download URL: reprit-0.2.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.5.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e473b90d7e1b9bbb69f4b354e7d5a2e861290e559a0ed0ed301a5d8217a3baf9
|
|
| MD5 |
3369c5f8eda8100c39d9f4934da1b48b
|
|
| BLAKE2b-256 |
83bae53f48e506430a194dfbbc4e868358ab5c264148d9f13cc0fb4403676f9e
|
File details
Details for the file reprit-0.2.0-py3-none-any.whl.
File metadata
- Download URL: reprit-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.5.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3fe8a09f73a0128bc03dae311f786eecdf179b816e938e14b937b47a4ae9de6
|
|
| MD5 |
fb03a95a549dbec51b83cdc746da725a
|
|
| BLAKE2b-256 |
2b87af108da4489d320c4c4882d0c66be1d7209ca16cae1be96efb4a4b5684da
|