MCPcopy Index your code
hub / github.com/ipython/ipython / pretty

Method pretty

IPython/lib/pretty.py:374–425  ·  view source on GitHub ↗

Pretty print the given object.

(self, obj)

Source from the content-addressed store, hash-verified

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 """

Callers 15

__call__Method · 0.95
prettyFunction · 0.95
pprintFunction · 0.95
test_basic_classFunction · 0.95
_repr_pretty_Method · 0.80
_default_pprintFunction · 0.80
innerFunction · 0.80
_super_pprintFunction · 0.80
_re_pattern_pprintFunction · 0.80
_exception_pprintFunction · 0.80
_defaultdict_pprintFunction · 0.80

Calls 8

_in_deferred_typesMethod · 0.95
_safe_getattrFunction · 0.85
_get_mroFunction · 0.85
_repr_pprintFunction · 0.85
_default_pprintFunction · 0.85
begin_groupMethod · 0.80
end_groupMethod · 0.80
popMethod · 0.80

Tested by 15

test_basic_classFunction · 0.76
_repr_pretty_Method · 0.64
test_indentationFunction · 0.64
test_dispatchFunction · 0.64
test_setsFunction · 0.64
test_pprint_nomodFunction · 0.64
test_pprint_breakFunction · 0.64
test_pprint_break_reprFunction · 0.64
test_bad_reprFunction · 0.64
test_really_bad_reprFunction · 0.64