Mercurial > cpython-lru_cache-weakref
annotate Lib/pprint.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 166c2232921a |
| children | 2e917f9372e0 |
| rev | line source |
|---|---|
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 # Author: Fred L. Drake, Jr. |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
2 # fdrake@cnri.reston.va.us, fdrake@acm.org |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 # |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 # This is a simple little module I wrote to make life easier. I didn't |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 # see anything quite like it in the library, though I may have overlooked |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 # something. I wrote this when I was trying to read some heavily nested |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 # tuples with fairly non-descriptive content. This is modelled very much |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 # after Lisp/Scheme - style pretty-printing of lists. If you find it |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 # useful, thank small children who sleep at night. |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 """Support to pretty-print lists, tuples, & dictionaries recursively. |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 Very simple, but useful, especially in debugging data structures. |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
15 Classes |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
16 ------- |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
17 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
18 PrettyPrinter() |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
19 Handle pretty-printing operations onto a stream using a configured |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
20 set of formatting parameters. |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
21 |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 Functions |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 --------- |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 pformat() |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 Format a Python object into a pretty-printed representation. |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 pprint() |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
29 Pretty-print a Python object to a stream [default is sys.sydout]. |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
31 saferepr() |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
32 Generate a 'standard' repr()-like value, but protect against recursive |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
33 data structures. |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 """ |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 from types import DictType, ListType, TupleType |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
39 try: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
40 from cStringIO import StringIO |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
41 except ImportError: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
42 from StringIO import StringIO |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
45 def pprint(object, stream=None): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
46 """Pretty-print a Python object to a stream [default is sys.sydout].""" |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
47 printer = PrettyPrinter(stream=stream) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
48 printer.pprint(object) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
49 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
50 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
51 def pformat(object): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
52 """Format a Python object into a pretty-printed representation.""" |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
53 return PrettyPrinter().pformat(object) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
54 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
55 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
56 def saferepr(object): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
57 """Version of repr() which can handle recursive data structures.""" |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
58 return _safe_repr(object, {}) |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
61 class PrettyPrinter: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
62 def __init__(self, indent=1, width=80, depth=None, stream=None): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
63 """Handle pretty printing operations onto a stream using a set of |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
64 configured parameters. |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
66 indent |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
67 Number of spaces to indent for each level of nesting. |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
68 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
69 width |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
70 Attempted maximum number of columns in the output. |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
71 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
72 depth |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
73 The maximum depth to print out nested structures. |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
75 stream |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
76 The desired output stream. If omitted (or false), the standard |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
77 output stream available at construction will be used. |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
78 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
79 """ |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
80 assert (not depth) or depth > 0, "depth may not be negative" |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
81 assert int(indent) or 1 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
82 assert int(width) or 1 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
83 self.__depth = depth |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
84 self.__indent_per_level = indent |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
85 self.__width = width |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
86 if stream: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
87 self.__stream = stream |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
88 else: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
89 import sys |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
90 self.__stream = sys.stdout |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
92 def pprint(self, object): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
93 self.__stream.write(self.pformat(object) + "\n") |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
94 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
95 def pformat(self, object): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
96 sio = StringIO() |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
97 self.__format(object, sio, 0, 0, {}, 0) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
98 return sio.getvalue() |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
100 def __format(self, object, stream, indent, allowance, context, level): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
101 level = level + 1 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
102 if context.has_key(id(object)): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
103 object = _Recursion(object) |
|
5337
166c2232921a
Fix a couple of glitches identified by Greg Stein.
Fred Drake <fdrake@acm.org>
parents:
5336
diff
changeset
|
104 rep = self.__repr(object, context, level - 1) |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
105 objid = id(object) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
106 context[objid] = 1 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
107 typ = type(object) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
108 sepLines = len(rep) > (self.__width - 1 - indent - allowance) |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
110 if sepLines and typ in (ListType, TupleType): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
111 # Pretty-print the sequence. |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
112 stream.write((typ is ListType) and '[' or '(') |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
113 length = len(object) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
114 if length: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
115 indent = indent + self.__indent_per_level |
|
5337
166c2232921a
Fix a couple of glitches identified by Greg Stein.
Fred Drake <fdrake@acm.org>
parents:
5336
diff
changeset
|
116 self.__format(object[0], stream, indent, allowance + 1, |
|
166c2232921a
Fix a couple of glitches identified by Greg Stein.
Fred Drake <fdrake@acm.org>
parents:
5336
diff
changeset
|
117 context, level) |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
118 if len(object) > 1: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
119 for ent in object[1:]: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
120 stream.write(',\n' + ' '*indent) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
121 self.__format(ent, stream, indent, |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
122 allowance + 1, context, level) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
123 indent = indent - self.__indent_per_level |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
124 stream.write(((typ is ListType) and ']') or ')') |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
126 elif sepLines and typ is DictType: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
127 stream.write('{') |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
128 length = len(object) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
129 if length: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
130 indent = indent + self.__indent_per_level |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
131 items = object.items() |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
132 items.sort() |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
133 key, ent = items[0] |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
134 rep = self.__repr(key, context, level) + ': ' |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
135 stream.write(rep) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
136 self.__format(ent, stream, indent + len(rep), |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
137 allowance + 1, context, level) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
138 if len(items) > 1: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
139 for key, ent in items[1:]: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
140 rep = self.__repr(key, context, level) + ': ' |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
141 stream.write(',\n' + ' '*indent + rep) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
142 self.__format(ent, stream, indent + len(rep), |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
143 allowance + 1, context, level) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
144 indent = indent - self.__indent_per_level |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
145 stream.write('}') |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
147 else: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
148 stream.write(rep) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
149 del context[objid] |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
151 def __repr(self, object, context, level): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
152 return _safe_repr(object, context, self.__depth, level) |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 |
|
5336
746f687acfc1
_safe_repr(): Make the context parameter required; ok since it's only for
Fred Drake <fdrake@acm.org>
parents:
5335
diff
changeset
|
155 def _safe_repr(object, context, maxlevels=None, level=0): |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
156 level = level + 1 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
157 typ = type(object) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
158 if not (typ in (DictType, ListType, TupleType) and object): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
159 return `object` |
|
5336
746f687acfc1
_safe_repr(): Make the context parameter required; ok since it's only for
Fred Drake <fdrake@acm.org>
parents:
5335
diff
changeset
|
160 if context.has_key(id(object)): |
|
746f687acfc1
_safe_repr(): Make the context parameter required; ok since it's only for
Fred Drake <fdrake@acm.org>
parents:
5335
diff
changeset
|
161 return `_Recursion(object)` |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
162 objid = id(object) |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
163 context[objid] = 1 |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
164 if typ is DictType: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
165 if maxlevels and level >= maxlevels: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
166 s = "{...}" |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
167 else: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
168 items = object.items() |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
169 k, v = items[0] |
|
5336
746f687acfc1
_safe_repr(): Make the context parameter required; ok since it's only for
Fred Drake <fdrake@acm.org>
parents:
5335
diff
changeset
|
170 s = "{%s: %s" % (_safe_repr(k, context, maxlevels, level), |
|
746f687acfc1
_safe_repr(): Make the context parameter required; ok since it's only for
Fred Drake <fdrake@acm.org>
parents:
5335
diff
changeset
|
171 _safe_repr(v, context, maxlevels, level)) |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
172 for k, v in items[1:]: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
173 s = "%s, %s: %s" \ |
|
5336
746f687acfc1
_safe_repr(): Make the context parameter required; ok since it's only for
Fred Drake <fdrake@acm.org>
parents:
5335
diff
changeset
|
174 % (s, _safe_repr(k, context, maxlevels, level), |
|
746f687acfc1
_safe_repr(): Make the context parameter required; ok since it's only for
Fred Drake <fdrake@acm.org>
parents:
5335
diff
changeset
|
175 _safe_repr(v, context, maxlevels, level)) |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
176 s = s + "}" |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
177 else: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
178 s, term = (typ is ListType) and ('[', ']') or ('(', ')') |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
179 if maxlevels and level >= maxlevels: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
180 s = s + "..." |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
181 else: |
|
5336
746f687acfc1
_safe_repr(): Make the context parameter required; ok since it's only for
Fred Drake <fdrake@acm.org>
parents:
5335
diff
changeset
|
182 s = s + _safe_repr(object[0], context, maxlevels, level) |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
183 for ent in object[1:]: |
|
5336
746f687acfc1
_safe_repr(): Make the context parameter required; ok since it's only for
Fred Drake <fdrake@acm.org>
parents:
5335
diff
changeset
|
184 s = "%s, %s" % (s, _safe_repr(ent, context, maxlevels, level)) |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
185 s = s + term |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
186 del context[objid] |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
187 return s |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 |
|
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
190 class _Recursion: |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
191 # represent a recursive relationship; really only used for the __repr__() |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
192 # method... |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
193 def __init__(self, object): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
194 self.__repr = "<Recursion on %s with id=%s>" \ |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
195 % (type(object).__name__, id(object)) |
|
5330
5a22f9b38aac
Checking in a copy of Fred Drake's data structure pretty-printer
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 |
|
5335
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
197 def __repr__(self): |
|
ee9275b0f5c8
Muchly changed and improved pprint.py:
Fred Drake <fdrake@acm.org>
parents:
5330
diff
changeset
|
198 return self.__repr |
