or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

comments.mdconfiguration.mdcore-printing.mddocument-system.mdextras.mdindex.mdregistration.md
tile.json

tessl/pypi-prettyprinter

Syntax-highlighting, declarative and composable pretty printer for Python 3.5+

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/prettyprinter@0.18.x

To install, run

npx @tessl/cli install tessl/pypi-prettyprinter@0.18.0

index.mddocs/

PrettyPrinter

A syntax-highlighting, declarative and composable pretty printer for Python 3.5+ that serves as a powerful drop-in replacement for the standard library's pprint module. PrettyPrinter implements a modified Wadler-Leijen layout algorithm for optimal text formatting and includes extensive customization capabilities, syntax highlighting, and a plugin system for third-party library support.

Package Information

  • Package Name: prettyprinter
  • Language: Python
  • Installation: pip install prettyprinter
  • Dependencies: colorful>=0.4.0, Pygments>=2.2.0

Core Imports

import prettyprinter

Common usage patterns:

from prettyprinter import pprint, cpprint, pformat, register_pretty

For custom pretty printers:

from prettyprinter import (
    register_pretty, pretty_call, comment, python_to_sdocs,
    default_render_to_stream
)

For document system functions:

from prettyprinter.doc import (
    concat, group, nest, annotate, contextual, always_break,
    flat_choice, align, hang, fill
)

Basic Usage

import prettyprinter
from prettyprinter import pprint, cpprint

# Basic data structures
data = {
    'users': [
        {'name': 'Alice', 'age': 30, 'scores': [95, 87, 92]},
        {'name': 'Bob', 'age': 25, 'scores': [88, 91, 85]}
    ],
    'settings': {'debug': True, 'max_connections': 100}
}

# Pretty print without color
pprint(data, width=60)

# Pretty print with syntax highlighting
cpprint(data, width=60, style='light')

# Get formatted string
formatted = prettyprinter.pformat(data, width=40)
print(formatted)

# Install extras for third-party library support
prettyprinter.install_extras(['numpy', 'requests', 'dataclasses'])

Architecture

PrettyPrinter is built around a flexible document-based layout system:

  • Document System: Abstract representation of formatted output using composable document primitives
  • Layout Algorithm: Modified Wadler-Leijen algorithm that optimally fits content within width constraints
  • Registration System: Extensible pretty printer registration for custom types using singledispatch
  • Context System: Immutable context objects that track formatting state during document construction
  • Rendering System: Separate renderers for plain text and syntax-highlighted output
  • Extras System: Plugin architecture for third-party library integration

This design enables declarative pretty printing where users focus on describing the desired output structure rather than managing layout details, while providing extensive customization capabilities for advanced use cases.

Capabilities

Core Pretty Printing

Primary functions for pretty printing Python objects with configurable formatting options, including both plain text and syntax-highlighted output.

def pprint(object, stream=None, indent=4, width=79, depth=None, *, 
          compact=False, ribbon_width=71, max_seq_len=1000, 
          sort_dict_keys=False, end='\n'): ...

def cpprint(object, stream=None, indent=4, width=79, depth=None, *, 
           compact=False, ribbon_width=71, max_seq_len=1000, 
           sort_dict_keys=False, style=None, end='\n'): ...

def pformat(object, indent=4, width=79, depth=None, *, 
           ribbon_width=71, max_seq_len=1000, compact=False, 
           sort_dict_keys=False) -> str: ...

def pretty_repr(instance) -> str: ...

def python_to_sdocs(value, indent, width, depth, ribbon_width, 
                   max_seq_len, sort_dict_keys): ...

def default_render_to_stream(stream, sdocs, newline='\n', separator=' '): ...

Core Pretty Printing

Pretty Printer Registration

System for registering custom pretty printers for user-defined types and creating formatted function call representations.

def register_pretty(type=None, predicate=None): ...

def pretty_call(ctx, fn, *args, **kwargs): ...

def pretty_call_alt(ctx, fn, args=(), kwargs=()): ...

def is_registered(type, *, check_superclasses=False, check_deferred=True, 
                 register_deferred=True) -> bool: ...

Pretty Printer Registration

Configuration Management

Functions for managing global configuration settings and styling options for pretty printing output.

def set_default_config(*, style=None, max_seq_len=None, width=None, 
                      ribbon_width=None, depth=None, sort_dict_keys=None): ...

def get_default_config(): ...

def set_default_style(style): ...

Configuration Management

Document System

Low-level document creation and manipulation functions for building custom layout algorithms and pretty printers.

def concat(docs): ...
def group(doc): ...
def nest(i, doc): ...
def annotate(annotation, doc): ...
def contextual(fn): ...
def always_break(doc): ...
def flat_choice(when_broken, when_flat): ...
def align(doc): ...
def hang(i, doc): ...
def fill(docs): ...

Document System

Comments and Annotations

Functions for adding comments and annotations to pretty printed output.

def comment(value, comment_text): ...
def trailing_comment(value, comment_text): ...

Comments and Annotations

Extras and Extensions

System for installing and managing third-party library integrations and extensions.

def install_extras(include=ALL_EXTRAS, *, exclude=frozenset(), 
                  raise_on_error=False, warn_on_error=True): ...

ALL_EXTRAS: frozenset

Extras and Extensions

Types

class PrettyPrinter:
    """Pretty printer class providing pprint-compatible interface."""
    def __init__(self, *args, **kwargs): ...
    def pprint(self, object): ...
    def pformat(self, object) -> str: ...
    def isrecursive(self, object) -> bool: ...
    def isreadable(self, object) -> bool: ...
    def format(self, object): ...

class PrettyContext:
    """Immutable context object used during pretty printer execution."""
    def __init__(self, indent, depth_left, visited=None, 
                multiline_strategy='MULTILINE_STRATEGY_PLAIN',
                max_seq_len=1000, sort_dict_keys=False, user_ctx=None): ...
    def assoc(self, key, value): ...
    def get(self, key, default=None): ...
    def nested_call(): ...
    def start_visit(self, value): ...
    def end_visit(self, value): ...
    def is_visited(self, value) -> bool: ...

class Doc:
    """Base class for document types in the layout system."""
    def normalize(self): ...

class Concat(Doc):
    """Document representing concatenation of multiple documents."""
    def __init__(self, docs): ...

class Group(Doc):
    """Document that attempts single-line layout when possible."""
    def __init__(self, doc): ...

class Nest(Doc):
    """Document with increased indentation level."""
    def __init__(self, indent, doc): ...

class Annotated(Doc):
    """Document with annotation metadata."""
    def __init__(self, doc, annotation): ...

class FlatChoice(Doc):
    """Document with conditional layout options."""
    def __init__(self, when_broken, when_flat, normalize_on_access=False): ...