MCPcopy Create free account
hub / github.com/ipython/ipython / pretty

Method pretty

IPython/lib/pretty.py:356–399  ·  view source on GitHub ↗

Pretty print the given object.

(self, obj)

Source from the content-addressed store, hash-verified

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:
382 return printer(obj, self, cycle)
383 else:
384 # Finally look for special method names.
385 # Some objects automatically create any requested
386 # attribute. Try to ignore most of them by checking for
387 # callability.
388 if '_repr_pretty_' in cls.__dict__:
389 meth = cls._repr_pretty_
390 if callable(meth):
391 return meth(obj, self, cycle)
392 if cls is not object \
393 and callable(cls.__dict__.get('__repr__')):
394 return _repr_pprint(obj, self, cycle)
395
396 return _default_pprint(obj, self, cycle)
397 finally:
398 self.end_group()
399 self.stack.pop()
400
401 def _in_deferred_types(self, cls):
402 """

Callers 15

__call__Method · 0.95
prettyFunction · 0.95
pprintFunction · 0.95
test_basic_classFunction · 0.95
_default_pprintFunction · 0.80
innerFunction · 0.80
_super_pprintFunction · 0.80
_exception_pprintFunction · 0.80
_defaultdict_pprintFunction · 0.80
_ordereddict_pprintFunction · 0.80
_deque_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