Registry / serialization / cheap-repr

cheap-repr

JSON →
library0.5.2pypypi✓ verified 86d ago

cheap-repr is a Python library that provides a more efficient, configurable, and short string representation of objects, improving upon the standard library's `reprlib`. It offers an easy API to register custom representation functions for various classes. The current version is 0.5.2, released on August 10, 2024, and the project appears to be actively maintained.

pip install cheap-repr
INSTALL
IMPORT
SIG · CHEAP-REPR
C
cheap-repr
serializationpythonv0.5.2
Install
1.5s avg
Import
33ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.5.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.034s · 17.8MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.5s · import 0.033s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

cheap_repr
from cheap_repr import cheap_repr
register_repr
from cheap_repr import register_repr
basic_repr
from cheap_repr import basic_repr
Useful for registering a default-like object representation.
normal_repr
from cheap_repr import normal_repr
Use to ensure a class's own __repr__ is used without suppression.

This quickstart demonstrates how to use `cheap_repr` to get a condensed string representation of an object, including registering a custom repr function for your own classes using `@register_repr`. It also shows how `suppression_threshold` can be adjusted on the `cheap_repr` function itself.

from cheap_repr import cheap_repr, register_repr class MyClass: def __init__(self, items): self.items = items @register_repr(MyClass) def repr_my_class(x, helper): return helper.repr_iterable(x.items, 'MyClass([', '])') # Example usage with cheap_repr obj1 = MyClass(list(range(1000))) print(f"Custom repr: {cheap_repr(obj1)}") # Example with default cheap_repr behavior for a built-in list long_list = list(range(100)) print(f"List repr: {cheap_repr(long_list)}") # Adjusting suppression threshold on the function itself cheap_repr.suppression_threshold = 50 # Temporarily reduce threshold print(f"List repr (short threshold): {cheap_repr(long_list)}") cheap_repr.suppression_threshold = 300 # Reset to default # Using basic_repr for a class class AnotherClass: def __init__(self, value): self.value = value # Register basic_repr for AnotherClass register_repr(AnotherClass)(basic_repr) obj2 = AnotherClass(123) print(f"Basic repr: {cheap_repr(obj2)}")
Debug
Known issues
gotchaThe `suppression_threshold` and `max_level` configurations for `cheap_repr` are attributes on the `cheap_repr` *function object* itself, not module-level global variables. Changing `cheap_repr.suppression_threshold` or `cheap_repr.max_level` will affect all subsequent calls to that specific imported `cheap_repr` function, but might not be immediately obvious as a global setting.
fix
Access and modify `cheap_repr.suppression_threshold` or `cheap_repr.max_level` directly on the imported function object (e.g., `cheap_repr.suppression_threshold = 50`). Be aware that the `level` argument in a `cheap_repr()` call will override `max_level`.
affects: 0.3.0+
gotchaWhen using `cheap_repr`, it doesn't automatically detect and use a class's existing `__repr__` method if its output is long or if specific formatting is desired. It has its own internal logic for suppression and level management.
fix
If you want `cheap_repr` to respect a class's `__repr__` method, you need to explicitly register `normal_repr` for that class using `@register_repr(MyClass)(normal_repr)`. For custom formatting that integrates `cheap_repr`'s features (like level handling), define and register a custom `repr` function that utilizes the `helper` object.
affects: 0.3.0+
Errors
Common errors & fixes
NameError: name 'cheap_repr' is not defined
The `cheap_repr` function was used without being imported.
fix
Add `from cheap_repr import cheap_repr` at the beginning of your Python file.
My custom `__repr__` method is ignored or truncated when I use `cheap_repr`.
By default, `cheap_repr` applies its own length and depth limitations. It does not automatically prioritize or fully respect a class's `__repr__` method unless explicitly instructed or if the output is within its default limits.
fix
To make `cheap_repr` use your class's `__repr__` for specific types, register `normal_repr` for that class: `from cheap_repr import register_repr, normal_repr; @register_repr(MyClass)(normal_repr)`. For custom, length-aware representations, write and register a dedicated function: `from cheap_repr import register_repr; @register_repr(MyClass) def my_custom_repr(obj, helper): return helper.repr_iterable(obj.items, 'MyClass(', ')')`.
Changes to `cheap_repr.max_level` or `cheap_repr.suppression_threshold` are not consistently applied.
You might be modifying a local variable or a different instance of the `cheap_repr` function, or the `level` argument is explicitly passed to `cheap_repr()`, overriding `max_level`. The configurations are on the function object itself.
fix
Ensure you are modifying the attributes on the *imported* `cheap_repr` function directly (e.g., `from cheap_repr import cheap_repr; cheap_repr.max_level = 5`). Remember that an explicit `level` argument in `cheap_repr(obj, level=...)` will always take precedence over `cheap_repr.max_level`.
Upgrade
Version history
0.5.2latest on PyPI · released Aug 10, 2024
Audit
Dependencies
numpyrequiredRequired for certain features, possibly related to data structure representation.
pandasrequiredRequired for certain features, possibly related to data structure representation.
djangooptionalOptional, likely for integration or specific object representations within Django projects.
pytestoptionalOptional, likely for testing utilities or pytest integration.
Agent activity
13 hits · last 30 days
node
12
OpenAI (training)
1
Resources
cheap-repr — pip install cheap-repr · libregistry