Skip to main content

crepr

Create a __repr__ for your python classes.

crepr is a Python script that takes a file name as a command-line argument, imports the specified module, and then adds or removes a __repr__ method for each class defined in the module. It uses the definition found in the __init__ method of the class to create a useful representation of the object. 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

Features

  • Automatically generates __repr__ methods for all classes in a Python file
  • Uses the __init__ method's arguments to create a meaningful representation
  • Can add or remove __repr__ methods
  • Provides options to display the diff or apply changes directly to the file

Install

pip install crepr

Usage

To add a __repr__ method to all classes in a file:

crepr add <file_name> [--kwarg-splat "{}"] [--diff/--inline]

To remove the __repr__ method from all classes in a file:

crepr remove <file_name> [--diff/--inline]

Options

  • <file_name>: The name of the Python file to process.
  • --kwarg-splat: The string to use for the **kwargs splat (default: "{}").
  • --diff: Display the diff of the changes.
  • --inline: Apply the changes directly to the file.
  • --ignore-existing: Add __repr__ regardless if one exists

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:

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.6.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

crepr-0.6.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: crepr-0.6.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for crepr-0.6.0.tar.gz
Algorithm Hash digest
SHA256 48cfb22a937ba8b8ef114d60e211628035eb379a50c4d0c8653dfffff91aa561
MD5 787c07d84cdcd3619c9340ed065ec984
BLAKE2b-256 ea2952007be897dbdda5be8b4ea58a3ef914e2bc6505257dc9d969adbecf6987

See more details on using hashes here.

Provenance

The following attestation bundles were made for crepr-0.6.0.tar.gz:

Publisher: run-all-tests.yml on cleder/crepr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: crepr-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for crepr-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c4d968ca7680d04fadc0292cdecdf7d528ef44b3ed8d6fcde00fa1ba17c82b60
MD5 42fb581076b977fb2806ab196cd5f096
BLAKE2b-256 0722c7e8b04dd8e257b9232b95359abf0b1403f5ed1c343d3e9a0320e85ee073

See more details on using hashes here.

Provenance

The following attestation bundles were made for crepr-0.6.0-py3-none-any.whl:

Publisher: run-all-tests.yml on cleder/crepr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 files

0.5.0

2 files

0.4.0

2 files

0.3.0

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page