Show More
@@ -343,8 +343,8 b' class PlainTextFormatter(BaseFormatter):' | |||
|
343 | 343 | # something. |
|
344 | 344 | enabled = Bool(True, config=False) |
|
345 | 345 | |
|
346 |
# Look for a __pretty |
|
|
347 |
print_method = Str('__pretty_ |
|
|
346 | # Look for a _repr_pretty_ methods to use for pretty printing. | |
|
347 | print_method = Str('_repr_pretty_') | |
|
348 | 348 | |
|
349 | 349 | # Whether to pretty-print or not. |
|
350 | 350 | pprint = Bool(True, config=True) |
@@ -29,12 +29,12 b'' | |||
|
29 | 29 | |
|
30 | 30 | The pretty library allows developers to add pretty printing rules for their |
|
31 | 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 | 33 | pretty printer passed:: |
|
34 | 34 | |
|
35 | 35 | class MyObject(object): |
|
36 | 36 | |
|
37 |
def __pretty |
|
|
37 | def _repr_pretty_(self, p, cycle): | |
|
38 | 38 | ... |
|
39 | 39 | |
|
40 | 40 | Depending on the python version you want to support you have two |
@@ -42,13 +42,13 b'' | |||
|
42 | 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 | 46 | subclass for python 2.5 and higher (python 2.5 requires the with statement |
|
47 | 47 | __future__ import):: |
|
48 | 48 | |
|
49 | 49 | class MyList(list): |
|
50 | 50 | |
|
51 |
def __pretty |
|
|
51 | def _repr_pretty_(self, p, cycle): | |
|
52 | 52 | if cycle: |
|
53 | 53 | p.text('MyList(...)') |
|
54 | 54 | else: |
@@ -75,7 +75,7 b'' | |||
|
75 | 75 | |
|
76 | 76 | class MyList(list): |
|
77 | 77 | |
|
78 |
def __pretty |
|
|
78 | def _repr_pretty_(self, p, cycle): | |
|
79 | 79 | if cycle: |
|
80 | 80 | p.text('MyList(...)') |
|
81 | 81 | else: |
@@ -164,7 +164,7 b' class PrettyPrinter(_PrettyPrinterBase):' | |||
|
164 | 164 | """ |
|
165 | 165 | Baseclass for the `RepresentationPrinter` prettyprinter that is used to |
|
166 | 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 | 168 | callback method. |
|
169 | 169 | """ |
|
170 | 170 | |
@@ -330,14 +330,14 b' class RepresentationPrinter(PrettyPrinter):' | |||
|
330 | 330 | self.begin_group() |
|
331 | 331 | try: |
|
332 | 332 | obj_class = getattr(obj, '__class__', None) or type(obj) |
|
333 | if hasattr(obj_class, '__pretty__'): | |
|
334 | return obj_class.__pretty__(obj, self, cycle) | |
|
333 | # First try to find registered singleton printers for the type. | |
|
335 | 334 | try: |
|
336 | 335 | printer = self.singleton_pprinters[obj_id] |
|
337 | 336 | except (TypeError, KeyError): |
|
338 | 337 | pass |
|
339 | 338 | else: |
|
340 | 339 | return printer(obj, self, cycle) |
|
340 | # Next look for type_printers. | |
|
341 | 341 | for cls in _get_mro(obj_class): |
|
342 | 342 | if cls in self.type_pprinters: |
|
343 | 343 | return self.type_pprinters[cls](obj, self, cycle) |
@@ -345,6 +345,9 b' class RepresentationPrinter(PrettyPrinter):' | |||
|
345 | 345 | printer = self._in_deferred_types(cls) |
|
346 | 346 | if printer is not None: |
|
347 | 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 | 351 | return _default_pprint(obj, self, cycle) |
|
349 | 352 | finally: |
|
350 | 353 | self.end_group() |
General Comments 0
You need to be logged in to leave comments.
Login now