Skip to content

Commit 02b7765

Browse files
authored
Merge pull request #852 from jasontread/jread-52
jread-52
2 parents ce13de0 + a95f7fd commit 02b7765

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

52/jread/logtimes.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
import sys
3+
import re
4+
5+
from datetime import datetime
6+
7+
DATETIME_REGEX = re.compile(r'.*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).*')
8+
SHUTDOWN_EVENT = 'Shutdown initiated'
9+
10+
with open(os.path.join(sys.path[0], "messages.log"), "r") as f:
11+
loglines = f.readlines()
12+
13+
14+
def convert_to_datetime(line):
15+
match = re.match(DATETIME_REGEX, line)
16+
return datetime(int(match.group(1)), int(match.group(2)), int(match.group(3)), int(match.group(4)),
17+
int(match.group(5)), int(match.group(6))) if match else None
18+
19+
20+
def time_between_shutdowns(loglines):
21+
start = None
22+
end = None
23+
for line in loglines:
24+
if SHUTDOWN_EVENT in line:
25+
if dt := convert_to_datetime(line):
26+
if start:
27+
end = dt
28+
else:
29+
start = dt
30+
return end - start if end and start else None

52/jread/pomodoro_timer.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import time
2+
3+
from datetime import datetime
4+
from datetime import timedelta
5+
6+
# Note - this Pomodoro timer uses seconds instead of minutes for practical reasons - a real Pomodoro timer would use
7+
# minutes instead
8+
9+
# Start time is now
10+
start = datetime.now()
11+
12+
# Run timer for this duration
13+
duration = timedelta(seconds=60)
14+
15+
# Stop the timer once duration is reached
16+
stop = start + duration
17+
18+
# Take breaks at this frequency
19+
break_frequency = timedelta(seconds=25)
20+
21+
# Breaks are this duration
22+
break_duration = timedelta(seconds=5)
23+
24+
# Work status - working or break
25+
work_status = 'working'
26+
27+
# When to take the first break (transition from work to break)
28+
next_transition = start + break_frequency
29+
30+
# Seconds counter
31+
secs = 0
32+
33+
print(f'Starting pomodoro timer [start_time={start}] [stop_time={stop}] [break_frequency={break_frequency}] '
34+
f'[first_break={next_transition}')
35+
36+
# Run the timer from now until the stop time designated above
37+
while datetime.now() < stop:
38+
# If at a transition time (ignore milliseconds), then change the status from working => break or break => working
39+
# and set the next transition time (+5 if transitioning to break, +25 if transitioning to work)
40+
if str(datetime.now()).split('.')[0] == str(next_transition).split('.')[0]:
41+
next_transition = datetime.now() + (break_duration if work_status == 'working' else break_frequency)
42+
work_status = 'break' if work_status == 'working' else 'working'
43+
secs += 1
44+
print(f'[{secs:03}] Work status={work_status}')
45+
time.sleep(1)

52/jread/test_logtimes.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from datetime import datetime, timedelta
2+
3+
from logtimes import loglines, convert_to_datetime, time_between_shutdowns
4+
5+
6+
def test_convert_to_datetime():
7+
line1 = 'ERROR 2014-07-03T23:24:31 supybot Invalid user dictionary file'
8+
line2 = 'INFO 2015-10-03T10:12:51 supybot Shutdown initiated.'
9+
line3 = 'INFO 2016-09-03T02:11:22 supybot Shutdown complete.'
10+
assert convert_to_datetime(line1) == datetime(2014, 7, 3, 23, 24, 31)
11+
assert convert_to_datetime(line2) == datetime(2015, 10, 3, 10, 12, 51)
12+
assert convert_to_datetime(line3) == datetime(2016, 9, 3, 2, 11, 22)
13+
14+
15+
def test_time_between_events():
16+
diff = time_between_shutdowns(loglines)
17+
assert type(diff) == timedelta
18+
assert str(diff) == '0:03:31'

0 commit comments

Comments
 (0)