Skip to content

Commit eb07d9a

Browse files
committed
Days 1 and 2
1 parent 0820b2c commit eb07d9a

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from datetime import date, timedelta, datetime
2+
3+
start_100days = date(2017, 3, 30)
4+
pybites_founded = date(2016, 12, 19)
5+
pycon_date = date(2018, 5, 8)
6+
7+
8+
def get_hundred_days_end_date():
9+
"""Return a string of yyyy-mm-dd"""
10+
result = start_100days + timedelta(days=100)
11+
return result.strftime("%Y-%m-%d")
12+
13+
14+
def get_days_between_pb_start_first_joint_pycon():
15+
"""Return the int number of days"""
16+
result = pycon_date - pybites_founded
17+
return result.days
18+
19+
20+
end_date = get_hundred_days_end_date()
21+
date_difference = get_days_between_pb_start_first_joint_pycon()
22+
print(end_date)
23+
print(date_difference)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
from datetime import datetime
3+
4+
THIS_YEAR = 2018
5+
6+
7+
def years_ago(date):
8+
"""Receives a date string of 'DD MMM, YYYY', for example: 8 Aug, 2015
9+
Convert this date str to a datetime object (use strptime).
10+
Then extract the year from the obtained datetime object and subtract
11+
it from the THIS_YEAR constant above, returning the int difference.
12+
So in this example you would get: 2018 - 2015 = 3"""
13+
converted_date = datetime.strptime(date, "%d %b, %Y")
14+
return THIS_YEAR - converted_date.year
15+
16+
17+
def convert_eu_to_us_date(date):
18+
"""Receives a date string in European format of dd/mm/yyyy, e.g. 11/03/2002
19+
Convert it to an American date: mm/dd/yyyy (in this case 03/11/2002).
20+
To enforce the use of datetime's strptime / strftime (over slicing)
21+
the tests check if a ValueError is raised for invalid day/month/year
22+
ranges (no need to code this, datetime does this out of the box)"""
23+
converted_date = datetime.strptime(date, "%d/%m/%Y")
24+
return converted_date.strftime("%m/%d/%Y")
25+
26+
27+
def test_years_ago():
28+
assert years_ago('8 Aug, 2015') == 3
29+
assert years_ago('6 Sep, 2014') == 4
30+
assert years_ago('1 Oct, 2010') == 8
31+
assert years_ago('31 Dec, 1958') == 60
32+
33+
34+
def test_convert_eu_to_us_date():
35+
assert convert_eu_to_us_date('11/03/2002') == '03/11/2002'
36+
assert convert_eu_to_us_date('18/04/2008') == '04/18/2008'
37+
assert convert_eu_to_us_date('12/12/2014') == '12/12/2014'
38+
assert convert_eu_to_us_date('1/3/2004') == '03/01/2004'
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from datetime import datetime, timedelta
2+
import pytest
3+
import os
4+
import urllib.request
5+
6+
SHUTDOWN_EVENT = 'Shutdown initiated'
7+
8+
# prep: read in the logfile
9+
tmp = os.getenv("TMP", "/tmp")
10+
logfile = os.path.join(tmp, 'log')
11+
urllib.request.urlretrieve(
12+
'https://bites-data.s3.us-east-2.amazonaws.com/messages.log',
13+
logfile
14+
)
15+
16+
with open(logfile) as f:
17+
loglines = f.readlines()
18+
19+
20+
# for you to code:
21+
22+
def convert_to_datetime(line):
23+
"""TODO 1:
24+
Extract timestamp from logline and convert it to a datetime object.
25+
For example calling the function with:
26+
INFO 2014-07-03T23:27:51 supybot Shutdown complete.
27+
returns:
28+
datetime(2014, 7, 3, 23, 27, 51)
29+
"""
30+
parsed_line = line.split()
31+
return datetime.strptime(parsed_line[1], "%Y-%m-%dT%H:%M:%S")
32+
33+
34+
def time_between_shutdowns(loglines):
35+
"""TODO 2:
36+
Extract shutdown events ("Shutdown initiated") from loglines and
37+
calculate the timedelta between the first and last one.
38+
Return this datetime.timedelta object.
39+
"""
40+
sd_initiated_logs = list(filter(lambda x: SHUTDOWN_EVENT in x, loglines))
41+
first_entry = convert_to_datetime(sd_initiated_logs[0])
42+
second_entry = convert_to_datetime(sd_initiated_logs[1])
43+
return second_entry - first_entry
44+
# shutdown_entries = [line for line in loglines if SHUTDOWN_EVENT in line]
45+
# shutdown_times = [convert_to_datetime(event) for event in shutdown_entries]
46+
# return max(shutdown_times) - min(shutdown_times)
47+
48+
49+
def test_convert_to_datetime():
50+
line1 = 'ERROR 2014-07-03T23:24:31 supybot Invalid user dictionary file'
51+
line2 = 'INFO 2015-10-03T10:12:51 supybot Shutdown initiated.'
52+
line3 = 'INFO 2016-09-03T02:11:22 supybot Shutdown complete.'
53+
assert convert_to_datetime(line1) == datetime(2014, 7, 3, 23, 24, 31)
54+
assert convert_to_datetime(line2) == datetime(2015, 10, 3, 10, 12, 51)
55+
assert convert_to_datetime(line3) == datetime(2016, 9, 3, 2, 11, 22)
56+
57+
58+
def test_time_between_events():
59+
diff = time_between_shutdowns(loglines)
60+
assert type(diff) == timedelta
61+
assert str(diff) == '0:03:31'
62+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from datetime import datetime
2+
from datetime import date
3+
from datetime import timedelta
4+
5+
6+
today = datetime.today()
7+
print(str(today))
8+
9+
today_date = today.date()
10+
print(str(today_date))
11+
print(type(today_date))
12+
13+
four_days_later = today.__add__(timedelta(days=4))
14+
print(four_days_later)
15+
16+
print(today_date.__sub__(timedelta(days=4)))
17+
18+
19+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
line_list = [
2+
"ERROR 2014-07-03T23:24:31 supybot Invalid user dictionary file, resetting to empty.",
3+
"ERROR 2014-07-03T23:24:31 supybot Exact error: IOError: [Errno 2] No such file or directory: 'conf/users.conf'",
4+
"ERROR 2014-07-03T23:24:31 supybot Invalid channel database, resetting to empty.",
5+
"ERROR 2014-07-03T23:24:31 supybot Exact error: IOError: [Errno 2] No such file or directory: 'conf/channels.conf'",
6+
"WARNING 2014-07-03T23:24:31 supybot Couldn't open ignore database: [Errno 2] No such file or directory: 'conf/ignores.conf'",
7+
"INFO 2014-07-03T23:27:51 supybot Shutdown initiated.",
8+
"INFO 2014-07-03T23:27:51 supybot Killing Driver objects.",
9+
"INFO 2014-07-03T23:31:22 supybot Shutdown initiated.",
10+
"INFO 2014-07-03T23:31:22 supybot Killing Driver objects.",
11+
"INFO 2014-07-03T23:31:22 supybot Shutdown complete."
12+
]
13+
14+
new_list = list(filter(lambda x: "Shutdown initiated" in x, line_list))
15+
print(new_list)
16+

0 commit comments

Comments
 (0)