1998-03-26 21:13:24 +00:00
|
|
|
# Author: Fred L. Drake, Jr.
|
|
|
|
# fdrake@cnri.reston.va.us, fdrake@acm.org
|
1997-04-16 00:49:59 +00:00
|
|
|
#
|
|
|
|
# This is a simple little module I wrote to make life easier. I didn't
|
|
|
|
# see anything quite like it in the library, though I may have overlooked
|
|
|
|
# something. I wrote this when I was trying to read some heavily nested
|
2000-07-16 12:04:32 +00:00
|
|
|
# tuples with fairly non-descriptive content. This is modeled very much
|
1997-04-16 00:49:59 +00:00
|
|
|
# after Lisp/Scheme - style pretty-printing of lists. If you find it
|
|
|
|
# useful, thank small children who sleep at night.
|
|
|
|
|
|
|
|
"""Support to pretty-print lists, tuples, & dictionaries recursively.
|
|
|
|
|
|
|
|
Very simple, but useful, especially in debugging data structures.
|
|
|
|
|
1997-04-16 16:59:30 +00:00
|
|
|
Classes
|
|
|
|
-------
|
|
|
|
|
|
|
|
PrettyPrinter()
|
|
|
|
Handle pretty-printing operations onto a stream using a configured
|
|
|
|
set of formatting parameters.
|
|
|
|
|
1997-04-16 00:49:59 +00:00
|
|
|
Functions
|
|
|
|
---------
|
|
|
|
|
|
|
|
pformat()
|
|
|
|
Format a Python object into a pretty-printed representation.
|
|
|
|
|
|
|
|
pprint()
|
1997-04-16 16:59:30 +00:00
|
|
|
Pretty-print a Python object to a stream [default is sys.sydout].
|
1997-04-16 00:49:59 +00:00
|
|
|
|
1997-04-16 16:59:30 +00:00
|
|
|
saferepr()
|
|
|
|
Generate a 'standard' repr()-like value, but protect against recursive
|
|
|
|
data structures.
|
1997-04-16 00:49:59 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
from types import DictType, ListType, TupleType
|
|
|
|
|
1997-04-16 16:59:30 +00:00
|
|
|
try:
|
|
|
|
from cStringIO import StringIO
|
|
|
|
except ImportError:
|
|
|
|
from StringIO import StringIO
|
|
|
|
|
2001-02-12 02:00:42 +00:00
|
|
|
__all__ = ["pprint","pformat","isreadable","isrecursive","saferepr",
|
|
|
|
"PrettyPrinter"]
|
1997-04-16 16:59:30 +00:00
|
|
|
|
|
|
|
def pprint(object, stream=None):
|
|
|
|
"""Pretty-print a Python object to a stream [default is sys.sydout]."""
|
|
|
|
printer = PrettyPrinter(stream=stream)
|
|
|
|
printer.pprint(object)
|
|
|
|
|
|
|
|
def pformat(object):
|
|
|
|
"""Format a Python object into a pretty-printed representation."""
|
|
|
|
return PrettyPrinter().pformat(object)
|
|
|
|
|
2001-05-14 07:05:58 +00:00
|
|
|
def saferepr(object):
|
|
|
|
"""Version of repr() which can handle recursive data structures."""
|
|
|
|
return _safe_repr(object, {})[0]
|
1997-04-16 16:59:30 +00:00
|
|
|
|
1997-07-18 20:42:39 +00:00
|
|
|
def isreadable(object):
|
|
|
|
"""Determine if saferepr(object) is readable by eval()."""
|
2001-05-14 07:05:58 +00:00
|
|
|
return _safe_repr(object, {})[1]
|
1997-07-18 20:42:39 +00:00
|
|
|
|
|
|
|
def isrecursive(object):
|
|
|
|
"""Determine if object requires a recursive representation."""
|
2001-05-14 07:05:58 +00:00
|
|
|
return _safe_repr(object, {})[2]
|
1997-04-16 16:59:30 +00:00
|
|
|
|
|
|
|
class PrettyPrinter:
|
|
|
|
def __init__(self, indent=1, width=80, depth=None, stream=None):
|
1998-03-26 21:13:24 +00:00
|
|
|
"""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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
indent = int(indent)
|
|
|
|
width = int(width)
|
|
|
|
assert indent >= 0
|
2001-05-14 07:05:58 +00:00
|
|
|
assert depth is None or depth > 0, "depth must be > 0"
|
1998-03-26 21:13:24 +00:00
|
|
|
assert width
|
|
|
|
self.__depth = depth
|
|
|
|
self.__indent_per_level = indent
|
|
|
|
self.__width = width
|
|
|
|
if stream:
|
|
|
|
self.__stream = stream
|
|
|
|
else:
|
|
|
|
import sys
|
|
|
|
self.__stream = sys.stdout
|
1997-04-16 16:59:30 +00:00
|
|
|
|
|
|
|
def pprint(self, object):
|
1998-03-26 21:13:24 +00:00
|
|
|
self.__stream.write(self.pformat(object) + "\n")
|
1997-04-16 16:59:30 +00:00
|
|
|
|
|
|
|
def pformat(self, object):
|
1998-03-26 21:13:24 +00:00
|
|
|
sio = StringIO()
|
|
|
|
self.__format(object, sio, 0, 0, {}, 0)
|
|
|
|
return sio.getvalue()
|
1997-04-16 16:59:30 +00:00
|
|
|
|
1997-07-18 20:42:39 +00:00
|
|
|
def isrecursive(self, object):
|
1998-03-26 21:13:24 +00:00
|
|
|
self.__recursive = 0
|
|
|
|
self.pformat(object)
|
|
|
|
return self.__recursive
|
1997-07-18 20:42:39 +00:00
|
|
|
|
|
|
|
def isreadable(self, object):
|
1998-03-26 21:13:24 +00:00
|
|
|
self.__recursive = 0
|
|
|
|
self.__readable = 1
|
|
|
|
self.pformat(object)
|
|
|
|
return self.__readable and not self.__recursive
|
1997-07-18 20:42:39 +00:00
|
|
|
|
1997-04-16 16:59:30 +00:00
|
|
|
def __format(self, object, stream, indent, allowance, context, level):
|
1998-03-26 21:13:24 +00:00
|
|
|
level = level + 1
|
|
|
|
if context.has_key(id(object)):
|
|
|
|
object = _Recursion(object)
|
|
|
|
self.__recursive = 1
|
|
|
|
rep = self.__repr(object, context, level - 1)
|
|
|
|
objid = id(object)
|
|
|
|
context[objid] = 1
|
|
|
|
typ = type(object)
|
|
|
|
sepLines = len(rep) > (self.__width - 1 - indent - allowance)
|
|
|
|
|
|
|
|
if sepLines and typ in (ListType, TupleType):
|
|
|
|
# Pretty-print the sequence.
|
|
|
|
stream.write((typ is ListType) and '[' or '(')
|
|
|
|
if self.__indent_per_level > 1:
|
|
|
|
stream.write((self.__indent_per_level - 1) * ' ')
|
|
|
|
length = len(object)
|
|
|
|
if length:
|
|
|
|
indent = indent + self.__indent_per_level
|
|
|
|
self.__format(object[0], stream, indent, allowance + 1,
|
|
|
|
context, level)
|
1999-12-22 21:52:32 +00:00
|
|
|
if length > 1:
|
1998-03-26 21:13:24 +00:00
|
|
|
for ent in object[1:]:
|
|
|
|
stream.write(',\n' + ' '*indent)
|
|
|
|
self.__format(ent, stream, indent,
|
|
|
|
allowance + 1, context, level)
|
|
|
|
indent = indent - self.__indent_per_level
|
|
|
|
if typ is TupleType and length == 1:
|
|
|
|
stream.write(',')
|
|
|
|
stream.write(((typ is ListType) and ']') or ')')
|
|
|
|
|
|
|
|
elif sepLines and typ is DictType:
|
|
|
|
stream.write('{')
|
|
|
|
if self.__indent_per_level > 1:
|
|
|
|
stream.write((self.__indent_per_level - 1) * ' ')
|
|
|
|
length = len(object)
|
|
|
|
if length:
|
|
|
|
indent = indent + self.__indent_per_level
|
|
|
|
items = object.items()
|
|
|
|
items.sort()
|
|
|
|
key, ent = items[0]
|
|
|
|
rep = self.__repr(key, context, level) + ': '
|
|
|
|
stream.write(rep)
|
|
|
|
self.__format(ent, stream, indent + len(rep),
|
|
|
|
allowance + 1, context, level)
|
|
|
|
if len(items) > 1:
|
|
|
|
for key, ent in items[1:]:
|
|
|
|
rep = self.__repr(key, context, level) + ': '
|
|
|
|
stream.write(',\n' + ' '*indent + rep)
|
|
|
|
self.__format(ent, stream, indent + len(rep),
|
|
|
|
allowance + 1, context, level)
|
|
|
|
indent = indent - self.__indent_per_level
|
|
|
|
stream.write('}')
|
|
|
|
|
|
|
|
else:
|
|
|
|
stream.write(rep)
|
1999-09-02 15:09:44 +00:00
|
|
|
|
|
|
|
del context[objid]
|
1997-04-16 00:49:59 +00:00
|
|
|
|
1997-04-16 16:59:30 +00:00
|
|
|
def __repr(self, object, context, level):
|
2001-05-14 07:05:58 +00:00
|
|
|
repr, readable, recursive = _safe_repr(object, context,
|
|
|
|
self.__depth, level)
|
1998-03-26 21:13:24 +00:00
|
|
|
if not readable:
|
|
|
|
self.__readable = 0
|
2001-05-14 07:05:58 +00:00
|
|
|
if recursive:
|
|
|
|
self.__recursive = 1
|
1998-03-26 21:13:24 +00:00
|
|
|
return repr
|
1997-04-16 00:49:59 +00:00
|
|
|
|
2001-05-14 07:05:58 +00:00
|
|
|
# Return triple (repr_string, isreadable, isrecursive).
|
1997-04-16 00:49:59 +00:00
|
|
|
|
1997-04-16 18:55:58 +00:00
|
|
|
def _safe_repr(object, context, maxlevels=None, level=0):
|
2001-05-14 18:39:41 +00:00
|
|
|
level += 1
|
1997-04-16 16:59:30 +00:00
|
|
|
typ = type(object)
|
|
|
|
if not (typ in (DictType, ListType, TupleType) and object):
|
1998-03-26 21:13:24 +00:00
|
|
|
rep = `object`
|
2001-05-14 07:05:58 +00:00
|
|
|
return rep, (rep and (rep[0] != '<')), 0
|
2001-05-14 18:39:41 +00:00
|
|
|
|
1997-04-16 18:55:58 +00:00
|
|
|
if context.has_key(id(object)):
|
2001-05-14 07:05:58 +00:00
|
|
|
return `_Recursion(object)`, 0, 1
|
1997-04-16 16:59:30 +00:00
|
|
|
objid = id(object)
|
|
|
|
context[objid] = 1
|
2001-05-14 18:39:41 +00:00
|
|
|
|
1999-02-17 17:30:52 +00:00
|
|
|
readable = 1
|
2001-05-14 07:05:58 +00:00
|
|
|
recursive = 0
|
2001-05-14 18:39:41 +00:00
|
|
|
startchar, endchar = {ListType: "[]",
|
|
|
|
TupleType: "()",
|
|
|
|
DictType: "{}"}[typ]
|
|
|
|
if maxlevels and level > maxlevels:
|
|
|
|
with_commas = "..."
|
|
|
|
readable = 0
|
|
|
|
|
|
|
|
elif typ is DictType:
|
|
|
|
components = []
|
|
|
|
for k, v in object.iteritems():
|
2001-05-14 07:05:58 +00:00
|
|
|
krepr, kreadable, krecur = _safe_repr(k, context, maxlevels,
|
|
|
|
level)
|
|
|
|
vrepr, vreadable, vrecur = _safe_repr(v, context, maxlevels,
|
|
|
|
level)
|
2001-05-14 18:39:41 +00:00
|
|
|
components.append("%s: %s" % (krepr, vrepr))
|
1998-03-26 21:13:24 +00:00
|
|
|
readable = readable and kreadable and vreadable
|
2001-05-14 07:05:58 +00:00
|
|
|
recursive = recursive or krecur or vrecur
|
2001-05-14 18:39:41 +00:00
|
|
|
with_commas = ", ".join(components)
|
|
|
|
|
|
|
|
else: # list or tuple
|
|
|
|
assert typ in (ListType, TupleType)
|
|
|
|
components = []
|
|
|
|
for element in object:
|
2001-05-14 07:05:58 +00:00
|
|
|
subrepr, subreadable, subrecur = _safe_repr(
|
2001-05-14 18:39:41 +00:00
|
|
|
element, context, maxlevels, level)
|
|
|
|
components.append(subrepr)
|
1998-03-26 21:13:24 +00:00
|
|
|
readable = readable and subreadable
|
2001-05-14 07:05:58 +00:00
|
|
|
recursive = recursive or subrecur
|
2001-05-14 18:39:41 +00:00
|
|
|
if len(components) == 1 and typ is TupleType:
|
|
|
|
components[0] += ","
|
|
|
|
with_commas = ", ".join(components)
|
|
|
|
|
|
|
|
s = "%s%s%s" % (startchar, with_commas, endchar)
|
1997-04-16 16:59:30 +00:00
|
|
|
del context[objid]
|
2001-05-14 07:05:58 +00:00
|
|
|
return s, readable and not recursive, recursive
|
1997-04-16 16:59:30 +00:00
|
|
|
|
|
|
|
class _Recursion:
|
|
|
|
# represent a recursive relationship; really only used for the __repr__()
|
|
|
|
# method...
|
|
|
|
def __init__(self, object):
|
1998-03-26 21:13:24 +00:00
|
|
|
self.__repr = "<Recursion on %s with id=%s>" \
|
|
|
|
% (type(object).__name__, id(object))
|
1997-04-16 16:59:30 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
1998-03-26 21:13:24 +00:00
|
|
|
return self.__repr
|