-
Notifications
You must be signed in to change notification settings - Fork 507
/
Copy pathchallenge.py
61 lines (47 loc) · 1.19 KB
/
challenge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Python Object Oriented Programming by Joe Marini course example
# Programming challenge: implement a dataclass
from dataclasses import dataclass
from abc import ABC, abstractmethod
@dataclass
class Asset(ABC):
price: float
@abstractmethod
def __lt__(self, other):
pass
@dataclass
class Stock(Asset):
ticker: str
company: str
def __lt__(self, other):
return self.price < other.price
@dataclass
class Bond(Asset):
description: str
duration: int
yieldamt: float
def __lt__(self, other):
return self.yieldamt < other.yieldamt
# ~~~~~~~~~ TEST CODE ~~~~~~~~~
stocks = [
Stock(342.0, "MSFT", "Microsoft Corp"),
Stock(135.0, "GOOG", "Google Inc"),
Stock(275.0, "META", "Meta Platforms Inc"),
Stock(120.0, "AMZN", "Amazon Inc")
]
bonds = [
Bond(95.31, "30 Year US Treasury", 30, 4.38),
Bond(96.70, "10 Year US Treasury", 10, 4.28),
Bond(98.65, "5 Year US Treasury", 5, 4.43),
Bond(99.57, "2 Year US Treasury", 2, 4.98)
]
try:
ast = Asset(100.0)
except:
print("Can't instantiate Asset!")
stocks.sort()
bonds.sort()
for stock in stocks:
print(stock)
print("-----------")
for bond in bonds:
print(bond)