Issue #23870: The pprint module now supports all standard collections
except named tuples.
This commit is contained in:
parent
0c59ff6430
commit
bedbf96e84
|
@ -404,6 +404,86 @@ class PrettyPrinter:
|
||||||
"""
|
"""
|
||||||
return _safe_repr(object, context, maxlevels, level)
|
return _safe_repr(object, context, maxlevels, level)
|
||||||
|
|
||||||
|
def _pprint_default_dict(self, object, stream, indent, allowance, context, level):
|
||||||
|
if not len(object):
|
||||||
|
stream.write(repr(object))
|
||||||
|
return
|
||||||
|
rdf = self._repr(object.default_factory, context, level)
|
||||||
|
cls = object.__class__
|
||||||
|
indent += len(cls.__name__) + 1
|
||||||
|
stream.write('%s(%s,\n%s' % (cls.__name__, rdf, ' ' * indent))
|
||||||
|
self._pprint_dict(object, stream, indent, allowance + 1, context, level)
|
||||||
|
stream.write(')')
|
||||||
|
|
||||||
|
_dispatch[_collections.defaultdict.__repr__] = _pprint_default_dict
|
||||||
|
|
||||||
|
def _pprint_counter(self, object, stream, indent, allowance, context, level):
|
||||||
|
if not len(object):
|
||||||
|
stream.write(repr(object))
|
||||||
|
return
|
||||||
|
cls = object.__class__
|
||||||
|
stream.write(cls.__name__ + '({')
|
||||||
|
if self._indent_per_level > 1:
|
||||||
|
stream.write((self._indent_per_level - 1) * ' ')
|
||||||
|
items = object.most_common()
|
||||||
|
self._format_dict_items(items, stream,
|
||||||
|
indent + len(cls.__name__) + 1, allowance + 2,
|
||||||
|
context, level)
|
||||||
|
stream.write('})')
|
||||||
|
|
||||||
|
_dispatch[_collections.Counter.__repr__] = _pprint_counter
|
||||||
|
|
||||||
|
def _pprint_chain_map(self, object, stream, indent, allowance, context, level):
|
||||||
|
if not len(object.maps):
|
||||||
|
stream.write(repr(object))
|
||||||
|
return
|
||||||
|
cls = object.__class__
|
||||||
|
stream.write(cls.__name__ + '(')
|
||||||
|
indent += len(cls.__name__) + 1
|
||||||
|
for i, m in enumerate(object.maps):
|
||||||
|
if i == len(object.maps) - 1:
|
||||||
|
self._format(m, stream, indent, allowance + 1, context, level)
|
||||||
|
stream.write(')')
|
||||||
|
else:
|
||||||
|
self._format(m, stream, indent, 1, context, level)
|
||||||
|
stream.write(',\n' + ' ' * indent)
|
||||||
|
|
||||||
|
_dispatch[_collections.ChainMap.__repr__] = _pprint_chain_map
|
||||||
|
|
||||||
|
def _pprint_deque(self, object, stream, indent, allowance, context, level):
|
||||||
|
if not len(object):
|
||||||
|
stream.write(repr(object))
|
||||||
|
return
|
||||||
|
cls = object.__class__
|
||||||
|
stream.write(cls.__name__ + '(')
|
||||||
|
indent += len(cls.__name__) + 1
|
||||||
|
stream.write('[')
|
||||||
|
if object.maxlen is None:
|
||||||
|
self._format_items(object, stream, indent, allowance + 2,
|
||||||
|
context, level)
|
||||||
|
stream.write('])')
|
||||||
|
else:
|
||||||
|
self._format_items(object, stream, indent, 2,
|
||||||
|
context, level)
|
||||||
|
rml = self._repr(object.maxlen, context, level)
|
||||||
|
stream.write('],\n%smaxlen=%s)' % (' ' * indent, rml))
|
||||||
|
|
||||||
|
_dispatch[_collections.deque.__repr__] = _pprint_deque
|
||||||
|
|
||||||
|
def _pprint_user_dict(self, object, stream, indent, allowance, context, level):
|
||||||
|
self._format(object.data, stream, indent, allowance, context, level - 1)
|
||||||
|
|
||||||
|
_dispatch[_collections.UserDict.__repr__] = _pprint_user_dict
|
||||||
|
|
||||||
|
def _pprint_user_list(self, object, stream, indent, allowance, context, level):
|
||||||
|
self._format(object.data, stream, indent, allowance, context, level - 1)
|
||||||
|
|
||||||
|
_dispatch[_collections.UserList.__repr__] = _pprint_user_list
|
||||||
|
|
||||||
|
def _pprint_user_string(self, object, stream, indent, allowance, context, level):
|
||||||
|
self._format(object.data, stream, indent, allowance, context, level - 1)
|
||||||
|
|
||||||
|
_dispatch[_collections.UserString.__repr__] = _pprint_user_string
|
||||||
|
|
||||||
# Return triple (repr_string, isreadable, isrecursive).
|
# Return triple (repr_string, isreadable, isrecursive).
|
||||||
|
|
||||||
|
|
|
@ -843,6 +843,154 @@ bytearray(b'\\x00\\x01\\x02\\x03'
|
||||||
[[[[[bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07'
|
[[[[[bytearray(b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07'
|
||||||
b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f')]]]]]""")
|
b'\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f')]]]]]""")
|
||||||
|
|
||||||
|
def test_default_dict(self):
|
||||||
|
d = collections.defaultdict(int)
|
||||||
|
self.assertEqual(pprint.pformat(d, width=1), "defaultdict(<class 'int'>, {})")
|
||||||
|
words = 'the quick brown fox jumped over a lazy dog'.split()
|
||||||
|
d = collections.defaultdict(int, zip(words, itertools.count()))
|
||||||
|
self.assertEqual(pprint.pformat(d),
|
||||||
|
"""\
|
||||||
|
defaultdict(<class 'int'>,
|
||||||
|
{'a': 6,
|
||||||
|
'brown': 2,
|
||||||
|
'dog': 8,
|
||||||
|
'fox': 3,
|
||||||
|
'jumped': 4,
|
||||||
|
'lazy': 7,
|
||||||
|
'over': 5,
|
||||||
|
'quick': 1,
|
||||||
|
'the': 0})""")
|
||||||
|
|
||||||
|
def test_counter(self):
|
||||||
|
d = collections.Counter()
|
||||||
|
self.assertEqual(pprint.pformat(d, width=1), "Counter()")
|
||||||
|
d = collections.Counter('senselessness')
|
||||||
|
self.assertEqual(pprint.pformat(d, width=40),
|
||||||
|
"""\
|
||||||
|
Counter({'s': 6,
|
||||||
|
'e': 4,
|
||||||
|
'n': 2,
|
||||||
|
'l': 1})""")
|
||||||
|
|
||||||
|
def test_chainmap(self):
|
||||||
|
d = collections.ChainMap()
|
||||||
|
self.assertEqual(pprint.pformat(d, width=1), "ChainMap({})")
|
||||||
|
words = 'the quick brown fox jumped over a lazy dog'.split()
|
||||||
|
items = list(zip(words, itertools.count()))
|
||||||
|
d = collections.ChainMap(dict(items))
|
||||||
|
self.assertEqual(pprint.pformat(d),
|
||||||
|
"""\
|
||||||
|
ChainMap({'a': 6,
|
||||||
|
'brown': 2,
|
||||||
|
'dog': 8,
|
||||||
|
'fox': 3,
|
||||||
|
'jumped': 4,
|
||||||
|
'lazy': 7,
|
||||||
|
'over': 5,
|
||||||
|
'quick': 1,
|
||||||
|
'the': 0})""")
|
||||||
|
d = collections.ChainMap(dict(items), collections.OrderedDict(items))
|
||||||
|
self.assertEqual(pprint.pformat(d),
|
||||||
|
"""\
|
||||||
|
ChainMap({'a': 6,
|
||||||
|
'brown': 2,
|
||||||
|
'dog': 8,
|
||||||
|
'fox': 3,
|
||||||
|
'jumped': 4,
|
||||||
|
'lazy': 7,
|
||||||
|
'over': 5,
|
||||||
|
'quick': 1,
|
||||||
|
'the': 0},
|
||||||
|
OrderedDict([('the', 0),
|
||||||
|
('quick', 1),
|
||||||
|
('brown', 2),
|
||||||
|
('fox', 3),
|
||||||
|
('jumped', 4),
|
||||||
|
('over', 5),
|
||||||
|
('a', 6),
|
||||||
|
('lazy', 7),
|
||||||
|
('dog', 8)]))""")
|
||||||
|
|
||||||
|
def test_deque(self):
|
||||||
|
d = collections.deque()
|
||||||
|
self.assertEqual(pprint.pformat(d, width=1), "deque([])")
|
||||||
|
d = collections.deque(maxlen=7)
|
||||||
|
self.assertEqual(pprint.pformat(d, width=1), "deque([], maxlen=7)")
|
||||||
|
words = 'the quick brown fox jumped over a lazy dog'.split()
|
||||||
|
d = collections.deque(zip(words, itertools.count()))
|
||||||
|
self.assertEqual(pprint.pformat(d),
|
||||||
|
"""\
|
||||||
|
deque([('the', 0),
|
||||||
|
('quick', 1),
|
||||||
|
('brown', 2),
|
||||||
|
('fox', 3),
|
||||||
|
('jumped', 4),
|
||||||
|
('over', 5),
|
||||||
|
('a', 6),
|
||||||
|
('lazy', 7),
|
||||||
|
('dog', 8)])""")
|
||||||
|
d = collections.deque(zip(words, itertools.count()), maxlen=7)
|
||||||
|
self.assertEqual(pprint.pformat(d),
|
||||||
|
"""\
|
||||||
|
deque([('brown', 2),
|
||||||
|
('fox', 3),
|
||||||
|
('jumped', 4),
|
||||||
|
('over', 5),
|
||||||
|
('a', 6),
|
||||||
|
('lazy', 7),
|
||||||
|
('dog', 8)],
|
||||||
|
maxlen=7)""")
|
||||||
|
|
||||||
|
def test_user_dict(self):
|
||||||
|
d = collections.UserDict()
|
||||||
|
self.assertEqual(pprint.pformat(d, width=1), "{}")
|
||||||
|
words = 'the quick brown fox jumped over a lazy dog'.split()
|
||||||
|
d = collections.UserDict(zip(words, itertools.count()))
|
||||||
|
self.assertEqual(pprint.pformat(d),
|
||||||
|
"""\
|
||||||
|
{'a': 6,
|
||||||
|
'brown': 2,
|
||||||
|
'dog': 8,
|
||||||
|
'fox': 3,
|
||||||
|
'jumped': 4,
|
||||||
|
'lazy': 7,
|
||||||
|
'over': 5,
|
||||||
|
'quick': 1,
|
||||||
|
'the': 0}""")
|
||||||
|
|
||||||
|
def test_user_dict(self):
|
||||||
|
d = collections.UserList()
|
||||||
|
self.assertEqual(pprint.pformat(d, width=1), "[]")
|
||||||
|
words = 'the quick brown fox jumped over a lazy dog'.split()
|
||||||
|
d = collections.UserList(zip(words, itertools.count()))
|
||||||
|
self.assertEqual(pprint.pformat(d),
|
||||||
|
"""\
|
||||||
|
[('the', 0),
|
||||||
|
('quick', 1),
|
||||||
|
('brown', 2),
|
||||||
|
('fox', 3),
|
||||||
|
('jumped', 4),
|
||||||
|
('over', 5),
|
||||||
|
('a', 6),
|
||||||
|
('lazy', 7),
|
||||||
|
('dog', 8)]""")
|
||||||
|
|
||||||
|
def test_user_string(self):
|
||||||
|
d = collections.UserString('')
|
||||||
|
self.assertEqual(pprint.pformat(d, width=1), "''")
|
||||||
|
d = collections.UserString('the quick brown fox jumped over a lazy dog')
|
||||||
|
self.assertEqual(pprint.pformat(d, width=20),
|
||||||
|
"""\
|
||||||
|
('the quick brown '
|
||||||
|
'fox jumped over '
|
||||||
|
'a lazy dog')""")
|
||||||
|
self.assertEqual(pprint.pformat({1: d}, width=20),
|
||||||
|
"""\
|
||||||
|
{1: 'the quick '
|
||||||
|
'brown fox '
|
||||||
|
'jumped over a '
|
||||||
|
'lazy dog'}""")
|
||||||
|
|
||||||
|
|
||||||
class DottedPrettyPrinter(pprint.PrettyPrinter):
|
class DottedPrettyPrinter(pprint.PrettyPrinter):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue