Skip to main content

Create a __repr__ for your python classes from the definition found in __init__

Project description

crepr

Create a __repr__ for your python classes.

A Python script that takes a file path as a command-line argument, imports the specified file, and creates a __repr__ method for each class defined in the module. It uses the definition found in the __init__ method of the class. It is pronounced /kɹeɪpr/, like 🇳🇿 crêpe.

Have a look at the blog-post Love Your Representation for the rationale of this package.

Tests codecov pre-commit MyPy Black MIT License Python Version PyPI - Version Status

Install

pip install crepr

Usage

 crepr  --help
Usage: crepr [OPTIONS] COMMAND [ARGS]...

Options:
  --install-completion [bash|zsh|fish|powershell|pwsh]
                                  Install completion for the specified shell.
  --show-completion [bash|zsh|fish|powershell|pwsh]
                                  Show completion for the specified shell, to
                                  copy it or customize the installation.
  --help                          Show this message and exit.

Commands:
  add     Add __repr__ to all classes in the source code.
  remove  Remove the __repr__ method from all classes in the source code.

Add

The command crepr add ... adds a __repr__ method to all classes in this file, that have a __init__ method with no positional only arguments.

 crepr add  --help
Usage: crepr add [OPTIONS] FILES...

  Add __repr__ to all classes in the source code.

Arguments:
  FILES...  The python source file(s)  [required]

Options:
  --kwarg-splat TEXT  The **kwarg splat  [default: ...]
  --diff / --inline   Display the diff / Apply changes to the file(s)
  --help              Show this message and exit.

Remove

The command crepr remove ... removes the __repr__ methods from all classes that have an __init__ method with no positional only arguments.

 crepr remove  --help
Usage: crepr remove [OPTIONS] FILES...

  Remove the __repr__ method from all classes in the source code.

Arguments:
  FILES...  The python source file(s)  [required]

Options:
  --diff / --inline  Display the diff / Apply changes to the file(s)
  --help             Show this message and exit.

Example

Given the file tests/classes/kw_only_test.py with the contents:

class KwOnly:
    def __init__(self, name: str, *, age: int) -> None:
        self.name = name
        self.age = age

The command:

 crepr add tests/classes/kw_only_test.py

produces

class KwOnly:
    def __init__(self, name: str, *, age: int) -> None:
        self.name = name
        self.age = age

    def __repr__(self) -> str:
        """Create a string (c)representation for KwOnly."""
        return (f'{self.__class__.__module__}.{self.__class__.__name__}('
            f'name={self.name!r}, '
            f'age={self.age!r}, '
        ')')

The repr() of an instance of this class will be:

>>> from tests.classes.kw_only_test import KwOnly
>>> kwo = KwOnly('Christian', age=25)
>>> kwo
tests.classes.kw_only_test.KwOnly(name='Christian', age=25, )

Apply the changes to the file with:

 crepr add tests/classes/kw_only_test.py --inline

Give your representations some love.

❤️.__repr__(self) -> str:

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

crepr-0.3.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

crepr-0.3.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file crepr-0.3.0.tar.gz.

File metadata

  • Download URL: crepr-0.3.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for crepr-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c60966f76d6ac9d648963d864f7eae839a5cd0d7439bc2bdc815ecc5388ddf71
MD5 a57151715e24c43996af7d34ada54b8f
BLAKE2b-256 10fbbf3fc2068aa8c36445a3abbf13f1cff49853a005b71a4c7623414773b6e7

See more details on using hashes here.

File details

Details for the file crepr-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: crepr-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for crepr-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ecdc7e60c6a7d851e8043df9a6a6ff1aa9dbf317eabe97fa6db78057c8e625c8
MD5 b46d9d9d26cb70c9fd6b988927663947
BLAKE2b-256 ebefe45182389629c38d63318f7e67f7087d64f4e68ad9ea0c74ea1fc45ff77b

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