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