-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_collector.py
289 lines (210 loc) · 9.52 KB
/
data_collector.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
Main file
"""
from datetime import datetime
import get_preferences
import time
import winsound
import os
import importlib
def show_options():
""" As the name says - just print available options """
print('Here are your options: ')
print('A : Manage preferences. Start here if you are a new user. ')
print('B : Gather general data e.g. sleep time and mood. ')
print('C : Start working. ')
print('D : Add work data manually. ')
print('q : Quit program and do other things! ')
print('Please choose one of the options when asked. ')
def get_nice_datetime():
""" Returns current datetime in my favourite format """
return datetime.now().strftime('%Y-%m-%d %H:%M')
def yn_input_validator(prompt):
""" Check if answer is an integer as requested """
while True:
answer = input(prompt).lower()
if answer in ('n', 'no'):
return False
elif answer in ('y', 'yes'):
return True
else:
print('I don\'t understand. Can you answer as requested? [y/n] ')
def int_input_validator(prompt):
""" Checks if input is in correct type """
while True:
answer = input(prompt)
try:
answer = int(answer)
return answer
except ValueError:
print('Please, give me an integer!')
def date_input_validator(prompt):
""" Check if answer is a date as requested """
while True:
answer = input(prompt)
try:
answer = datetime.strptime(answer, '%Y-%m-%d').date()
return answer
except ValueError:
print('Please, give me a date! ')
def time_input_validator(prompt):
""" Check if answer is a time as requested """
while True:
answer = input(prompt)
try:
time.strptime(answer, '%H:%M')
return answer
except ValueError:
print('Please, give me time! ')
def gather_general_data():
""" Gather additional data """
is_today = yn_input_validator('Are you adding data from today? [y/n] ')
directory = 'collected_data'
file_name = 'general_data.csv'
while True:
if is_today:
day = datetime.now().date()
else:
day = date_input_validator('When was this data generated? [YYYY-MM-DD] ')
actual_get_out_of_bed_time = time_input_validator("What time did you actually get out of bed? [HH:MM] ")
good_sleep = yn_input_validator("Have you slept well? [y/n] ")
morning_routine = yn_input_validator("Have you done you morning workout routine? [y/n] ")
write_to_csv_file(directory, file_name, day, actual_get_out_of_bed_time, good_sleep, morning_routine)
is_end = yn_input_validator('Is this all of the data you wanted to include? [y/n] ')
if is_end: break
is_today = False
def manage_preferences():
""" View, create or update your preferences """
is_new_user = yn_input_validator('Are you a new user? [yn] ')
def show_current_preferences():
""" Show current preferences """
print('Your current preferences:')
print('User name: ' + get_preferences.get_user_name())
print('Work time unit: ' + str(get_preferences.get_work_time_unit()))
print('Short break time: ' + str(get_preferences.get_short_break_time()))
print('Long break time: ' + str(get_preferences.get_long_break_time()))
print('Work unit bundle: ' + str(get_preferences.get_work_unit_bundle()))
if is_new_user:
print('Hello! I\'ll ask you some questions')
name = input('What is your name? ')
work_time_unit = int_input_validator("How long would you like your work time unit to last (suggested: 25 min) ? ")
short_break_time = int_input_validator("How long would you like your break time unit to last (suggested: 5 min) ? ")
long_break_time = int_input_validator("How long would you like your long break time unit to last (suggested: 15 min) ? ")
work_unit_bundle = int_input_validator("After what amount of working unit would you like your long break (suggested: 4) ? ")
try:
with open('my_preferences.py', 'w') as f:
f.write('name = \'' + name + '\'\n')
f.write('work_time_unit = ' + str(work_time_unit) + '\n')
f.write('short_break_time = ' + str(short_break_time) + '\n')
f.write('long_break_time = ' + str(long_break_time)+ '\n')
f.write('work_unit_bundle = ' + str(work_unit_bundle) + '\n')
importlib.reload(get_preferences)
show_current_preferences()
except IOError:
print('Data file manipulation failed!')
print(sys.exc_info()[0])
else:
show_current_preferences()
is_change = yn_input_validator('Do you want to change something? [y/n] ')
if is_change:
print('Please, pretend you are a new user and just override all data. For now, promise!')
show_current_preferences()
def write_to_csv_file(directory, file_name, *params):
""" Avoiding repetition """
file_address = directory + '/' + file_name
text = ','.join(str(x) for x in params)
try:
with open(file_address, "a+") as f:
f.write(text + '\n')
except IOError:
print('Data file manipulation failed!')
print(sys.exc_info()[0])
def add_work_data_manually(directory, file_name):
""" Add data when worked outside of this tool """
print('So, I understand you worked with somebody else ;(')
is_today = yn_input_validator('Do you want to insert data only from today? [y/n] ')
if is_today:
no_session = int_input_validator('How many pomodoros have you worked today? [int] ')
day = datetime.now().date()
write_to_csv_file(directory, file_name, day, no_session)
else:
while True:
day = date_input_validator('When you worked? [YYYY-MM-DD] ')
no_session = int_input_validator('How much pomodoros? [int] ')
write_to_csv_file(directory, file_name, day, no_session)
is_end = yn_input_validator('Is this the end? [y/n] ')
if is_end: break
def countdown(countdown_minutes, session_name):
""" Function to display remaining time. Input wanted in minutes """
def display(seconds):
""" Function to format remaing time in user friendly way """
remaining_minutes, remaining_seconds = divmod(seconds, 60)
return '{:02d}:{:02d}'.format(remaining_minutes, remaining_seconds)
countdown_seconds = countdown_minutes # * 60 # for test!
while countdown_seconds > -1:
print(display(countdown_seconds), end='\r')
time.sleep(1)
countdown_seconds = countdown_seconds - 1
print(session_name + ' ended.')
# todo: change melody! this one will get lost
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
def working_body(directory, work_time_unit, short_break_time, long_break_time, work_unit_bundle):
""" Module for tracking time using pomodoro technique """
no_session = 0
consent = yn_input_validator('Are you ready to work? [y/n] ')
file_name = 'work_data_log.csv'
while consent:
no_session = no_session + 1
print('Starting work unit no ' + str(no_session) + '.')
work_start_at = get_nice_datetime()
unit_type = 'work time unit'
countdown(work_time_unit, 'Work time unit')
write_to_csv_file(directory, file_name, work_start_at, unit_type)
if_break = yn_input_validator('Do you want to have a break? [y/n] ')
if if_break:
break_start_at = get_nice_datetime()
break_time = long_break_time if no_session % work_unit_bundle == 0 else short_break_time
unit_type = 'long break' if break_time > 20 else 'short break'
print('Starting ' + unit_type)
countdown(break_time, 'Break')
write_to_csv_file(directory, file_name, break_start_at, unit_type)
print('Congratulations for finishing work unit! ')
consent = yn_input_validator('Do you want to work more? [y/n] ')
print('That\'s it! Good work, everybody. ')
return no_session
def main():
name = 'stranger'
day = datetime.now().date()
print('Hello, ' + name + '.')
show_options()
action = input('Which one do you choose? ')
directory = 'collected_data'
file_name = 'work_data.csv'
if not os.path.exists(directory):
try:
os.makedirs(directory)
except:
pass
while action != 'q':
if action in ('a', 'A'):
manage_preferences()
elif action in ('b', 'B'):
gather_general_data()
elif action in ('c', 'C'):
name = get_preferences.get_user_name()
work_time_unit = get_preferences.get_work_time_unit()
short_break_time = get_preferences.get_short_break_time()
long_break_time = get_preferences.get_long_break_time()
work_unit_bundle = get_preferences.get_work_unit_bundle()
no_session = working_body(directory, work_time_unit, short_break_time, long_break_time, work_unit_bundle)
write_to_csv_file(directory, file_name, day, no_session)
elif action in ('d', 'D'):
add_work_data_manually(directory, file_name)
else:
print('Of course.')
show_options()
action = input('If you want to quit, press \'q\'. If not, press something else. ')
print('Alright, it\'s a wrap!')
print('Bye, ' + name + '! ')
if __name__ == '__main__':
main()