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
| 322 | |
| 323 | |
| 324 | class RepresentationPrinter(PrettyPrinter): |
| 325 | """ |
| 326 | Special pretty printer that has a `pretty` method that calls the pretty |
| 327 | printer for a python object. |
| 328 | |
| 329 | This class stores processing data on `self` so you must *never* use |
| 330 | this class in a threaded environment. Always lock it or reinstanciate |
| 331 | it. |
| 332 | |
| 333 | Instances also have a verbose flag callbacks can access to control their |
| 334 | output. For example the default instance repr prints all attributes and |
| 335 | methods that are not prefixed by an underscore if the printer is in |
| 336 | verbose mode. |
| 337 | """ |
| 338 | |
| 339 | def __init__(self, output, verbose=False, max_width=79, newline='\n', |
| 340 | singleton_pprinters=None, type_pprinters=None, deferred_pprinters=None, |
| 341 | max_seq_length=MAX_SEQ_LENGTH): |
| 342 | |
| 343 | PrettyPrinter.__init__(self, output, max_width, newline, max_seq_length=max_seq_length) |
| 344 | self.verbose = verbose |
| 345 | self.stack = [] |
| 346 | if singleton_pprinters is None: |
| 347 | singleton_pprinters = _singleton_pprinters.copy() |
| 348 | self.singleton_pprinters = singleton_pprinters |
| 349 | if type_pprinters is None: |
| 350 | type_pprinters = _type_pprinters.copy() |
| 351 | self.type_pprinters = type_pprinters |
| 352 | if deferred_pprinters is None: |
| 353 | deferred_pprinters = _deferred_type_pprinters.copy() |
| 354 | self.deferred_pprinters = deferred_pprinters |
| 355 | |
| 356 | def pretty(self, obj): |
| 357 | """Pretty print the given object.""" |
| 358 | obj_id = id(obj) |
| 359 | cycle = obj_id in self.stack |
| 360 | self.stack.append(obj_id) |
| 361 | self.begin_group() |
| 362 | try: |
| 363 | obj_class = _safe_getattr(obj, '__class__', None) or type(obj) |
| 364 | # First try to find registered singleton printers for the type. |
| 365 | try: |
| 366 | printer = self.singleton_pprinters[obj_id] |
| 367 | except (TypeError, KeyError): |
| 368 | pass |
| 369 | else: |
| 370 | return printer(obj, self, cycle) |
| 371 | # Next walk the mro and check for either: |
| 372 | # 1) a registered printer |
| 373 | # 2) a _repr_pretty_ method |
| 374 | for cls in _get_mro(obj_class): |
| 375 | if cls in self.type_pprinters: |
| 376 | # printer registered in self.type_pprinters |
| 377 | return self.type_pprinters[cls](obj, self, cycle) |
| 378 | else: |
| 379 | # deferred printer |
| 380 | printer = self._in_deferred_types(cls) |
| 381 | if printer is not None: |