Pretty print the given object.
(self, obj)
| 372 | self.deferred_pprinters = deferred_pprinters |
| 373 | |
| 374 | def pretty(self, obj): |
| 375 | """Pretty print the given object.""" |
| 376 | obj_id = id(obj) |
| 377 | cycle = obj_id in self.stack |
| 378 | self.stack.append(obj_id) |
| 379 | self.begin_group() |
| 380 | try: |
| 381 | obj_class = _safe_getattr(obj, '__class__', None) or type(obj) |
| 382 | # First try to find registered singleton printers for the type. |
| 383 | try: |
| 384 | printer = self.singleton_pprinters[obj_id] |
| 385 | except (TypeError, KeyError): |
| 386 | pass |
| 387 | else: |
| 388 | return printer(obj, self, cycle) |
| 389 | # Next walk the mro and check for either: |
| 390 | # 1) a registered printer |
| 391 | # 2) a _repr_pretty_ method |
| 392 | for cls in _get_mro(obj_class): |
| 393 | if cls in self.type_pprinters: |
| 394 | # printer registered in self.type_pprinters |
| 395 | return self.type_pprinters[cls](obj, self, cycle) |
| 396 | else: |
| 397 | # deferred printer |
| 398 | printer = self._in_deferred_types(cls) |
| 399 | if printer is not None: |
| 400 | return printer(obj, self, cycle) |
| 401 | else: |
| 402 | # Finally look for special method names. |
| 403 | # Some objects automatically create any requested |
| 404 | # attribute. Try to ignore most of them by checking for |
| 405 | # callability. |
| 406 | if '_repr_pretty_' in cls.__dict__: |
| 407 | meth = cls._repr_pretty_ |
| 408 | if callable(meth): |
| 409 | return meth(obj, self, cycle) |
| 410 | if ( |
| 411 | cls is not object |
| 412 | # check if cls defines __repr__ |
| 413 | and "__repr__" in cls.__dict__ |
| 414 | # check if __repr__ is callable. |
| 415 | # Note: we need to test getattr(cls, '__repr__') |
| 416 | # instead of cls.__dict__['__repr__'] |
| 417 | # in order to work with descriptors like partialmethod, |
| 418 | and callable(_safe_getattr(cls, "__repr__", None)) |
| 419 | ): |
| 420 | return _repr_pprint(obj, self, cycle) |
| 421 | |
| 422 | return _default_pprint(obj, self, cycle) |
| 423 | finally: |
| 424 | self.end_group() |
| 425 | self.stack.pop() |
| 426 | |
| 427 | def _in_deferred_types(self, cls): |
| 428 | """ |