Created
March 3, 2011 18:27
-
-
Save EnigmaCurry/853227 to your computer and use it in GitHub Desktop.
@jessenoller lunch break challenge : http://twitter.com/#!/jessenoller/status/43371023306981376
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from threading import Timer | |
class SuicidalKey(object): | |
""" | |
>>> k = SuicidalKey("asdf",30) | |
>>> k.key | |
'asdf' | |
>>> # Before 30 seconds are up | |
>>> k.reset_expiration(30) | |
>>> # Wait 30 seconds | |
>>> k.key | |
None | |
""" | |
def __init__(self, key, expiration_time=300): | |
self.key = key | |
self.__expire_timer = Timer(0,None) | |
self.reset_expiration(expiration_time) | |
def __expire_key(self): | |
self.key = None | |
def reset_expiration(self, expiration_time=300): | |
if self.key: | |
self.__expire_timer.cancel() | |
self.__expire_timer = Timer(expiration_time, self.__expire_key) | |
self.__expire_timer.start() |
zzzeek
commented
Mar 3, 2011
zzzeek: consider using an abc instead so you can have full control on where the key is removed. Your implementation will fail by exchanging d["a"] with "a" in d, as an example.
yup, many methods will fail in this partial example including d.values(). was just to illustrate the method of associating timestamp.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment