Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions | Private Attributes
pprint.PrettyPrinter Class Reference
Inheritance diagram for pprint.PrettyPrinter:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def pprint
def pformat
def isrecursive
def isreadable
def format

Private Member Functions

def _format
def _repr

Private Attributes

 _depth
 _indent_per_level
 _width
 _stream
 _recursive
 _readable

Detailed Description

Definition at line 99 of file pprint.py.


Constructor & Destructor Documentation

def pprint.PrettyPrinter.__init__ (   self,
  indent = 1,
  width = 80,
  depth = None,
  stream = None 
)
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.

Definition at line 100 of file pprint.py.

00100 
00101     def __init__(self, indent=1, width=80, depth=None, stream=None):
00102         """Handle pretty printing operations onto a stream using a set of
00103         configured parameters.
00104 
00105         indent
00106             Number of spaces to indent for each level of nesting.
00107 
00108         width
00109             Attempted maximum number of columns in the output.
00110 
00111         depth
00112             The maximum depth to print out nested structures.
00113 
00114         stream
00115             The desired output stream.  If omitted (or false), the standard
00116             output stream available at construction will be used.
00117 
00118         """
00119         indent = int(indent)
00120         width = int(width)
00121         assert indent >= 0, "indent must be >= 0"
00122         assert depth is None or depth > 0, "depth must be > 0"
00123         assert width, "width must be != 0"
00124         self._depth = depth
00125         self._indent_per_level = indent
00126         self._width = width
00127         if stream is not None:
00128             self._stream = stream
00129         else:
00130             self._stream = _sys.stdout

Here is the caller graph for this function:


Member Function Documentation

def pprint.PrettyPrinter._format (   self,
  object,
  stream,
  indent,
  allowance,
  context,
  level 
) [private]

Definition at line 147 of file pprint.py.

00147 
00148     def _format(self, object, stream, indent, allowance, context, level):
00149         level = level + 1
00150         objid = _id(object)
00151         if objid in context:
00152             stream.write(_recursion(object))
00153             self._recursive = True
00154             self._readable = False
00155             return
00156         rep = self._repr(object, context, level - 1)
00157         typ = _type(object)
00158         sepLines = _len(rep) > (self._width - 1 - indent - allowance)
00159         write = stream.write
00160 
00161         if self._depth and level > self._depth:
00162             write(rep)
00163             return
00164 
00165         if sepLines:
00166             r = getattr(typ, "__repr__", None)
00167             if issubclass(typ, dict):
00168                 write('{')
00169                 if self._indent_per_level > 1:
00170                     write((self._indent_per_level - 1) * ' ')
00171                 length = _len(object)
00172                 if length:
00173                     context[objid] = 1
00174                     indent = indent + self._indent_per_level
00175                     if issubclass(typ, _OrderedDict):
00176                         items = list(object.items())
00177                     else:
00178                         items = sorted(object.items(), key=_safe_tuple)
00179                     key, ent = items[0]
00180                     rep = self._repr(key, context, level)
00181                     write(rep)
00182                     write(': ')
00183                     self._format(ent, stream, indent + _len(rep) + 2,
00184                                   allowance + 1, context, level)
00185                     if length > 1:
00186                         for key, ent in items[1:]:
00187                             rep = self._repr(key, context, level)
00188                             write(',\n%s%s: ' % (' '*indent, rep))
00189                             self._format(ent, stream, indent + _len(rep) + 2,
00190                                           allowance + 1, context, level)
00191                     indent = indent - self._indent_per_level
00192                     del context[objid]
00193                 write('}')
00194                 return
00195 
00196             if ((issubclass(typ, list) and r is list.__repr__) or
00197                 (issubclass(typ, tuple) and r is tuple.__repr__) or
00198                 (issubclass(typ, set) and r is set.__repr__) or
00199                 (issubclass(typ, frozenset) and r is frozenset.__repr__)
00200                ):
00201                 length = _len(object)
00202                 if issubclass(typ, list):
00203                     write('[')
00204                     endchar = ']'
00205                 elif issubclass(typ, set):
00206                     if not length:
00207                         write('set()')
00208                         return
00209                     write('{')
00210                     endchar = '}'
00211                     object = sorted(object, key=_safe_key)
00212                 elif issubclass(typ, frozenset):
00213                     if not length:
00214                         write('frozenset()')
00215                         return
00216                     write('frozenset({')
00217                     endchar = '})'
00218                     object = sorted(object, key=_safe_key)
00219                     indent += 10
00220                 else:
00221                     write('(')
00222                     endchar = ')'
00223                 if self._indent_per_level > 1:
00224                     write((self._indent_per_level - 1) * ' ')
00225                 if length:
00226                     context[objid] = 1
00227                     indent = indent + self._indent_per_level
00228                     self._format(object[0], stream, indent, allowance + 1,
00229                                  context, level)
00230                     if length > 1:
00231                         for ent in object[1:]:
00232                             write(',\n' + ' '*indent)
00233                             self._format(ent, stream, indent,
00234                                           allowance + 1, context, level)
00235                     indent = indent - self._indent_per_level
00236                     del context[objid]
00237                 if issubclass(typ, tuple) and length == 1:
00238                     write(',')
00239                 write(endchar)
00240                 return
00241 
00242         write(rep)

Here is the call graph for this function:

Here is the caller graph for this function:

def pprint.PrettyPrinter._repr (   self,
  object,
  context,
  level 
) [private]

Definition at line 243 of file pprint.py.

00243 
00244     def _repr(self, object, context, level):
00245         repr, readable, recursive = self.format(object, context.copy(),
00246                                                 self._depth, level)
00247         if not readable:
00248             self._readable = False
00249         if recursive:
00250             self._recursive = True
00251         return repr

Here is the call graph for this function:

def pprint.PrettyPrinter.format (   self,
  object,
  context,
  maxlevels,
  level 
)
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.

Reimplemented in test.test_pprint.DottedPrettyPrinter.

Definition at line 252 of file pprint.py.

00252 
00253     def format(self, object, context, maxlevels, level):
00254         """Format object for a specific context, returning a string
00255         and flags indicating whether the representation is 'readable'
00256         and whether the object represents a recursive construct.
00257         """
00258         return _safe_repr(object, context, maxlevels, level)
00259 
00260 
00261 # Return triple (repr_string, isreadable, isrecursive).

Here is the call graph for this function:

Here is the caller graph for this function:

def pprint.PrettyPrinter.isreadable (   self,
  object 
)

Definition at line 143 of file pprint.py.

00143 
00144     def isreadable(self, object):
00145         s, readable, recursive = self.format(object, {}, 0, 0)
00146         return readable and not recursive

Here is the call graph for this function:

def pprint.PrettyPrinter.isrecursive (   self,
  object 
)

Definition at line 140 of file pprint.py.

00140 
00141     def isrecursive(self, object):
00142         return self.format(object, {}, 0, 0)[2]

Here is the call graph for this function:

def pprint.PrettyPrinter.pformat (   self,
  object 
)

Definition at line 135 of file pprint.py.

00135 
00136     def pformat(self, object):
00137         sio = _StringIO()
00138         self._format(object, sio, 0, 0, {}, 0)
00139         return sio.getvalue()

Here is the call graph for this function:

def pprint.PrettyPrinter.pprint (   self,
  object 
)

Definition at line 131 of file pprint.py.

00131 
00132     def pprint(self, object):
00133         self._format(object, self._stream, 0, 0, {}, 0)
00134         self._stream.write("\n")

Here is the call graph for this function:


Member Data Documentation

Definition at line 123 of file pprint.py.

Definition at line 124 of file pprint.py.

Definition at line 153 of file pprint.py.

Definition at line 152 of file pprint.py.

Definition at line 127 of file pprint.py.

Definition at line 125 of file pprint.py.


The documentation for this class was generated from the following file: