-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.py
More file actions
253 lines (202 loc) · 7.14 KB
/
stats.py
File metadata and controls
253 lines (202 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import sqlite3
import os
from os import path
import calendar
from datetime import date
from datetime import timedelta
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.lines as lines
def initialize_database():
if not path.exists("crosswords/stats.db"):
conn = sqlite3.connect("crosswords/stats.db")
c = conn.cursor()
c.execute("CREATE TABLE stats (date text, dayOfWeek text, time integer, streak integer)")
conn.close()
def add_puzzle(puzzdate, time):
conn = sqlite3.connect("crosswords/stats.db")
c = conn.cursor()
year = int(puzzdate[0 : 4])
month = int(puzzdate[5 : 7])
day = int(puzzdate[8 : ])
c.execute("SELECT * from stats WHERE date=?", (puzzdate,))
if(c.fetchone()):
return
if str(date.today()) == puzzdate:
streak = 1
else:
streak = 0
dayOfWeek = calendar.day_name[calendar.weekday(year, month, day)]
c.execute("INSERT INTO stats VALUES (?, ?, ?, ?)", (puzzdate, dayOfWeek, time, streak))
conn.commit()
conn.close()
def remove_puzzle(puzzdate):
conn = sqlite3.connect("crosswords/stats.db")
c = conn.cursor()
c.execute("DELETE from stats WHERE date=?", (puzzdate,))
conn.commit()
conn.close()
def print_all():
conn = sqlite3.connect("crosswords/stats.db")
c = conn.cursor()
c.execute("DELETE from stats WHERE date=?", ("2020-08-24",))
c.execute("SELECT * from stats")
print("All Puzzles:")
print(c.fetchall())
conn.close()
def calc_average(dayOfWeek):
dayNum = list(calendar.day_name).index(dayOfWeek)
conn = sqlite3.connect("crosswords/stats.db")
c = conn.cursor()
c.execute("SELECT time from stats WHERE dayOfWeek=?", (dayOfWeek,))
times = c.fetchall()
if times == []:
return 0
avg = 0
for time in times:
avg += time[0]
avg /= len(times)
mins = int(avg // 60)
secs = int(avg % 60)
conn.close()
return avg
def calc_best(dayOfWeek):
conn = sqlite3.connect("crosswords/stats.db")
c = conn.cursor()
c.execute("SELECT time from stats WHERE dayOfWeek=?", (dayOfWeek,))
times = c.fetchall()
if times == []:
return 0
best = times[0][0]
for time in times:
if time[0] < best:
best = time[0]
mins = int(best // 60)
secs = int(best % 60)
return best
def most_recent(dayOfWeek):
conn = sqlite3.connect("crosswords/stats.db")
c = conn.cursor()
c.execute("SELECT time, date from stats WHERE dayOfWeek=?", (dayOfWeek,))
days = c.fetchall()
if days == []:
return 0
curyear = curmonth = curday = 0
for entry in days:
puzzdate = entry[1]
year = int(puzzdate[0 : 4])
month = int(puzzdate[5 : 7])
day = int(puzzdate[8 : ])
if year > curyear:
time = entry[0]
elif year == curyear and month > curmonth:
time = entry[0]
elif year == curyear and month == curmonth and day > curday:
time = entry[0]
mins = int(time // 60)
secs = int(time % 60)
return time
def puzzles_completed():
conn = sqlite3.connect("crosswords/stats.db")
c = conn.cursor()
c.execute("SELECT * from stats")
num = len(c.fetchall())
return num
def streak():
conn = sqlite3.connect("crosswords/stats.db")
c = conn.cursor()
c.execute("SELECT date from stats")
dates = c.fetchall()
streak = 0
if (str(date.today()),) in dates:
streak = 1
curdate = date.today() + timedelta(-1)
while (str(curdate),) in dates:
streak += 1
curdate = curdate + timedelta(-1)
return 501 + streak
def graph():
conn = sqlite3.connect("crosswords/stats.db")
c = conn.cursor()
today = calendar.day_name[date.today().weekday()]
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
datadict = {"Recent":{}, "Best":{}, "Average":{}, "Day":{}}
for i in range(0, 7):
datadict["Day"][days[i]] = days[i]
datadict["Recent"][days[i]] = most_recent(days[i])
datadict["Best"][days[i]] = calc_best(days[i])
datadict["Average"][days[i]] = calc_average(days[i])
data = pd.DataFrame.from_dict(datadict)
sns.set(style="whitegrid")
# Initialize the matplotlib figure
f, ax = plt.subplots(figsize=(12, 9))
sns.barplot(x="Day", y="Recent", data=data, alpha=0)
for rect in ax.patches:
rect.set_width(rect.get_width() + .1)
rect.set_x(rect.get_x() - .05)
line = lines.Line2D([rect.get_x(), rect.get_x()+rect.get_width()], [rect.get_height()], color="black", linestyle="--")
ax.add_line(line)
clrs = ["lightcoral" if (x==today) else "cornflowerblue" for x in days]
sns.barplot(x="Day", y="Average", data=data, palette=clrs, edgecolor="black", linestyle="-")
clrs = ["red" if (x==today) else "dodgerblue" for x in days]
sns.barplot(x="Day", y="Best", data=data, palette=clrs, edgecolor="black")
for i in range(len(ax.patches)):
rect = ax.patches[i]
if rect.get_height() in datadict["Best"].values():
rect.set_width(rect.get_width() + .1)
rect.set_x(rect.get_x() - .05)
height = rect.get_height()
minutes = str(int(height // 60))
seconds = str(int(height % 60))
if len(seconds) == 1:
seconds = "0" + seconds
time = minutes + ":" + seconds
offset = -15
if not time == "0:00":
ax.annotate('{}'.format("Best: " + time), xy=(rect.get_x() + rect.get_width() / 2, height), xytext=(0, offset), textcoords="offset points", ha="center", va="bottom", fontweight="bold")
if rect.get_height() in datadict["Average"].values():
rect.set_width(rect.get_width() + .1)
rect.set_x(rect.get_x() - .05)
r,g,b,a = rect.get_facecolor()
rect.set_facecolor((r,g,b,.5))
height = rect.get_height()
minutes = str(int(height // 60))
seconds = str(int(height % 60))
if len(seconds) == 1:
seconds = "0" + seconds
time = minutes + ":" + seconds
if int(ax.patches[i-7].get_height()) in range(int(height) - 5, int(height)+50):
offset = -15
else:
offset = 1
if not time == "0:00":
ax.annotate('{}'.format("Average: " + time), xy=(rect.get_x() + rect.get_width() / 2, height), xytext=(0, offset), textcoords="offset points", ha="center", va="bottom", fontweight="bold")
if rect.get_height() in datadict["Recent"].values():
if rect.get_height() == datadict["Recent"][today]:
label = "Today: "
else:
label = "Recent: "
height = rect.get_height()
minutes = str(int(height // 60))
seconds = str(int(height % 60))
if len(seconds) == 1:
seconds = "0" + seconds
time = minutes + ":" + seconds
if int(ax.patches[i+7].get_height()) in range(int(height), int(height) + 50):
offset = -15
else:
offset = 1
if not time == "0:00":
ax.annotate('{}'.format(label + time), xy=(rect.get_x() + rect.get_width() / 2, height), xytext=(0, offset), textcoords="offset points", ha="center", va="bottom", fontweight="bold")
ax.annotate('{}'.format(str(streak())) + " day streak", xy=(0, calc_average("Sunday")), xytext=(0.001, 0), textcoords="offset points", ha="left", va="bottom", fontsize=20)
sns.despine(left=True, bottom=True, top=True, right=True)
f.axes[0] = ""
ax.set_ylabel("")
ax.set_xlabel("")
ax.grid(False)
ax.axes.yaxis.set_visible(False)
thismanager = plt.get_current_fig_manager()
thismanager.window.wm_geometry("+350+30")
plt.show()