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.1.tar.gz (15.6 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.1-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: reprobate-0.1.1.tar.gz
  • Upload date:
  • Size: 15.6 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.1.tar.gz
Algorithm Hash digest
SHA256 7afecb107bc209993e3fffcd2f0067664f546f37da0b7c5f8cc6ab1bbe1b8f14
MD5 fd0f964c8d1db1392ed294bcd7303672
BLAKE2b-256 ea4676ecb1d7a3b1fa3a6b9fceef1dfa316dfe035f1b7f7ee80b93842ee22d5b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: reprobate-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.3 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a0205da5008cb3a8a662888c0b29fd304171feb6a002aa698cb69513cd717aad
MD5 e1f6fd7c8e8741a7a8f093f23220bdb4
BLAKE2b-256 a98bf5226983f9283829def06664d99c806f5029a24e6d19ccf305d28efa2d87

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