Show More
@@ -816,6 +816,51 b' _singleton_pprinters = dict.fromkeys(map(id, [None, True, False, Ellipsis,' | |||||
816 | NotImplemented]), _repr_pprint) |
|
816 | NotImplemented]), _repr_pprint) | |
817 |
|
817 | |||
818 |
|
818 | |||
|
819 | def _defaultdict_pprint(obj, p, cycle): | |||
|
820 | name = 'defaultdict' | |||
|
821 | p.begin_group(len(name) + 1, name + '(') | |||
|
822 | if cycle: | |||
|
823 | p.text('...') | |||
|
824 | else: | |||
|
825 | p.pretty(obj.default_factory) | |||
|
826 | p.text(',') | |||
|
827 | p.breakable() | |||
|
828 | p.pretty(dict(obj)) | |||
|
829 | p.end_group(len(name) + 1, ')') | |||
|
830 | ||||
|
831 | def _ordereddict_pprint(obj, p, cycle): | |||
|
832 | name = 'OrderedDict' | |||
|
833 | p.begin_group(len(name) + 1, name + '(') | |||
|
834 | if cycle: | |||
|
835 | p.text('...') | |||
|
836 | elif len(obj): | |||
|
837 | p.pretty(list(obj.items())) | |||
|
838 | p.end_group(len(name) + 1, ')') | |||
|
839 | ||||
|
840 | def _deque_pprint(obj, p, cycle): | |||
|
841 | name = 'deque' | |||
|
842 | p.begin_group(len(name) + 1, name + '(') | |||
|
843 | if cycle: | |||
|
844 | p.text('...') | |||
|
845 | else: | |||
|
846 | p.pretty(list(obj)) | |||
|
847 | p.end_group(len(name) + 1, ')') | |||
|
848 | ||||
|
849 | ||||
|
850 | def _counter_pprint(obj, p, cycle): | |||
|
851 | name = 'Counter' | |||
|
852 | p.begin_group(len(name) + 1, name + '(') | |||
|
853 | if cycle: | |||
|
854 | p.text('...') | |||
|
855 | elif len(obj): | |||
|
856 | p.pretty(dict(obj)) | |||
|
857 | p.end_group(len(name) + 1, ')') | |||
|
858 | ||||
|
859 | for_type_by_name('collections', 'defaultdict', _defaultdict_pprint) | |||
|
860 | for_type_by_name('collections', 'OrderedDict', _ordereddict_pprint) | |||
|
861 | for_type_by_name('collections', 'deque', _deque_pprint) | |||
|
862 | for_type_by_name('collections', 'Counter', _counter_pprint) | |||
|
863 | ||||
819 | if __name__ == '__main__': |
|
864 | if __name__ == '__main__': | |
820 | from random import randrange |
|
865 | from random import randrange | |
821 | class Foo(object): |
|
866 | class Foo(object): |
@@ -6,6 +6,8 b'' | |||||
6 |
|
6 | |||
7 | from __future__ import print_function |
|
7 | from __future__ import print_function | |
8 |
|
8 | |||
|
9 | from collections import Counter, defaultdict, deque, OrderedDict | |||
|
10 | ||||
9 | import nose.tools as nt |
|
11 | import nose.tools as nt | |
10 |
|
12 | |||
11 | from IPython.lib import pretty |
|
13 | from IPython.lib import pretty | |
@@ -268,3 +270,89 b' def test_basic_class():' | |||||
268 |
|
270 | |||
269 | nt.assert_equal(output, '%s.MyObj' % __name__) |
|
271 | nt.assert_equal(output, '%s.MyObj' % __name__) | |
270 | nt.assert_true(type_pprint_wrapper.called) |
|
272 | nt.assert_true(type_pprint_wrapper.called) | |
|
273 | ||||
|
274 | ||||
|
275 | def test_collections_defaultdict(): | |||
|
276 | # Create defaultdicts with cycles | |||
|
277 | a = defaultdict() | |||
|
278 | a.default_factory = a | |||
|
279 | b = defaultdict(list) | |||
|
280 | b['key'] = b | |||
|
281 | ||||
|
282 | # Dictionary order cannot be relied on, test against single keys. | |||
|
283 | cases = [ | |||
|
284 | (defaultdict(list), 'defaultdict(list, {})'), | |||
|
285 | (defaultdict(list, {'key': '-' * 50}), | |||
|
286 | "defaultdict(list,\n" | |||
|
287 | " {'key': '--------------------------------------------------'})"), | |||
|
288 | (a, 'defaultdict(defaultdict(...), {})'), | |||
|
289 | (b, "defaultdict(list, {'key': defaultdict(...)})"), | |||
|
290 | ] | |||
|
291 | for obj, expected in cases: | |||
|
292 | nt.assert_equal(pretty.pretty(obj), expected) | |||
|
293 | ||||
|
294 | ||||
|
295 | def test_collections_ordereddict(): | |||
|
296 | # Create OrderedDict with cycle | |||
|
297 | a = OrderedDict() | |||
|
298 | a['key'] = a | |||
|
299 | ||||
|
300 | cases = [ | |||
|
301 | (OrderedDict(), 'OrderedDict()'), | |||
|
302 | (OrderedDict((i, i) for i in range(1000, 1010)), | |||
|
303 | 'OrderedDict([(1000, 1000),\n' | |||
|
304 | ' (1001, 1001),\n' | |||
|
305 | ' (1002, 1002),\n' | |||
|
306 | ' (1003, 1003),\n' | |||
|
307 | ' (1004, 1004),\n' | |||
|
308 | ' (1005, 1005),\n' | |||
|
309 | ' (1006, 1006),\n' | |||
|
310 | ' (1007, 1007),\n' | |||
|
311 | ' (1008, 1008),\n' | |||
|
312 | ' (1009, 1009)])'), | |||
|
313 | (a, "OrderedDict([('key', OrderedDict(...))])"), | |||
|
314 | ] | |||
|
315 | for obj, expected in cases: | |||
|
316 | nt.assert_equal(pretty.pretty(obj), expected) | |||
|
317 | ||||
|
318 | ||||
|
319 | def test_collections_deque(): | |||
|
320 | # Create deque with cycle | |||
|
321 | a = deque() | |||
|
322 | a.append(a) | |||
|
323 | ||||
|
324 | cases = [ | |||
|
325 | (deque(), 'deque([])'), | |||
|
326 | (deque(i for i in range(1000, 1020)), | |||
|
327 | 'deque([1000,\n' | |||
|
328 | ' 1001,\n' | |||
|
329 | ' 1002,\n' | |||
|
330 | ' 1003,\n' | |||
|
331 | ' 1004,\n' | |||
|
332 | ' 1005,\n' | |||
|
333 | ' 1006,\n' | |||
|
334 | ' 1007,\n' | |||
|
335 | ' 1008,\n' | |||
|
336 | ' 1009,\n' | |||
|
337 | ' 1010,\n' | |||
|
338 | ' 1011,\n' | |||
|
339 | ' 1012,\n' | |||
|
340 | ' 1013,\n' | |||
|
341 | ' 1014,\n' | |||
|
342 | ' 1015,\n' | |||
|
343 | ' 1016,\n' | |||
|
344 | ' 1017,\n' | |||
|
345 | ' 1018,\n' | |||
|
346 | ' 1019])'), | |||
|
347 | (a, 'deque([deque(...)])'), | |||
|
348 | ] | |||
|
349 | for obj, expected in cases: | |||
|
350 | nt.assert_equal(pretty.pretty(obj), expected) | |||
|
351 | ||||
|
352 | def test_collections_counter(): | |||
|
353 | cases = [ | |||
|
354 | (Counter(), 'Counter()'), | |||
|
355 | (Counter(a=1), "Counter({'a': 1})"), | |||
|
356 | ] | |||
|
357 | for obj, expected in cases: | |||
|
358 | nt.assert_equal(pretty.pretty(obj), expected) |
General Comments 0
You need to be logged in to leave comments.
Login now