Skip to main content

Budget-controlled repr for Python objects.

Project description

reprobate 🖨️

Budget-controlled repr for Python objects.

Renders any Python object into a string that fits within a character budget. Nested structures degrade gracefully: full values, then type stubs, then counts. Zero dependencies. Pluggable via a type registry and a __budget_repr__ protocol.

Features

  • Hard budget guarantee -- output is always <= budget characters
  • Three-phase degradation -- full render, then name=<type(len)> stubs, then ...N more counts
  • Greedy and even policies -- prioritize depth (first fields in detail) or breadth (all fields equally)
  • Cycle detection -- circular references render as <...> instead of stack overflows
  • Type registry -- @register(MyType) for custom budget-aware renderers
  • Protocol method -- __budget_repr__(self, budget) on any class
  • Optional extensions -- arrow, numpy, pandas, pil, polars, pydantic renderers (guarded imports, zero cost if absent)

Install

pip install reprobate

Zero dependencies. Optional renderers activate automatically when their libraries are already installed (numpy, pandas, polars, pyarrow, Pillow, pydantic).

Quick example

import reprobate

reprobate.render({"name": "alice", "scores": [98, 87, 95, 72, 88]}, 60)
# "{'name': 'alice', 'scores': [98, 87, 95, 72, 88]}"

reprobate.render({"name": "alice", "scores": [98, 87, 95, 72, 88]}, 30)
# "{'name': 'alice', ...1 more}"

reprobate.render(list(range(1000)), 40)
# "[0, 1, 2, 3, ...996 more]"

Policies

from dataclasses import dataclass

@dataclass
class Agent:
    desc: str = "A very long description that eats the budget"
    important_note: str = "critical info here"
    status: str = "running"
    config: dict = None
    history: list = None

# Greedy: first fields get full detail
reprobate.render(agent, 100, policy="greedy")
# "Agent(desc='A very long description that eats the budget', important_note=<str(18)>, ...3 more)"

# Even: all fields get comparable detail
reprobate.render(agent, 100, policy="even")
# "Agent(desc='A very long...', important_note='critical info...', status='running', ...2 more)"

Custom renderers

Register a renderer for any type:

@reprobate.register(MyType)
def render_my_type(obj: MyType, budget: int) -> str:
    return f"MyType({obj.key})"[:budget]

Or implement the protocol directly:

class MyType:
    def __budget_repr__(self, budget: int) -> str:
        return f"MyType({self.key})"[:budget]

For renderers that recurse into child objects, use render_child (inherits policy and cycle detection) and render_attrs (standard TypeName(key=val, ...) pattern):

from reprobate import register, render_child, render_attrs

@register(MyContainer)
def render_my_container(obj: MyContainer, budget: int) -> str:
    # render_child for recursive rendering
    inner = render_child(obj.value, budget - 10)
    return f"MyContainer({inner})"

@register(MyModel)
def render_my_model(obj: MyModel, budget: int) -> str:
    # render_attrs for the standard object pattern
    attrs = {"name": obj.name, "data": obj.data}
    return render_attrs(attrs, "MyModel", budget)

Supported types

Category Types Behavior
Primitives None, bool, int, float repr(), truncated with ... if needed
Strings str, bytes Quoted, truncated with ... preserving quotes
Containers list, tuple, set, frozenset Head items + ...N more, tail peek when budget allows
Dicts dict Key-value pairs + ...N more
Collections deque, defaultdict, Counter Type-aware wrappers (factory name, most-common order)
Structured dataclass, namedtuple Field-aware decomposition, respects repr=False
Objects anything with __dict__ Attribute decomposition, public attrs only
Optional numpy, pandas, polars, pyarrow, Pillow, pydantic Shape/dtype/columns summary (auto-activates when lib is installed)

Development

uv sync --extra dev
uv run pytest

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

reprobate-0.1.0.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reprobate-0.1.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: reprobate-0.1.0.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for reprobate-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c529570189fc3016ea641bd78e64395a602b5ef4ef97e36f7d7e5188d9b4fb9a
MD5 52c97bebe3bbe44953ff9025fe50e92a
BLAKE2b-256 a3de67a8b52b7bc3df9f9fc145725bcc4667c921800ee4726a042e681ffba8f0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: reprobate-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for reprobate-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f6605e4e4cebd3228e63e528b83e8e7c245ac731e5f12f15af5c5e1817dd20a
MD5 c6e3cb3370b048bfc4074942d93ad760
BLAKE2b-256 dab96ef1b2e0e1b8118fdf70c2e6c73a29ad24fa8ed489701f47b37252570c49

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