Skip to main content

Auto __repr__ method generation.

Project description

reprit

In what follows

  • python is an alias for python3.5 or any later version (python3.6 and so on),
  • pypy is an alias for pypy3.5 or any later version (pypy3.6 and so on).

Installation

Install the latest pip & setuptools packages versions:

  • with CPython
    python -m pip install --upgrade pip setuptools
    
  • with PyPy
    pypy -m pip install --upgrade pip setuptools
    

User

Download and install the latest stable version from PyPI repository:

  • with CPython
    python -m pip install --upgrade reprit
    
  • with PyPy
    pypy -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 CPython
    python setup.py install
    
  • with PyPy
    pypy setup.py install
    

Usage

Let's suppose we are defining a class and we want to have __repr__, that:

  1. 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 sessions, logging, in failed test cases with randomly generated data, etc.
  2. 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 MyClass:
    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

>>> MyClass(range(10), 2, 3, keyword_only='some', a={'sample': 42}, b={1, 2})
MyClass(range(0, 10), 2, 3, keyword_only='some', a={'sample': 42}, b={1, 2})

Note: this method doesn't automatically handle changes during runtime (e.g. if someone deletes instance field or replaces MyClass.__init__ 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 --tag release

Bump version

bump2version --verbose --tag release

This will set version to major.minor.patch and add Git tag.

Notes

To avoid inconsistency between branches and pull requests, bumping version should be merged into master branch as separate pull request.

Running tests

Plain:

  • with CPython
    python setup.py test
    
  • with PyPy
    pypy setup.py test
    

Inside Docker container:

  • with CPython
    docker-compose --file docker-compose.cpython.yml up
    
  • with PyPy
    docker-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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reprit-0.0.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

reprit-0.0.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file reprit-0.0.0.tar.gz.

File metadata

  • Download URL: reprit-0.0.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4

File hashes

Hashes for reprit-0.0.0.tar.gz
Algorithm Hash digest
SHA256 4da2d3a7a9ff7e0c4c7abf58b1fd20b97525662edc494d49a52706cfef3c6b55
MD5 ec73782ae662ba17fbae6e1df2f9d212
BLAKE2b-256 b626ec453c47294bdce1fe737fef8cb0d3cf0366b5ed23cb912af82c508c24a5

See more details on using hashes here.

File details

Details for the file reprit-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: reprit-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.4

File hashes

Hashes for reprit-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 607e5055380599cababea06994a481c2f03e6c84c3e9ee110a93f384c5f3c422
MD5 317ed23808c56ef5088e200b6cbc0acd
BLAKE2b-256 e846cc8afbad3acc65c9f6866af488341bcc74c8c404fe68fc83563bedf06b35

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