2121"""
2222from __future__ import absolute_import
2323
24- import abc
2524import collections
26- import functools
2725import sys
28- from numbers import Integral
2926
3027__version__ = "1.1.0"
3128
4744
4845common = ['native_dict' , 'native_round' , 'native_filter' , 'native_map' , 'native_range' , 'native_str' , 'native_chr' ,
4946 'native_input' , 'PY2' , 'PY3' , 'u' , 'itemsview' , 'valuesview' , 'keysview' , 'execute' , 'integer_types' ,
50- 'native_next' , 'native_object' , 'with_metaclass' , 'OrderedDict' , ' lru_cache' ]
47+ 'native_next' , 'native_object' , 'with_metaclass' , 'lru_cache' ]
5148
5249
5350def with_metaclass (meta , * bases ):
@@ -85,32 +82,12 @@ def unmodified_isinstance(*bases):
8582
8683 """
8784 class UnmodifiedIsInstance (type ):
88- if sys .version_info [0 ] == 2 and sys .version_info [1 ] <= 6 :
89-
90- @classmethod
91- def __instancecheck__ (cls , instance ):
92- if cls .__name__ in (str (base .__name__ ) for base in bases ):
93- return isinstance (instance , bases )
94-
95- subclass = getattr (instance , '__class__' , None )
96- subtype = type (instance )
97- instance_type = getattr (abc , '_InstanceType' , None )
98- if not instance_type :
99- class test_object :
100- pass
101- instance_type = type (test_object )
102- if subtype is instance_type :
103- subtype = subclass
104- if subtype is subclass or subclass is None :
105- return cls .__subclasscheck__ (subtype )
106- return (cls .__subclasscheck__ (subclass ) or cls .__subclasscheck__ (subtype ))
107- else :
108- @classmethod
109- def __instancecheck__ (cls , instance ):
110- if cls .__name__ in (str (base .__name__ ) for base in bases ):
111- return isinstance (instance , bases )
85+ @classmethod
86+ def __instancecheck__ (cls , instance ):
87+ if cls .__name__ in (str (base .__name__ ) for base in bases ):
88+ return isinstance (instance , bases )
11289
113- return type .__instancecheck__ (cls , instance )
90+ return type .__instancecheck__ (cls , instance )
11491
11592 return with_metaclass (UnmodifiedIsInstance , * bases )
11693
@@ -148,12 +125,11 @@ def callable(entity):
148125
149126 __all__ = common + ['urllib' ]
150127else :
151- from itertools import ifilter as filter
152- from itertools import imap as map
153- from itertools import izip as zip
128+ from itertools import ifilter as filter # noqa: F401
129+ from itertools import imap as map # noqa: F401
130+ from itertools import izip as zip # noqa: F401
154131 from decimal import Decimal , ROUND_HALF_EVEN
155132
156- import codecs
157133 str = unicode
158134 chr = unichr
159135 input = raw_input
@@ -281,167 +257,17 @@ def __new__(cls, name, bases, dct):
281257 dct ['__str__' ] = lambda self : self .__unicode__ ().encode ('utf-8' )
282258 return type .__new__ (cls , name , bases , dct )
283259
284- if sys .version_info [1 ] <= 6 :
285- def __instancecheck__ (cls , instance ):
286- if cls .__name__ == "object" :
287- return isinstance (instance , native_object )
288-
289- subclass = getattr (instance , '__class__' , None )
290- subtype = type (instance )
291- instance_type = getattr (abc , '_InstanceType' , None )
292- if not instance_type :
293- class test_object :
294- pass
295- instance_type = type (test_object )
296- if subtype is instance_type :
297- subtype = subclass
298- if subtype is subclass or subclass is None :
299- return cls .__subclasscheck__ (subtype )
300- return (cls .__subclasscheck__ (subclass ) or cls .__subclasscheck__ (subtype ))
301- else :
302- def __instancecheck__ (cls , instance ):
303- if cls .__name__ == "object" :
304- return isinstance (instance , native_object )
305- return type .__instancecheck__ (cls , instance )
260+ def __instancecheck__ (cls , instance ):
261+ if cls .__name__ == "object" :
262+ return isinstance (instance , native_object )
263+ return type .__instancecheck__ (cls , instance )
306264
307265 class object (with_metaclass (FixStr , object )):
308266 pass
309267
310268 __all__ = common + ['round' , 'dict' , 'apply' , 'cmp' , 'coerce' , 'execfile' , 'raw_input' , 'unpacks' , 'str' , 'chr' ,
311269 'input' , 'range' , 'filter' , 'map' , 'zip' , 'object' ]
312270
313- if sys .version_info [0 ] == 2 and sys .version_info [1 ] < 7 :
314- # OrderedDict
315- # Copyright (c) 2009 Raymond Hettinger
316- #
317- # Permission is hereby granted, free of charge, to any person
318- # obtaining a copy of this software and associated documentation files
319- # (the "Software"), to deal in the Software without restriction,
320- # including without limitation the rights to use, copy, modify, merge,
321- # publish, distribute, sublicense, and/or sell copies of the Software,
322- # and to permit persons to whom the Software is furnished to do so,
323- # subject to the following conditions:
324- #
325- # The above copyright notice and this permission notice shall be
326- # included in all copies or substantial portions of the Software.
327- #
328- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
329- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
330- # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
331- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
332- # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
333- # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
334- # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
335- # OTHER DEALINGS IN THE SOFTWARE.
336-
337- from UserDict import DictMixin
338-
339- class OrderedDict (dict , DictMixin ):
340-
341- def __init__ (self , * args , ** kwds ):
342- if len (args ) > 1 :
343- raise TypeError ('expected at most 1 arguments, got %d' % len (args ))
344- try :
345- self .__end
346- except AttributeError :
347- self .clear ()
348- self .update (* args , ** kwds )
349-
350- def clear (self ):
351- self .__end = end = []
352- end += [None , end , end ] # sentinel node for doubly linked list
353- self .__map = {} # key --> [key, prev, next]
354- dict .clear (self )
355-
356- def __setitem__ (self , key , value ):
357- if key not in self :
358- end = self .__end
359- curr = end [1 ]
360- curr [2 ] = end [1 ] = self .__map [key ] = [key , curr , end ]
361- dict .__setitem__ (self , key , value )
362-
363- def __delitem__ (self , key ):
364- dict .__delitem__ (self , key )
365- key , prev , next = self .__map .pop (key )
366- prev [2 ] = next
367- next [1 ] = prev
368-
369- def __iter__ (self ):
370- end = self .__end
371- curr = end [2 ]
372- while curr is not end :
373- yield curr [0 ]
374- curr = curr [2 ]
375-
376- def __reversed__ (self ):
377- end = self .__end
378- curr = end [1 ]
379- while curr is not end :
380- yield curr [0 ]
381- curr = curr [1 ]
382-
383- def popitem (self , last = True ):
384- if not self :
385- raise KeyError ('dictionary is empty' )
386- if last :
387- key = reversed (self ).next ()
388- else :
389- key = iter (self ).next ()
390- value = self .pop (key )
391- return key , value
392-
393- def __reduce__ (self ):
394- items = [[k , self [k ]] for k in self ]
395- tmp = self .__map , self .__end
396- del self .__map , self .__end
397- inst_dict = vars (self ).copy ()
398- self .__map , self .__end = tmp
399- if inst_dict :
400- return (self .__class__ , (items ,), inst_dict )
401- return self .__class__ , (items ,)
402-
403- def keys (self ):
404- return list (self )
405-
406- setdefault = DictMixin .setdefault
407- update = DictMixin .update
408- pop = DictMixin .pop
409- values = DictMixin .values
410- items = DictMixin .items
411- iterkeys = DictMixin .iterkeys
412- itervalues = DictMixin .itervalues
413- iteritems = DictMixin .iteritems
414-
415- def __repr__ (self ):
416- if not self :
417- return '%s()' % (self .__class__ .__name__ ,)
418- return '%s(%r)' % (self .__class__ .__name__ , self .items ())
419-
420- def copy (self ):
421- return self .__class__ (self )
422-
423- @classmethod
424- def fromkeys (cls , iterable , value = None ):
425- d = cls ()
426- for key in iterable :
427- d [key ] = value
428- return d
429-
430- def __eq__ (self , other ):
431- if isinstance (other , OrderedDict ):
432- if len (self ) != len (other ):
433- return False
434- for p , q in zip (self .items (), other .items ()):
435- if p != q :
436- return False
437- return True
438- return dict .__eq__ (self , other )
439-
440- def __ne__ (self , other ):
441- return not self == other
442- else :
443- from collections import OrderedDict
444-
445271
446272if sys .version_info < (3 , 2 ):
447273 try :
@@ -451,6 +277,8 @@ def __ne__(self, other):
451277
452278 from functools import wraps
453279
280+ _CacheInfo = collections .namedtuple ("CacheInfo" , "hits misses maxsize currsize" )
281+
454282 def lru_cache (maxsize = 100 ):
455283 """Least-recently-used cache decorator.
456284 Taking from: https://github.com/MiCHiLU/python-functools32/blob/master/functools32/functools32.py
@@ -461,7 +289,7 @@ def lru_cache(maxsize=100):
461289 View the cache statistics named tuple (hits, misses, maxsize, currsize) with
462290 f.cache_info(). Clear the cache and statistics with f.cache_clear().
463291 Access the underlying function with f.__wrapped__.
464- See: http ://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used
292+ See: https ://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used
465293
466294 """
467295 def decorating_function (user_function , tuple = tuple , sorted = sorted , len = len , KeyError = KeyError ):
@@ -470,7 +298,7 @@ def decorating_function(user_function, tuple=tuple, sorted=sorted, len=len, KeyE
470298 lock = Lock ()
471299
472300 if maxsize is None :
473- CACHE = dict ()
301+ CACHE = {}
474302
475303 @wraps (user_function )
476304 def wrapper (* args , ** kwds ):
@@ -488,7 +316,7 @@ def wrapper(*args, **kwds):
488316 misses [0 ] += 1
489317 return result
490318 else :
491- CACHE = OrderedDict ()
319+ CACHE = collections . OrderedDict ()
492320
493321 @wraps (user_function )
494322 def wrapper (* args , ** kwds ):
@@ -528,7 +356,7 @@ def cache_clear():
528356 return decorating_function
529357
530358else :
531- from functools import lru_cache
359+ from functools import lru_cache # noqa: F401
532360
533361
534362class OrderedSet (collections .MutableSet ):
0 commit comments