Show More
@@ -1,61 +1,58 b'' | |||
|
1 | 1 | """ Use pretty.py for configurable pretty-printing. |
|
2 | 2 | |
|
3 | 3 | Register pretty-printers for types using pretty.for_type() or |
|
4 | 4 | pretty.for_type_by_name(). For example, to make a pretty-printer for numpy dtype |
|
5 | 5 | 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 | from IPython.external import pretty | |
|
8 | 9 | |
|
9 | 10 | def dtype_pprinter(obj, p, cycle): |
|
10 | 11 | if cycle: |
|
11 | 12 | return p.text('dtype(...)') |
|
12 | 13 | if obj.fields is None: |
|
13 | 14 | p.text(repr(obj)) |
|
14 | 15 | else: |
|
15 | 16 | p.begin_group(7, 'dtype([') |
|
16 | 17 | for i, field in enumerate(obj.descr): |
|
17 | 18 | if i > 0: |
|
18 | 19 | p.text(',') |
|
19 | 20 | p.breakable() |
|
20 | 21 | p.pretty(field) |
|
21 | 22 | p.end_group(7, '])') |
|
22 | 23 | |
|
23 | 24 | # If you want to have numpy always imported anyways: |
|
24 | 25 | import numpy |
|
25 | 26 | pretty.for_type(numpy.dtype, dtype_pprinter) |
|
26 | 27 | |
|
27 | 28 | # If you don't want to have numpy imported until it needs to be: |
|
28 | 29 | pretty.for_type_by_name('numpy', 'dtype', dtype_pprinter) |
|
29 | 30 | """ |
|
30 | 31 | |
|
31 | 32 | import IPython.ipapi |
|
32 | 33 | from IPython.genutils import Term |
|
33 | 34 | |
|
34 |
from IPython. |
|
|
35 | from IPython.external import pretty | |
|
35 | 36 | |
|
36 | 37 | ip = IPython.ipapi.get() |
|
37 | 38 | |
|
38 | 39 | def pretty_result_display(self, arg): |
|
39 | 40 | """ Uber-pretty-printing display hook. |
|
40 | 41 | |
|
41 | 42 | Called for displaying the result to the user. |
|
42 | 43 | """ |
|
43 | 44 | |
|
44 |
if |
|
|
45 |
|
|
|
45 | if ip.options.pprint: | |
|
46 | verbose = getattr(ip.options, 'pretty_verbose', False) | |
|
47 | out = pretty.pretty(arg, verbose=verbose) | |
|
46 | 48 | if '\n' in out: |
|
47 | 49 | # So that multi-line strings line up with the left column of |
|
48 | 50 | # the screen, instead of having the output prompt mess up |
|
49 | 51 | # their first line. |
|
50 | 52 | Term.cout.write('\n') |
|
51 | 53 | print >>Term.cout, out |
|
52 | 54 | else: |
|
53 | # By default, the interactive prompt uses repr() to display results, | |
|
54 | # so we should honor this. Users who'd rather use a different | |
|
55 | # mechanism can easily override this hook. | |
|
56 | print >>Term.cout, repr(arg) | |
|
57 | # the default display hook doesn't manipulate the value to put in history | |
|
58 | return None | |
|
59 | ||
|
60 | ip.set_hook('result_display', pretty_result_display) | |
|
55 | raise TryNext | |
|
56 | ||
|
57 | ip.set_hook('result_display', pretty_result_display, priority=99) | |
|
61 | 58 |
|
1 | NO CONTENT: file renamed from IPython/Extensions/pretty.py to IPython/external/pretty.py |
General Comments 0
You need to be logged in to leave comments.
Login now