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.2.tar.gz (144.4 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.2-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: readable_reprs-0.1.2.tar.gz
  • Upload date:
  • Size: 144.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","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.2.tar.gz
Algorithm Hash digest
SHA256 a4e0d532ddf04a8209cbcf517c49cfe2f9daf30b6b3d7c643e692912bf64355c
MD5 e34f6ea0e7deba5c6a82744d966c670f
BLAKE2b-256 9ac7ebe6b21b84b52987e20f7adcc971e6c3c8c96e0f32aec5b03dade9da512f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: readable_reprs-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4d5a348ddd3c8eda37b26919631f5806707d693ddb8c9e8b552b3e074b5864c8
MD5 50e1b8911bc796e0b1abf82dc42a3e15
BLAKE2b-256 697161c9ea36c18ef2d0fbf81fded1d5e2b521400f12c42d9ead2184a98138a9

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