Skip to main content

Create string representations of classes

Build Status Code style: black License Pypi Version Updates

This module contains a class used to generate __repr__ methods for your classes. This can be done either by inheriting from the SimpleRepr class, or by creating a __repr__ function, and returing SimpleRepr.make_repr(self)

Using the SimpleRepr class

Inheritance

The easiest way to use this class is to inherit from it, as this saves you from defining a __repr__ method altogether.

from simple_repr import SimpleRepr


class User(SimpleRepr):
    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age


user = User('John', 25)
print(user)

>>> "User(args[name='John', age=25])"

Inheriting from SimpleRepr allows for the inclusion of class constants.

from simple_repr import SimpleRepr


class User(SimpleRepr):

    COMPANY = "Some Company"

    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age


user = User('John', 25)
print(user)

>>> "User(consts=[COMPANY='Some Company'], args[name='John', age=25])"

Defining a function

In case you don't want to inherit from the SimpleRepr class, or are already inheriting from another class, you can create a __repr__ function and return the staticmethod make_repr from the module.

from simple_repr import SimpleRepr


class User:
    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age

    def __repr__(self) -> str:
        return SimpleRepr().make_repr(self)


user = User('John', 25)
print(user)

>>> "User(args=[name='John', age=25])"

The downside to defining your own function is that class constants are no longer accessible, and therefore will not be included in the repr.

from simple_repr import SimpleRepr


class User:

    COMPANY = "Some Company"

    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age

    def __repr__(self) -> str:
        return SimpleRepr().make_repr(self)


user = User('John', 25)
print(user)

>>> "User(args[name='John', age=25])"

Creating an environment

Create a virtual development environment by using the virtualenv Python library. You can install this by executing pip install virtualenv.

To create your environment, type virtualenv venv --prompt "(your-env) ". Once created, you can activate it by using source venv/bin/activate. Once you are done developing, simply type deactivate in your terminal.

Installation

Using the library

  • Install this library by running pip install simple-repr.
  • Import the library by adding from simple_repr import SimpleRepr to your code.

Developing for this library

  • Install the Python libraries required by running pip install -r dev-requirements.txt.
  • Before making any commits, ensure that all nox sessions are passing.

Please ensure to create your environment before you execute any of the installation commands

Release files for simple-repr 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simple-repr 1.3.0
File Size Uploaded
simple_repr-1.3.0.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for simple-repr 1.3.0
File Interpreter ABI Platform
simple_repr-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.5 kB

Release files / simple_repr-1.3.0.tar.gz

Download URL simple_repr-1.3.0.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4747c4f82696dbfe0ba1f0f63a5e4ec93266155da7780e4485bbc7a8cd2ff980
BLAKE2b-256 checksum
How to use checksums
71832fc2c9b8df886bc08602ee20548ae0d814b13216466282a420aa8308684d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

Release files / simple_repr-1.3.0-py3-none-any.whl

Download URL simple_repr-1.3.0-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9dc6aac538fe75d6dd3b1ef228f5cad60ea19ed83798883c586e74972a66fd53
BLAKE2b-256 checksum
How to use checksums
3a2bc1f135ae6f0260c24935adcfb2f9debfc0a451d62743f21e65982a264da0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.10

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page