A Wadler–Lindig ✨pretty-printer✨ for Python
This library is for you if you need:
- Something like the built-in
pprint.pprint, but which consumes less horizontal space. For example in error messages. - If you have complicated custom types that you'd like to create pretty well-formatted reprs for. For example nested trees of dataclasses / PyTorch modules / etc.
Main features:
- Absolutely tiny implementation (96 lines of code for the main Wadler–Lindig algorithm, 342 more for teaching it how to handle all Python types).
- Simpler than the original algorithm by Wadler & Lindig (removes some dead code).
- Supports multi-line unbroken text strings.
- Supports ANSI escape codes and colours.
- Zero dependencies.
Installation
pip install wadler_lindig
Documentation
Available at https://docs.kidger.site/wadler_lindig.
Example
import dataclasses
import numpy as np
import wadler_lindig as wl
@dataclasses.dataclass
class MyDataclass:
x: list[str]
y: np.ndarray
obj = MyDataclass(["lorem", "ipsum", "dolor sit amet"], np.zeros((2, 3)))
wl.pprint(obj, width=30, indent=4)
# MyDataclass(
# x=[
# 'lorem',
# 'ipsum',
# 'dolor sit amet'
# ],
# y=f64[2,3](numpy)
# )
API at a glance
For day-to-day pretty-printing objects: pprint (to stdout), pformat (as a string), pdiff (between two objects).
For creating custom pretty-printed representations:
- The core Wadler–Lindig document types:
AbstractDoc,BreakDoc,ConcatDoc,GroupDoc,NestDoc,TextDoc. pdocwill convert any Python object to a Wadler–Lindig document, with the__pdoc__method called on custom types if it is available.ansi_format(to add ANSI colour codes),ansi_strip(to remove ANSI codes).- Several common helpers for creating Wadler–Lindig documents:
array_summary,bracketed,comma,join,named_objs.
FAQ
What is the difference to the built-in `pprint` library?
- The main difference is that the Wadler–Lindig algorithm produces output like
MyDataclass(
x=SomeNestedClass(
y=[1, 2, 3]
)
)
In contrast pprint produces output like
MyDataclass(x=SomeNestedClass(y=[1,
2,
3]))
which consumes a lot more horizontal space.
-
By default we print NumPy arrays / PyTorch tensors / etc. in a concise form e.g.
f32[2,3](numpy)to denote a NumPy array with shape(2, 3)and dtypefloat32. (Setshort_arrays=Falseto disable this.) -
We provide support for customising the pretty-printed representations of your custom types. Typically this is done via:
import wadler_lindig as wl class MyAmazingClass: def __pdoc__(self, **kwargs) -> wl.AbstractDoc: ... # Create your pretty representation here! def __repr__(self): # Calls `__pdoc__` and then formats to a particular width. return wl.pformat(self, width=80)
In addition we support a
wadler_lindig.pprint(..., custom=...)argument, if you don't own the type and so cannot add a__pdoc__method.
What is the difference to `black` or `rust format`?
The above are formatters for your source code. This wadler_lindig library is intended as an alternative to the built-in pprint library, which pretty-format Python objects at runtime.
Metadata
Release files for wadler-lindig 0.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| wadler_lindig-0.1.8.tar.gz | 16.3 kB | Without provenance | Details for wadler_lindig-0.1.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| wadler_lindig-0.1.8-py3-none-any.whl | Python 3 | none | any | Details for wadler_lindig-0.1.8-py3-none-any.whl |
Total release size: 37.1 kB
Release files / wadler_lindig-0.1.8.tar.gz
| Download URL | wadler_lindig-0.1.8.tar.gz |
|---|---|
| Size | 16.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1a94d51cd26100dda9e430c64539c5b92b9788c87f10db3e1c87503ab814411a
|
|
BLAKE2b-256 checksum How to use checksums |
eabf4caf1aada6c84eb3fadf9341a90367f436a759692456e1a234d444d428ee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.17
|
Release files / wadler_lindig-0.1.8-py3-none-any.whl
| Download URL | wadler_lindig-0.1.8-py3-none-any.whl |
|---|---|
| Size | 20.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8671e8c98449ae3f4e4ba720eccd63a502b3983042f8d112b49a9c6be49e8d92
|
|
BLAKE2b-256 checksum How to use checksums |
000bb170089cd44848f25dbc9efda08e5aa4a8402d9a13f0d4d0f3d7808be539
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.17
|
Security for wadler-lindig 0.1.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.