File tree Expand file tree Collapse file tree 6 files changed +73
-1
lines changed
section7/milestone_2/utils Expand file tree Collapse file tree 6 files changed +73
-1
lines changed Original file line number Diff line number Diff line change
1
+ * .key
2
+ * .indd
3
+ * .png
1
4
.DS_Store
2
5
.idea /
3
6
__pycache__ /
Original file line number Diff line number Diff line change
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.' )
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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 })<
7DF9
/div>
30
+ email_engaged_user (my_user )
Original file line number Diff line number Diff line change @@ -19,7 +19,6 @@ def get_all_books() -> List[Book]:
19
19
20
20
cursor .execute ('SELECT * FROM books' )
21
21
books = cursor .fetchall ()
22
-
23
22
return books
24
23
25
24
You can’t perform that action at this time.
0 commit comments