Show More
@@ -43,11 +43,11 def reescape(pat): | |||||
43 | return pat |
|
43 | return pat | |
44 | return pat.encode('latin1') |
|
44 | return pat.encode('latin1') | |
45 |
|
45 | |||
46 | def pprint(o, bprefix=False, indent=0): |
|
46 | def pprint(o, bprefix=False, indent=0, level=0): | |
47 | """Pretty print an object.""" |
|
47 | """Pretty print an object.""" | |
48 | return b''.join(pprintgen(o, bprefix=bprefix, indent=indent)) |
|
48 | return b''.join(pprintgen(o, bprefix=bprefix, indent=indent, level=level)) | |
49 |
|
49 | |||
50 |
def pprintgen(o, bprefix=False, indent=0, |
|
50 | def pprintgen(o, bprefix=False, indent=0, level=0): | |
51 | """Pretty print an object to a generator of atoms. |
|
51 | """Pretty print an object to a generator of atoms. | |
52 |
|
52 | |||
53 | ``bprefix`` is a flag influencing whether bytestrings are preferred with |
|
53 | ``bprefix`` is a flag influencing whether bytestrings are preferred with | |
@@ -56,6 +56,8 def pprintgen(o, bprefix=False, indent=0 | |||||
56 | ``indent`` controls whether collections and nested data structures |
|
56 | ``indent`` controls whether collections and nested data structures | |
57 | span multiple lines via the indentation amount in spaces. By default, |
|
57 | span multiple lines via the indentation amount in spaces. By default, | |
58 | no newlines are emitted. |
|
58 | no newlines are emitted. | |
|
59 | ||||
|
60 | ``level`` specifies the initial indent level. Used if ``indent > 0``. | |||
59 | """ |
|
61 | """ | |
60 |
|
62 | |||
61 | if isinstance(o, bytes): |
|
63 | if isinstance(o, bytes): | |
@@ -75,26 +77,26 def pprintgen(o, bprefix=False, indent=0 | |||||
75 | yield '[' |
|
77 | yield '[' | |
76 |
|
78 | |||
77 | if indent: |
|
79 | if indent: | |
78 |
|
|
80 | level += 1 | |
79 | yield '\n' |
|
81 | yield '\n' | |
80 |
yield ' ' * ( |
|
82 | yield ' ' * (level * indent) | |
81 |
|
83 | |||
82 | for i, a in enumerate(o): |
|
84 | for i, a in enumerate(o): | |
83 | for chunk in pprintgen(a, bprefix=bprefix, indent=indent, |
|
85 | for chunk in pprintgen(a, bprefix=bprefix, indent=indent, | |
84 |
|
|
86 | level=level): | |
85 | yield chunk |
|
87 | yield chunk | |
86 |
|
88 | |||
87 | if i + 1 < len(o): |
|
89 | if i + 1 < len(o): | |
88 | if indent: |
|
90 | if indent: | |
89 | yield ',\n' |
|
91 | yield ',\n' | |
90 |
yield ' ' * ( |
|
92 | yield ' ' * (level * indent) | |
91 | else: |
|
93 | else: | |
92 | yield ', ' |
|
94 | yield ', ' | |
93 |
|
95 | |||
94 | if indent: |
|
96 | if indent: | |
95 |
|
|
97 | level -= 1 | |
96 | yield '\n' |
|
98 | yield '\n' | |
97 |
yield ' ' * ( |
|
99 | yield ' ' * (level * indent) | |
98 |
|
100 | |||
99 | yield ']' |
|
101 | yield ']' | |
100 | elif isinstance(o, dict): |
|
102 | elif isinstance(o, dict): | |
@@ -105,32 +107,32 def pprintgen(o, bprefix=False, indent=0 | |||||
105 | yield '{' |
|
107 | yield '{' | |
106 |
|
108 | |||
107 | if indent: |
|
109 | if indent: | |
108 |
|
|
110 | level += 1 | |
109 | yield '\n' |
|
111 | yield '\n' | |
110 |
yield ' ' * ( |
|
112 | yield ' ' * (level * indent) | |
111 |
|
113 | |||
112 | for i, (k, v) in enumerate(sorted(o.items())): |
|
114 | for i, (k, v) in enumerate(sorted(o.items())): | |
113 | for chunk in pprintgen(k, bprefix=bprefix, indent=indent, |
|
115 | for chunk in pprintgen(k, bprefix=bprefix, indent=indent, | |
114 |
|
|
116 | level=level): | |
115 | yield chunk |
|
117 | yield chunk | |
116 |
|
118 | |||
117 | yield ': ' |
|
119 | yield ': ' | |
118 |
|
120 | |||
119 | for chunk in pprintgen(v, bprefix=bprefix, indent=indent, |
|
121 | for chunk in pprintgen(v, bprefix=bprefix, indent=indent, | |
120 |
|
|
122 | level=level): | |
121 | yield chunk |
|
123 | yield chunk | |
122 |
|
124 | |||
123 | if i + 1 < len(o): |
|
125 | if i + 1 < len(o): | |
124 | if indent: |
|
126 | if indent: | |
125 | yield ',\n' |
|
127 | yield ',\n' | |
126 |
yield ' ' * ( |
|
128 | yield ' ' * (level * indent) | |
127 | else: |
|
129 | else: | |
128 | yield ', ' |
|
130 | yield ', ' | |
129 |
|
131 | |||
130 | if indent: |
|
132 | if indent: | |
131 |
|
|
133 | level -= 1 | |
132 | yield '\n' |
|
134 | yield '\n' | |
133 |
yield ' ' * ( |
|
135 | yield ' ' * (level * indent) | |
134 |
|
136 | |||
135 | yield '}' |
|
137 | yield '}' | |
136 | elif isinstance(o, set): |
|
138 | elif isinstance(o, set): | |
@@ -141,26 +143,26 def pprintgen(o, bprefix=False, indent=0 | |||||
141 | yield 'set([' |
|
143 | yield 'set([' | |
142 |
|
144 | |||
143 | if indent: |
|
145 | if indent: | |
144 |
|
|
146 | level += 1 | |
145 | yield '\n' |
|
147 | yield '\n' | |
146 |
yield ' ' * ( |
|
148 | yield ' ' * (level * indent) | |
147 |
|
149 | |||
148 | for i, k in enumerate(sorted(o)): |
|
150 | for i, k in enumerate(sorted(o)): | |
149 | for chunk in pprintgen(k, bprefix=bprefix, indent=indent, |
|
151 | for chunk in pprintgen(k, bprefix=bprefix, indent=indent, | |
150 |
|
|
152 | level=level): | |
151 | yield chunk |
|
153 | yield chunk | |
152 |
|
154 | |||
153 | if i + 1 < len(o): |
|
155 | if i + 1 < len(o): | |
154 | if indent: |
|
156 | if indent: | |
155 | yield ',\n' |
|
157 | yield ',\n' | |
156 |
yield ' ' * ( |
|
158 | yield ' ' * (level * indent) | |
157 | else: |
|
159 | else: | |
158 | yield ', ' |
|
160 | yield ', ' | |
159 |
|
161 | |||
160 | if indent: |
|
162 | if indent: | |
161 |
|
|
163 | level -= 1 | |
162 | yield '\n' |
|
164 | yield '\n' | |
163 |
yield ' ' * ( |
|
165 | yield ' ' * (level * indent) | |
164 |
|
166 | |||
165 | yield '])' |
|
167 | yield '])' | |
166 | elif isinstance(o, tuple): |
|
168 | elif isinstance(o, tuple): | |
@@ -171,26 +173,26 def pprintgen(o, bprefix=False, indent=0 | |||||
171 | yield '(' |
|
173 | yield '(' | |
172 |
|
174 | |||
173 | if indent: |
|
175 | if indent: | |
174 |
|
|
176 | level += 1 | |
175 | yield '\n' |
|
177 | yield '\n' | |
176 |
yield ' ' * ( |
|
178 | yield ' ' * (level * indent) | |
177 |
|
179 | |||
178 | for i, a in enumerate(o): |
|
180 | for i, a in enumerate(o): | |
179 | for chunk in pprintgen(a, bprefix=bprefix, indent=indent, |
|
181 | for chunk in pprintgen(a, bprefix=bprefix, indent=indent, | |
180 |
|
|
182 | level=level): | |
181 | yield chunk |
|
183 | yield chunk | |
182 |
|
184 | |||
183 | if i + 1 < len(o): |
|
185 | if i + 1 < len(o): | |
184 | if indent: |
|
186 | if indent: | |
185 | yield ',\n' |
|
187 | yield ',\n' | |
186 |
yield ' ' * ( |
|
188 | yield ' ' * (level * indent) | |
187 | else: |
|
189 | else: | |
188 | yield ', ' |
|
190 | yield ', ' | |
189 |
|
191 | |||
190 | if indent: |
|
192 | if indent: | |
191 |
|
|
193 | level -= 1 | |
192 | yield '\n' |
|
194 | yield '\n' | |
193 |
yield ' ' * ( |
|
195 | yield ' ' * (level * indent) | |
194 |
|
196 | |||
195 | yield ')' |
|
197 | yield ')' | |
196 | elif isinstance(o, types.GeneratorType): |
|
198 | elif isinstance(o, types.GeneratorType): | |
@@ -204,9 +206,9 def pprintgen(o, bprefix=False, indent=0 | |||||
204 | yield 'gen[' |
|
206 | yield 'gen[' | |
205 |
|
207 | |||
206 | if indent: |
|
208 | if indent: | |
207 |
|
|
209 | level += 1 | |
208 | yield '\n' |
|
210 | yield '\n' | |
209 |
yield ' ' * ( |
|
211 | yield ' ' * (level * indent) | |
210 |
|
212 | |||
211 | last = False |
|
213 | last = False | |
212 |
|
214 | |||
@@ -219,20 +221,20 def pprintgen(o, bprefix=False, indent=0 | |||||
219 | last = True |
|
221 | last = True | |
220 |
|
222 | |||
221 | for chunk in pprintgen(current, bprefix=bprefix, indent=indent, |
|
223 | for chunk in pprintgen(current, bprefix=bprefix, indent=indent, | |
222 |
|
|
224 | level=level): | |
223 | yield chunk |
|
225 | yield chunk | |
224 |
|
226 | |||
225 | if not last: |
|
227 | if not last: | |
226 | if indent: |
|
228 | if indent: | |
227 | yield ',\n' |
|
229 | yield ',\n' | |
228 |
yield ' ' * ( |
|
230 | yield ' ' * (level * indent) | |
229 | else: |
|
231 | else: | |
230 | yield ', ' |
|
232 | yield ', ' | |
231 |
|
233 | |||
232 | if indent: |
|
234 | if indent: | |
233 |
|
|
235 | level -= 1 | |
234 | yield '\n' |
|
236 | yield '\n' | |
235 |
yield ' ' * ( |
|
237 | yield ' ' * (level * indent) | |
236 |
|
238 | |||
237 | yield ']' |
|
239 | yield ']' | |
238 | else: |
|
240 | else: |
General Comments 0
You need to be logged in to leave comments.
Login now