##// 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 def _repr_pretty_(self, p, cycle):
34 def _repr_pretty_(self, p, cycle):
35 ...
35 ...
36
36
37 Depending on the python version you want to support you have two
37 Here is an example implementation of a `_repr_pretty_` method for a list
38 possibilities. The following list shows the python 2.5 version and the
38 subclass::
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)::
45
39
46 class MyList(list):
40 class MyList(list):
47
41
@@ -64,36 +58,16 b' default space. `p.pretty` prettyprints another object using the pretty print'
64 method.
58 method.
65
59
66 The first parameter to the `group` function specifies the extra indentation
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
61 of the next line. In this example the next item will either be on the same
68 breaked (if the items are short enough) or aligned with the right edge of
62 line (if the items are short enough) or aligned with the right edge of the
69 the opening bracked of `MyList`.
63 opening bracket 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, '])')
86
64
87 If you just want to indent something you can use the group function
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
66 without open / close parameters. Yu can also use this code::
89 code::
90
67
91 with p.indent(2):
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 Inheritance diagram:
71 Inheritance diagram:
98
72
99 .. inheritance-diagram:: IPython.lib.pretty
73 .. inheritance-diagram:: IPython.lib.pretty
@@ -818,7 +792,7 b' _singleton_pprinters = dict.fromkeys(map(id, [None, True, False, Ellipsis,'
818
792
819 def _defaultdict_pprint(obj, p, cycle):
793 def _defaultdict_pprint(obj, p, cycle):
820 name = 'defaultdict'
794 name = 'defaultdict'
821 p.begin_group(len(name) + 1, name + '(')
795 with p.group(len(name) + 1, name + '(', ')'):
822 if cycle:
796 if cycle:
823 p.text('...')
797 p.text('...')
824 else:
798 else:
@@ -826,35 +800,31 b' def _defaultdict_pprint(obj, p, cycle):'
826 p.text(',')
800 p.text(',')
827 p.breakable()
801 p.breakable()
828 p.pretty(dict(obj))
802 p.pretty(dict(obj))
829 p.end_group(len(name) + 1, ')')
830
803
831 def _ordereddict_pprint(obj, p, cycle):
804 def _ordereddict_pprint(obj, p, cycle):
832 name = 'OrderedDict'
805 name = 'OrderedDict'
833 p.begin_group(len(name) + 1, name + '(')
806 with p.group(len(name) + 1, name + '(', ')'):
834 if cycle:
807 if cycle:
835 p.text('...')
808 p.text('...')
836 elif len(obj):
809 elif len(obj):
837 p.pretty(list(obj.items()))
810 p.pretty(list(obj.items()))
838 p.end_group(len(name) + 1, ')')
839
811
840 def _deque_pprint(obj, p, cycle):
812 def _deque_pprint(obj, p, cycle):
841 name = 'deque'
813 name = 'deque'
842 p.begin_group(len(name) + 1, name + '(')
814 with p.group(len(name) + 1, name + '(', ')'):
843 if cycle:
815 if cycle:
844 p.text('...')
816 p.text('...')
845 else:
817 else:
846 p.pretty(list(obj))
818 p.pretty(list(obj))
847 p.end_group(len(name) + 1, ')')
848
819
849
820
850 def _counter_pprint(obj, p, cycle):
821 def _counter_pprint(obj, p, cycle):
851 name = 'Counter'
822 name = 'Counter'
852 p.begin_group(len(name) + 1, name + '(')
823 with p.group(len(name) + 1, name + '(', ')'):
853 if cycle:
824 if cycle:
854 p.text('...')
825 p.text('...')
855 elif len(obj):
826 elif len(obj):
856 p.pretty(dict(obj))
827 p.pretty(dict(obj))
857 p.end_group(len(name) + 1, ')')
858
828
859 for_type_by_name('collections', 'defaultdict', _defaultdict_pprint)
829 for_type_by_name('collections', 'defaultdict', _defaultdict_pprint)
860 for_type_by_name('collections', 'OrderedDict', _ordereddict_pprint)
830 for_type_by_name('collections', 'OrderedDict', _ordereddict_pprint)
General Comments 0
You need to be logged in to leave comments. Login now