Show More
@@ -343,8 +343,8 b' class PlainTextFormatter(BaseFormatter):' | |||||
343 | # something. |
|
343 | # something. | |
344 | enabled = Bool(True, config=False) |
|
344 | enabled = Bool(True, config=False) | |
345 |
|
345 | |||
346 |
# Look for a __pretty |
|
346 | # Look for a _repr_pretty_ methods to use for pretty printing. | |
347 |
print_method = Str('__pretty_ |
|
347 | print_method = Str('_repr_pretty_') | |
348 |
|
348 | |||
349 | # Whether to pretty-print or not. |
|
349 | # Whether to pretty-print or not. | |
350 | pprint = Bool(True, config=True) |
|
350 | pprint = Bool(True, config=True) |
@@ -29,12 +29,12 b'' | |||||
29 |
|
29 | |||
30 | The pretty library allows developers to add pretty printing rules for their |
|
30 | The pretty library allows developers to add pretty printing rules for their | |
31 | own objects. This process is straightforward. All you have to do is to |
|
31 | own objects. This process is straightforward. All you have to do is to | |
32 |
add a `__pretty |
|
32 | add a `_repr_pretty_` method to your object and call the methods on the | |
33 | pretty printer passed:: |
|
33 | pretty printer passed:: | |
34 |
|
34 | |||
35 | class MyObject(object): |
|
35 | class MyObject(object): | |
36 |
|
36 | |||
37 |
def __pretty |
|
37 | def _repr_pretty_(self, p, cycle): | |
38 | ... |
|
38 | ... | |
39 |
|
39 | |||
40 | Depending on the python version you want to support you have two |
|
40 | Depending on the python version you want to support you have two | |
@@ -42,13 +42,13 b'' | |||||
42 | compatibility one. |
|
42 | compatibility one. | |
43 |
|
43 | |||
44 |
|
44 | |||
45 |
Here the example implementation of a `__pretty |
|
45 | Here the example implementation of a `_repr_pretty_` method for a list | |
46 | subclass for python 2.5 and higher (python 2.5 requires the with statement |
|
46 | subclass for python 2.5 and higher (python 2.5 requires the with statement | |
47 | __future__ import):: |
|
47 | __future__ import):: | |
48 |
|
48 | |||
49 | class MyList(list): |
|
49 | class MyList(list): | |
50 |
|
50 | |||
51 |
def __pretty |
|
51 | def _repr_pretty_(self, p, cycle): | |
52 | if cycle: |
|
52 | if cycle: | |
53 | p.text('MyList(...)') |
|
53 | p.text('MyList(...)') | |
54 | else: |
|
54 | else: | |
@@ -75,7 +75,7 b'' | |||||
75 |
|
75 | |||
76 | class MyList(list): |
|
76 | class MyList(list): | |
77 |
|
77 | |||
78 |
def __pretty |
|
78 | def _repr_pretty_(self, p, cycle): | |
79 | if cycle: |
|
79 | if cycle: | |
80 | p.text('MyList(...)') |
|
80 | p.text('MyList(...)') | |
81 | else: |
|
81 | else: | |
@@ -164,7 +164,7 b' class PrettyPrinter(_PrettyPrinterBase):' | |||||
164 | """ |
|
164 | """ | |
165 | Baseclass for the `RepresentationPrinter` prettyprinter that is used to |
|
165 | Baseclass for the `RepresentationPrinter` prettyprinter that is used to | |
166 | generate pretty reprs of objects. Contrary to the `RepresentationPrinter` |
|
166 | generate pretty reprs of objects. Contrary to the `RepresentationPrinter` | |
167 |
this printer knows nothing about the default pprinters or the `__pretty |
|
167 | this printer knows nothing about the default pprinters or the `_repr_pretty_` | |
168 | callback method. |
|
168 | callback method. | |
169 | """ |
|
169 | """ | |
170 |
|
170 | |||
@@ -330,14 +330,14 b' class RepresentationPrinter(PrettyPrinter):' | |||||
330 | self.begin_group() |
|
330 | self.begin_group() | |
331 | try: |
|
331 | try: | |
332 | obj_class = getattr(obj, '__class__', None) or type(obj) |
|
332 | obj_class = getattr(obj, '__class__', None) or type(obj) | |
333 | if hasattr(obj_class, '__pretty__'): |
|
333 | # First try to find registered singleton printers for the type. | |
334 | return obj_class.__pretty__(obj, self, cycle) |
|
|||
335 | try: |
|
334 | try: | |
336 | printer = self.singleton_pprinters[obj_id] |
|
335 | printer = self.singleton_pprinters[obj_id] | |
337 | except (TypeError, KeyError): |
|
336 | except (TypeError, KeyError): | |
338 | pass |
|
337 | pass | |
339 | else: |
|
338 | else: | |
340 | return printer(obj, self, cycle) |
|
339 | return printer(obj, self, cycle) | |
|
340 | # Next look for type_printers. | |||
341 | for cls in _get_mro(obj_class): |
|
341 | for cls in _get_mro(obj_class): | |
342 | if cls in self.type_pprinters: |
|
342 | if cls in self.type_pprinters: | |
343 | return self.type_pprinters[cls](obj, self, cycle) |
|
343 | return self.type_pprinters[cls](obj, self, cycle) | |
@@ -345,6 +345,9 b' class RepresentationPrinter(PrettyPrinter):' | |||||
345 | printer = self._in_deferred_types(cls) |
|
345 | printer = self._in_deferred_types(cls) | |
346 | if printer is not None: |
|
346 | if printer is not None: | |
347 | return printer(obj, self, cycle) |
|
347 | return printer(obj, self, cycle) | |
|
348 | # Finally look for special method names. | |||
|
349 | if hasattr(obj_class, '_repr_pretty_'): | |||
|
350 | return obj_class._repr_pretty_(obj, self, cycle) | |||
348 | return _default_pprint(obj, self, cycle) |
|
351 | return _default_pprint(obj, self, cycle) | |
349 | finally: |
|
352 | finally: | |
350 | self.end_group() |
|
353 | self.end_group() |
@@ -14,8 +14,6 b' class Circle(object):' | |||||
14 | def _repr_pretty_(self, p, cycle): |
|
14 | def _repr_pretty_(self, p, cycle): | |
15 | p.text(u"\u25CB") |
|
15 | p.text(u"\u25CB") | |
16 |
|
16 | |||
17 | __pretty__ = _repr_pretty_ |
|
|||
18 |
|
||||
19 | def _repr_html_(self): |
|
17 | def _repr_html_(self): | |
20 | return "<h1>Cirle: radius=%s</h1>" % self.radius |
|
18 | return "<h1>Cirle: radius=%s</h1>" % self.radius | |
21 |
|
19 |
General Comments 0
You need to be logged in to leave comments.
Login now