Skip to content

Instantly share code, notes, and snippets.

@Alligator
Created November 6, 2012 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alligator/4021302 to your computer and use it in GitHub Desktop.
Save Alligator/4021302 to your computer and use it in GitHub Desktop.
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