Created
November 6, 2012 00:09
-
-
Save Alligator/4021302 to your computer and use it in GitHub Desktop.
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
class IRCStats(object): | |
def __init__(self, path): | |
self.path = path | |
try: | |
self.users = json.loads(open(self.path, 'r').read()) | |
except Exception, e: | |
self.users = {} | |
# JSON makes defaultdict weird, roll yer own | |
def add(self, nick): | |
return self.users.setdefault(nick, { | |
'aliases': [], | |
'lines': [0]*24 | |
}) | |
def new_line(self, nick, hour): | |
self.add(nick) | |
self.users[nick]['lines'][hour] += 1 | |
self.write() | |
def nick_change(self, old, new): | |
# if the new nick is already there just use it | |
if new in self.users: | |
return | |
# switch to new key | |
self.users[new] = self.users[old] | |
del self.users[old] | |
# add old key to aliases | |
if old not in self.users[new]['aliases']: | |
self.users[new]['aliases'].append(old) | |
# remove new nick from anyone else who might have it | |
for user, data in self.users.iteritems(): | |
if new in data['aliases']: | |
data['aliases'].remove(new) | |
self.write() | |
def write(self): | |
open(self.path, 'w').write(json.dumps(self.users)) | |
def __str__(self): | |
return str(dict(self.users)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment