Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 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 __contains__(self, item):
  48.         for n in xrange(0, len(self.objs)):
  49.             if self.objs[n].get_object() == item:
  50.                 return True;
  51.            
  52.         return False;
  53.    
  54.     def set_lifetime(self, lifetime):
  55.         debug_print("Lifetime=%f" % (lifetime));
  56.         self.lifetime = lifetime;
  57.    
  58.     def add(self, obj, lifetime=None):
  59.         if lifetime == None:
  60.             lifetime = self.lifetime;
  61.        
  62.         debug_print("Added %s (lifetime=%f)" % (str(obj), lifetime));
  63.         self.objs.append(TimedObject(obj, lifetime));
  64.        
  65.     def remove(self, obj):
  66.         debug_print("Removing %s" % (str(obj)));
  67.         index = self.index_of(obj);
  68.         if index < 0:
  69.             return False;
  70.        
  71.         del self.objs[index];
  72.         return True;
  73.        
  74.     def index_of(self, obj):
  75.         for n in xrange(0, len(self.objs)):
  76.             if self.objs[n] == obj:
  77.                 return n;
  78.        
  79.         return -1;
  80.    
  81.     def clear(self):
  82.         debug_print("Cleared");
  83.         del self.objs;
  84.         self.objs = [];
  85.        
  86.     def update(self):
  87.         remove_list = [];
  88.         for item in self.objs:
  89.             if item.expired():
  90.                 remove_list.append(item);
  91.        
  92.         for item in remove_list:
  93.             assert self.remove(item);
  94.    
  95.  
  96. if __name__ == '__main__':
  97.     # Run tests.
  98.     debug_mode = True;
  99.    
  100.     col = TimedCollection();
  101.     col.set_lifetime(5.0);
  102.     col.add("hi");
  103.     col.add("how are you?", 7.5);
  104.     col.add("what's up?", 9.0);
  105.    
  106.     seconds_passed = 0;
  107.     while len(col) != 0:
  108.         time.sleep(1.0);
  109.         col.update();
  110.         seconds_passed += 1;
  111.         assert seconds_passed < 15;
  112.    
  113.     debug_print("Testing successful.");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement