Permalink
Browse files

ENH: Refactor pretty to allow it to run without global type registries.

  • Loading branch information...
1 parent ba56cc9 commit 428050f8d67b2c143d9999cb1e7f57c702f9dff8 @rkern rkern committed Oct 29, 2010
Showing with 18 additions and 8 deletions.
  1. +18 −8 IPython/external/pretty.py
@@ -306,10 +306,21 @@ class RepresentationPrinter(PrettyPrinter):
verbose mode.
"""
- def __init__(self, output, verbose=False, max_width=79, newline='\n'):
+ def __init__(self, output, verbose=False, max_width=79, newline='\n',
+ singleton_pprinters=None, type_pprinters=None, deferred_pprinters=None):
+
PrettyPrinter.__init__(self, output, max_width, newline)
self.verbose = verbose
self.stack = []
+ if singleton_pprinters is None:
+ singleton_pprinters = _singleton_pprinters.copy()
+ self.singleton_pprinters = singleton_pprinters
+ if type_pprinters is None:
+ type_pprinters = _type_pprinters.copy()
+ self.type_pprinters = type_pprinters
+ if deferred_pprinters is None:
+ deferred_pprinters = _deferred_type_pprinters.copy()
+ self.deferred_pprinters = deferred_pprinters
def pretty(self, obj):
"""Pretty print the given object."""
@@ -322,14 +333,14 @@ def pretty(self, obj):
if hasattr(obj_class, '__pretty__'):
return obj_class.__pretty__(obj, self, cycle)
try:
- printer = _singleton_pprinters[obj_id]
+ printer = self.singleton_pprinters[obj_id]
except (TypeError, KeyError):
pass
else:
return printer(obj, self, cycle)
for cls in _get_mro(obj_class):
- if cls in _type_pprinters:
- return _type_pprinters[cls](obj, self, cycle)
+ if cls in self.type_pprinters:
+ return self.type_pprinters[cls](obj, self, cycle)
else:
printer = self._in_deferred_types(cls)
if printer is not None:
@@ -351,14 +362,13 @@ class is not in the registry. Successful matches will be moved to the
name = getattr(cls, '__name__', None)
key = (mod, name)
printer = None
- if key in _deferred_type_pprinters:
+ if key in self.deferred_pprinters:
# Move the printer over to the regular registry.
- printer = _deferred_type_pprinters.pop(key)
- _type_pprinters[cls] = printer
+ printer = self.deferred_pprinters.pop(key)
+ self.type_pprinters[cls] = printer
return printer
-
class Printable(object):
def output(self, stream, output_width):

0 comments on commit 428050f

Please sign in to comment.