Back to index

python3.2  3.2.2
Classes | Functions | Variables
pprint Namespace Reference

Classes

class  _safe_key
class  PrettyPrinter

Functions

def pprint
def pformat
def saferepr
def isreadable
def isrecursive
def _safe_tuple
def _safe_repr
def _recursion
def _perfcheck

Variables

list __all__
string _commajoin = ", "
 _id = id
 _len = len
 _type = type

Function Documentation

def pprint._perfcheck (   object = None) [private]

Definition at line 352 of file pprint.py.

00352 
00353 def _perfcheck(object=None):
00354     import time
00355     if object is None:
00356         object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000
00357     p = PrettyPrinter()
00358     t1 = time.time()
00359     _safe_repr(object, {}, None, 0)
00360     t2 = time.time()
00361     p.pformat(object)
00362     t3 = time.time()
00363     print("_safe_repr:", t2 - t1)
00364     print("pformat:", t3 - t2)

Here is the call graph for this function:

def pprint._recursion (   object) [private]

Definition at line 347 of file pprint.py.

00347 
00348 def _recursion(object):
00349     return ("<Recursion on %s with id=%s>"
00350             % (_type(object).__name__, _id(object)))
00351 

Here is the caller graph for this function:

def pprint._safe_repr (   object,
  context,
  maxlevels,
  level 
) [private]

Definition at line 262 of file pprint.py.

00262 
00263 def _safe_repr(object, context, maxlevels, level):
00264     typ = _type(object)
00265     if typ is str:
00266         if 'locale' not in _sys.modules:
00267             return repr(object), True, False
00268         if "'" in object and '"' not in object:
00269             closure = '"'
00270             quotes = {'"': '\\"'}
00271         else:
00272             closure = "'"
00273             quotes = {"'": "\\'"}
00274         qget = quotes.get
00275         sio = _StringIO()
00276         write = sio.write
00277         for char in object:
00278             if char.isalpha():
00279                 write(char)
00280             else:
00281                 write(qget(char, repr(char)[1:-1]))
00282         return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False
00283 
00284     r = getattr(typ, "__repr__", None)
00285     if issubclass(typ, dict) and r is dict.__repr__:
00286         if not object:
00287             return "{}", True, False
00288         objid = _id(object)
00289         if maxlevels and level >= maxlevels:
00290             return "{...}", False, objid in context
00291         if objid in context:
00292             return _recursion(object), False, True
00293         context[objid] = 1
00294         readable = True
00295         recursive = False
00296         components = []
00297         append = components.append
00298         level += 1
00299         saferepr = _safe_repr
00300         items = sorted(object.items(), key=_safe_tuple)
00301         for k, v in items:
00302             krepr, kreadable, krecur = saferepr(k, context, maxlevels, level)
00303             vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level)
00304             append("%s: %s" % (krepr, vrepr))
00305             readable = readable and kreadable and vreadable
00306             if krecur or vrecur:
00307                 recursive = True
00308         del context[objid]
00309         return "{%s}" % _commajoin(components), readable, recursive
00310 
00311     if (issubclass(typ, list) and r is list.__repr__) or \
00312        (issubclass(typ, tuple) and r is tuple.__repr__):
00313         if issubclass(typ, list):
00314             if not object:
00315                 return "[]", True, False
00316             format = "[%s]"
00317         elif _len(object) == 1:
00318             format = "(%s,)"
00319         else:
00320             if not object:
00321                 return "()", True, False
00322             format = "(%s)"
00323         objid = _id(object)
00324         if maxlevels and level >= maxlevels:
00325             return format % "...", False, objid in context
00326         if objid in context:
00327             return _recursion(object), False, True
00328         context[objid] = 1
00329         readable = True
00330         recursive = False
00331         components = []
00332         append = components.append
00333         level += 1
00334         for o in object:
00335             orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level)
00336             append(orepr)
00337             if not oreadable:
00338                 readable = False
00339             if orecur:
00340                 recursive = True
00341         del context[objid]
00342         return format % _commajoin(components), readable, recursive
00343 
00344     rep = repr(object)
00345     return rep, (rep and not rep.startswith('<')), False
00346 

Here is the call graph for this function:

Here is the caller graph for this function:

def pprint._safe_tuple (   t) [private]

Definition at line 95 of file pprint.py.

00095 
00096 def _safe_tuple(t):
00097     "Helper function for comparing 2-tuples"
00098     return _safe_key(t[0]), _safe_key(t[1])

def pprint.isreadable (   object)
Determine if saferepr(object) is readable by eval().

Definition at line 65 of file pprint.py.

00065 
00066 def isreadable(object):
00067     """Determine if saferepr(object) is readable by eval()."""
00068     return _safe_repr(object, {}, None, 0)[1]

Here is the call graph for this function:

Here is the caller graph for this function:

def pprint.isrecursive (   object)
Determine if object requires a recursive representation.

Definition at line 69 of file pprint.py.

00069 
00070 def isrecursive(object):
00071     """Determine if object requires a recursive representation."""
00072     return _safe_repr(object, {}, None, 0)[2]

Here is the call graph for this function:

Here is the caller graph for this function:

def pprint.pformat (   object,
  indent = 1,
  width = 80,
  depth = None 
)
Format a Python object into a pretty-printed representation.

Definition at line 57 of file pprint.py.

00057 
00058 def pformat(object, indent=1, width=80, depth=None):
00059     """Format a Python object into a pretty-printed representation."""
00060     return PrettyPrinter(indent=indent, width=width, depth=depth).pformat(object)

Here is the caller graph for this function:

def pprint.pprint (   object,
  stream = None,
  indent = 1,
  width = 80,
  depth = None 
)
Pretty-print a Python object to a stream [default is sys.stdout].

Definition at line 51 of file pprint.py.

00051 
00052 def pprint(object, stream=None, indent=1, width=80, depth=None):
00053     """Pretty-print a Python object to a stream [default is sys.stdout]."""
00054     printer = PrettyPrinter(
00055         stream=stream, indent=indent, width=width, depth=depth)
00056     printer.pprint(object)

Here is the caller graph for this function:

def pprint.saferepr (   object)
Version of repr() which can handle recursive data structures.

Definition at line 61 of file pprint.py.

00061 
00062 def saferepr(object):
00063     """Version of repr() which can handle recursive data structures."""
00064     return _safe_repr(object, {}, None, 0)[0]

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Initial value:
00001 ["pprint","pformat","isreadable","isrecursive","saferepr",
00002            "PrettyPrinter"]

Definition at line 41 of file pprint.py.

Definition at line 45 of file pprint.py.

pprint._id = id

Definition at line 46 of file pprint.py.

Definition at line 47 of file pprint.py.

Definition at line 48 of file pprint.py.