Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. import time;
  2. now = time.time;
  3.  
  4. debug_mode = False;
  5.  
  6. def debug_print(message):
  7.     if debug_mode:
  8.         print "[TimedCollection]: %s" % (message);
  9.  
  10. class TimedObject:
  11.     def __init__(self, obj, lifetime):
  12.         self.obj = obj;
  13.         self.timestamp = now();
  14.         self.lifetime = lifetime;
  15.         self.expire_flag = False;
  16.    
  17.     def __str__(self):
  18.         return str(self.obj);
  19.    
  20.     def expired(self):
  21.         if self.expire_flag:
  22.             return True;
  23.        
  24.         current_time = now();
  25.         if current_time - self.timestamp > self.lifetime:
  26.             self.expire_flag = True;
  27.             return True;
  28.        
  29.         return False;
  30.        
  31.     def get_object(self):
  32.         return self.obj;
  33.  
  34. class TimedCollection:
  35.     DEFAULT_LIFETIME = 5.0; # seconds
  36.    
  37.     def __init__(self, lifetime=DEFAULT_LIFETIME):
  38.         self.objs = [];
  39.         self.lifetime = lifetime;
  40.    
  41.     def __delitem__(self, item):
  42.         self.remove(item);
  43.        
  44.     def __len__(self):
  45.         return len(self.objs);
  46.    
  47.     def set_lifetime(self, lifetime):
  48.         debug_print("Lifetime=%f" % (lifetime));
  49.         self.lifetime = lifetime;
  50.    
  51.     def add(self, obj, lifetime=None):
  52.         if lifetime == None:
  53.             lifetime = self.lifetime;
  54.        
  55.         debug_print("Added %s (lifetime=%f)" % (str(obj), lifetime));
  56.         self.objs.append(TimedObject(obj, lifetime));
  57.        
  58.     def remove(self, obj):
  59.         debug_print("Removing %s" % (str(obj)));
  60.         index = self.index_of(obj);
  61.         if index < 0:
  62.             return False;
  63.        
  64.         del self.objs[index];
  65.         return True;
  66.        
  67.     def index_of(self, obj):
  68.         for n in xrange(0, len(self.objs)):
  69.             if self.objs[n] == obj:
  70.                 return n;
  71.        
  72.         return -1;
  73.    
  74.     def clear(self):
  75.         debug_print("Cleared");
  76.         del self.objs;
  77.         self.objs = [];
  78.        
  79.     def update(self):
  80.         remove_list = [];
  81.         for item in self.objs:
  82.             if item.expired():
  83.                 remove_list.append(item);
  84.        
  85.         for item in remove_list:
  86.             assert self.remove(item);
  87.    
  88.  
  89. if __name__ == '__main__':
  90.     # Run tests.
  91.     debug_mode = False;
  92.    
  93.     col = TimedCollection();
  94.     col.set_lifetime(5.0);
  95.     col.add("hi");
  96.     col.add("how are you?", 7.5);
  97.     col.add("what's up?", 9.0);
  98.    
  99.     seconds_passed = 0;
  100.     while len(col) != 0:
  101.         time.sleep(1.0);
  102.         col.update();
  103.         seconds_passed += 1;
  104.         assert seconds_passed < 15;
  105.    
  106.     debug_print("Testing successful.");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement