-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze.py
141 lines (125 loc) · 6.31 KB
/
analyze.py
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
from db import get_counter_data, get_tracker_data_spec, get_habits_periodicity
from datetime import date, datetime, timedelta
def calculate_count(db, name):
"""
Calculate the count of the counter.
:param db: An initialized sqlite3 database connection
:param name: The name of the counter present in the DB
:return: Length of the counter increment events
"""
data = get_counter_data(db, name)
return len(data)
def streak_count_daily(db):
"""
Calculate the current and record streak of a daily habit
:param db: An initialized sqlite3 database connection
:return: The current, record and overall record streak for daily habits
"""
# Returns the daily habits that were created and saves the data in a list.
periodicity = "Daily"
list_daily = get_habits_periodicity(db, periodicity)
lower_periodicty = str.lower(periodicity)
dict_overall = {}
# Only gives back the currently created names of the habits
# Selecting only the incremented dates of a specific habit
for i in list_daily:
name = (','.join(i))
data_1 = sorted(get_tracker_data_spec(db, name))
count = calculate_count(db, name)
counter = 0
streak = 0
list_streak_habit = []
if count == 0:
continue
else:
# Analyzes the dates for the habits and calculates the current streaks for each habit.
while counter < len(data_1)-1:
date_0 = datetime.strptime((','.join(data_1[counter])), '%Y-%m-%d').date()
date_1 = datetime.strptime((','.join(data_1[counter+1])), '%Y-%m-%d').date()
date_last = datetime.strptime((','.join(data_1[len(data_1) - 1])), '%Y-%m-%d').date()
# A habit is not checked if the habit is completed twice on the same day
if date_0 == date_1:
streak += 0
# Calculates the time difference between two habits and increments the current streak
elif date_0 + timedelta(days=1) == date_1 and date_last >= date.today() - timedelta(days=1):
streak += 1
else:
streak = 0
counter += 1
list_streak_habit.append(streak)
print(f" {name}: Current streak of {streak} ({lower_periodicty})!")
print(f" {name} All-time record: {max(list_streak_habit)} ({lower_periodicty})!")
print("")
dict_overall[name] = max(list_streak_habit)
# returns the current streak for the test module
return streak
#returns the highest overall streak for each habit for the test module
return dict_overall.get(max(dict_overall, key=dict_overall.get))
print(f" Overall record streak: "
f"{dict_overall.get(max(dict_overall, key=dict_overall.get, default=0))} for "
f"{max(dict_overall, key=dict_overall.get, default=0)}! ({lower_periodicty})!")
def streak_count_weekly(db):
"""
Calculate the current and record streak of a weekly habit
:param db: An initialized sqlite3 database connection
:return: The current, record and overall record streak for weekly habits
"""
list_streak_overall = []
# Returns the daily habits that were created and saves the data in a list.
periodicity = "Weekly"
list_weekly = get_habits_periodicity(db, periodicity)
lower_periodicty = str.lower(periodicity)
dict_overall = {}
# Only gives back the currently created names of the habits
# Selecting only the incremented dates of a specific habit
for i in list_weekly:
name = (','.join(i))
data_1 = sorted(get_tracker_data_spec(db, name))
count = calculate_count(db, name)
counter = 0
streak = 0
list_streak_habit = []
if count == 0:
continue
else:
# Analyzes the dates for the habits and calculates the current streaks for each habit.
while counter < len(data_1)-1:
date_0 = datetime.strptime((','.join(data_1[counter])), '%Y-%m-%d').date()
date_1 = datetime.strptime((','.join(data_1[counter+1])), '%Y-%m-%d').date()
date_last = datetime.strptime((','.join(data_1[len(data_1) - 1])), '%Y-%m-%d').date()
# A habit is not checked if the habit is completed twice on the same day
if date_0 == date_1:
streak += 0
# Calculates the time difference between two habits and increments the current streak
elif date_1 - date_0 <= timedelta(days=7):
streak += 1
elif date_0 + timedelta(days=7) == date_1 and date_last >= date.today() - timedelta(days=7):
streak += 1
else:
streak = 0
counter += 1
else:
# Only completed weeks are counted as succeeded streaks
if date.today() - (datetime.strptime((','.join(data_1[0])), '%Y-%m-%d').date()) <= timedelta(days=7):
streak += 0
else:
overall_weeks = ((date.today() - datetime.strptime(
(','.join(data_1[0])), '%Y-%m-%d').date())/7).days
if overall_weeks < streak:
streak = overall_weeks
else:
streak += 0
list_streak_habit.append(streak)
list_streak_overall.append(streak)
max_habit_streak = max(list_streak_habit)
print(f" {name}: Current streak of {streak} ({lower_periodicty})!")
print(f" {name} All-time record: {max_habit_streak} ({lower_periodicty})!")
print("")
dict_overall[name] = max(list_streak_habit, default=0)
# returns the current streak for the test module
return streak
# returns the highest overall streak for each habit for the test module
return dict_overall.get(max(dict_overall, key=dict_overall.get))
print(f" Overall record streak: "
f"{dict_overall.get(max(dict_overall, key=dict_overall.get, default=0))} for "
f"{max(dict_overall, key=dict_overall.get, default=0)}! ({lower_periodicty})!")