Show More
@@ -1,6 +1,35 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """Displayhook formatters. |
|
3 | 3 | |
|
4 | The DefaultFormatter is always present and may be configured from the ipython_config.py file. For example, to add a pretty-printer for a numpy.dtype object:: | |
|
5 | ||
|
6 | def dtype_pprinter(obj, p, cycle): | |
|
7 | if cycle: | |
|
8 | return p.text('dtype(...)') | |
|
9 | if hasattr(obj, 'fields'): | |
|
10 | if obj.fields is None: | |
|
11 | p.text(repr(obj)) | |
|
12 | else: | |
|
13 | p.begin_group(7, 'dtype([') | |
|
14 | for i, field in enumerate(obj.descr): | |
|
15 | if i > 0: | |
|
16 | p.text(',') | |
|
17 | p.breakable() | |
|
18 | p.pretty(field) | |
|
19 | p.end_group(7, '])') | |
|
20 | ||
|
21 | c.DefaultFormatter.deferred_pprinters = { | |
|
22 | ('numpy', 'dtype'): dtype_pprinter, | |
|
23 | } | |
|
24 | ||
|
25 | The deferred_pprinters dictionary is the preferred way to configure these | |
|
26 | pretty-printers. This allows you to define the pretty-printer without needing to | |
|
27 | import the type itself. The dictionary maps (modulename, typename) pairs to | |
|
28 | a function. | |
|
29 | ||
|
30 | See the `IPython.external.pretty` documentation for how to write | |
|
31 | pretty-printer functions. | |
|
32 | ||
|
4 | 33 | Authors: |
|
5 | 34 | |
|
6 | 35 | * Robert Kern |
General Comments 0
You need to be logged in to leave comments.
Login now