Skip to content

Commit 1117272

Browse files
committed
fix formatting
1 parent 1c7e698 commit 1117272

File tree

6 files changed

+159
-4
lines changed

6 files changed

+159
-4
lines changed

Finished/Ch 3/challenge.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __lt__(self, other):
5959
bonds.sort()
6060

6161
for stock in stocks:
62-
print(stock)
62+
print(stock)
6363
print("-----------")
6464
for bond in bonds:
65-
print(bond)
65+
print(bond)

Finished/Ch 4/challenge.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __lt__(self, other):
5555
bonds.sort()
5656

5757
for stock in stocks:
58-
print(stock)
58+
print(stock)
5959
print("-----------")
6060
for bond in bonds:
61-
print(bond)
61+
print(bond)

Start/Ch 1/challenge.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Python Object Oriented Programming by Joe Marini course example
2+
# Programming challenge: define a class to represent a stock symbol
3+
4+
5+
class Stock:
6+
pass
7+
8+
# ~~~~~~~~~ TEST CODE ~~~~~~~~~
9+
msft = Stock("MSFT", 342.0, "Microsoft Corp")
10+
goog = Stock("GOOG", 135.0, "Google Inc")
11+
meta = Stock("META", 275.0, "Meta Platforms Inc")
12+
amzn = Stock("AMZN", 135.0, "Amazon Inc")
13+
14+
print(msft.get_description())
15+
print(goog.get_description())
16+
print(meta.get_description())
17+
print(amzn.get_description())

Start/Ch 2/challenge.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Python Object Oriented Programming by Joe Marini course example
2+
# Programming challenge: use inheritance and abstract classes
3+
4+
from abc import ABC, abstractmethod
5+
6+
class Asset():
7+
pass
8+
9+
class Stock():
10+
pass
11+
12+
class Bond():
13+
pass
14+
15+
16+
# ~~~~~~~~~ TEST CODE ~~~~~~~~~
17+
try:
18+
ast = Asset(100.0)
19+
except:
20+
print("Can't instantiate Asset!")
21+
22+
msft = Stock("MSFT", 342.0, "Microsoft Corp")
23+
goog = Stock("GOOG", 135.0, "Google Inc")
24+
meta = Stock("META", 275.0, "Meta Platforms Inc")
25+
amzn = Stock("AMZN", 135.0, "Amazon Inc")
26+
27+
us30yr = Bond(95.31, "30 Year US Treasury", 30, 4.38)
28+
us10yr = Bond(96.70, "10 Year US Treasury", 10, 4.28)
29+
us5yr = Bond(98.65, "5 Year US Treasury", 5, 4.43)
30+
us2yr = Bond(99.57, "2 Year US Treasury", 2, 4.98)
31+
32+
print(msft.get_description())
33+
print(goog.get_description())
34+
print(meta.get_description())
35+
print(amzn.get_description())
36+
37+
print(us30yr.get_description())
38+
print(us10yr.get_description())
39+
print(us5yr.get_description())
40+
print(us2yr.get_description())

Start/Ch 3/challenge.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Python Object Oriented Programming by Joe Marini course example
2+
# Programming challenge: add methods for comparison and equality
3+
4+
from abc import ABC, abstractmethod
5+
6+
7+
class Asset(ABC):
8+
def __init__(self, price):
9+
self.price = price
10+
11+
@abstractmethod
12+
def __str__(self):
13+
pass
14+
15+
16+
class Stock(Asset):
17+
def __init__(self, ticker, price, company):
18+
super().__init__(price)
19+
self.company = company
20+
self.ticker = ticker
21+
22+
23+
class Bond(Asset):
24+
def __init__(self, price, description, duration, yieldamt):
25+
super().__init__(price)
26+
self.description = description
27+
self.duration = duration
28+
self.yieldamt = yieldamt
29+
30+
31+
# ~~~~~~~~~ TEST CODE ~~~~~~~~~
32+
stocks = [
33+
Stock("MSFT", 342.0, "Microsoft Corp"),
34+
Stock("GOOG", 135.0, "Google Inc"),
35+
Stock("META", 275.0, "Meta Platforms Inc"),
36+
Stock("AMZN", 120.0, "Amazon Inc")
37+
]
38+
39+
bonds = [
40+
Bond(95.31, "30 Year US Treasury", 30, 4.38),
41+
Bond(96.70, "10 Year US Treasury", 10, 4.28),
42+
Bond(98.65, "5 Year US Treasury", 5, 4.43),
43+
Bond(99.57, "2 Year US Treasury", 2, 4.98)
44+
]
45+
46+
stocks.sort()
47+
bonds.sort()
48+
49+
for stock in stocks:
50+
print(stock)
51+
print("-----------")
52+
for bond in bonds:
53+
print(bond)

Start/Ch 4/challenge.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Python Object Oriented Programming by Joe Marini course example
2+
# Programming challenge: implement a dataclass
3+
4+
from dataclasses import dataclass
5+
from abc import ABC, abstractmethod
6+
7+
class Asset():
8+
pass
9+
10+
11+
class Stock(Asset):
12+
pass
13+
14+
15+
class Bond(Asset):
16+
pass
17+
18+
# ~~~~~~~~~ TEST CODE ~~~~~~~~~
19+
stocks = [
20+
Stock("MSFT", 342.0, "Microsoft Corp"),
21+
Stock("GOOG", 135.0, "Google Inc"),
22+
Stock("META", 275.0, "Meta Platforms Inc"),
23+
Stock("AMZN", 120.0, "Amazon Inc")
24+
]
25+
26+
bonds = [
27+
Bond(95.31, "30 Year US Treasury", 30, 4.38),
28+
Bond(96.70, "10 Year US Treasury", 10, 4.28),
29+
Bond(98.65, "5 Year US Treasury", 5, 4.43),
30+
Bond(99.57, "2 Year US Treasury", 2, 4.98)
31+
]
32+
33+
try:
34+
ast = Asset(100.0)
35+
except:
36+
print("Can't instantiate Asset!")
37+
38+
stocks.sort()
39+
bonds.sort()
40+
41+
for stock in stocks:
42+
print(stock)
43+
print("-----------")
44+
for bond in bonds:
45+
print(bond)

0 commit comments

Comments
 (0)