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.0.tar.gz (142.8 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.0-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: readable_reprs-0.1.0.tar.gz
  • Upload date:
  • Size: 142.8 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.0.tar.gz
Algorithm Hash digest
SHA256 88059b79e72d89f476b9f98f43f37fa946cc556892670dd302a86235b3c4e036
MD5 5b26f71385f26dbac52e6c921d42f009
BLAKE2b-256 9e65c67795015876f34caafb79af3e4aa3ec2084af7294d66c268b563b5ec084

See more details on using hashes here.

File details

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

File metadata

  • Download URL: readable_reprs-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.2 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9dd74ed1c30e12149e94f7edee432b285e0d2dc6d5ac2544fdbb5c68a8127bf3
MD5 acc1f613a0f6775f83271019446330b8
BLAKE2b-256 c7f8026d7de1c921f785bdf0014801e371fed32467ed4955cbbe4fc708b7662e

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