From 02bd06e18576ae6ac724abd5cd2e060c2d9c25b2 Mon Sep 17 00:00:00 2001 From: Touhidul Haider <163761310+TouhidulHaider@users.noreply.github.com> Date: Wed, 1 Jan 2025 22:40:14 +0600 Subject: [PATCH 1/5] ch1_challenge.py --- Start/Ch 1/challenge.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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") From 3d10429c7a75666611b4c9bdf6996f2847151965 Mon Sep 17 00:00:00 2001 From: Touhidul Haider <163761310+TouhidulHaider@users.noreply.github.com> Date: Thu, 2 Jan 2025 12:35:58 +0600 Subject: [PATCH 2/5] ch2_challenge.py --- Start/Ch 2/challenge.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) 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 ~~~~~~~~~ From 222b3749d7e130ac75a249a31999c53f04f9be0c Mon Sep 17 00:00:00 2001 From: Touhidul Haider <163761310+TouhidulHaider@users.noreply.github.com> Date: Thu, 2 Jan 2025 12:48:44 +0600 Subject: [PATCH 3/5] ch3_challenge.py --- Start/Ch 3/challenge.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 = [ From 332834b7fee0e56895825dcfe976d73a1f616277 Mon Sep 17 00:00:00 2001 From: Touhidul Haider <163761310+TouhidulHaider@users.noreply.github.com> Date: Thu, 2 Jan 2025 14:18:49 +0600 Subject: [PATCH 4/5] 01_ch4_challenge.py Output format is not fully accurate --- Start/Ch 4/challenge.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/Start/Ch 4/challenge.py b/Start/Ch 4/challenge.py index f7634d0..12e4148 100644 --- a/Start/Ch 4/challenge.py +++ b/Start/Ch 4/challenge.py @@ -4,17 +4,33 @@ # 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, field -class Asset(): - pass - +@dataclass +class Asset(ABC): + price: float + @abstractmethod + def __str__(self): + pass + +@dataclass(order=True) class Stock(Asset): - pass + ticker: str = field(compare=False) + company: str = field(compare=False) + def __str__(self): + return f"{self.ticker}: {self.company} -- ${self.price}" +@dataclass(order=True) class Bond(Asset): - pass + description: str = field(compare=False) + duration: int = field(compare=False) + yieldamt: float + + def __str__(self): + return f"{self.description}: {self.duration}yr : ${self.price} : {self.yieldamt}%" # ~~~~~~~~~ TEST CODE ~~~~~~~~~ stocks = [ From 15b03c52ffa0b55d5d8eab6512d544c5c1f207f3 Mon Sep 17 00:00:00 2001 From: Touhidul Haider <163761310+TouhidulHaider@users.noreply.github.com> Date: Thu, 2 Jan 2025 20:13:00 +0600 Subject: [PATCH 5/5] ch4_challenge.py The output is formatted here --- Start/Ch 4/challenge.py | 43 +++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/Start/Ch 4/challenge.py b/Start/Ch 4/challenge.py index f7634d0..a6bb562 100644 --- a/Start/Ch 4/challenge.py +++ b/Start/Ch 4/challenge.py @@ -4,32 +4,47 @@ # 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 - +@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)