Advertisement
Guest User

ndarray-aware pprint

a guest
Jan 13th, 2015
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.28 KB | None | 0 0
  1. #  Small modification of pprint, to print resumedly numpy.ndarray objects
  2. #  Original author of pprint:   Fred L. Drake, Jr.
  3. #                               [email protected]
  4. #
  5. #  This is a simple little module I wrote to make life easier.  I didn't
  6. #  see anything quite like it in the library, though I may have overlooked
  7. #  something.  I wrote this when I was trying to read some heavily nested
  8. #  tuples with fairly non-descriptive content.  This is modeled very much
  9. #  after Lisp/Scheme - style pretty-printing of lists.  If you find it
  10. #  useful, thank small children who sleep at night.
  11.  
  12. """Support to pretty-print lists, tuples, & dictionaries recursively.
  13.  
  14. Very simple, but useful, especially in debugging data structures.
  15.  
  16. Classes
  17. -------
  18.  
  19. PrettyPrinter()
  20.    Handle pretty-printing operations onto a stream using a configured
  21.    set of formatting parameters.
  22.  
  23. Functions
  24. ---------
  25.  
  26. pformat()
  27.    Format a Python object into a pretty-printed representation.
  28.  
  29. pprint()
  30.    Pretty-print a Python object to a stream [default is sys.stdout].
  31.  
  32. saferepr()
  33.    Generate a 'standard' repr()-like value, but protect against recursive
  34.    data structures.
  35.  
  36. """
  37.  
  38. import sys as _sys
  39. import warnings
  40. try:
  41.     from numpy import ndarray
  42.     np_arrays = True
  43. except ImportError:
  44.     np_arrays = False
  45.  
  46. try:
  47.     from cStringIO import StringIO as _StringIO
  48. except ImportError:
  49.     from StringIO import StringIO as _StringIO
  50.  
  51. __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr",
  52.            "PrettyPrinter"]
  53.  
  54. # cache these for faster access:
  55. _commajoin = ", ".join
  56. _id = id
  57. _len = len
  58. _type = type
  59.  
  60.  
  61. def pprint(object, stream=None, indent=1, width=80, depth=None):
  62.     """Pretty-print a Python object to a stream [default is sys.stdout]."""
  63.     printer = PrettyPrinter(
  64.         stream=stream, indent=indent, width=width, depth=depth)
  65.     printer.pprint(object)
  66.  
  67. def pformat(object, indent=1, width=80, depth=None):
  68.     """Format a Python object into a pretty-printed representation."""
  69.     return PrettyPrinter(indent=indent, width=width, depth=depth).pformat(object)
  70.  
  71. def saferepr(object):
  72.     """Version of repr() which can handle recursive data structures."""
  73.     return _safe_repr(object, {}, None, 0)[0]
  74.  
  75. def isreadable(object):
  76.     """Determine if saferepr(object) is readable by eval()."""
  77.     return _safe_repr(object, {}, None, 0)[1]
  78.  
  79. def isrecursive(object):
  80.     """Determine if object requires a recursive representation."""
  81.     return _safe_repr(object, {}, None, 0)[2]
  82.  
  83. def _sorted(iterable):
  84.     with warnings.catch_warnings():
  85.         if _sys.py3kwarning:
  86.             warnings.filterwarnings("ignore", "comparing unequal types "
  87.                                     "not supported", DeprecationWarning)
  88.         return sorted(iterable)
  89.  
  90. class PrettyPrinter:
  91.     def __init__(self, indent=1, width=80, depth=None, stream=None):
  92.         """Handle pretty printing operations onto a stream using a set of
  93.        configured parameters.
  94.  
  95.        indent
  96.            Number of spaces to indent for each level of nesting.
  97.  
  98.        width
  99.            Attempted maximum number of columns in the output.
  100.  
  101.        depth
  102.            The maximum depth to print out nested structures.
  103.  
  104.        stream
  105.            The desired output stream.  If omitted (or false), the standard
  106.            output stream available at construction will be used.
  107.  
  108.        """
  109.         indent = int(indent)
  110.         width = int(width)
  111.         assert indent >= 0, "indent must be >= 0"
  112.         assert depth is None or depth > 0, "depth must be > 0"
  113.         assert width, "width must be != 0"
  114.         self._depth = depth
  115.         self._indent_per_level = indent
  116.         self._width = width
  117.         if stream is not None:
  118.             self._stream = stream
  119.         else:
  120.             self._stream = _sys.stdout
  121.  
  122.     def pprint(self, object):
  123.         self._format(object, self._stream, 0, 0, {}, 0)
  124.         self._stream.write("\n")
  125.  
  126.     def pformat(self, object):
  127.         sio = _StringIO()
  128.         self._format(object, sio, 0, 0, {}, 0)
  129.         return sio.getvalue()
  130.  
  131.     def isrecursive(self, object):
  132.         return self.format(object, {}, 0, 0)[2]
  133.  
  134.     def isreadable(self, object):
  135.         s, readable, recursive = self.format(object, {}, 0, 0)
  136.         return readable and not recursive
  137.  
  138.     def _format(self, object, stream, indent, allowance, context, level):
  139.         level = level + 1
  140.         objid = _id(object)
  141.         if objid in context:
  142.             stream.write(_recursion(object))
  143.             self._recursive = True
  144.             self._readable = False
  145.             return
  146.         rep = self._repr(object, context, level - 1)
  147.         typ = _type(object)
  148.         sepLines = _len(rep) > (self._width - 1 - indent - allowance)
  149.         write = stream.write
  150.  
  151.         if self._depth and level > self._depth:
  152.             write(rep)
  153.             return
  154.  
  155.         # Format numpy.ndarray object representations
  156.         if np_arrays and issubclass(typ, ndarray):
  157.             write('array(dtype:' + str(object.dtype))
  158.             write('; shape: ' + str(object.shape) + ')')
  159.             return
  160.  
  161.         r = getattr(typ, "__repr__", None)
  162.         if issubclass(typ, dict) and r is dict.__repr__:
  163.             write('{')
  164.             if self._indent_per_level > 1:
  165.                 write((self._indent_per_level - 1) * ' ')
  166.             length = _len(object)
  167.             if length:
  168.                 context[objid] = 1
  169.                 indent = indent + self._indent_per_level
  170.                 items = _sorted(object.items())
  171.                 key, ent = items[0]
  172.                 rep = self._repr(key, context, level)
  173.                 write(rep)
  174.                 write(': ')
  175.                 self._format(ent, stream, indent + _len(rep) + 2,
  176.                               allowance + 1, context, level)
  177.                 if length > 1:
  178.                     for key, ent in items[1:]:
  179.                         rep = self._repr(key, context, level)
  180.                         if sepLines:
  181.                             write(',\n%s%s: ' % (' '*indent, rep))
  182.                         else:
  183.                             write(', %s: ' % rep)
  184.                         self._format(ent, stream, indent + _len(rep) + 2,
  185.                                       allowance + 1, context, level)
  186.                 indent = indent - self._indent_per_level
  187.                 del context[objid]
  188.             write('}')
  189.             return
  190.  
  191.         if ((issubclass(typ, list) and r is list.__repr__) or
  192.             (issubclass(typ, tuple) and r is tuple.__repr__) or
  193.             (issubclass(typ, set) and r is set.__repr__) or
  194.             (issubclass(typ, frozenset) and r is frozenset.__repr__)
  195.            ):
  196.             length = _len(object)
  197.             if issubclass(typ, list):
  198.                 write('[')
  199.                 endchar = ']'
  200.             elif issubclass(typ, tuple):
  201.                 write('(')
  202.                 endchar = ')'
  203.             else:
  204.                 if not length:
  205.                     write(rep)
  206.                     return
  207.                 write(typ.__name__)
  208.                 write('([')
  209.                 endchar = '])'
  210.                 indent += len(typ.__name__) + 1
  211.                 object = _sorted(object)
  212.             if self._indent_per_level > 1 and sepLines:
  213.                 write((self._indent_per_level - 1) * ' ')
  214.             if length:
  215.                 context[objid] = 1
  216.                 indent = indent + self._indent_per_level
  217.                 self._format(object[0], stream, indent, allowance + 1,
  218.                              context, level)
  219.                 if length > 1:
  220.                     for ent in object[1:]:
  221.                         if sepLines:
  222.                             write(',\n' + ' '*indent)
  223.                         else:
  224.                             write(', ')
  225.                         self._format(ent, stream, indent,
  226.                                       allowance + 1, context, level)
  227.                 indent = indent - self._indent_per_level
  228.                 del context[objid]
  229.             if issubclass(typ, tuple) and length == 1:
  230.                 write(',')
  231.             write(endchar)
  232.             return
  233.  
  234.         write(rep)
  235.  
  236.     def _repr(self, object, context, level):
  237.         repr, readable, recursive = self.format(object, context.copy(),
  238.                                                 self._depth, level)
  239.         if not readable:
  240.             self._readable = False
  241.         if recursive:
  242.             self._recursive = True
  243.         return repr
  244.  
  245.     def format(self, object, context, maxlevels, level):
  246.         """Format object for a specific context, returning a string
  247.        and flags indicating whether the representation is 'readable'
  248.        and whether the object represents a recursive construct.
  249.        """
  250.         return _safe_repr(object, context, maxlevels, level)
  251.  
  252.  
  253. # Return triple (repr_string, isreadable, isrecursive).
  254.  
  255. def _safe_repr(object, context, maxlevels, level):
  256.     typ = _type(object)
  257.     if typ is str:
  258.         if 'locale' not in _sys.modules:
  259.             return repr(object), True, False
  260.         if "'" in object and '"' not in object:
  261.             closure = '"'
  262.             quotes = {'"': '\\"'}
  263.         else:
  264.             closure = "'"
  265.             quotes = {"'": "\\'"}
  266.         qget = quotes.get
  267.         sio = _StringIO()
  268.         write = sio.write
  269.         for char in object:
  270.             if char.isalpha():
  271.                 write(char)
  272.             else:
  273.                 write(qget(char, repr(char)[1:-1]))
  274.         return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False
  275.  
  276.     r = getattr(typ, "__repr__", None)
  277.     if issubclass(typ, dict) and r is dict.__repr__:
  278.         if not object:
  279.             return "{}", True, False
  280.         objid = _id(object)
  281.         if maxlevels and level >= maxlevels:
  282.             return "{...}", False, objid in context
  283.         if objid in context:
  284.             return _recursion(object), False, True
  285.         context[objid] = 1
  286.         readable = True
  287.         recursive = False
  288.         components = []
  289.         append = components.append
  290.         level += 1
  291.         saferepr = _safe_repr
  292.         for k, v in _sorted(object.items()):
  293.             krepr, kreadable, krecur = saferepr(k, context, maxlevels, level)
  294.             vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level)
  295.             append("%s: %s" % (krepr, vrepr))
  296.             readable = readable and kreadable and vreadable
  297.             if krecur or vrecur:
  298.                 recursive = True
  299.         del context[objid]
  300.         return "{%s}" % _commajoin(components), readable, recursive
  301.  
  302.     if (issubclass(typ, list) and r is list.__repr__) or \
  303.        (issubclass(typ, tuple) and r is tuple.__repr__):
  304.         if issubclass(typ, list):
  305.             if not object:
  306.                 return "[]", True, False
  307.             format = "[%s]"
  308.         elif _len(object) == 1:
  309.             format = "(%s,)"
  310.         else:
  311.             if not object:
  312.                 return "()", True, False
  313.             format = "(%s)"
  314.         objid = _id(object)
  315.         if maxlevels and level >= maxlevels:
  316.             return format % "...", False, objid in context
  317.         if objid in context:
  318.             return _recursion(object), False, True
  319.         context[objid] = 1
  320.         readable = True
  321.         recursive = False
  322.         components = []
  323.         append = components.append
  324.         level += 1
  325.         for o in object:
  326.             orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level)
  327.             append(orepr)
  328.             if not oreadable:
  329.                 readable = False
  330.             if orecur:
  331.                 recursive = True
  332.         del context[objid]
  333.         return format % _commajoin(components), readable, recursive
  334.  
  335.     rep = repr(object)
  336.     return rep, (rep and not rep.startswith('<')), False
  337.  
  338.  
  339. def _recursion(object):
  340.     return ("<Recursion on %s with id=%s>"
  341.             % (_type(object).__name__, _id(object)))
  342.  
  343.  
  344. def _perfcheck(object=None):
  345.     import time
  346.     if object is None:
  347.         object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000
  348.     p = PrettyPrinter()
  349.     t1 = time.time()
  350.     _safe_repr(object, {}, None, 0)
  351.     t2 = time.time()
  352.     p.pformat(object)
  353.     t3 = time.time()
  354.     print "_safe_repr:", t2 - t1
  355.     print "pformat:", t3 - t2
  356.  
  357. if __name__ == "__main__":
  358.     _perfcheck()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement