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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file readable_reprs-0.1.1rc1.tar.gz.
File metadata
- Download URL: readable_reprs-0.1.1rc1.tar.gz
- Upload date:
- Size: 144.1 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
73b3cf60a99afd6f7a526a50646c15d0423442eeb986bc3ee54cc3ec97495a59
|
|
| MD5 |
b66e983316d2060707997ebd88129684
|
|
| BLAKE2b-256 |
4963c257e210d1e25332d814308b3299f227bcb7b4698d9b750b0ac13b0aa30a
|
File details
Details for the file readable_reprs-0.1.1rc1-py3-none-any.whl.
File metadata
- Download URL: readable_reprs-0.1.1rc1-py3-none-any.whl
- Upload date:
- Size: 4.0 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b460a107a76912b41b71e82833075901b13e674ceb3beb4e809d3cc5201f1bff
|
|
| MD5 |
c97a67846dc054bcd546929a52060599
|
|
| BLAKE2b-256 |
6eb66a522faf359774e83977c4ad3aed53601aeab63994fd0284d74556b380ed
|