Advertisement
narthollis

Untitled

Aug 15th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import datetime
  4.    
  5. class Seen(dict):
  6.     def __init__(self, *args, **kargs):
  7.         super().__init__(self, *args, **kargs)
  8.         self.__oldName = {}
  9.  
  10.     def __getitem__(self, nick):
  11.         if not nick in self.keys():
  12.             if nick in self.__oldName.keys():
  13.                 return self[self.__oldName[nick]]
  14.  
  15.         return super().__getitem__(nick)
  16.  
  17.     def __setitem__(self, nick, msg):
  18.         super().__setitem__(nick, {'time':datetime.datetime.now(), 'msg':msg})
  19.  
  20.     def change(self, old, new):
  21.         if old in super().keys():
  22.             super().__setitem__(new, super().__getitem__(old))
  23.             super().__delitem__(old)
  24.             self.__oldName[old] = new
  25.  
  26. if __name__ == "__main__":
  27.     s = Seen()
  28.     s['narthollis'] = 'test'
  29.     assert s['narthollis']['msg'] == 'test'
  30.     print('Test 1 Passed')
  31.    
  32.     s.change('narthollis', 'narth')
  33.     assert s['narthollis']['msg'] == 'test'
  34.     assert s['narth']['msg'] == 'test'
  35.     print('Test 2 Passed')
  36.    
  37.     s.change('narth', 'nartho][is')    
  38.     assert s['narthollis']['msg'] == 'test'
  39.     assert s['narth']['msg'] == 'test'
  40.     assert s['nartho][is']['msg'] == 'test'
  41.     print('Test 3 Passed')
  42.    
  43.     s['bob'] = 'test 2'
  44.     assert s['narthollis']['msg'] == 'test'
  45.     assert s['narth']['msg'] == 'test'
  46.     assert s['nartho][is']['msg'] == 'test'
  47.     assert s['bob']['msg'] == 'test 2'
  48.     print('Test 4 Passed')
  49.    
  50.     s.change('bob', 'bobett')
  51.     assert s['narthollis']['msg'] == 'test'
  52.     assert s['narth']['msg'] == 'test'
  53.     assert s['nartho][is']['msg'] == 'test'
  54.     assert s['bob']['msg'] == 'test 2'
  55.     assert s['bobett']['msg'] == 'test 2'
  56.     print('Test 5 Passed')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement