##// END OF EJS Templates
ENH: Implement and test the default pretty formatter.
Robert Kern -
Show More
@@ -0,0 +1,124 b''
1 # -*- coding: utf-8 -*-
2 """Displayhook formatters.
3
4 Authors:
5
6 * Robert Kern
7 """
8
9 import abc
10 from cStringIO import StringIO
11
12 from IPython.config.configurable import Configurable
13 from IPython.external import pretty
14 from IPython.utils.traitlets import Bool, Dict, Int, Str
15
16
17 class DefaultFormatter(Configurable):
18 """ The default pretty-printer.
19 """
20
21 # The ID of the formatter.
22 id = Str('default')
23
24 # The kind of data returned.
25 format = Str('text')
26
27 # Whether to pretty-print or not.
28 pprint = Bool(True)
29
30 # Whether to be verbose or not.
31 verbose = Bool(False)
32
33 # The maximum width.
34 max_width = Int(79)
35
36 # The newline character.
37 newline = Str('\n')
38
39 # The singleton prettyprinters.
40 # Maps the IDs of the builtin singleton objects to the format functions.
41 singleton_pprinters = Dict()
42 def _singleton_pprinters_default(self):
43 return pretty._singleton_pprinters.copy()
44
45 # The type-specific prettyprinters.
46 # Map type objects to the format functions.
47 type_pprinters = Dict()
48 def _type_pprinters_default(self):
49 return pretty._type_pprinters.copy()
50
51 # The deferred-import type-specific prettyprinters.
52 # Map (modulename, classname) pairs to the format functions.
53 deferred_pprinters = Dict()
54 def _deferred_pprinters_default(self):
55 return pretty._deferred_type_pprinters.copy()
56
57 #### FormatterABC interface ####
58
59 def __call__(self, obj):
60 """ Format the object.
61 """
62 if not self.pprint:
63 r = repr(obj)
64 if r is None:
65 # It can happen.
66 r = ''
67 return r
68 else:
69 stream = StringIO()
70 printer = pretty.RepresentationPrinter(stream, self.verbose,
71 self.max_width, self.newline,
72 singleton_pprinters=self.singleton_pprinters,
73 type_pprinters=self.type_pprinters,
74 deferred_pprinters=self.deferred_pprinters)
75 printer.pretty(obj)
76 printer.flush()
77 return stream.getvalue()
78
79
80 #### DefaultFormatter interface ####
81
82 def for_type(self, typ, func):
83 """
84 Add a pretty printer for a given type.
85 """
86 oldfunc = self.type_pprinters.get(typ, None)
87 if func is not None:
88 # To support easy restoration of old pprinters, we need to ignore
89 # Nones.
90 self.type_pprinters[typ] = func
91 return oldfunc
92
93 def for_type_by_name(self, type_module, type_name, func):
94 """
95 Add a pretty printer for a type specified by the module and name of
96 a type rather than the type object itself.
97 """
98 key = (type_module, type_name)
99 oldfunc = self.deferred_pprinters.get(key, None)
100 if func is not None:
101 # To support easy restoration of old pprinters, we need to ignore
102 # Nones.
103 self.deferred_pprinters[key] = func
104 return oldfunc
105
106
107 class FormatterABC(object):
108 """ Abstract base class for Formatters.
109 """
110 __metaclass__ = abc.ABCMeta
111
112 # The ID of the formatter.
113 id = 'abstract'
114
115 # The kind of data returned.
116 format = 'text'
117
118 @abc.abstractmethod
119 def __call__(self, obj):
120 """ Return a JSONable representation of the object.
121 """
122 return repr(obj)
123
124 FormatterABC.register(DefaultFormatter)
@@ -0,0 +1,30 b''
1 """Tests for the Formatters.
2 """
3
4 import nose.tools as nt
5
6 from IPython.core.formatters import FormatterABC, DefaultFormatter
7
8 class A(object):
9 def __repr__(self):
10 return 'A()'
11
12 class B(A):
13 def __repr__(self):
14 return 'B()'
15
16 def foo_printer(obj, pp, cycle):
17 pp.text('foo')
18
19 def test_pretty():
20 f = DefaultFormatter()
21 f.for_type(A, foo_printer)
22 nt.assert_equals(f(A()), 'foo')
23 nt.assert_equals(f(B()), 'foo')
24 f.pprint = False
25 nt.assert_equals(f(A()), 'A()')
26 nt.assert_equals(f(B()), 'B()')
27
28 def test_deferred():
29 f = DefaultFormatter()
30
General Comments 0
You need to be logged in to leave comments. Login now