clean up the database code

This commit is contained in:
randomuser 2021-07-20 23:51:54 -05:00
parent 616a2ccd17
commit 7a8f286e78
1 changed files with 9 additions and 2 deletions

11
db.py
View File

@ -6,10 +6,12 @@ class DuckDB:
fd = open(location, "r") fd = open(location, "r")
self.parse(fd) self.parse(fd)
fd.close() fd.close()
def parse(self, fd): def parse(self, fd):
lines = [i.rstrip() for i in fd.readlines()] lines = [i.rstrip() for i in fd.readlines()]
for i in lines: for i in lines:
self.db.append(DuckEvent(i)) self.db.append(DuckEvent(i))
def output(self, fd): def output(self, fd):
for i in self.db: for i in self.db:
fd.write(i.stringify() + "\n") fd.write(i.stringify() + "\n")
@ -22,6 +24,7 @@ class DuckEvent:
self.offset = None self.offset = None
self.channel = None self.channel = None
if not line == None: self.internalize(line) if not line == None: self.internalize(line)
def internalize(self, line): def internalize(self, line):
self.status = line[0].upper() self.status = line[0].upper()
spl = line.split(' ') spl = line.split(' ')
@ -29,6 +32,7 @@ class DuckEvent:
self.time = float(spl[1]) self.time = float(spl[1])
self.offset = float(spl[2]) self.offset = float(spl[2])
self.channel = spl[3] self.channel = spl[3]
def stringify(self): def stringify(self):
return "{}{} {} {} {}".format( return "{}{} {} {} {}".format(
self.status, self.status,
@ -41,15 +45,18 @@ class DuckEvent:
class DuckStats: class DuckStats:
def __init__(self, db): def __init__(self, db):
self.db = db self.db = db
def countstatus(self, nick, status): def countstatus(self, nick, status):
cnt = 0 cnt = 0
for i in self.db.db: for i in self.db.db:
if i.status == status and i.nick == nick: if i.status == status and i.nick == nick: cnt += 1
cnt += 1
return cnt return cnt
def cought(self, nick): def cought(self, nick):
return self.countstatus(nick, "B") return self.countstatus(nick, "B")
def missed(self, nick): def missed(self, nick):
return self.countstatus(nick, "M") return self.countstatus(nick, "M")
def ratio(self, nick): def ratio(self, nick):
return (self.cought(nick) / self.missed(nick)) * 100 return (self.cought(nick) / self.missed(nick)) * 100