Wadler-Lindig Pretty Printer

0.1.7 · active · verified Thu Apr 09

Wadler-Lindig is a Python library that provides a pretty-printer based on the Wadler–Lindig algorithm. It serves as an alternative to the built-in `pprint.pprint`, aiming to produce output that consumes less horizontal space, which is particularly useful for complex custom types, error messages, or nested data structures. The library is characterized by its compact implementation, support for multi-line strings and ANSI escape codes, and zero runtime dependencies. The current version is 0.1.7, and it appears to be actively maintained.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic usage of `wadler_lindig.pprint` and `pformat` with a dataclass containing a list and a NumPy array. It also illustrates how to customize the pretty-printing for a custom class using the `__pdoc__` method. Note that `numpy` is used for demonstration purposes, but is not a core dependency of the `wadler-lindig` library itself.

import dataclasses
import numpy as np
import wadler_lindig as wl

@dataclasses.dataclass
class MyDataclass:
    x: list[str]
    y: np.ndarray

obj = MyDataclass(
    x=["lorem", "ipsum", "dolor sit amet", "consectetur", "adipiscing elit"],
    y=np.zeros((2, 3))
)

print("\n--- Default pprint output ---")
print(obj)

print("\n--- wadler_lindig.pprint output (width=30, indent=4) ---")
wl.pprint(obj, width=30, indent=4)

print("\n--- wadler_lindig.pformat output (width=20) ---")
formatted_str = wl.pformat(obj, width=20, indent=2)
print(formatted_str)

# Example of custom pretty-printing for a non-dataclass type
class MyCustomClass:
    def __init__(self, value):
        self.value = value
    
    def __pdoc__(self, **kwargs) -> wl.AbstractDoc:
        return wl.TextDoc(f"<MyCustomClass: {self.value}> ")

    def __repr__(self):
        return wl.pformat(self, width=80)

custom_obj = MyCustomClass(123)
print("\n--- Custom object with __pdoc__ ---")
wl.pprint(custom_obj)

view raw JSON →