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

Uploaded Source

Built Distribution

crepr-0.4.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for crepr-0.4.0.tar.gz
Algorithm Hash digest
SHA256 9b9387628c00ee259d00a7ed59f31bb24cdb648dd076afdcf5beb7f0339ed08a
MD5 3e7d770f29fcc4575840eedaf6a1760b
BLAKE2b-256 17a82d1acb530754703af1f9aabfdb6ba453980e0e4e156597a391a8f2076d3c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for crepr-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 85b451c7f4eaa1777114c60db21c8ea9c8edfce0f59cec02754d76821e20498c
MD5 fef7d5532c95a82dd0e8df9c0d50f89b
BLAKE2b-256 059887b4ff98f2c645779b61d30e9bef2e2afc93de8e54fd2926e5db2e9e6c7b

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