Skip to content

Commit 748812b

Browse files
committed
Day 1-3 Commit changes and added files
1 parent 9a29a76 commit 748812b

File tree

4 files changed

+146
-6
lines changed

4 files changed

+146
-6
lines changed

days/01-03-datetimes/code/Day 2.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from datetime import datetime, timedelta
2+
from datetime import date
3+
4+
5+
#bite 128
6+
7+
8+
myStrDate = '11/03/2002'
9+
print("testing")
10+
print(datetime.strptime(myStrDate, '%d/%m/%Y'))
11+
print(datetime.strptime(myStrDate, '%d%/%m/%Y').strftime('%m/%d/%Y'))
12+
mydate = datetime.strptime(myStrDate, '%d $b, %Y').year
13+
print(mydate)
14+
15+
16+
)
17+
#bite 67
18+
19+
start_100days = date(2017, 3, 30)
20+
pybites_founded = date(2016, 12, 19)
21+
pycon_date = date(2018, 5, 8)
22+
23+
24+
mydate = start_100days + timedelta(days=100)
25+
print(mydate)
26+
print(date.__format__(mydate, "yyyy-mm-dd"))
27+
print(mydate.strftime("%Y-%m-%d"))
28+
29+
print((pycon_date - pybites_founded).days)
30+
31+
# DAY 2 - Bite 7 Parsing dates from logs
32+
33+
from datetime import datetime, timedelta
34+
import os
35+
import urllib.request
36+
37+
SHUTDOWN_EVENT = 'Shutdown initiated'
38+
39+
# prep: read in the logfile
40+
tmp = os.getenv("TMP", "/tmp")
41+
logfile = os.path.join(tmp, 'log')
42+
urllib.request.urlretrieve(
43+
'https://bites-data.s3.us-east-2.amazonaws.com/messages.log',
44+
logfile
45+
)
46+
47+
with open(logfile) as f:
48+
loglines = f.readlines()
49+
50+
print(loglines)
51+
52+
53+
54+
# for you to code:
55+
56+
def convert_to_datetime(line):
57+
return datetime.strptime(line.split()[1], "%Y-%m-%dT%H:%M:%S")
58+
pass
59+
60+
print(convert_to_datetime(loglines[len(loglines) - 1]))
61+
print(convert_to_datetime(loglines[0]))
62+
print(convert_to_datetime(loglines[len(loglines)-1]) - convert_to_datetime(loglines[0]))
63+
firststring = ""
64+
laststring = ""
65+
for line in loglines:
66+
if line.split()[3] == "Shutdown" and line.split()[4] == "initiated.":
67+
if firststring == "":
68+
firststring = line
69+
else:
70+
laststring = line
71+
print(convert_to_datetime(laststring) - convert_to_datetime(firststring))
72+
print("frist string is " + firststring)
73+
print("second string is " + laststring)
74+
75+
def time_between_shutdowns(loglines):
76+
print(convert_to_datetime(loglines[len(loglines) - 1]) )
77+
print(convert_to_datetime(loglines[0]))
78+
79+
return loglines[len(loglines)-1] - loglines[0]
80+
pass
81+
82+
83+
def test_convert_to_datetime():
84+
line1 = 'ERROR 2014-07-03T23:24:31 supybot Invalid user dictionary file'
85+
line2 = 'INFO 2015-10-03T10:12:51 supybot Shutdown initiated.'
86+
line3 = 'INFO 2016-09-03T02:11:22 supybot Shutdown complete.'
87+
assert convert_to_datetime(line1) == datetime(2014, 7, 3, 23, 24, 31)
88+
assert convert_to_datetime(line2) == datetime(2015, 10, 3, 10, 12, 51)
89+
assert convert_to_datetime(line3) == datetime(2016, 9, 3, 2, 11, 22)
90+
91+
92+
def test_time_between_events():
93+
print(loglines)
94+
diff = time_between_shutdowns(loglines)
95+
assert type(diff) == timedelta
96+
assert str(diff) == '0:03:31'
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Pomodoro Timer
2+
3+
from time import sleep
4+
import os
5+
6+
longDuration = 25 #1500
7+
shortDuration = 5 #300 #input("Choose a break time (3-5 minutes).")
8+
iterations = 0
9+
longBreak = 30 #1800
10+
firstTime = True
11+
12+
13+
def longDurationFunc():
14+
sleep(longDuration)
15+
os.system('say "Take a break"')
16+
17+
18+
def breakFunc(longBreak):
19+
if longBreak:
20+
sleep(longBreak)
21+
else:
22+
sleep(longDuration)
23+
os.system('say "Back to work"')
24+
25+
26+
while True:
27+
if firstTime:
28+
firstTime = False
29+
longDurationFunc()
30+
iterations = iterations + 1
31+
while iterations < 4:
32+
breakFunc(False)
33+
longDurationFunc()
34+
iterations = iterations + 1
35+
iterations = 0
36+
if input("Continue (after a break)? y/n ") == "y":
37+
breakFunc(True)
38+
firstTime = True
39+
else:
40+
break
41+
42+

days/01-03-datetimes/code/datetime_date.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
#19
3232

3333

34-
christmas = date(2018, 12, 25)
34+
christmas = date(todaydate.year, 12, 25)
3535
christmas
3636
#datetime.date(2018, 12, 25)
3737

3838
if christmas is not todaydate:
39-
print("Sorry there are still " + str((christmas - todaydate).days) + " until Christmas!")
39+
print("Sorry there are still " + str((christmas - todaydate).days) + " days until Christmas!")
4040
else:
4141
print("Yay it's Christmas!")
42+
43+

days/01-03-datetimes/code/datetime_timedelta.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
t.seconds
1212
#36000
1313

14-
t.hours
14+
#t.hours
1515
#Traceback (most recent call last):
1616
#File "<pyshell#119>", line 1, in <module> t.hours
1717
#AttributeError: 'datetime.timedelta' object has no attribute 'hours'
@@ -29,11 +29,11 @@
2929

3030
today = datetime.today()
3131

32-
today
32+
print(today)
3333
#datetime.datetime(2018, 2, 19, 14, 55, 19, 197404
3434

35-
today + eta
35+
print(today + eta)
3636
#datetime.datetime(2018, 2, 19, 20, 55, 19, 197404)
3737

38-
str(today + eta)
38+
print(str(today + eta))
3939
#'2018-02-19 20:55:19.197404'

0 commit comments

Comments
 (0)