summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-10-02 08:56:18 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-10-02 08:56:18 (GMT)
commit7c411a40413d9525aeb6114d71be3e012dbf81f5 (patch)
tree240af349b039b0efd5c52903bff110748815280a /Lib
parent092bd388ced26650cf0a5a4838a87f7ca8a9ea97 (diff)
downloadcpython-7c411a40413d9525aeb6114d71be3e012dbf81f5.zip
cpython-7c411a40413d9525aeb6114d71be3e012dbf81f5.tar.gz
cpython-7c411a40413d9525aeb6114d71be3e012dbf81f5.tar.bz2
Issue #19132: The pprint module now supports compact mode.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/pprint.py53
-rw-r--r--Lib/test/test_pprint.py12
2 files changed, 51 insertions, 14 deletions
diff --git a/Lib/pprint.py b/Lib/pprint.py
index f4bc59b..a7dfe28 100644
--- a/Lib/pprint.py
+++ b/Lib/pprint.py
@@ -49,15 +49,18 @@ _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."""
@@ -102,7 +105,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.
@@ -119,6 +123,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)
@@ -132,6 +139,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)
@@ -223,15 +231,9 @@ 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(',')
@@ -271,6 +273,29 @@ class PrettyPrinter:
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)
diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py
index 5a2050f..3d364c4 100644
--- a/Lib/test/test_pprint.py
+++ b/Lib/test/test_pprint.py
@@ -568,6 +568,18 @@ frozenset2({0,
formatted = pprint.pformat(special, width=width)
self.assertEqual(eval("(" + formatted + ")"), special)
+ def test_compact(self):
+ o = ([list(range(i * i)) for i in range(5)] +
+ [list(range(i)) for i in range(6)])
+ expected = """\
+[[], [0], [0, 1, 2, 3],
+ [0, 1, 2, 3, 4, 5, 6, 7, 8],
+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
+ 14, 15],
+ [], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3],
+ [0, 1, 2, 3, 4]]"""
+ self.assertEqual(pprint.pformat(o, width=48, compact=True), expected)
+
class DottedPrettyPrinter(pprint.PrettyPrinter):