A Wadler–Lindig pretty-printer for Python.
Project description
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 (77 lines of code for the main Wadler–Lindig algorithm, 223 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.
Example use cases:
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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wadler_lindig-0.1.0.tar.gz.
File metadata
- Download URL: wadler_lindig-0.1.0.tar.gz
- Upload date:
- Size: 14.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d09b27488dda4b9ef70cd0b5e46722c81e86b994afb32d86b98a5eb0e3cf3d6e
|
|
| MD5 |
766d1af9683f8698e69d137e24d97df2
|
|
| BLAKE2b-256 |
08ac29b2e03c8bf1f44c5b7eec534a9c69ade8576b296f77280f9ac32d980b42
|
File details
Details for the file wadler_lindig-0.1.0-py3-none-any.whl.
File metadata
- Download URL: wadler_lindig-0.1.0-py3-none-any.whl
- Upload date:
- Size: 19.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3fa7649704fa68d32afaa66afc04f6fca258f070ea0de541784039102cbd487a
|
|
| MD5 |
4dadb3c5560915281e0d785dc5543923
|
|
| BLAKE2b-256 |
63ee778b26f76765582672c5b589396c8a9595bfe2d5a762a261a4cef91afd2e
|