Skip to main content

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.
  • pdoc will 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?
  1. 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.

  1. 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 dtype float32. (Set short_arrays=False to disable this.)

  2. 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

wadler_lindig-0.1.2.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

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

wadler_lindig-0.1.2-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file wadler_lindig-0.1.2.tar.gz.

File metadata

  • Download URL: wadler_lindig-0.1.2.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.11

File hashes

Hashes for wadler_lindig-0.1.2.tar.gz
Algorithm Hash digest
SHA256 999e63ebf93f43e72a23b1a83c52ba4ebb4c16648148ace062a73c3a6e0854da
MD5 753affaca630d063cce4a25b825d7338
BLAKE2b-256 35ad1c2a9b42a82792d9e160d58be4602d508513d10c5ce5720952cbec8f6ce7

See more details on using hashes here.

File details

Details for the file wadler_lindig-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: wadler_lindig-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.11

File hashes

Hashes for wadler_lindig-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c4a003aaff91ab8da45b7660918f8203800b5b69efefffa223bcfc785bb7be0e
MD5 152fa704b7efe4f937632b3785f61b4a
BLAKE2b-256 26de4ee24291c8538b70250aad405e4cf911610f5091ae4d54aad3ce087c8d72

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