Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import datetime
- class Seen(dict):
- def __init__(self, *args, **kargs):
- super().__init__(self, *args, **kargs)
- self.__oldName = {}
- def __getitem__(self, nick):
- if not nick in self.keys():
- if nick in self.__oldName.keys():
- return self[self.__oldName[nick]]
- return super().__getitem__(nick)
- def __setitem__(self, nick, msg):
- super().__setitem__(nick, {'time':datetime.datetime.now(), 'msg':msg})
- def change(self, old, new):
- if old in super().keys():
- super().__setitem__(new, super().__getitem__(old))
- super().__delitem__(old)
- self.__oldName[old] = new
- if __name__ == "__main__":
- s = Seen()
- s['narthollis'] = 'test'
- assert s['narthollis']['msg'] == 'test'
- print('Test 1 Passed')
- s.change('narthollis', 'narth')
- assert s['narthollis']['msg'] == 'test'
- assert s['narth']['msg'] == 'test'
- print('Test 2 Passed')
- s.change('narth', 'nartho][is')
- assert s['narthollis']['msg'] == 'test'
- assert s['narth']['msg'] == 'test'
- assert s['nartho][is']['msg'] == 'test'
- print('Test 3 Passed')
- s['bob'] = 'test 2'
- assert s['narthollis']['msg'] == 'test'
- assert s['narth']['msg'] == 'test'
- assert s['nartho][is']['msg'] == 'test'
- assert s['bob']['msg'] == 'test 2'
- print('Test 4 Passed')
- s.change('bob', 'bobett')
- assert s['narthollis']['msg'] == 'test'
- assert s['narth']['msg'] == 'test'
- assert s['nartho][is']['msg'] == 'test'
- assert s['bob']['msg'] == 'test 2'
- assert s['bobett']['msg'] == 'test 2'
- print('Test 5 Passed')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement