-
Notifications
You must be signed in to change notification settings - Fork 512
/
Copy pathchallenge.py
74 lines (59 loc) · 1.6 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
62
63
64
65
66
67
68
69
70
71
72
73
74
# Python Object Oriented Programming by Joe Marini course example
# Programming challenge: implement a dataclass
# Challenge: convert your classes to dataclasses
# The subclasses are required to override the magic method
# that makes them sortable
#%%
from dataclasses import dataclass
from abc import ABC, abstractmethod
@dataclass
class Asset(ABC):
price: float
# @abstractmethod
# def __str__(self):
# pass
@abstractmethod
def __lt__(self, other):
pass
@dataclass
class Stock(Asset):
ticker: str
company: str
# def __str__(self):
# return f"{self.ticker}: {self.company} -- ${self.price}"
def __lt__(self, other):
return self.price < other.price
@dataclass
class Bond(Asset):
description: str
duration: int
yieldamt: float
# def __str__(self):
# return f"{self.description}: {self.duration}yr : ${self.price} : {self.yieldamt}%"
def __lt__(self, other):
return self.yieldamt < other.yieldamt
# ~~~~~~~~~ TEST CODE ~~~~~~~~~
stocks = [
Stock("MSFT", 342.0, "Microsoft Corp"),
Stock("GOOG", 135.0, "Google Inc"),
Stock("META", 275.0, "Meta Platforms Inc"),
Stock("AMZN", 120.0, "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)
# %%