##// END OF EJS Templates
stringutil: make level parameter of pprintgen() 0-origin...
Yuya Nishihara -
r40311:f2fbd32c default
parent child Browse files
Show More
@@ -47,7 +47,7 b' def pprint(o, bprefix=False, indent=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))
49
49
50 def pprintgen(o, bprefix=False, indent=0, _level=1):
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
@@ -75,12 +75,13 b' def pprintgen(o, bprefix=False, indent=0'
75 yield '['
75 yield '['
76
76
77 if indent:
77 if indent:
78 _level += 1
78 yield '\n'
79 yield '\n'
79 yield ' ' * (_level * indent)
80 yield ' ' * (_level * indent)
80
81
81 for i, a in enumerate(o):
82 for i, a in enumerate(o):
82 for chunk in pprintgen(a, bprefix=bprefix, indent=indent,
83 for chunk in pprintgen(a, bprefix=bprefix, indent=indent,
83 _level=_level + 1):
84 _level=_level):
84 yield chunk
85 yield chunk
85
86
86 if i + 1 < len(o):
87 if i + 1 < len(o):
@@ -91,8 +92,9 b' def pprintgen(o, bprefix=False, indent=0'
91 yield ', '
92 yield ', '
92
93
93 if indent:
94 if indent:
95 _level -= 1
94 yield '\n'
96 yield '\n'
95 yield ' ' * ((_level - 1) * indent)
97 yield ' ' * (_level * indent)
96
98
97 yield ']'
99 yield ']'
98 elif isinstance(o, dict):
100 elif isinstance(o, dict):
@@ -103,18 +105,19 b' def pprintgen(o, bprefix=False, indent=0'
103 yield '{'
105 yield '{'
104
106
105 if indent:
107 if indent:
108 _level += 1
106 yield '\n'
109 yield '\n'
107 yield ' ' * (_level * indent)
110 yield ' ' * (_level * indent)
108
111
109 for i, (k, v) in enumerate(sorted(o.items())):
112 for i, (k, v) in enumerate(sorted(o.items())):
110 for chunk in pprintgen(k, bprefix=bprefix, indent=indent,
113 for chunk in pprintgen(k, bprefix=bprefix, indent=indent,
111 _level=_level + 1):
114 _level=_level):
112 yield chunk
115 yield chunk
113
116
114 yield ': '
117 yield ': '
115
118
116 for chunk in pprintgen(v, bprefix=bprefix, indent=indent,
119 for chunk in pprintgen(v, bprefix=bprefix, indent=indent,
117 _level=_level + 1):
120 _level=_level):
118 yield chunk
121 yield chunk
119
122
120 if i + 1 < len(o):
123 if i + 1 < len(o):
@@ -125,8 +128,9 b' def pprintgen(o, bprefix=False, indent=0'
125 yield ', '
128 yield ', '
126
129
127 if indent:
130 if indent:
131 _level -= 1
128 yield '\n'
132 yield '\n'
129 yield ' ' * ((_level - 1) * indent)
133 yield ' ' * (_level * indent)
130
134
131 yield '}'
135 yield '}'
132 elif isinstance(o, set):
136 elif isinstance(o, set):
@@ -137,12 +141,13 b' def pprintgen(o, bprefix=False, indent=0'
137 yield 'set(['
141 yield 'set(['
138
142
139 if indent:
143 if indent:
144 _level += 1
140 yield '\n'
145 yield '\n'
141 yield ' ' * (_level * indent)
146 yield ' ' * (_level * indent)
142
147
143 for i, k in enumerate(sorted(o)):
148 for i, k in enumerate(sorted(o)):
144 for chunk in pprintgen(k, bprefix=bprefix, indent=indent,
149 for chunk in pprintgen(k, bprefix=bprefix, indent=indent,
145 _level=_level + 1):
150 _level=_level):
146 yield chunk
151 yield chunk
147
152
148 if i + 1 < len(o):
153 if i + 1 < len(o):
@@ -153,8 +158,9 b' def pprintgen(o, bprefix=False, indent=0'
153 yield ', '
158 yield ', '
154
159
155 if indent:
160 if indent:
161 _level -= 1
156 yield '\n'
162 yield '\n'
157 yield ' ' * ((_level - 1) * indent)
163 yield ' ' * (_level * indent)
158
164
159 yield '])'
165 yield '])'
160 elif isinstance(o, tuple):
166 elif isinstance(o, tuple):
@@ -165,12 +171,13 b' def pprintgen(o, bprefix=False, indent=0'
165 yield '('
171 yield '('
166
172
167 if indent:
173 if indent:
174 _level += 1
168 yield '\n'
175 yield '\n'
169 yield ' ' * (_level * indent)
176 yield ' ' * (_level * indent)
170
177
171 for i, a in enumerate(o):
178 for i, a in enumerate(o):
172 for chunk in pprintgen(a, bprefix=bprefix, indent=indent,
179 for chunk in pprintgen(a, bprefix=bprefix, indent=indent,
173 _level=_level + 1):
180 _level=_level):
174 yield chunk
181 yield chunk
175
182
176 if i + 1 < len(o):
183 if i + 1 < len(o):
@@ -181,8 +188,9 b' def pprintgen(o, bprefix=False, indent=0'
181 yield ', '
188 yield ', '
182
189
183 if indent:
190 if indent:
191 _level -= 1
184 yield '\n'
192 yield '\n'
185 yield ' ' * ((_level - 1) * indent)
193 yield ' ' * (_level * indent)
186
194
187 yield ')'
195 yield ')'
188 elif isinstance(o, types.GeneratorType):
196 elif isinstance(o, types.GeneratorType):
@@ -196,6 +204,7 b' def pprintgen(o, bprefix=False, indent=0'
196 yield 'gen['
204 yield 'gen['
197
205
198 if indent:
206 if indent:
207 _level += 1
199 yield '\n'
208 yield '\n'
200 yield ' ' * (_level * indent)
209 yield ' ' * (_level * indent)
201
210
@@ -210,7 +219,7 b' def pprintgen(o, bprefix=False, indent=0'
210 last = True
219 last = True
211
220
212 for chunk in pprintgen(current, bprefix=bprefix, indent=indent,
221 for chunk in pprintgen(current, bprefix=bprefix, indent=indent,
213 _level=_level + 1):
222 _level=_level):
214 yield chunk
223 yield chunk
215
224
216 if not last:
225 if not last:
@@ -221,8 +230,9 b' def pprintgen(o, bprefix=False, indent=0'
221 yield ', '
230 yield ', '
222
231
223 if indent:
232 if indent:
233 _level -= 1
224 yield '\n'
234 yield '\n'
225 yield ' ' * ((_level -1) * indent)
235 yield ' ' * (_level * indent)
226
236
227 yield ']'
237 yield ']'
228 else:
238 else:
General Comments 0
You need to be logged in to leave comments. Login now