diff options
Diffstat (limited to 'Lib/pprint.py')
-rw-r--r-- | Lib/pprint.py | 130 |
1 files changed, 92 insertions, 38 deletions
diff --git a/Lib/pprint.py b/Lib/pprint.py index 22be0b4b..2cbffed 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -34,6 +34,7 @@ saferepr() """ +import re import sys as _sys from collections import OrderedDict as _OrderedDict from io import StringIO as _StringIO @@ -41,22 +42,19 @@ from io import StringIO as _StringIO __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", "PrettyPrinter"] -# cache these for faster access: -_commajoin = ", ".join -_id = id -_len = len -_type = type - -def pprint(object, stream=None, indent=1, width=80, depth=None): +def pprint(object, stream=None, indent=1, width=80, depth=None, *, + compact=False): """Pretty-print a Python object to a stream [default is sys.stdout].""" printer = PrettyPrinter( - stream=stream, indent=indent, width=width, depth=depth) + stream=stream, indent=indent, width=width, depth=depth, + compact=compact) printer.pprint(object) -def pformat(object, indent=1, width=80, depth=None): +def pformat(object, indent=1, width=80, depth=None, *, compact=False): """Format a Python object into a pretty-printed representation.""" - return PrettyPrinter(indent=indent, width=width, depth=depth).pformat(object) + return PrettyPrinter(indent=indent, width=width, depth=depth, + compact=compact).pformat(object) def saferepr(object): """Version of repr() which can handle recursive data structures.""" @@ -101,7 +99,8 @@ def _safe_tuple(t): return _safe_key(t[0]), _safe_key(t[1]) class PrettyPrinter: - def __init__(self, indent=1, width=80, depth=None, stream=None): + def __init__(self, indent=1, width=80, depth=None, stream=None, *, + compact=False): """Handle pretty printing operations onto a stream using a set of configured parameters. @@ -118,6 +117,9 @@ class PrettyPrinter: The desired output stream. If omitted (or false), the standard output stream available at construction will be used. + compact + If true, several items will be combined in one line. + """ indent = int(indent) width = int(width) @@ -131,6 +133,7 @@ class PrettyPrinter: self._stream = stream else: self._stream = _sys.stdout + self._compact = bool(compact) def pprint(self, object): self._format(object, self._stream, 0, 0, {}, 0) @@ -150,28 +153,25 @@ class PrettyPrinter: def _format(self, object, stream, indent, allowance, context, level): level = level + 1 - objid = _id(object) + objid = id(object) if objid in context: stream.write(_recursion(object)) self._recursive = True self._readable = False return rep = self._repr(object, context, level - 1) - typ = _type(object) - sepLines = _len(rep) > (self._width - 1 - indent - allowance) + typ = type(object) + max_width = self._width - 1 - indent - allowance + sepLines = len(rep) > max_width write = stream.write - if self._depth and level > self._depth: - write(rep) - return - if sepLines: r = getattr(typ, "__repr__", None) if issubclass(typ, dict): write('{') if self._indent_per_level > 1: write((self._indent_per_level - 1) * ' ') - length = _len(object) + length = len(object) if length: context[objid] = 1 indent = indent + self._indent_per_level @@ -183,13 +183,13 @@ class PrettyPrinter: rep = self._repr(key, context, level) write(rep) write(': ') - self._format(ent, stream, indent + _len(rep) + 2, + self._format(ent, stream, indent + len(rep) + 2, allowance + 1, context, level) if length > 1: for key, ent in items[1:]: rep = self._repr(key, context, level) write(',\n%s%s: ' % (' '*indent, rep)) - self._format(ent, stream, indent + _len(rep) + 2, + self._format(ent, stream, indent + len(rep) + 2, allowance + 1, context, level) indent = indent - self._indent_per_level del context[objid] @@ -201,7 +201,7 @@ class PrettyPrinter: (issubclass(typ, set) and r is set.__repr__) or (issubclass(typ, frozenset) and r is frozenset.__repr__) ): - length = _len(object) + length = len(object) if issubclass(typ, list): write('[') endchar = ']' @@ -225,23 +225,77 @@ class PrettyPrinter: write((self._indent_per_level - 1) * ' ') if length: context[objid] = 1 - indent = indent + self._indent_per_level - self._format(object[0], stream, indent, allowance + 1, - context, level) - if length > 1: - for ent in object[1:]: - write(',\n' + ' '*indent) - self._format(ent, stream, indent, - allowance + 1, context, level) - indent = indent - self._indent_per_level + self._format_items(object, stream, + indent + self._indent_per_level, + allowance + 1, context, level) del context[objid] if issubclass(typ, tuple) and length == 1: write(',') write(endchar) return + if issubclass(typ, str) and len(object) > 0 and r is str.__repr__: + chunks = [] + lines = object.splitlines(True) + if level == 1: + indent += 1 + max_width -= 2 + for i, line in enumerate(lines): + rep = repr(line) + if len(rep) <= max_width: + chunks.append(rep) + else: + # A list of alternating (non-space, space) strings + parts = re.split(r'(\s+)', line) + [''] + current = '' + for i in range(0, len(parts), 2): + part = parts[i] + parts[i+1] + candidate = current + part + if len(repr(candidate)) > max_width: + if current: + chunks.append(repr(current)) + current = part + else: + current = candidate + if current: + chunks.append(repr(current)) + if len(chunks) == 1: + write(rep) + return + if level == 1: + write('(') + for i, rep in enumerate(chunks): + if i > 0: + write('\n' + ' '*indent) + write(rep) + if level == 1: + write(')') + return write(rep) + def _format_items(self, items, stream, indent, allowance, context, level): + write = stream.write + delimnl = ',\n' + ' ' * indent + delim = '' + width = max_width = self._width - indent - allowance + 2 + for ent in items: + if self._compact: + rep = self._repr(ent, context, level) + w = len(rep) + 2 + if width < w: + width = max_width + if delim: + delim = delimnl + if width >= w: + width -= w + write(delim) + delim = ', ' + write(rep) + continue + write(delim) + delim = delimnl + self._format(ent, stream, indent, allowance, context, level) + def _repr(self, object, context, level): repr, readable, recursive = self.format(object, context.copy(), self._depth, level) @@ -262,7 +316,7 @@ class PrettyPrinter: # Return triple (repr_string, isreadable, isrecursive). def _safe_repr(object, context, maxlevels, level): - typ = _type(object) + typ = type(object) if typ is str: if 'locale' not in _sys.modules: return repr(object), True, False @@ -286,7 +340,7 @@ def _safe_repr(object, context, maxlevels, level): if issubclass(typ, dict) and r is dict.__repr__: if not object: return "{}", True, False - objid = _id(object) + objid = id(object) if maxlevels and level >= maxlevels: return "{...}", False, objid in context if objid in context: @@ -307,7 +361,7 @@ def _safe_repr(object, context, maxlevels, level): if krecur or vrecur: recursive = True del context[objid] - return "{%s}" % _commajoin(components), readable, recursive + return "{%s}" % ", ".join(components), readable, recursive if (issubclass(typ, list) and r is list.__repr__) or \ (issubclass(typ, tuple) and r is tuple.__repr__): @@ -315,13 +369,13 @@ def _safe_repr(object, context, maxlevels, level): if not object: return "[]", True, False format = "[%s]" - elif _len(object) == 1: + elif len(object) == 1: format = "(%s,)" else: if not object: return "()", True, False format = "(%s)" - objid = _id(object) + objid = id(object) if maxlevels and level >= maxlevels: return format % "...", False, objid in context if objid in context: @@ -340,7 +394,7 @@ def _safe_repr(object, context, maxlevels, level): if orecur: recursive = True del context[objid] - return format % _commajoin(components), readable, recursive + return format % ", ".join(components), readable, recursive rep = repr(object) return rep, (rep and not rep.startswith('<')), False @@ -348,7 +402,7 @@ def _safe_repr(object, context, maxlevels, level): def _recursion(object): return ("<Recursion on %s with id=%s>" - % (_type(object).__name__, _id(object))) + % (type(object).__name__, id(object))) def _perfcheck(object=None): |