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.

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.

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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: crepr-0.5.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for crepr-0.5.0.tar.gz
Algorithm Hash digest
SHA256 e5aabe1ac20de8e06e95178932477c58caa65336226ff1e6d3656acd622fde06
MD5 23401a82c427f3a84862d0fe33280738
BLAKE2b-256 d90cf0c968462b214f0e1f265c9f3d85f4f0a54b95b0e18796024735d95a2a7e

See more details on using hashes here.

Provenance

File details

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

File metadata

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

File hashes

Hashes for crepr-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 870b8cc244975ced6e59bfa780f117679b49c66c32a28f52b6438009d3e6c57f
MD5 9e4a74055cbc95af4e8db6a4c3ae05cb
BLAKE2b-256 74dbaa1f73d29f46351f293276d4c58ae90b730c247afacc02abd7ce2d3331bb

See more details on using hashes here.

Provenance

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