diff --git a/Start/Ch 1/challenge.py b/Start/Ch 1/challenge.py index 050cd6d..594e067 100644 --- a/Start/Ch 1/challenge.py +++ b/Start/Ch 1/challenge.py @@ -10,7 +10,13 @@ # of "Ticker: Company -- $Price" class Stock: - pass + def __init__(self, ticker, price, company): + self.ticker = ticker + self.price = price + self.company = company + + def get_description(self): + return f"{self.ticker}: {self.company} -- {self.price}" # ~~~~~~~~~ TEST CODE ~~~~~~~~~ msft = Stock("MSFT", 342.0, "Microsoft Corp") diff --git a/Start/Ch 2/challenge.py b/Start/Ch 2/challenge.py index 556fd1f..32c0cdb 100644 --- a/Start/Ch 2/challenge.py +++ b/Start/Ch 2/challenge.py @@ -12,14 +12,32 @@ # For stocks: "Ticker: Company -- $Price" # For bonds: "description: duration'yr' : $price : yieldamt%" -class Asset(): - pass +class Asset(ABC): + def __init__(self, price): + self.price = price -class Stock(): - pass + @abstractmethod + def get_description(self): + pass -class Bond(): - pass +class Stock(Asset): + def __init__(self, ticker, price, company_name): + super().__init__(price) + self.ticker = ticker + self.company_name = company_name + + def get_description(self): + return f"{self.ticker}: {self.company_name} -- ${self.price}" + +class Bond(Asset): + def __init__(self, price, description, duration, yieldamt): + super().__init__(price) + self.description = description + self.duration = duration + self.yieldamt = yieldamt + + def get_description(self): + return f"{self.description}: {self.duration}yr : ${self.price} : {self.yieldamt}%" # ~~~~~~~~~ TEST CODE ~~~~~~~~~ diff --git a/Start/Ch 3/challenge.py b/Start/Ch 3/challenge.py index 679db2b..3bb6e99 100644 --- a/Start/Ch 3/challenge.py +++ b/Start/Ch 3/challenge.py @@ -23,6 +23,12 @@ def __init__(self, ticker, price, company): self.company = company self.ticker = ticker + def __str__(self): + return f"{self.ticker}: {self.company_name} -- ${self.price}" + + def __lt__(self, other): + return self.price < other.price + class Bond(Asset): def __init__(self, price, description, duration, yieldamt): @@ -31,6 +37,12 @@ def __init__(self, price, description, duration, yieldamt): self.duration = duration self.yieldamt = yieldamt + 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 = [ diff --git a/Start/Ch 4/challenge.py b/Start/Ch 4/challenge.py index f7634d0..022424a 100644 --- a/Start/Ch 4/challenge.py +++ b/Start/Ch 4/challenge.py @@ -4,32 +4,49 @@ # Challenge: convert your classes to dataclasses # The subclasses are required to override the magic method # that makes them sortable +from abc import ABC, abstractmethod +from dataclasses import dataclass -class Asset(): - pass - +from dataclasses import dataclass, field +@dataclass +class Asset(ABC): + price: float + + @abstractmethod + def __lt__(self): + pass + +@dataclass class Stock(Asset): - pass + ticker: str + company: str + def __lt__(self, other): + return self.price < other.price +@dataclass class Bond(Asset): - pass + description: str + duration: int + yieldamt: float + 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") + Stock(ticker="MSFT", price=342.0, company="Microsoft Corp"), + Stock(ticker="GOOG", price=135.0, company="Google Inc"), + Stock(ticker="META", price=275.0, company="Meta Platforms Inc"), + Stock(ticker="AMZN", price=120.0, company="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) -] + Bond(price=95.31, description="30 Year US Treasury", duration=30, yieldamt=4.38), + Bond(price=96.70, description="10 Year US Treasury", duration=10, yieldamt=4.28), + Bond(price=98.65, description="5 Year US Treasury", duration=5, yieldamt=4.43), + Bond(price=99.57, description="2 Year US Treasury", duration=2, yieldamt=4.98) try: ast = Asset(100.0)