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.
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:
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
48cfb22a937ba8b8ef114d60e211628035eb379a50c4d0c8653dfffff91aa561
|
|
| MD5 |
787c07d84cdcd3619c9340ed065ec984
|
|
| BLAKE2b-256 |
ea2952007be897dbdda5be8b4ea58a3ef914e2bc6505257dc9d969adbecf6987
|
Provenance
The following attestation bundles were made for crepr-0.6.0.tar.gz:
Publisher:
run-all-tests.yml on cleder/crepr
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
crepr-0.6.0.tar.gz -
Subject digest:
48cfb22a937ba8b8ef114d60e211628035eb379a50c4d0c8653dfffff91aa561 - Sigstore transparency entry: 575546770
- Sigstore integration time:
-
Permalink:
cleder/crepr@f0648ac0e3448c0ff2a0aa55c0acaa5f5c521ae6 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/cleder
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
run-all-tests.yml@f0648ac0e3448c0ff2a0aa55c0acaa5f5c521ae6 -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4d968ca7680d04fadc0292cdecdf7d528ef44b3ed8d6fcde00fa1ba17c82b60
|
|
| MD5 |
42fb581076b977fb2806ab196cd5f096
|
|
| BLAKE2b-256 |
0722c7e8b04dd8e257b9232b95359abf0b1403f5ed1c343d3e9a0320e85ee073
|
Provenance
The following attestation bundles were made for crepr-0.6.0-py3-none-any.whl:
Publisher:
run-all-tests.yml on cleder/crepr
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
crepr-0.6.0-py3-none-any.whl -
Subject digest:
c4d968ca7680d04fadc0292cdecdf7d528ef44b3ed8d6fcde00fa1ba17c82b60 - Sigstore transparency entry: 575546772
- Sigstore integration time:
-
Permalink:
cleder/crepr@f0648ac0e3448c0ff2a0aa55c0acaa5f5c521ae6 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/cleder
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
run-all-tests.yml@f0648ac0e3448c0ff2a0aa55c0acaa5f5c521ae6 -
Trigger Event:
push
-
Statement type: