4242__all__ = ["pprint" ,"pformat" ,"isreadable" ,"isrecursive" ,"saferepr" ,
4343 "PrettyPrinter" ]
4444
45- # cache these for faster access:
46- _commajoin = ", " .join
47- _id = id
48- _len = len
49- _type = type
50-
5145
5246def pprint (object , stream = None , indent = 1 , width = 80 , depth = None , * ,
5347 compact = False ):
@@ -96,8 +90,8 @@ def __lt__(self, other):
9690 rv = NotImplemented
9791
9892 if rv is NotImplemented :
99- rv = (str (_type (self .obj )), _id (self .obj )) < \
100- (str (_type (other .obj )), _id (other .obj ))
93+ rv = (str (type (self .obj )), id (self .obj )) < \
94+ (str (type (other .obj )), id (other .obj ))
10195 return rv
10296
10397def _safe_tuple (t ):
@@ -159,16 +153,16 @@ def isreadable(self, object):
159153
160154 def _format (self , object , stream , indent , allowance , context , level ):
161155 level = level + 1
162- objid = _id (object )
156+ objid = id (object )
163157 if objid in context :
164158 stream .write (_recursion (object ))
165159 self ._recursive = True
166160 self ._readable = False
167161 return
168162 rep = self ._repr (object , context , level - 1 )
169- typ = _type (object )
163+ typ = type (object )
170164 max_width = self ._width - 1 - indent - allowance
171- sepLines = _len (rep ) > max_width
165+ sepLines = len (rep ) > max_width
172166 write = stream .write
173167
174168 if sepLines :
@@ -177,7 +171,7 @@ def _format(self, object, stream, indent, allowance, context, level):
177171 write ('{' )
178172 if self ._indent_per_level > 1 :
179173 write ((self ._indent_per_level - 1 ) * ' ' )
180- length = _len (object )
174+ length = len (object )
181175 if length :
182176 context [objid ] = 1
183177 indent = indent + self ._indent_per_level
@@ -189,13 +183,13 @@ def _format(self, object, stream, indent, allowance, context, level):
189183 rep = self ._repr (key , context , level )
190184 write (rep )
191185 write (': ' )
192- self ._format (ent , stream , indent + _len (rep ) + 2 ,
186+ self ._format (ent , stream , indent + len (rep ) + 2 ,
193187 allowance + 1 , context , level )
194188 if length > 1 :
195189 for key , ent in items [1 :]:
196190 rep = self ._repr (key , context , level )
197191 write (',\n %s%s: ' % (' ' * indent , rep ))
198- self ._format (ent , stream , indent + _len (rep ) + 2 ,
192+ self ._format (ent , stream , indent + len (rep ) + 2 ,
199193 allowance + 1 , context , level )
200194 indent = indent - self ._indent_per_level
201195 del context [objid ]
@@ -207,7 +201,7 @@ def _format(self, object, stream, indent, allowance, context, level):
207201 (issubclass (typ , set ) and r is set .__repr__ ) or
208202 (issubclass (typ , frozenset ) and r is frozenset .__repr__ )
209203 ):
210- length = _len (object )
204+ length = len (object )
211205 if issubclass (typ , list ):
212206 write ('[' )
213207 endchar = ']'
@@ -225,7 +219,7 @@ def _format(self, object, stream, indent, allowance, context, level):
225219 write (typ .__name__ )
226220 write ('({' )
227221 endchar = '})'
228- indent += _len (typ .__name__ ) + 1
222+ indent += len (typ .__name__ ) + 1
229223 object = sorted (object , key = _safe_key )
230224 if self ._indent_per_level > 1 :
231225 write ((self ._indent_per_level - 1 ) * ' ' )
@@ -240,7 +234,7 @@ def _format(self, object, stream, indent, allowance, context, level):
240234 write (endchar )
241235 return
242236
243- if issubclass (typ , str ) and _len (object ) > 0 and r is str .__repr__ :
237+ if issubclass (typ , str ) and len (object ) > 0 and r is str .__repr__ :
244238 def _str_parts (s ):
245239 """
246240 Return a list of string literals comprising the repr()
@@ -249,16 +243,16 @@ def _str_parts(s):
249243 lines = s .splitlines (True )
250244 for i , line in enumerate (lines ):
251245 rep = repr (line )
252- if _len (rep ) <= max_width :
246+ if len (rep ) <= max_width :
253247 yield rep
254248 else :
255249 # A list of alternating (non-space, space) strings
256250 parts = re .split (r'(\s+)' , line ) + ['' ]
257251 current = ''
258- for i in range (0 , _len (parts ), 2 ):
252+ for i in range (0 , len (parts ), 2 ):
259253 part = parts [i ] + parts [i + 1 ]
260254 candidate = current + part
261- if _len (repr (candidate )) > max_width :
255+ if len (repr (candidate )) > max_width :
262256 if current :
263257 yield repr (current )
264258 current = part
@@ -281,7 +275,7 @@ def _format_items(self, items, stream, indent, allowance, context, level):
281275 for ent in items :
282276 if self ._compact :
283277 rep = self ._repr (ent , context , level )
284- w = _len (rep ) + 2
278+ w = len (rep ) + 2
285279 if width < w :
286280 width = max_width
287281 if delim :
@@ -316,7 +310,7 @@ def format(self, object, context, maxlevels, level):
316310# Return triple (repr_string, isreadable, isrecursive).
317311
318312def _safe_repr (object , context , maxlevels , level ):
319- typ = _type (object )
313+ typ = type (object )
320314 if typ is str :
321315 if 'locale' not in _sys .modules :
322316 return repr (object ), True , False
@@ -340,7 +334,7 @@ def _safe_repr(object, context, maxlevels, level):
340334 if issubclass (typ , dict ) and r is dict .__repr__ :
341335 if not object :
342336 return "{}" , True , False
343- objid = _id (object )
337+ objid = id (object )
344338 if maxlevels and level >= maxlevels :
345339 return "{...}" , False , objid in context
346340 if objid in context :
@@ -361,21 +355,21 @@ def _safe_repr(object, context, maxlevels, level):
361355 if krecur or vrecur :
362356 recursive = True
363357 del context [objid ]
364- return "{%s}" % _commajoin (components ), readable , recursive
358+ return "{%s}" % ", " . join (components ), readable , recursive
365359
366360 if (issubclass (typ , list ) and r is list .__repr__ ) or \
367361 (issubclass (typ , tuple ) and r is tuple .__repr__ ):
368362 if issubclass (typ , list ):
369363 if not object :
370364 return "[]" , True , False
371365 format = "[%s]"
372- elif _len (object ) == 1 :
366+ elif len (object ) == 1 :
373367 format = "(%s,)"
374368 else :
375369 if not object :
376370 return "()" , True , False
377371 format = "(%s)"
378- objid = _id (object )
372+ objid = id (object )
379373 if maxlevels and level >= maxlevels :
380374 return format % "..." , False , objid in context
381375 if objid in context :
@@ -394,15 +388,15 @@ def _safe_repr(object, context, maxlevels, level):
394388 if orecur :
395389 recursive = True
396390 del context [objid ]
397- return format % _commajoin (components ), readable , recursive
391+ return format % ", " . join (components ), readable , recursive
398392
399393 rep = repr (object )
400394 return rep , (rep and not rep .startswith ('<' )), False
401395
402396
403397def _recursion (object ):
404398 return ("<Recursion on %s with id=%s>"
405- % (_type (object ).__name__ , _id (object )))
399+ % (type (object ).__name__ , id (object )))
406400
407401
408402def _perfcheck (object = None ):
0 commit comments