def __init__(self): self.db = DB() self.solves = self.db.load() self.solvecount = self.solveamount() self.oldbest = [0] * 4 self.oldlast = [0] * 4 try: self.currentpb = sorted(self.gettimes())[0] except IndexError: pass
class History(): def __init__(self): self.db = DB() self.solves = self.db.load() self.solvecount = self.solveamount() self.oldbest = [0] * 4 self.oldlast = [0] * 4 try: self.currentpb = sorted(self.gettimes())[0] except IndexError: pass def solveamount(self): return len(self.solves['3x3']) def gettimes(self): ''' returns a list with times of all past 3x3 solves in chronological order ''' return [pastSolve.time for pastSolve in self.solves['3x3']] def globalavg(self, times): totaltime = 0 for duration in times: totaltime += duration if not len(times): return '-----' return '{:.2f}'.format(totaltime / len(times)) def average(self, avgof, times): ''' calculate last average of 5/12/50/100 ''' if len(times) < avgof: return '-----' tocount = {5: 1, 12: 1, 50: 3, 100: 5} counting = times[tocount[avgof]:avgof - tocount[avgof]] sumtime = 0 for solvetime in counting: sumtime += solvetime return '{:.2f}'.format(sumtime / len(counting)) def bestaverage(self, avgof, times): if len(times) < avgof: return '-----' times = self.chunk(times, avgof) averages = [self.average(avgof, solvetimes) for solvetimes in times] averages = [x for x in averages if x != '-----'] return sorted(averages)[0] def chunk(self, times, avgof): """ slices times into blocks of avgof """ return (times[pos:pos + avgof] for pos in range(len(times))) def save(self, timer, scramble, mode): solve = Solve(timer.begin, timer.gettime(), scramble, mode) self.solves[mode].append(solve) self.db.insert(solve) self._last = solve def deletelast(self): self.solves[self._last.mode].pop() self.db.delete(self._last.rowid) print("Last solve deleted!\n0.00", end="\r") def set_dnf(self): self._last.time = -1 self._last.dnf = True self.db.setflag("dnf", self._last.rowid) def set_plustwo(self): self._last.plustwo = True self.db.setflag("plustwo", self._last.rowid) def getlast(self, mode, n): return self.solves[mode][-n:] def close(self): self.db.close() def __str__(self): avgno = [5, 12, 50, 100] pastsolves = self.gettimes()[::-1] bestsolves = sorted(pastsolves) lastavgof = [self.average(x, pastsolves) for x in avgno] bestavgof = [self.bestaverage(x, pastsolves) for x in avgno] recents = ['{:.2f}'.format(x) for x in pastsolves[:12]] alltimepb = '{:.2f}'.format(bestsolves[0]) if len(bestsolves) else '-----' # prettify average if new pb for x in range(4): try: if float(alltimepb) < float(self.currentpb): alltimepb = bcolors.FATGOLD + alltimepb + bcolors.ENDC if float(bestavgof[x]) < float(self.oldbest[x]): bestavgof[x] = bcolors.FATGOLD + bestavgof[x] + bcolors.ENDC if float(lastavgof[x]) < float(self.oldlast[x]): lastavgof[x] = bcolors.FATGOLD + lastavgof[x] + bcolors.ENDC except ValueError: pass out = ('Total : ' + str(self.solveamount()) + '\n' 'Best Ao5 : ' + bestavgof[0] + ' Last Ao5 : ' + lastavgof[0] + '\n' 'Best Ao12 : ' + bestavgof[1] + ' Last Ao12 : ' + lastavgof[1] + '\n' 'Best Ao50 : ' + bestavgof[2] + ' Last Ao50 : ' + lastavgof[2] + '\n' 'Best Ao100 : ' + bestavgof[3] + ' Last Ao100 : ' + lastavgof[3] + '\n\n' 'Alltime PB : ' + alltimepb + ' Global: ' + self.globalavg(pastsolves) + '\n\n' 'Recent Solves') rowcnt = 0 if recents: best = min(recents) worst = max(recents) for x in recents: if x == best: x = bcolors.FATBLU + str(x) + bcolors.ENDC if x == worst: x = bcolors.FATRED + str(x) + bcolors.ENDC if rowcnt % 4 == 0: out += '\n' rowcnt += 1 out += '{} '.format(x) out += '\n' self.oldbest, self.oldlast, self.currentpb = bestavgof, lastavgof, alltimepb return out