scitools.pprint2

Support to pretty-print lists, tuples, & dictionaries recursively.

Very simple, but useful, especially in debugging data structures.

Slight extension of the original pprint module in Python such that floating-point numbers can be written with a specificed format (float_format) and not by their repr string (which explicitly displays round-off errors).

>>> data = [1./3, 1./49*49, 84.6/423]
>>> data
[0.3333333333333333, 0.9999999999999999, 0.19999999999999998]
>>> import pprint
>>> pprint.pprint(data)
[0.3333333333333333, 0.9999999999999999, 0.19999999999999998]
>>> import scitools.pprint2 as pprint2
>>> pprint2.pprint(data)
[0.333333, 1, 0.2]
>>> pprint2.float_format = '%.12E'
>>> pprint2.pprint(data)
[3.333333333333E-01, 1.000000000000E+00, 2.000000000000E-01]

Classes:

  • PrettyPrinter(): Handle pretty-printing operations onto a stream using a configured set of formatting parameters.

Functions:

  • pformat(): Format a Python object into a pretty-printed representation.
  • pprint(): Pretty-print a Python object to a stream [default is sys.stdout].
  • saferepr(): Generate a ‘standard’ repr()-like value, but protect against recursive data structures.

Global data:

  • float_format: String containing the printf format for formatting float objects.
scitools.pprint2.pprint(object, stream=None, indent=1, width=80, depth=None)[source]

Pretty-print a Python object to a stream [default is sys.stdout].

scitools.pprint2.pformat(object, indent=1, width=80, depth=None)[source]

Format a Python object into a pretty-printed representation.

scitools.pprint2.isreadable(object)[source]

Determine if saferepr(object) is readable by eval().

scitools.pprint2.isrecursive(object)[source]

Determine if object requires a recursive representation.

scitools.pprint2.saferepr(object)[source]

Version of repr() which can handle recursive data structures.

class scitools.pprint2.PrettyPrinter(indent=1, width=80, depth=None, stream=None)[source]

Methods

format(object, context, maxlevels, level) Format object for a specific context, returning a string
isreadable(object)
isrecursive(object)
pformat(object)
pprint(object)
__init__(indent=1, width=80, depth=None, stream=None)[source]

Handle pretty printing operations onto a stream using a set of configured parameters.

indent
Number of spaces to indent for each level of nesting.
width
Attempted maximum number of columns in the output.
depth
The maximum depth to print out nested structures.
stream
The desired output stream. If omitted (or false), the standard output stream available at construction will be used.
__module__ = 'scitools.pprint2'
format(object, context, maxlevels, level)[source]

Format object for a specific context, returning a string and flags indicating whether the representation is ‘readable’ and whether the object represents a recursive construct.

isreadable(object)[source]
isrecursive(object)[source]
pformat(object)[source]
pprint(object)[source]

This Page