Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions 52/jread/logtimes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
import sys
import re

from datetime import datetime

DATETIME_REGEX = re.compile(r'.*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).*')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice use of constants

SHUTDOWN_EVENT = 'Shutdown initiated'

with open(os.path.join(sys.path[0], "messages.log"), "r") as f:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put this into a function

loglines = f.readlines()


def convert_to_datetime(line):
match = re.match(DATETIME_REGEX, line)
return datetime(int(match.group(1)), int(match.group(2)), int(match.group(3)), int(match.group(4)),
int(match.group(5)), int(match.group(6))) if match else None


def time_between_shutdowns(loglines):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use a list comprehension and built-in functions, similar function from one of our Bites:

def time_between_shutdowns(loglines):
    """TODO 2:
       Extract shutdown events ("Shutdown initiated") from loglines and
       calculate the timedelta between the first and last one.
       Return this datetime.timedelta object.
    """
    shutdown_entries = [line for line in loglines if SHUTDOWN_EVENT in line]
    shutdown_times = [convert_to_datetime(event) for event in shutdown_entries]
    return max(shutdown_times) - min(shutdown_times)

start = None
end = None
for line in loglines:
if SHUTDOWN_EVENT in line:
if dt := convert_to_datetime(line):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

walrus, nice :)

if start:
end = dt
else:
start = dt
return end - start if end and start else None
45 changes: 45 additions & 0 deletions 52/jread/pomodoro_timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import time

from datetime import datetime
from datetime import timedelta

# Note - this Pomodoro timer uses seconds instead of minutes for practical reasons - a real Pomodoro timer would use
# minutes instead

# Start time is now
start = datetime.now()

# Run timer for this duration
duration = timedelta(seconds=60)

# Stop the timer once duration is reached
stop = start + duration

# Take breaks at this frequency
break_frequency = timedelta(seconds=25)

# Breaks are this duration
break_duration = timedelta(seconds=5)

# Work status - working or break
work_status = 'working'

# When to take the first break (transition from work to break)
next_transition = start + break_frequency

# Seconds counter
secs = 0

print(f'Starting pomodoro timer [start_time={start}] [stop_time={stop}] [break_frequency={break_frequency}] '
f'[first_break={next_transition}')

# Run the timer from now until the stop time designated above
while datetime.now() < stop:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put this in a function

# If at a transition time (ignore milliseconds), then change the status from working => break or break => working
# and set the next transition time (+5 if transitioning to break, +25 if transitioning to work)
if str(datetime.now()).split('.')[0] == str(next_transition).split('.')[0]:
next_transition = datetime.now() + (break_duration if work_status == 'working' else break_frequency)
work_status = 'break' if work_status == 'working' else 'working'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put states in constants

secs += 1
print(f'[{secs:03}] Work status={work_status}')
time.sleep(1)
18 changes: 18 additions & 0 deletions 52/jread/test_logtimes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from datetime import datetime, timedelta

from logtimes import loglines, convert_to_datetime, time_between_shutdowns


def test_convert_to_datetime():
line1 = 'ERROR 2014-07-03T23:24:31 supybot Invalid user dictionary file'
line2 = 'INFO 2015-10-03T10:12:51 supybot Shutdown initiated.'
line3 = 'INFO 2016-09-03T02:11:22 supybot Shutdown complete.'
assert convert_to_datetime(line1) == datetime(2014, 7, 3, 23, 24, 31)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use pytest.mark.parametrize - https://pybit.es/articles/pytest-coding-100-tests/ - no 9

assert convert_to_datetime(line2) == datetime(2015, 10, 3, 10, 12, 51)
assert convert_to_datetime(line3) == datetime(2016, 9, 3, 2, 11, 22)


def test_time_between_events():
diff = time_between_shutdowns(loglines)
assert type(diff) == timedelta
assert str(diff) == '0:03:31'