Skip to content
/ crepr Public

Create a ❤️__repr__🤗️ for your python classes from the definition found in __init__

License

Notifications You must be signed in to change notification settings

cleder/crepr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

crepr

Create a __repr__ for your python classes.

A Python script that takes a file path as a command-line argument, imports the specified file, and creates a __repr__ method for each class defined in the module. It uses the definition found in the __init__ method of the class. 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

Install

pip install crepr

Usage

❯ crepr  --help
Usage: crepr [OPTIONS] COMMAND [ARGS]...

Options:
  --install-completion [bash|zsh|fish|powershell|pwsh]
                                  Install completion for the specified shell.
  --show-completion [bash|zsh|fish|powershell|pwsh]
                                  Show completion for the specified shell, to
                                  copy it or customize the installation.
  --help                          Show this message and exit.

Commands:
  add     Add __repr__ to all classes in the source code.
  remove  Remove the __repr__ method from all classes in the source code.

Add

The command crepr add ... adds a __repr__ method to all classes in this file, that have a __init__ method with no positional only arguments.

❯ crepr add  --help
Usage: crepr add [OPTIONS] FILES...

  Add __repr__ to all classes in the source code.

Arguments:
  FILES...  The python source file(s)  [required]

Options:
  --kwarg-splat TEXT  The **kwarg splat  [default: ...]
  --diff / --inline   Display the diff / Apply changes to the file(s)
  --help              Show this message and exit.

Remove

The command crepr remove ... removes the __repr__ methods from all classes that have an __init__ method with no positional only arguments.

❯ crepr remove  --help
Usage: crepr remove [OPTIONS] FILES...

  Remove the __repr__ method from all classes in the source code.

Arguments:
  FILES...  The python source file(s)  [required]

Options:
  --diff / --inline  Display the diff / Apply changes to the file(s)
  --help             Show this message and exit.

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:

About

Create a ❤️__repr__🤗️ for your python classes from the definition found in __init__

Resources

License

Stars

Watchers

Forks

Packages

No packages published