Show More
@@ -1,132 +1,132 b'' | |||||
1 | """ Use pretty.py for configurable pretty-printing. |
|
1 | """ Use pretty.py for configurable pretty-printing. | |
2 |
|
2 | |||
3 | Register pretty-printers for types using ipy_pretty.for_type() or |
|
3 | Register pretty-printers for types using ipy_pretty.for_type() or | |
4 | ipy_pretty.for_type_by_name(). For example, to use the example pretty-printer |
|
4 | ipy_pretty.for_type_by_name(). For example, to use the example pretty-printer | |
5 | for numpy dtype objects, add the following to your ipy_user_conf.py:: |
|
5 | for numpy dtype objects, add the following to your ipy_user_conf.py:: | |
6 |
|
6 | |||
7 | from IPython.Extensions import ipy_pretty |
|
7 | from IPython.Extensions import ipy_pretty | |
8 |
|
8 | |||
9 | ipy_pretty.activate() |
|
9 | ipy_pretty.activate() | |
10 |
|
10 | |||
11 | # If you want to have numpy always imported anyways: |
|
11 | # If you want to have numpy always imported anyways: | |
12 | import numpy |
|
12 | import numpy | |
13 | ipy_pretty.for_type(numpy.dtype, ipy_pretty.dtype_pprinter) |
|
13 | ipy_pretty.for_type(numpy.dtype, ipy_pretty.dtype_pprinter) | |
14 |
|
14 | |||
15 | # If you don't want to have numpy imported until it needs to be: |
|
15 | # If you don't want to have numpy imported until it needs to be: | |
16 | ipy_pretty.for_type_by_name('numpy', 'dtype', ipy_pretty.dtype_pprinter) |
|
16 | ipy_pretty.for_type_by_name('numpy', 'dtype', ipy_pretty.dtype_pprinter) | |
17 | """ |
|
17 | """ | |
18 |
|
18 | |||
19 | import IPython.ipapi |
|
19 | import IPython.ipapi | |
20 | from IPython.genutils import Term |
|
20 | from IPython.genutils import Term | |
21 |
|
21 | |||
22 | from IPython.external import pretty |
|
22 | from IPython.external import pretty | |
23 |
|
23 | |||
24 | ip = IPython.ipapi.get() |
|
24 | ip = IPython.ipapi.get() | |
25 |
|
25 | |||
26 |
|
26 | |||
27 | #### Implementation ############################################################ |
|
27 | #### Implementation ############################################################ | |
28 |
|
28 | |||
29 | def pretty_result_display(self, arg): |
|
29 | def pretty_result_display(self, arg): | |
30 | """ Uber-pretty-printing display hook. |
|
30 | """ Uber-pretty-printing display hook. | |
31 |
|
31 | |||
32 | Called for displaying the result to the user. |
|
32 | Called for displaying the result to the user. | |
33 | """ |
|
33 | """ | |
34 |
|
34 | |||
35 | if ip.options.pprint: |
|
35 | if ip.options.pprint: | |
36 | verbose = getattr(ip.options, 'pretty_verbose', False) |
|
36 | verbose = getattr(ip.options, 'pretty_verbose', False) | |
37 | out = pretty.pretty(arg, verbose=verbose) |
|
37 | out = pretty.pretty(arg, verbose=verbose) | |
38 | if '\n' in out: |
|
38 | if '\n' in out: | |
39 | # So that multi-line strings line up with the left column of |
|
39 | # So that multi-line strings line up with the left column of | |
40 | # the screen, instead of having the output prompt mess up |
|
40 | # the screen, instead of having the output prompt mess up | |
41 | # their first line. |
|
41 | # their first line. | |
42 | Term.cout.write('\n') |
|
42 | Term.cout.write('\n') | |
43 | print >>Term.cout, out |
|
43 | print >>Term.cout, out | |
44 | else: |
|
44 | else: | |
45 | raise TryNext |
|
45 | raise IPython.ipapi.TryNext | |
46 |
|
46 | |||
47 |
|
47 | |||
48 | #### API ####################################################################### |
|
48 | #### API ####################################################################### | |
49 |
|
49 | |||
50 | # Expose the for_type and for_type_by_name functions for easier use. |
|
50 | # Expose the for_type and for_type_by_name functions for easier use. | |
51 | for_type = pretty.for_type |
|
51 | for_type = pretty.for_type | |
52 | for_type_by_name = pretty.for_type_by_name |
|
52 | for_type_by_name = pretty.for_type_by_name | |
53 |
|
53 | |||
54 |
|
54 | |||
55 | # FIXME: write deactivate(). We need a way to remove a hook. |
|
55 | # FIXME: write deactivate(). We need a way to remove a hook. | |
56 | def activate(): |
|
56 | def activate(): | |
57 | """ Activate this extension. |
|
57 | """ Activate this extension. | |
58 | """ |
|
58 | """ | |
59 | ip.set_hook('result_display', pretty_result_display, priority=99) |
|
59 | ip.set_hook('result_display', pretty_result_display, priority=99) | |
60 |
|
60 | |||
61 |
|
61 | |||
62 | #### Example pretty-printers ################################################### |
|
62 | #### Example pretty-printers ################################################### | |
63 |
|
63 | |||
64 | def dtype_pprinter(obj, p, cycle): |
|
64 | def dtype_pprinter(obj, p, cycle): | |
65 | """ A pretty-printer for numpy dtype objects. |
|
65 | """ A pretty-printer for numpy dtype objects. | |
66 | """ |
|
66 | """ | |
67 | if cycle: |
|
67 | if cycle: | |
68 | return p.text('dtype(...)') |
|
68 | return p.text('dtype(...)') | |
69 | if obj.fields is None: |
|
69 | if obj.fields is None: | |
70 | p.text(repr(obj)) |
|
70 | p.text(repr(obj)) | |
71 | else: |
|
71 | else: | |
72 | p.begin_group(7, 'dtype([') |
|
72 | p.begin_group(7, 'dtype([') | |
73 | for i, field in enumerate(obj.descr): |
|
73 | for i, field in enumerate(obj.descr): | |
74 | if i > 0: |
|
74 | if i > 0: | |
75 | p.text(',') |
|
75 | p.text(',') | |
76 | p.breakable() |
|
76 | p.breakable() | |
77 | p.pretty(field) |
|
77 | p.pretty(field) | |
78 | p.end_group(7, '])') |
|
78 | p.end_group(7, '])') | |
79 |
|
79 | |||
80 |
|
80 | |||
81 | #### Tests ##################################################################### |
|
81 | #### Tests ##################################################################### | |
82 |
|
82 | |||
83 | def test_pretty(): |
|
83 | def test_pretty(): | |
84 | """ |
|
84 | """ | |
85 | In [1]: from IPython.Extensions import ipy_pretty |
|
85 | In [1]: from IPython.Extensions import ipy_pretty | |
86 |
|
86 | |||
87 | In [2]: ipy_pretty.activate() |
|
87 | In [2]: ipy_pretty.activate() | |
88 |
|
88 | |||
89 | In [3]: class A(object): |
|
89 | In [3]: class A(object): | |
90 | ...: def __repr__(self): |
|
90 | ...: def __repr__(self): | |
91 | ...: return 'A()' |
|
91 | ...: return 'A()' | |
92 | ...: |
|
92 | ...: | |
93 | ...: |
|
93 | ...: | |
94 |
|
94 | |||
95 | In [4]: a = A() |
|
95 | In [4]: a = A() | |
96 |
|
96 | |||
97 | In [5]: a |
|
97 | In [5]: a | |
98 | Out[5]: A() |
|
98 | Out[5]: A() | |
99 |
|
99 | |||
100 | In [6]: def a_pretty_printer(obj, p, cycle): |
|
100 | In [6]: def a_pretty_printer(obj, p, cycle): | |
101 | ...: p.text('<A>') |
|
101 | ...: p.text('<A>') | |
102 | ...: |
|
102 | ...: | |
103 | ...: |
|
103 | ...: | |
104 |
|
104 | |||
105 | In [7]: ipy_pretty.for_type(A, a_pretty_printer) |
|
105 | In [7]: ipy_pretty.for_type(A, a_pretty_printer) | |
106 |
|
106 | |||
107 | In [8]: a |
|
107 | In [8]: a | |
108 | Out[8]: <A> |
|
108 | Out[8]: <A> | |
109 |
|
109 | |||
110 | In [9]: class B(object): |
|
110 | In [9]: class B(object): | |
111 | ...: def __repr__(self): |
|
111 | ...: def __repr__(self): | |
112 | ...: return 'B()' |
|
112 | ...: return 'B()' | |
113 | ...: |
|
113 | ...: | |
114 | ...: |
|
114 | ...: | |
115 |
|
115 | |||
116 | In [10]: B.__module__, B.__name__ |
|
116 | In [10]: B.__module__, B.__name__ | |
117 | Out[10]: ('__main__', 'B') |
|
117 | Out[10]: ('__main__', 'B') | |
118 |
|
118 | |||
119 | In [11]: def b_pretty_printer(obj, p, cycle): |
|
119 | In [11]: def b_pretty_printer(obj, p, cycle): | |
120 | ....: p.text('<B>') |
|
120 | ....: p.text('<B>') | |
121 | ....: |
|
121 | ....: | |
122 | ....: |
|
122 | ....: | |
123 |
|
123 | |||
124 | In [12]: ipy_pretty.for_type_by_name('__main__', 'B', b_pretty_printer) |
|
124 | In [12]: ipy_pretty.for_type_by_name('__main__', 'B', b_pretty_printer) | |
125 |
|
125 | |||
126 | In [13]: b = B() |
|
126 | In [13]: b = B() | |
127 |
|
127 | |||
128 | In [14]: b |
|
128 | In [14]: b | |
129 | Out[14]: <B> |
|
129 | Out[14]: <B> | |
130 | """ |
|
130 | """ | |
131 | assert False, "This should only be doctested, not run." |
|
131 | assert False, "This should only be doctested, not run." | |
132 |
|
132 |
General Comments 0
You need to be logged in to leave comments.
Login now