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