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.

A Python script that takes a file path as a command-line argument, imports the specified file, and then prints 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 like crêpe, the French pancake.

Tests codecov

Install

pip install crepr

Usage

❯ crepr --help
Usage: crepr [OPTIONS] FILE_PATH

  Create a __repr__ method for each class of a python file.

Arguments:
  FILE_PATH  [required]

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

For a class with

    def __init__(
        self,
        ns: Optional[str] = None,
        href: Optional[str] = None,
        rel: Optional[str] = None,
        type: Optional[str] = None,
        hreflang: Optional[str] = None,
        title: Optional[str] = None,
        length: Optional[int] = None,
    ) -> None:

it will create

    def __repr__(self) -> str:
        return (
            f"{self.__class__.__name__}("
            f"ns={self.ns!r}, "
            f"href={self.href!r}, "
            f"rel={self.rel!r}, "
            f"type={self.type!r}, "
            f"hreflang={self.hreflang!r}, "
            f"title={self.title!r}, "
            f"length={self.length!r}, "
            ")"
        )

Example

Given the file tests/kw_only_test.py with the contents:

class KwOnly:
    """The happy path class."""

    def __init__(self, name: str, *, age: int) -> None:
        """Initialize the class."""
        self.name = name  # pragma: no cover
        self.age = age  # pragma: no cover

It will produce:

❯ crepr tests/kw_only_test.py
class KwOnly:
    """The happy path class."""

    def __init__(self, name: str, *, age: int) -> None:
        """Initialize the class."""
        self.name = name  # pragma: no cover
        self.age = age  # pragma: no cover

    def __repr__(self) -> str:
        """Create a string (c)representation of the class."""
        return (f'{self.__class__.__name__}('
            f'name={self.name!r}, '
            f'age={self.age!r}, '
        ')')

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

Uploaded Source

Built Distribution

crepr-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: crepr-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for crepr-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d795d214603ee5917d333b0793cbb0f149b6b238fa2a60f920ec287230dcced7
MD5 be121fe563a4adb916fd39837aad0950
BLAKE2b-256 8bcb1f331d5d4dd3999c99a01197aadc523edbf94ed34130bf9eec28823723f7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: crepr-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for crepr-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2e48f7876ccea63b00f234c1c059e49275532380ff03b913fa8a4287c5a65b1
MD5 3e53a2ba40caf44a45999a0899173168
BLAKE2b-256 fc28d9a50e05c6f8e9a4f6acbe204de1a9b79717eb191f6b4166ec2a3e82681c

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