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