Skip to main content

Format Python objects as repr-like plain text or width-aware Rich renderables.

Project description

✨ Features

  • Plain-text formatting for logs and snapshots, plus a Rich renderable path for width-aware console output.
  • Built-in handling for common containers, fieldz-compatible models, and objects with __rich_repr__.
  • Lazy summaries for NumPy, JAX, Torch, and Warp arrays when those libraries are already imported.
  • Shared-reference and cycle tracking for referencable objects such as mappings, sets, frozensets, and custom containers.
  • A small customization surface through __pretty__(), register_type(), register_func(), and register_lazy().
  • Environment-backed defaults through PRETTY_*, with per-call overrides for logs, snapshots, tests, and interactive debugging.

📦 Installation

uv add liblaf-pretty
# or
pip install liblaf-pretty

🧪 Quick Start

Use pformat() when you want stable plain text:

from liblaf.pretty import pformat

print(pformat({"alpha": [1, 2, 3]}), end="")
{'alpha': [1, 2, 3]}

Use plower() when you want a Rich renderable whose layout is chosen later by the target Console. If you already have a console, use pprint() or its alias pp() for the print-now path.

🎛️ Configuration

The public formatting helpers accept per-call overrides for:

  • max_level, max_list, max_array, max_dict
  • max_string, max_long, max_other
  • indent
  • hide_defaults

Those values can also come from environment variables:

export PRETTY_MAX_LIST=2
export PRETTY_INDENT='[bold]>>[/] '

indent accepts plain text, Rich markup, ANSI-colored strings, or rich.text.Text.

🔢 Arrays

Array libraries are optional. Once NumPy, JAX, Torch, or Warp is already loaded, liblaf.pretty formats small arrays as compact dtype-and-shape summaries:

import numpy as np

from liblaf.pretty import pformat

print(pformat(np.zeros((2, 3), dtype=np.float32)), end="")
f32[2,3](numpy)

If any dimension is larger than max_array, the formatter falls back to the object's normal repr instead of importing or traversing large values.

🔁 Shared References

Repeated referencable objects are annotated on first appearance and then render as <Type @ hexid> references later in the output. Lists and tuples stay safe too, but they repeat their value instead of collapsing into a reference tag.

🧩 Custom Formatting

Reach for the smallest hook that matches the job:

  • Implement __pretty__(self, ctx) when you own the class.
  • Use register_type() for a concrete type and its subclasses.
  • Use register_func() for structural matching.
  • Use register_lazy() for optional dependencies that should only activate after their module is already imported.

PrettyContext gives custom formatters the builder helpers they usually need: ctx.container(), ctx.leaf(), ctx.positional(), ctx.name_value(), and ctx.key_value().

For the full walkthrough, see the custom formatter guide.

🛠️ Development

Common workflows:

mise run lint
mise run docs:build
mise run docs:serve
mise run gen:ref-pages
nox

nox runs the test matrix across Python 3.12, 3.13, and 3.14, with both highest and lowest-direct dependency resolution.


📝 License

Copyright © 2026 liblaf.
This project is MIT licensed.

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

liblaf_pretty-0.2.0.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

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

liblaf_pretty-0.2.0-py3-none-any.whl (45.7 kB view details)

Uploaded Python 3

File details

Details for the file liblaf_pretty-0.2.0.tar.gz.

File metadata

  • Download URL: liblaf_pretty-0.2.0.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for liblaf_pretty-0.2.0.tar.gz
Algorithm Hash digest
SHA256 335fe1985a488fe7903bdf2a3dcec8996037b340db17db87b36f94a1921979ef
MD5 28333fe62dabd3fe3e3618662495bd57
BLAKE2b-256 3b0d98f54b92677bfff585574d32d1ff7777cd21df77fd3fd63b2a39a7f41a7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for liblaf_pretty-0.2.0.tar.gz:

Publisher: python-release.yaml on liblaf/pretty

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file liblaf_pretty-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: liblaf_pretty-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 45.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for liblaf_pretty-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ace1f75120c52babe4ba82a63aa0c71340b94aa651892e949b5152e2a8d0e8d2
MD5 0c595805a2f5bc4040fd3b96fa5985b8
BLAKE2b-256 22fc463736ab283f35a8844fe4d342d25229b276b771db0f78899d2fc59e0274

See more details on using hashes here.

Provenance

The following attestation bundles were made for liblaf_pretty-0.2.0-py3-none-any.whl:

Publisher: python-release.yaml on liblaf/pretty

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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