Skip to content

Commit 42183b8

Browse files
committed
Update code for class on March 10, 2020
1 parent 543fed0 commit 42183b8

18 files changed

Lines changed: 251 additions & 260 deletions

Examples/enums.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from enum import Enum
2+
3+
4+
class Weekday(Enum):
5+
MONDAY = 1
6+
TUESDAY = 2
7+
WEDNESDAY = 3
8+
THURSDAY = 4
9+
FRIDAY = 5
10+
SATURDAY = 6
11+
SUNDAY = 7
12+
13+
14+
weekdays = (Weekday.MONDAY, Weekday.TUESDAY, Weekday.WEDNESDAY, Weekday.THURSDAY, Weekday.FRIDAY)
15+
weekends = (Weekday.SATURDAY, Weekday.SUNDAY)
16+
17+
weekday_wake_up = "7am"
18+
weekend_wake_up = "9am"
19+
20+
for day in Weekday:
21+
print(f'Today is {day.name.capitalize()}')
22+
wake_time = weekday_wake_up if day in weekdays else weekend_wake_up
23+
print(f'Wake up at {wake_time}\n')

Examples/example_10_inheritance.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ def __init__(self, length):
4040
super().__init__(length, length)
4141

4242

43-
shapes = [Square(10), Circle(20), Rectangle(3.4, 1.5)]
43+
if __name__ == '__main__':
44+
shapes = [Square(10), Circle(20), Rectangle(3.4, 1.5)]
4445

45-
for shape in shapes:
46-
print(f'{shape} area is {shape.area()}')
46+
for shape in shapes:
47+
print(f'{shape} area is {shape.area()}')

Examples/example_11_args_kwargs.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def __init__(self, length, *args, **kwargs):
4646
super().__init__(length, length, *args, **kwargs)
4747

4848

49-
shapes = [Square(10, x=0, y=0), Circle(20, -1, 1), Rectangle(3.4, 1.5, 20, y=5)]
49+
if __name__ == '__main__':
50+
shapes = [Square(10, x=0, y=0), Circle(20, -1, 1), Rectangle(3.4, 1.5, 20, y=5)]
5051

51-
for shape in shapes:
52-
print(f'{shape} area is {shape.area()}')
52+
for shape in shapes:
53+
print(f'{shape} area is {shape.area()}')
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import abc # Abstract Base Class
2+
from math import pi
3+
4+
5+
class Shape(abc.ABC):
6+
@abc.abstractmethod
7+
def area(self):
8+
pass
9+
10+
@abc.abstractmethod
11+
def circumference(self):
12+
pass
13+
14+
def __str__(self):
15+
return type(self).__name__
16+
17+
18+
class Circle(Shape):
19+
def __init__(self, r):
20+
self.r = r
21+
22+
def area(self):
23+
return pi * self.r ** 2
24+
25+
def circumference(self):
26+
return 2 * pi * self.r
27+
28+
29+
class Rectangle(Shape):
30+
def __init__(self, length, width):
31+
self.l = length
32+
self.w = width
33+
34+
def area(self):
35+
return self.l * self.w
36+
37+
def circumference(self):
38+
return 2 * self.l + 2 * self.w
39+
40+
41+
class Square(Rectangle):
42+
def __init__(self, length):
43+
super().__init__(length, length)
44+
45+
46+
if __name__ == '__main__':
47+
shapes = [Square(10), Circle(20), Rectangle(3.4, 1.5)]
48+
49+
for shape in shapes:
50+
print(f'{shape} area is {shape.area()}')
Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,38 @@
1-
from datetime import datetime
1+
def update_sale_price(bike, sale_price):
2+
if bike['sold'] is True:
3+
raise Exception("Action not allowed. Bike has already been sold")
4+
bike['sale_price'] = sale_price
25

36

4-
def lookup_msrp_value(make, model):
5-
"""
6-
Determine original sale price of a bike when new
7-
"""
8-
return 1000
9-
10-
11-
def set_sale_price(bike):
12-
original_value = lookup_msrp_value(bike['make'], bike['model'])
13-
current_year = datetime.now().year
14-
current_value = original_value * (1 - (current_year - bike['year']) * 0.015)
15-
current_value = current_value * bike['condition']
16-
bike['sale_price'] = current_value
17-
18-
19-
def create_bike(cost, make, model, year, condition):
7+
def create_bike(description, cost, sale_price, condition):
208
return {
9+
'description': description,
2110
'cost': cost,
22-
'make': make,
23-
'model': model,
24-
'year': year,
11+
'sale_price': sale_price,
2512
'condition': condition,
2613
'sold': False,
2714
}
2815

2916

30-
def sell(bike):
17+
def sell(bike, sold_for=None):
18+
if sold_for:
19+
update_sale_price(bike, sold_for)
3120
bike['sold'] = True
3221
profit = bike['sale_price'] - bike['cost']
3322
return profit
3423

3524

36-
bike1 = create_bike(100, 'Univega', 'Alpina', 1999, 0.5)
25+
bike1 = create_bike('Univega Alpina, orange', cost=100, sale_price=500, condition=0.5)
3726
# bike1 = {
3827
# 'cost': 100,
39-
# 'make': 'Univega',
40-
# 'model': 'Alpina',
41-
# 'year': 1999,
4228
# 'condition': 0.5,
29+
# 'description': 'Univega Alpina, orange',
30+
# 'sale_price': 500,
4331
# 'sold': False,
4432
# }
4533

46-
set_sale_price(bike1)
34+
update_sale_price(bike1, 350)
4735
# bike1['sale_price'] = 350.00
4836

49-
sell(bike1)
37+
print(sell(bike1))
5038
# bike1['sold'] = True
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from Examples.example_5_methods import Bike, Condition
22

3-
bike = Bike(100, 'Univega', 'Alpina', 1999, Condition.OKAY)
3+
bike = Bike('Univega Alpina, orange', Condition.OKAY, sale_price=500, cost=100)
44

5-
bike.update_sale_price() # sale price = $350
5+
bike.service(spent=30, sale_price=600) # cost=$130, sale_price=$600
66

7-
bike.service(30, Condition.GOOD) # cost = $130
7+
print(bike.sale_price) # 600
88

9-
print(bike.sale_price) # sale price = $560
10-
11-
bike.sell() # profit = $430
9+
print(bike.sell()) # sold=True

Examples/example_4_attributes.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
class Bike(object):
2-
def __init__(self, cost, make, model, year, condition):
2+
def __init__(self, description, condition, sale_price, cost=0):
33
# Different initial values for every new instance
4-
self.cost = cost
5-
self.make = make
6-
self.model = model
7-
self.year = year
4+
self.description = description
85
self.condition = condition
6+
self.sale_price = sale_price
7+
self.cost = cost
98

10-
# Same initial values for every new instance
11-
self.sale_price = None
9+
# Same initial value for every new instance
1210
self.sold = False
1311

1412
def update_sale_price(self):
@@ -17,5 +15,5 @@ def update_sale_price(self):
1715
def sell(self):
1816
pass
1917

20-
def service(self, cost, new_condition):
18+
def service(self):
2119
pass

Examples/example_5_methods.py

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from datetime import datetime
21
from enum import Enum
32

43

@@ -9,27 +8,23 @@ class Condition(Enum):
98
BAD = 0.2
109

1110

11+
class MethodNotAllowed(Exception):
12+
pass
13+
14+
1215
class Bike(object):
13-
def __init__(self, cost, make, model, year, condition):
14-
self.cost = cost
15-
self.make = make
16-
self.model = model
17-
self.year = year
16+
def __init__(self, description, condition, sale_price, cost=0):
17+
self.description = description
1818
self.condition = condition
19+
self.sale_price = sale_price
20+
self.cost = cost
1921

20-
self.sale_price = self.update_sale_price()
2122
self.sold = False
2223

23-
def update_sale_price(self):
24-
"""
25-
Set the current sale price based on the make, model, age, and condition
26-
"""
27-
original_value = lookup_msrp_value(self.make, self.model)
28-
current_year = datetime.now().year
29-
current_value = original_value * (1 - (current_year - self.year) * 0.015)
30-
current_value = current_value * self.condition.value
31-
self.sale_price = current_value
32-
return self.sale_price
24+
def update_sale_price(self, sale_price):
25+
if self.sold:
26+
raise MethodNotAllowed('Bike has already been sold')
27+
self.sale_price = sale_price
3328

3429
def sell(self):
3530
"""
@@ -39,18 +34,12 @@ def sell(self):
3934
profit = self.sale_price - self.cost
4035
return profit
4136

42-
def service(self, cost, new_condition):
37+
def service(self, spent, sale_price=None, condition=None):
4338
"""
44-
Service the bike and update sale price
39+
Service the bike and update attributes
4540
"""
46-
self.cost += cost
47-
self.condition = new_condition
48-
self.update_sale_price()
49-
return self.sale_price
50-
51-
52-
def lookup_msrp_value(make, model):
53-
"""
54-
Determine original sale price of a bike when new
55-
"""
56-
return 1000
41+
self.cost += spent
42+
if sale_price:
43+
self.update_sale_price(sale_price)
44+
if self.condition:
45+
self.condition = condition
Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from datetime import datetime
21
from enum import Enum
32

43

@@ -10,32 +9,22 @@ class Condition(Enum):
109

1110

1211
class Bike(object):
13-
14-
def __init__(self, cost, make, model, year, condition):
15-
self.cost = cost
16-
self.make = make
17-
self.model = model
18-
self.year = year
12+
def __init__(self, description, condition, sale_price, cost=0):
13+
self.description = description
1914
self.condition = condition
15+
self.sale_price = sale_price
16+
self.cost = cost
2017

21-
self.sale_price = self.update_sale_price()
2218
self.sold = False
23-
2419
print(f'New bike: {self}')
2520

2621
def __del__(self):
2722
print(f'Deleting bike: {self}')
2823

29-
def update_sale_price(self):
30-
"""
31-
Set the current sale price based on the make, model, age, and condition
32-
"""
33-
original_value = lookup_msrp_value(self.make, self.model)
34-
current_year = datetime.now().year
35-
current_value = original_value * (1 - (current_year - self.year) * 0.015)
36-
current_value = current_value * self.condition.value
37-
self.sale_price = current_value
38-
return self.sale_price
24+
def update_sale_price(self, sale_price):
25+
if self.sold:
26+
return Exception('Action not allowed. Bike has already been sold')
27+
self.sale_price = sale_price
3928

4029
def sell(self):
4130
"""
@@ -45,36 +34,33 @@ def sell(self):
4534
profit = self.sale_price - self.cost
4635
return profit
4736

48-
def service(self, cost, new_condition):
37+
def service(self, spent, sale_price=None, condition=None):
4938
"""
50-
Service the bike and update sale price
39+
Service the bike and update attributes
5140
"""
52-
self.cost += cost
53-
self.condition = new_condition
54-
self.update_sale_price()
55-
return self.sale_price
41+
self.cost += spent
42+
if sale_price:
43+
self.update_sale_price(sale_price)
44+
if self.condition:
45+
self.condition = condition
5646

5747
def __repr__(self):
58-
return f'Bike: {self.year} {self.make} {self.model} ({"sold" if self.sold else self.sale_price})'
48+
sold_or_price = "sold" if self.sold else f"${self.sale_price}"
49+
return f'Bike: {self.description} ({sold_or_price})'
5950

6051
def __str__(self):
61-
return f'{self.year} {self.make} {self.model}'
62-
63-
64-
def lookup_msrp_value(make, model):
65-
"""
66-
Determine original sale price of a bike when new
67-
"""
68-
return 1000
52+
return self.description
6953

7054

7155
if __name__ == '__main__':
72-
bike = Bike(100, 'Univega', 'Alpina', 1999, Condition.OKAY) # __init__ called
56+
bike = Bike('Univega Alpina, orange', Condition.OKAY, sale_price=500, cost=100)
7357

74-
print(bike) # __str__ called
58+
print(bike) # __str__ called
59+
print(str(bike)) # __str__ called
7560

76-
print([bike]) # __repr__ called
61+
print([bike]) # __repr__ called
62+
print(repr(bike)) # __repr__ called
7763

78-
del bike # __del__ called
64+
del bike # __del__ called
7965

80-
Bike(0, 'Raleigh', 'Talus 2', 2013, Condition.BAD) # __init__ and __del__ called
66+
bike = Bike('Raleigh Talus 2', Condition.BAD, sale_price=20) # __init__ and __del__ called

0 commit comments

Comments
 (0)