Skip to content

Commit 121f942

Browse files
latest work
1 parent 4762b31 commit 121f942

File tree

10 files changed

+5275
-0
lines changed

10 files changed

+5275
-0
lines changed

.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
conda activate 100-DAYS-OF-CODE-WITH-PYTHON

.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
conda deactivate

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python: Current File",
9+
"type": "python",
10+
"request": "launch",
11+
"program": "${file}",
12+
"console": "integratedTerminal"
13+
}
14+
]
15+
}

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.pythonPath": "/Users/aiuw003/miniconda3/envs/100-DAYS-OF-CODE-WITH-PYTHON/bin/python"
3+
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from datetime import date, timedelta
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+
end_date = start_100days + timedelta(days=100)
11+
return end_date.strftime("%Y-%m-%d")
12+
13+
14+
def get_days_between_pb_start_first_joint_pycon():
15+
"""Return the int number of days"""
16+
delta = pycon_date - pybites_founded
17+
return delta.days
18+
19+
20+
end_date = get_hundred_days_end_date()
21+
print(f"end date: {end_date}")
22+
23+
days = get_days_between_pb_start_first_joint_pycon()
24+
print(f"days: {days}")
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from datetime import datetime
2+
3+
THIS_YEAR = 2018
4+
5+
6+
def years_ago(date):
7+
"""Receives a date string of 'DD MMM, YYYY', for example: 8 Aug, 2015
8+
Convert this date str to a datetime object (use strptime).
9+
Then extract the year from the obtained datetime object and subtract
10+
it from the THIS_YEAR constant above, returning the int difference.
11+
So in this example you would get: 2018 - 2015 = 3"""
12+
dt = datetime.strptime(date, "%d %b, %Y")
13+
years = THIS_YEAR - dt.year
14+
return years
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+
dt = datetime.strptime(date, "%d/%m/%Y")
24+
american_date = dt.strftime("%m/%d/%Y")
25+
return american_date
26+
27+
28+
years = years_ago("8 Aug, 2015")
29+
print(f"years: {years}")
30+
31+
us_date = convert_eu_to_us_date("11/03/2002")
32+
print(f"date: {us_date}")

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import os
2+
import urllib.request
3+
from datetime import datetime, timedelta
4+
5+
SHUTDOWN_EVENT = "Shutdown initiated"
6+
7+
# prep: read in the logfile
8+
tmp = os.getenv("TMP", "/tmp")
9+
logfile = os.path.join(tmp, "log")
10+
urllib.request.urlretrieve(
11+
"https://bites-data.s3.us-east-2.amazonaws.com/messages.log", logfile
12+
)
13+
14+
with open(logfile) as f:
15+
loglines = f.readlines()
16+
17+
18+
# for you to code:
19+
20+
21+
def convert_to_datetime(line):
22+
"""TODO 1:
23+
Extract timestamp from logline and convert it to a datetime object.
24+
For example calling the function with:
25+
INFO 2014-07-03T23:27:51 supybot Shutdown complete.
26+
returns:
27+
datetime(2014, 7, 3, 23, 27, 51)
28+
"""
29+
datetime_segment = line.split(" ")[1]
30+
dt = datetime.strptime(datetime_segment, "%Y-%m-%dT%H:%M:%S")
31+
return dt
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+
shutdown_loglines = [line for line in loglines if "Shutdown initiated" in line]
41+
42+
first_datetime = convert_to_datetime(shutdown_loglines[0])
43+
last_datetime = convert_to_datetime(shutdown_loglines[-1])
44+
45+
delta = last_datetime - first_datetime
46+
return delta
47+
48+
49+
time_diff = time_between_shutdowns(loglines)
50+
print(f"The time difference is: {time_diff}")

days/04-06-collections/movie_metadata.csv

Lines changed: 5044 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import re
2+
3+
cars = {
4+
"Ford": ["Falcon", "Focus", "Festiva", "Fairlane"],
5+
"Holden": ["Commodore", "Captiva", "Barina", "Trailblazer"],
6+
"Nissan": ["Maxima", "Pulsar", "350Z", "Navara"],
7+
"Honda": ["Civic", "Accord", "Odyssey", "Jazz"],
8+
"Jeep": ["Grand Cherokee", "Cherokee", "Trailhawk", "Trackhawk"],
9+
}
10+
11+
12+
def get_all_jeeps(cars=cars):
13+
"""return a comma + space (', ') separated string of jeep models
14+
(original order)"""
15+
16+
all_jeeps = ", ".join(cars["Jeep"])
17+
return all_jeeps
18+
19+
20+
def get_first_model_each_manufacturer(cars=cars):
21+
"""return a list of matching models (original ordering)"""
22+
23+
first_models = [v[0] for k, v in cars.items()]
24+
return first_models
25+
26+
27+
def get_all_matching_models(cars=cars, grep="trail"):
28+
"""return a list of all models containing the case insensitive
29+
'grep' string which defaults to 'trail' for this exercise,
30+
sort the resulting sequence alphabetically"""
31+
32+
# for models in cars.values():
33+
# for model in models:
34+
# m = model
35+
36+
matching_models = [
37+
model
38+
for models in cars.values()
39+
for model in models
40+
if re.search(grep, model, re.IGNORECASE) is not None
41+
]
42+
matching_models.sort()
43+
return matching_models
44+
45+
46+
def sort_car_models(cars=cars):
47+
"""return a copy of the cars dict with the car models (values)
48+
sorted alphabetically"""
49+
50+
sorted_models = {}
51+
for k, v in cars.items():
52+
sorted_models[k] = v
53+
sorted_models[k].sort()
54+
return sorted_models
55+
56+
57+
def main():
58+
all_jeeps = get_all_jeeps()
59+
first_models = get_first_model_each_manufacturer()
60+
matching_models = get_all_matching_models()
61+
sorted_models = sort_car_models()
62+
63+
64+
if __name__ == "__main__":
65+
main()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from cars import (get_all_jeeps, get_first_model_each_manufacturer,
2+
get_all_matching_models, sort_car_models)
3+
4+
5+
def test_get_all_jeeps():
6+
expected = 'Grand Cherokee, Cherokee, Trailhawk, Trackhawk'
7+
actual = get_all_jeeps()
8+
assert type(actual) == str
9+
assert actual == expected
10+
11+
12+
def test_get_first_model_each_manufacturer():
13+
actual = get_first_model_each_manufacturer()
14+
expected = ['Falcon', 'Commodore', 'Maxima', 'Civic', 'Grand Cherokee']
15+
assert actual == expected
16+
17+
18+
def test_get_all_matching_models_default_grep():
19+
expected = ['Trailblazer', 'Trailhawk']
20+
assert get_all_matching_models() == expected
21+
22+
23+
def test_get_all_matching_models_different_grep():
24+
expected = ['Accord', 'Commodore', 'Falcon']
25+
assert get_all_matching_models(grep='CO') == expected
26+
27+
28+
def test_sort_dict_alphabetically():
29+
actual = sort_car_models()
30+
# Order of keys should not matter, two dicts are equal if they have the
31+
# same keys and the same values.
32+
# The car models (values) need to be sorted here though
33+
expected = {
34+
'Ford': ['Fairlane', 'Falcon', 'Festiva', 'Focus'],
35+
'Holden': ['Barina', 'Captiva', 'Commodore', 'Trailblazer'],
36+
'Honda': ['Accord', 'Civic', 'Jazz', 'Odyssey'],
37+
'Jeep': ['Cherokee', 'Grand Cherokee', 'Trackhawk', 'Trailhawk'],
38+
'Nissan': ['350Z', 'Maxima', 'Navara', 'Pulsar'],
39+
}
40+
assert actual == expected

0 commit comments

Comments
 (0)