Special pretty printer that has a `pretty` method that calls the pretty printer for a python object. This class stores processing data on `self` so you must *never* use this class in a threaded environment. Always lock it or reinstanciate it. Instances also have a verbose
| 340 | |
| 341 | |
| 342 | class RepresentationPrinter(PrettyPrinter): |
| 343 | """ |
| 344 | Special pretty printer that has a `pretty` method that calls the pretty |
| 345 | printer for a python object. |
| 346 | |
| 347 | This class stores processing data on `self` so you must *never* use |
| 348 | this class in a threaded environment. Always lock it or reinstanciate |
| 349 | it. |
| 350 | |
| 351 | Instances also have a verbose flag callbacks can access to control their |
| 352 | output. For example the default instance repr prints all attributes and |
| 353 | methods that are not prefixed by an underscore if the printer is in |
| 354 | verbose mode. |
| 355 | """ |
| 356 | |
| 357 | def __init__(self, output, verbose=False, max_width=79, newline='\n', |
| 358 | singleton_pprinters=None, type_pprinters=None, deferred_pprinters=None, |
| 359 | max_seq_length=MAX_SEQ_LENGTH): |
| 360 | |
| 361 | PrettyPrinter.__init__(self, output, max_width, newline, max_seq_length=max_seq_length) |
| 362 | self.verbose = verbose |
| 363 | self.stack = [] |
| 364 | if singleton_pprinters is None: |
| 365 | singleton_pprinters = _singleton_pprinters.copy() |
| 366 | self.singleton_pprinters = singleton_pprinters |
| 367 | if type_pprinters is None: |
| 368 | type_pprinters = _type_pprinters.copy() |
| 369 | self.type_pprinters = type_pprinters |
| 370 | if deferred_pprinters is None: |
| 371 | deferred_pprinters = _deferred_type_pprinters.copy() |
| 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: |