-
Notifications
You must be signed in to change notification settings - Fork 2.2k
jread-52 #852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jread-52 #852
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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}).*') | ||
| SHUTDOWN_EVENT = 'Shutdown initiated' | ||
|
|
||
| with open(os.path.join(sys.path[0], "messages.log"), "r") as f: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: |
||
| start = None | ||
| end = None | ||
| for line in loglines: | ||
| if SHUTDOWN_EVENT in line: | ||
| if dt := convert_to_datetime(line): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| 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: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| 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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
| 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' | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice use of constants