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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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