##// END OF EJS Templates
Remove refs to Python 2.4, use group context manager
Frazer McLean -
Show More
@@ -34,14 +34,8 b' pretty printer passed::'
34 34 def _repr_pretty_(self, p, cycle):
35 35 ...
36 36
37 Depending on the python version you want to support you have two
38 possibilities. The following list shows the python 2.5 version and the
39 compatibility one.
40
41
42 Here the example implementation of a `_repr_pretty_` method for a list
43 subclass for python 2.5 and higher (python 2.5 requires the with statement
44 __future__ import)::
37 Here is an example implementation of a `_repr_pretty_` method for a list
38 subclass::
45 39
46 40 class MyList(list):
47 41
@@ -64,36 +58,16 b' default space. `p.pretty` prettyprints another object using the pretty print'
64 58 method.
65 59
66 60 The first parameter to the `group` function specifies the extra indentation
67 of the next line. In this example the next item will either be not
68 breaked (if the items are short enough) or aligned with the right edge of
69 the opening bracked of `MyList`.
70
71 If you want to support python 2.4 and lower you can use this code::
72
73 class MyList(list):
74
75 def _repr_pretty_(self, p, cycle):
76 if cycle:
77 p.text('MyList(...)')
78 else:
79 p.begin_group(8, 'MyList([')
80 for idx, item in enumerate(self):
81 if idx:
82 p.text(',')
83 p.breakable()
84 p.pretty(item)
85 p.end_group(8, '])')
61 of the next line. In this example the next item will either be on the same
62 line (if the items are short enough) or aligned with the right edge of the
63 opening bracket of `MyList`.
86 64
87 65 If you just want to indent something you can use the group function
88 without open / close parameters. Under python 2.5 you can also use this
89 code::
66 without open / close parameters. Yu can also use this code::
90 67
91 68 with p.indent(2):
92 69 ...
93 70
94 Or under python2.4 you might want to modify ``p.indentation`` by hand but
95 this is rather ugly.
96
97 71 Inheritance diagram:
98 72
99 73 .. inheritance-diagram:: IPython.lib.pretty
@@ -818,43 +792,39 b' _singleton_pprinters = dict.fromkeys(map(id, [None, True, False, Ellipsis,'
818 792
819 793 def _defaultdict_pprint(obj, p, cycle):
820 794 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, ')')
795 with p.group(len(name) + 1, name + '(', ')'):
796 if cycle:
797 p.text('...')
798 else:
799 p.pretty(obj.default_factory)
800 p.text(',')
801 p.breakable()
802 p.pretty(dict(obj))
830 803
831 804 def _ordereddict_pprint(obj, p, cycle):
832 805 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, ')')
806 with p.group(len(name) + 1, name + '(', ')'):
807 if cycle:
808 p.text('...')
809 elif len(obj):
810 p.pretty(list(obj.items()))
839 811
840 812 def _deque_pprint(obj, p, cycle):
841 813 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, ')')
814 with p.group(len(name) + 1, name + '(', ')'):
815 if cycle:
816 p.text('...')
817 else:
818 p.pretty(list(obj))
848 819
849 820
850 821 def _counter_pprint(obj, p, cycle):
851 822 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, ')')
823 with p.group(len(name) + 1, name + '(', ')'):
824 if cycle:
825 p.text('...')
826 elif len(obj):
827 p.pretty(dict(obj))
858 828
859 829 for_type_by_name('collections', 'defaultdict', _defaultdict_pprint)
860 830 for_type_by_name('collections', 'OrderedDict', _ordereddict_pprint)
General Comments 0
You need to be logged in to leave comments. Login now