8000 Added errors project and also section 3 project brief. · jaaks/complete-python-course@80edac9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 80edac9

Browse files
committed
Added errors project and also section 3 project brief.
1 parent 56c3570 commit 80edac9

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
*.key
2+
*.indd
3+
*.png
14
.DS_Store
25
.idea/
36
__pycache__/

section3/milestone_project_brief.pdf

566 KB
Binary file not shown.

section5/errors_project/app.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Car:
2+
def __init__(self, make, model):
3+
self.make = make
4+
self.model = model
5+
6+
def __repr__(self):
7+
return f'<Car {self.make} {self.model}>'
8+
9+
10+
class Garage:
11+
def __init__(self):
12+
self.cars = []
13+
14+
def __len__(self):
15+
return len(self.cars)
16+
17+
def add_car(self, car):
18+
if not isinstance(car, Car):
19+
raise ValueError(f'Tried to add a `{car.__class__.__name__}` to the garage, but you can only add `Car` objects.')
20+
self.cars.append(car)
21+
22+
23+
ford = Garage()
24+
fiesta = Car('Ford', 'Fiesta')
25+
26+
try:
27+
ford.add_car('Fiesta')
28+
except TypeError:
29+
print('Your car was not a Car!')
30+
except ValueError:
31+
print('Something weird happened...')
32+
finally:
33+
print(f'The garage now has {len(ford)} cars.')

section5/errors_project/errors.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class RuntimeErrorWithCode(Exception):
2+
def __init__(self, message, code):
3+
super().__init__(f'Error code {code}: {message}')
4+
self.code = code
5+
6+
7+
err = RuntimeErrorWithCode('An error happened.', 500)

section5/errors_project/user_score.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class User:
2+
def __init__(self, name, engagement):
3+
self.name = name
4+
self.engagement_metrics = engagement
5+
self.score = 0
6+
7+
def __repr__(self):
8+
return f'<User {self.name}>'
9+
10+
11+
def email_engaged_user(user):
12+
try:
13+
user.score = perform_calculation(user.engagement_metrics)
14+
except KeyError:
15+
print('Incorrect values provided to our calculation function.')
16+
else:
17+
if user.score > 500:
18+
send_engagement_notification(user)
19+
20+
21+
def perform_calculation(metrics):
22+
return metrics['clicks'] * 5 + metrics['hits'] * 2
23+
24+
25+
def send_engagement_notification(user):
26+
print(f'Notification sent to {user}.')
27+
28+
29+
my_user = User('Rolf', {'clicks': 61, 'hits': 100})
30+
email_engaged_user(my_user)

section7/milestone_2/utils/database.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def get_all_books() -> List[Book]:
1919

2020
cursor.execute('SELECT * FROM books')
2121
books = cursor.fetchall()
22-
2322
return books
2423

2524

0 commit comments

Comments
 (0)
0