Cheap Repr
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.
Common errors
-
NameError: name 'cheap_repr' is not defined
cause The `cheap_repr` function was used without being imported.fixAdd `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`.
cause 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.fixTo 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.
cause 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.fixEnsure 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`.
Warnings
- gotcha The `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.
- gotcha When 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.
Install
-
pip install cheap-repr
Imports
- cheap_repr
from cheap_repr import cheap_repr
- register_repr
from cheap_repr import register_repr
- basic_repr
from cheap_repr import basic_repr
- normal_repr
from cheap_repr import normal_repr
Quickstart
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)}")