Skip to main content

Make stdlib reprs evaluatable

Project description

readable-reprs

Patch the reprs of existing types so that the string representation is a valid Python expression.

This is really useful for REPL-like work and debugging because it allows you to log the program state and copy-paste it into another Python program for experimentation. This is especially useful for when you have large nested objects.

Please file an issue if you notice anything else in the Python standard library that should have a readable repr.

Version Support

Python versions 3.8 and above are supported and tested in CI. Lower versions may work too.

Example Usage

Let's model Alice having two pets, a dog and a cat:

from dataclasses import dataclass
from enum import Enum, auto


class Animal(Enum):
    DOG = auto()
    CAT = auto()
    BIRD = auto()


@dataclass
class Human:
    name: str
    pets: list[Animal]


if __name__ == '__main__':
    human = Human(name='Alice', pets=[])
    human.pets.append(Animal.DOG)
    human.pets.append(Animal.CAT)
    print(human)

Printing out Alice yields this string:

Human(name='Alice', pets=[<Animal.DOG: 1>, <Animal.CAT: 2>])

Which isn't particularly nice because it contains angled brackets, which aren't valid Python code.

Adding a patch_reprs call to the start of your program fixes this:

from readable_reprs import patch_reprs

patch_reprs()

# ... as before ...
Human(name='Alice', pets=[Animal.DOG, Animal.CAT])

You can use this string as a valid Python expression to recreate the object in another Python program.

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

readable_reprs-0.1.1.tar.gz (144.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

readable_reprs-0.1.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file readable_reprs-0.1.1.tar.gz.

File metadata

  • Download URL: readable_reprs-0.1.1.tar.gz
  • Upload date:
  • Size: 144.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for readable_reprs-0.1.1.tar.gz
Algorithm Hash digest
SHA256 91ba90c08731d83fbb7ae316673e98c13c7f7119d625c1a8c2914caa7b094f7c
MD5 a75445aba31665c472acf98b5f7d9c7c
BLAKE2b-256 20cfa8c8ce9dba57d699443b5c5807bb4bf88fd2e9fd13752732bd23ae935665

See more details on using hashes here.

File details

Details for the file readable_reprs-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: readable_reprs-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for readable_reprs-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7a0aaffa398d5f8c964cb8808467f81f54216c7fd8333143cb8460bbee777e89
MD5 ac5ca500ecc64aebfb5ba476d7c81a28
BLAKE2b-256 17d63ea857486b9180dbdb407fdbd486710fb8f76ec1bb24429d92fd713e3d33

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page