pprint.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # Author: Fred L. Drake, Jr.
  2. # [email protected]
  3. #
  4. # This is a simple little module I wrote to make life easier. I didn't
  5. # see anything quite like it in the library, though I may have overlooked
  6. # something. I wrote this when I was trying to read some heavily nested
  7. # tuples with fairly non-descriptive content. This is modeled very much
  8. # after Lisp/Scheme - style pretty-printing of lists. If you find it
  9. # useful, thank small children who sleep at night.
  10. """Support to pretty-print lists, tuples, & dictionaries recursively.
  11. Very simple, but useful, especially in debugging data structures.
  12. Classes
  13. -------
  14. PrettyPrinter()
  15. Handle pretty-printing operations onto a stream using a configured
  16. set of formatting parameters.
  17. Functions
  18. ---------
  19. pformat()
  20. Format a Python object into a pretty-printed representation.
  21. pprint()
  22. Pretty-print a Python object to a stream [default is sys.stdout].
  23. saferepr()
  24. Generate a 'standard' repr()-like value, but protect against recursive
  25. data structures.
  26. """
  27. import sys as _sys
  28. from cStringIO import StringIO as _StringIO
  29. __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr",
  30. "PrettyPrinter"]
  31. # cache these for faster access:
  32. _commajoin = ", ".join
  33. _id = id
  34. _len = len
  35. _type = type
  36. def pprint(object, stream=None, indent=1, width=80, depth=None):
  37. """Pretty-print a Python object to a stream [default is sys.stdout]."""
  38. printer = PrettyPrinter(
  39. stream=stream, indent=indent, width=width, depth=depth)
  40. printer.pprint(object)
  41. def pformat(object, indent=1, width=80, depth=None):
  42. """Format a Python object into a pretty-printed representation."""
  43. return PrettyPrinter(indent=indent, width=width, depth=depth).pformat(object)
  44. def saferepr(object):
  45. """Version of repr() which can handle recursive data structures."""
  46. return _safe_repr(object, {}, None, 0)[0]
  47. def isreadable(object):
  48. """Determine if saferepr(object) is readable by eval()."""
  49. return _safe_repr(object, {}, None, 0)[1]
  50. def isrecursive(object):
  51. """Determine if object requires a recursive representation."""
  52. return _safe_repr(object, {}, None, 0)[2]
  53. class PrettyPrinter:
  54. def __init__(self, indent=1, width=80, depth=None, stream=None):
  55. """Handle pretty printing operations onto a stream using a set of
  56. configured parameters.
  57. indent
  58. Number of spaces to indent for each level of nesting.
  59. width
  60. Attempted maximum number of columns in the output.
  61. depth
  62. The maximum depth to print out nested structures.
  63. stream
  64. The desired output stream. If omitted (or false), the standard
  65. output stream available at construction will be used.
  66. """
  67. indent = int(indent)
  68. width = int(width)
  69. assert indent >= 0, "indent must be >= 0"
  70. assert depth is None or depth > 0, "depth must be > 0"
  71. assert width, "width must be != 0"
  72. self._depth = depth
  73. self._indent_per_level = indent
  74. self._width = width
  75. if stream is not None:
  76. self._stream = stream
  77. else:
  78. self._stream = _sys.stdout
  79. def pprint(self, object):
  80. self._stream.write(self.pformat(object) + "\n")
  81. def pformat(self, object):
  82. sio = _StringIO()
  83. self._format(object, sio, 0, 0, {}, 0)
  84. return sio.getvalue()
  85. def isrecursive(self, object):
  86. return self.format(object, {}, 0, 0)[2]
  87. def isreadable(self, object):
  88. s, readable, recursive = self.format(object, {}, 0, 0)
  89. return readable and not recursive
  90. def _format(self, object, stream, indent, allowance, context, level):
  91. level = level + 1
  92. objid = _id(object)
  93. if objid in context:
  94. stream.write(_recursion(object))
  95. self._recursive = True
  96. self._readable = False
  97. return
  98. rep = self._repr(object, context, level - 1)
  99. typ = _type(object)
  100. sepLines = _len(rep) > (self._width - 1 - indent - allowance)
  101. write = stream.write
  102. if sepLines:
  103. r = getattr(typ, "__repr__", None)
  104. if issubclass(typ, dict) and r is dict.__repr__:
  105. write('{')
  106. if self._indent_per_level > 1:
  107. write((self._indent_per_level - 1) * ' ')
  108. length = _len(object)
  109. if length:
  110. context[objid] = 1
  111. indent = indent + self._indent_per_level
  112. items = object.items()
  113. items.sort()
  114. key, ent = items[0]
  115. rep = self._repr(key, context, level)
  116. write(rep)
  117. write(': ')
  118. self._format(ent, stream, indent + _len(rep) + 2,
  119. allowance + 1, context, level)
  120. if length > 1:
  121. for key, ent in items[1:]:
  122. rep = self._repr(key, context, level)
  123. write(',\n%s%s: ' % (' '*indent, rep))
  124. self._format(ent, stream, indent + _len(rep) + 2,
  125. allowance + 1, context, level)
  126. indent = indent - self._indent_per_level
  127. del context[objid]
  128. write('}')
  129. return
  130. if (issubclass(typ, list) and r is list.__repr__) or \
  131. (issubclass(typ, tuple) and r is tuple.__repr__):
  132. if issubclass(typ, list):
  133. write('[')
  134. endchar = ']'
  135. else:
  136. write('(')
  137. endchar = ')'
  138. if self._indent_per_level > 1:
  139. write((self._indent_per_level - 1) * ' ')
  140. length = _len(object)
  141. if length:
  142. context[objid] = 1
  143. indent = indent + self._indent_per_level
  144. self._format(object[0], stream, indent, allowance + 1,
  145. context, level)
  146. if length > 1:
  147. for ent in object[1:]:
  148. write(',\n' + ' '*indent)
  149. self._format(ent, stream, indent,
  150. allowance + 1, context, level)
  151. indent = indent - self._indent_per_level
  152. del context[objid]
  153. if issubclass(typ, tuple) and length == 1:
  154. write(',')
  155. write(endchar)
  156. return
  157. write(rep)
  158. def _repr(self, object, context, level):
  159. repr, readable, recursive = self.format(object, context.copy(),
  160. self._depth, level)
  161. if not readable:
  162. self._readable = False
  163. if recursive:
  164. self._recursive = True
  165. return repr
  166. def format(self, object, context, maxlevels, level):
  167. """Format object for a specific context, returning a string
  168. and flags indicating whether the representation is 'readable'
  169. and whether the object represents a recursive construct.
  170. """
  171. return _safe_repr(object, context, maxlevels, level)
  172. # Return triple (repr_string, isreadable, isrecursive).
  173. def _safe_repr(object, context, maxlevels, level):
  174. typ = _type(object)
  175. if typ is str:
  176. if 'locale' not in _sys.modules:
  177. return repr(object), True, False
  178. if "'" in object and '"' not in object:
  179. closure = '"'
  180. quotes = {'"': '\\"'}
  181. else:
  182. closure = "'"
  183. quotes = {"'": "\\'"}
  184. qget = quotes.get
  185. sio = _StringIO()
  186. write = sio.write
  187. for char in object:
  188. if char.isalpha():
  189. write(char)
  190. else:
  191. write(qget(char, repr(char)[1:-1]))
  192. return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False
  193. r = getattr(typ, "__repr__", None)
  194. if issubclass(typ, dict) and r is dict.__repr__:
  195. if not object:
  196. return "{}", True, False
  197. objid = _id(object)
  198. if maxlevels and level > maxlevels:
  199. return "{...}", False, objid in context
  200. if objid in context:
  201. return _recursion(object), False, True
  202. context[objid] = 1
  203. readable = True
  204. recursive = False
  205. components = []
  206. append = components.append
  207. level += 1
  208. saferepr = _safe_repr
  209. for k, v in object.iteritems():
  210. krepr, kreadable, krecur = saferepr(k, context, maxlevels, level)
  211. vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level)
  212. append("%s: %s" % (krepr, vrepr))
  213. readable = readable and kreadable and vreadable
  214. if krecur or vrecur:
  215. recursive = True
  216. del context[objid]
  217. return "{%s}" % _commajoin(components), readable, recursive
  218. if (issubclass(typ, list) and r is list.__repr__) or \
  219. (issubclass(typ, tuple) and r is tuple.__repr__):
  220. if issubclass(typ, list):
  221. if not object:
  222. return "[]", True, False
  223. format = "[%s]"
  224. elif _len(object) == 1:
  225. format = "(%s,)"
  226. else:
  227. if not object:
  228. return "()", True, False
  229. format = "(%s)"
  230. objid = _id(object)
  231. if maxlevels and level > maxlevels:
  232. return format % "...", False, objid in context
  233. if objid in context:
  234. return _recursion(object), False, True
  235. context[objid] = 1
  236. readable = True
  237. recursive = False
  238. components = []
  239. append = components.append
  240. level += 1
  241. for o in object:
  242. orepr, oreadable, orecur = _safe_repr(o, context, maxlevels, level)
  243. append(orepr)
  244. if not oreadable:
  245. readable = False
  246. if orecur:
  247. recursive = True
  248. del context[objid]
  249. return format % _commajoin(components), readable, recursive
  250. rep = repr(object)
  251. return rep, (rep and not rep.startswith('<')), False
  252. def _recursion(object):
  253. return ("<Recursion on %s with id=%s>"
  254. % (_type(object).__name__, _id(object)))
  255. def _perfcheck(object=None):
  256. import time
  257. if object is None:
  258. object = [("string", (1, 2), [3, 4], {5: 6, 7: 8})] * 100000
  259. p = PrettyPrinter()
  260. t1 = time.time()
  261. _safe_repr(object, {}, None, 0)
  262. t2 = time.time()
  263. p.pformat(object)
  264. t3 = time.time()
  265. print "_safe_repr:", t2 - t1
  266. print "pformat:", t3 - t2
  267. if __name__ == "__main__":
  268. _perfcheck()