https://github.com/cleder/crepr
Create a ❤️__repr__🤗️ for your python classes from the definition found in __init__
https://github.com/cleder/crepr
code-generation hacktoberfest
Last synced: about 1 year ago
JSON representation
Create a ❤️__repr__🤗️ for your python classes from the definition found in __init__
- Host: GitHub
- URL: https://github.com/cleder/crepr
- Owner: cleder
- License: mit
- Created: 2023-11-01T19:37:42.000Z (over 2 years ago)
- Default Branch: develop
- Last Pushed: 2025-04-08T09:11:30.000Z (about 1 year ago)
- Last Synced: 2025-04-08T10:24:19.016Z (about 1 year ago)
- Topics: code-generation, hacktoberfest
- Language: Python
- Homepage:
- Size: 245 KB
- Stars: 6
- Watchers: 2
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
](https://dev.to/ldrscke/love-your-representation-27mm) for the rationale of this package.
[](https://github.com/cleder/crepr/actions/workflows/run-all-tests.yml)
[](https://codecov.io/gh/cleder/crepr)
[
](https://github.com/pre-commit/pre-commit)
[
](http://mypy-lang.org/)
[
](https://github.com/psf/black)
[](https://opensource.org/license/mit/)
[](https://www.python.org/)
[](https://pypi.org/project/crepr/)
[](https://pypi.org/project/crepr/)
## 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
```bash
pip install crepr
```
## Usage
To add a `__repr__` method to all classes in a file:
```bash
crepr add [--kwarg-splat "..."] [--diff/--inline]
```
To remove the `__repr__` method from all classes in a file:
```bash
crepr remove [--diff/--inline]
```
### Options
* ``: 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:
```python
class KwOnly:
def __init__(self, name: str, *, age: int) -> None:
self.name = name
self.age = age
```
The command:
```bash
❯ crepr add tests/classes/kw_only_test.py
```
produces
```python
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:
```python
>>> 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:
```bash
❯ crepr add tests/classes/kw_only_test.py --inline
```
Give your representations some love.
❤️`.__repr__(self) -> str:`