Skip to content

Commit 4f34644

Browse files
committed
Completed Day 3
1 parent be8a979 commit 4f34644

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
""" PCC52 - Create your own Pomodoro Timer (24th of Sept 2018)
2+
Instructions + submit work: https://codechalleng.es/challenges/52"""
3+
4+
import datetime
5+
6+
get_time_delta = input('Please enter the timer length in minutes: ')
7+
8+
time_now = datetime.datetime.now()
9+
[print('\a') for x in range(1, 2)]
10+
11+
print('Current time: ' + str(time_now))
12+
13+
start_time = time_now
14+
finish_time = start_time + datetime.timedelta(minutes=int(get_time_delta))
15+
16+
while finish_time > datetime.datetime.now():
17+
pass
18+
else:
19+
[print('\a') for x in range(1, 10)]
20+
print('The time us UP!!!!! Current time is: ' + str(datetime.datetime.now()))
21+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'''Extract datetimes from log entries and calculate the time
2+
between the first and last shutdown events'''
3+
from datetime import datetime
4+
import os
5+
import urllib.request
6+
import re
7+
8+
SHUTDOWN_EVENT = 'Shutdown initiated'
9+
10+
# prep: read in the logfile
11+
logfile = os.path.join('/tmp', 'log')
12+
urllib.request.urlretrieve('http://bit.ly/2AKSIbf', logfile)
13+
14+
with open(logfile) as f:
15+
loglines = f.readlines()
16+
17+
18+
# for you to code:
19+
20+
def convert_to_datetime(line):
21+
'''TODO 1:
22+
Given a log line extract its timestamp and convert it to a datetime object.
23+
For example calling the function with:
24+
INFO 2014-07-03T23:27:51 supybot Shutdown complete.
25+
returns:
26+
datetime(2014, 7, 3, 23, 27, 51)'''
27+
28+
find_time = re.search('\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}', line)
29+
get_date = datetime.strptime(find_time.group(), '%Y-%m-%dT%H:%M:%S')
30+
31+
# pass
32+
return get_date
33+
34+
35+
def time_between_shutdowns(loglines):
36+
'''TODO 2:
37+
Extract shutdown events ("Shutdown initiated") from loglines and calculate the
38+
timedelta between the first and last one.
39+
Return this datetime.timedelta object.'''
40+
41+
first_event = None
42+
last_event = None
43+
for line in loglines:
44+
if 'Shutdown initiated' in line:
45+
if not first_event:
46+
first_event = line
47+
else:
48+
last_event = line
49+
50+
get_first_event_time = re.search('\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}', first_event)
51+
first_event_time = datetime.strptime(get_first_event_time.group(), '%Y-%m-%dT%H:%M:%S')
52+
53+
get_last_event_time = re.search('\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}', last_event)
54+
last_event_time = datetime.strptime(get_last_event_time.group(), '%Y-%m-%dT%H:%M:%S')
55+
56+
time_delta = last_event_time - first_event_time
57+
# pass
58+
return time_delta
59+

0 commit comments

Comments
 (0)