##// END OF EJS Templates
Add a types.MappingProxyType pretty printer #9821...
Danilo J. S. Bellini -
Show More
@@ -605,7 +605,8 b' def _dict_pprinter_factory(start, end, basetype=None):'
605 605
606 606 if cycle:
607 607 return p.text('{...}')
608 p.begin_group(1, start)
608 step = len(start)
609 p.begin_group(step, start)
609 610 keys = obj.keys()
610 611 # if dict isn't large enough to be truncated, sort keys before displaying
611 612 if not (p.max_seq_length and len(obj) >= p.max_seq_length):
@@ -621,7 +622,7 b' def _dict_pprinter_factory(start, end, basetype=None):'
621 622 p.pretty(key)
622 623 p.text(': ')
623 624 p.pretty(obj[key])
624 p.end_group(1, end)
625 p.end_group(step, end)
625 626 return inner
626 627
627 628
@@ -760,6 +761,8 b' try:'
760 761 _type_pprinters[types.ClassType] = _type_pprint
761 762 _type_pprinters[types.SliceType] = _repr_pprint
762 763 except AttributeError: # Python 3
764 _type_pprinters[types.MappingProxyType] = \
765 _dict_pprinter_factory('mappingproxy({', '})')
763 766 _type_pprinters[slice] = _repr_pprint
764 767
765 768 try:
@@ -7,11 +7,12 b''
7 7 from __future__ import print_function
8 8
9 9 from collections import Counter, defaultdict, deque, OrderedDict
10 import types, string
10 11
11 12 import nose.tools as nt
12 13
13 14 from IPython.lib import pretty
14 from IPython.testing.decorators import skip_without, py2_only
15 from IPython.testing.decorators import skip_without, py2_only, py3_only
15 16 from IPython.utils.py3compat import PY3, unicode_to_str
16 17
17 18 if PY3:
@@ -436,3 +437,48 b' def test_collections_counter():'
436 437 ]
437 438 for obj, expected in cases:
438 439 nt.assert_equal(pretty.pretty(obj), expected)
440
441 @py3_only
442 def test_mappingproxy():
443 MP = types.MappingProxyType
444 underlying_dict = {}
445 mp_recursive = MP(underlying_dict)
446 underlying_dict[2] = mp_recursive
447 underlying_dict[3] = underlying_dict
448
449 cases = [
450 (MP({}), "mappingproxy({})"),
451 (MP({None: MP({})}), "mappingproxy({None: mappingproxy({})})"),
452 (MP({k: k.upper() for k in string.ascii_lowercase}),
453 "mappingproxy({'a': 'A',\n"
454 " 'b': 'B',\n"
455 " 'c': 'C',\n"
456 " 'd': 'D',\n"
457 " 'e': 'E',\n"
458 " 'f': 'F',\n"
459 " 'g': 'G',\n"
460 " 'h': 'H',\n"
461 " 'i': 'I',\n"
462 " 'j': 'J',\n"
463 " 'k': 'K',\n"
464 " 'l': 'L',\n"
465 " 'm': 'M',\n"
466 " 'n': 'N',\n"
467 " 'o': 'O',\n"
468 " 'p': 'P',\n"
469 " 'q': 'Q',\n"
470 " 'r': 'R',\n"
471 " 's': 'S',\n"
472 " 't': 'T',\n"
473 " 'u': 'U',\n"
474 " 'v': 'V',\n"
475 " 'w': 'W',\n"
476 " 'x': 'X',\n"
477 " 'y': 'Y',\n"
478 " 'z': 'Z'})"),
479 (mp_recursive, "mappingproxy({2: {...}, 3: {2: {...}, 3: {...}}})"),
480 (underlying_dict,
481 "{2: mappingproxy({2: {...}, 3: {...}}), 3: {...}}"),
482 ]
483 for obj, expected in cases:
484 nt.assert_equal(pretty.pretty(obj), expected)
General Comments 0
You need to be logged in to leave comments. Login now