11"""Disk Cache Recipes
2-
32"""
43
54import functools
@@ -40,7 +39,7 @@ def __init__(self, cache, key, expire=None, tag=None):
4039 self ._tag = tag
4140
4241 def add (self , value ):
43- "Add `value` to average."
42+ """ Add `value` to average."" "
4443 with self ._cache .transact (retry = True ):
4544 total , count = self ._cache .get (self ._key , default = (0.0 , 0 ))
4645 total += value
@@ -53,12 +52,12 @@ def add(self, value):
5352 )
5453
5554 def get (self ):
56- "Get current average or return `None` if count equals zero."
55+ """ Get current average or return `None` if count equals zero."" "
5756 total , count = self ._cache .get (self ._key , default = (0.0 , 0 ), retry = True )
5857 return None if count == 0 else total / count
5958
6059 def pop (self ):
61- "Return current average and delete key."
60+ """ Return current average and delete key."" "
6261 total , count = self ._cache .pop (self ._key , default = (0.0 , 0 ), retry = True )
6362 return None if count == 0 else total / count
6463
@@ -83,7 +82,7 @@ def __init__(self, cache, key, expire=None, tag=None):
8382 self ._tag = tag
8483
8584 def acquire (self ):
86- "Acquire lock using spin-lock algorithm."
85+ """ Acquire lock using spin-lock algorithm."" "
8786 while True :
8887 added = self ._cache .add (
8988 self ._key ,
@@ -97,11 +96,11 @@ def acquire(self):
9796 time .sleep (0.001 )
9897
9998 def release (self ):
100- "Release lock by deleting key."
99+ """ Release lock by deleting key."" "
101100 self ._cache .delete (self ._key , retry = True )
102101
103102 def locked (self ):
104- "Return true if the lock is acquired."
103+ """ Return true if the lock is acquired."" "
105104 return self ._key in self ._cache
106105
107106 def __enter__ (self ):
@@ -137,7 +136,7 @@ def __init__(self, cache, key, expire=None, tag=None):
137136 self ._tag = tag
138137
139138 def acquire (self ):
140- "Acquire lock by incrementing count using spin-lock algorithm."
139+ """ Acquire lock by incrementing count using spin-lock algorithm."" "
141140 pid = os .getpid ()
142141 tid = threading .get_ident ()
143142 pid_tid = '{}-{}' .format (pid , tid )
@@ -156,7 +155,7 @@ def acquire(self):
156155 time .sleep (0.001 )
157156
158157 def release (self ):
159- "Release lock by decrementing count."
158+ """ Release lock by decrementing count."" "
160159 pid = os .getpid ()
161160 tid = threading .get_ident ()
162161 pid_tid = '{}-{}' .format (pid , tid )
@@ -206,7 +205,7 @@ def __init__(self, cache, key, value=1, expire=None, tag=None):
206205 self ._tag = tag
207206
208207 def acquire (self ):
209- "Acquire semaphore by decrementing value using spin-lock algorithm."
208+ """ Acquire semaphore by decrementing value using spin-lock algorithm."" "
210209 while True :
211210 with self ._cache .transact (retry = True ):
212211 value = self ._cache .get (self ._key , default = self ._value )
@@ -221,7 +220,7 @@ def acquire(self):
221220 time .sleep (0.001 )
222221
223222 def release (self ):
224- "Release semaphore by incrementing value."
223+ """ Release semaphore by incrementing value."" "
225224 with self ._cache .transact (retry = True ):
226225 value = self ._cache .get (self ._key , default = self ._value )
227226 assert self ._value > value , 'cannot release un-acquired semaphore'
@@ -396,19 +395,19 @@ def memoize_stampede(cache, expire, name=None, typed=False, tag=None, beta=1):
396395 """
397396 # Caution: Nearly identical code exists in Cache.memoize
398397 def decorator (func ):
399- "Decorator created by memoize call for callable."
398+ """ Decorator created by memoize call for callable."" "
400399 base = (full_name (func ),) if name is None else (name ,)
401400
402401 def timer (* args , ** kwargs ):
403- "Time execution of `func` and return result and time delta."
402+ """ Time execution of `func` and return result and time delta."" "
404403 start = time .time ()
405404 result = func (* args , ** kwargs )
406405 delta = time .time () - start
407406 return result , delta
408407
409408 @functools .wraps (func )
410409 def wrapper (* args , ** kwargs ):
411- "Wrapper for callable to cache arguments and return values."
410+ """ Wrapper for callable to cache arguments and return values."" "
412411 key = wrapper .__cache_key__ (* args , ** kwargs )
413412 pair , expire_time = cache .get (
414413 key ,
@@ -459,7 +458,7 @@ def recompute():
459458 return pair [0 ]
460459
461460 def __cache_key__ (* args , ** kwargs ):
462- "Make key for cache given function arguments."
461+ """ Make key for cache given function arguments."" "
463462 return args_to_key (base , args , kwargs , typed )
464463
465464 wrapper .__cache_key__ = __cache_key__
0 commit comments