{"id":7074,"library":"cheap-repr","title":"Cheap Repr","description":"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.","status":"active","version":"0.5.2","language":"en","source_language":"en","source_url":"https://github.com/alexmojaki/cheap_repr","tags":["repr","representation","debugging","utility","reprlib"],"install":[{"cmd":"pip install cheap-repr","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required for certain features, possibly related to data structure representation.","package":"numpy","optional":false},{"reason":"Required for certain features, possibly related to data structure representation.","package":"pandas","optional":false},{"reason":"Optional, likely for integration or specific object representations within Django projects.","package":"django","optional":true},{"reason":"Optional, likely for testing utilities or pytest integration.","package":"pytest","optional":true}],"imports":[{"symbol":"cheap_repr","correct":"from cheap_repr import cheap_repr"},{"symbol":"register_repr","correct":"from cheap_repr import register_repr"},{"note":"Useful for registering a default-like object representation.","symbol":"basic_repr","correct":"from cheap_repr import basic_repr"},{"note":"Use to ensure a class's own __repr__ is used without suppression.","symbol":"normal_repr","correct":"from cheap_repr import normal_repr"}],"quickstart":{"code":"from cheap_repr import cheap_repr, register_repr\n\nclass MyClass:\n    def __init__(self, items):\n        self.items = items\n\n@register_repr(MyClass)\ndef repr_my_class(x, helper):\n    return helper.repr_iterable(x.items, 'MyClass([', '])')\n\n\n# Example usage with cheap_repr\nobj1 = MyClass(list(range(1000)))\nprint(f\"Custom repr: {cheap_repr(obj1)}\")\n\n# Example with default cheap_repr behavior for a built-in list\nlong_list = list(range(100))\nprint(f\"List repr: {cheap_repr(long_list)}\")\n\n# Adjusting suppression threshold on the function itself\ncheap_repr.suppression_threshold = 50 # Temporarily reduce threshold\nprint(f\"List repr (short threshold): {cheap_repr(long_list)}\")\ncheap_repr.suppression_threshold = 300 # Reset to default\n\n# Using basic_repr for a class\nclass AnotherClass:\n    def __init__(self, value):\n        self.value = value\n\n# Register basic_repr for AnotherClass\nregister_repr(AnotherClass)(basic_repr)\nobj2 = AnotherClass(123)\nprint(f\"Basic repr: {cheap_repr(obj2)}\")\n","lang":"python","description":"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."},"warnings":[{"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`.","message":"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.","severity":"gotcha","affected_versions":"0.3.0+"},{"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.","message":"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.","severity":"gotcha","affected_versions":"0.3.0+"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Add `from cheap_repr import cheap_repr` at the beginning of your Python file.","cause":"The `cheap_repr` function was used without being imported.","error":"NameError: name 'cheap_repr' is not defined"},{"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(', ')')`.","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.","error":"My custom `__repr__` method is ignored or truncated when I use `cheap_repr`."},{"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`.","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.","error":"Changes to `cheap_repr.max_level` or `cheap_repr.suppression_threshold` are not consistently applied."}]}