mirror of
https://github.com/python/cpython.git
synced 2025-10-12 09:53:19 +00:00
Section-off the source for better readability.
This commit is contained in:
parent
dce583e0bd
commit
bc8e81dcc3
1 changed files with 23 additions and 0 deletions
|
@ -18,6 +18,11 @@ try:
|
||||||
except:
|
except:
|
||||||
from _dummy_thread import allocate_lock as Lock
|
from _dummy_thread import allocate_lock as Lock
|
||||||
|
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
### update_wrapper() and wraps() decorator
|
||||||
|
################################################################################
|
||||||
|
|
||||||
# update_wrapper() and wraps() are tools to help write
|
# update_wrapper() and wraps() are tools to help write
|
||||||
# wrapper functions that can handle naive introspection
|
# wrapper functions that can handle naive introspection
|
||||||
|
|
||||||
|
@ -66,6 +71,11 @@ def wraps(wrapped,
|
||||||
return partial(update_wrapper, wrapped=wrapped,
|
return partial(update_wrapper, wrapped=wrapped,
|
||||||
assigned=assigned, updated=updated)
|
assigned=assigned, updated=updated)
|
||||||
|
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
### total_ordering class decorator
|
||||||
|
################################################################################
|
||||||
|
|
||||||
def total_ordering(cls):
|
def total_ordering(cls):
|
||||||
"""Class decorator that fills in missing ordering methods"""
|
"""Class decorator that fills in missing ordering methods"""
|
||||||
convert = {
|
convert = {
|
||||||
|
@ -94,6 +104,11 @@ def total_ordering(cls):
|
||||||
setattr(cls, opname, opfunc)
|
setattr(cls, opname, opfunc)
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
### cmp_to_key() function converter
|
||||||
|
################################################################################
|
||||||
|
|
||||||
def cmp_to_key(mycmp):
|
def cmp_to_key(mycmp):
|
||||||
"""Convert a cmp= function into a key= function"""
|
"""Convert a cmp= function into a key= function"""
|
||||||
class K(object):
|
class K(object):
|
||||||
|
@ -120,6 +135,11 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
### LRU Cache function decorator
|
||||||
|
################################################################################
|
||||||
|
|
||||||
_CacheInfo = namedtuple("CacheInfo", ["hits", "misses", "maxsize", "currsize"])
|
_CacheInfo = namedtuple("CacheInfo", ["hits", "misses", "maxsize", "currsize"])
|
||||||
|
|
||||||
def lru_cache(maxsize=100, typed=False):
|
def lru_cache(maxsize=100, typed=False):
|
||||||
|
@ -170,6 +190,7 @@ def lru_cache(maxsize=100, typed=False):
|
||||||
return key
|
return key
|
||||||
|
|
||||||
if maxsize is None:
|
if maxsize is None:
|
||||||
|
|
||||||
@wraps(user_function)
|
@wraps(user_function)
|
||||||
def wrapper(*args, **kwds):
|
def wrapper(*args, **kwds):
|
||||||
# simple caching without ordering or size limit
|
# simple caching without ordering or size limit
|
||||||
|
@ -183,7 +204,9 @@ def lru_cache(maxsize=100, typed=False):
|
||||||
cache[key] = result
|
cache[key] = result
|
||||||
misses += 1
|
misses += 1
|
||||||
return result
|
return result
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
@wraps(user_function)
|
@wraps(user_function)
|
||||||
def wrapper(*args, **kwds):
|
def wrapper(*args, **kwds):
|
||||||
# size limited caching that tracks accesses by recency
|
# size limited caching that tracks accesses by recency
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue