diff --git a/.vscode/settings.json b/.vscode/settings.json index 32dee9a..74e879f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -17,9 +17,8 @@ "files.autoSave": "afterDelay", "screencastMode.onlyKeyboardShortcuts": true, "terminal.integrated.fontSize": 18, - "workbench.activityBar.visible": true, - "workbench.colorTheme": "Default Light Modern", + "workbench.colorTheme": "Visual Studio Dark", "workbench.fontAliasing": "antialiased", "workbench.statusBar.visible": true, "python.linting.enabled": false -} \ No newline at end of file +} diff --git a/Start/Ch 1/challenge.py b/Start/Ch 1/challenge.py index 050cd6d..e6578a9 100644 --- a/Start/Ch 1/challenge.py +++ b/Start/Ch 1/challenge.py @@ -8,9 +8,16 @@ # Company (string) # And a method get_description() which returns a string in the form # of "Ticker: Company -- $Price" +#%% +from abc import ABC, abstractmethod 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") @@ -22,3 +29,5 @@ class Stock: print(goog.get_description()) print(meta.get_description()) print(amzn.get_description()) + +# %% diff --git a/Start/Ch 2/challenge.py b/Start/Ch 2/challenge.py index 556fd1f..47af365 100644 --- a/Start/Ch 2/challenge.py +++ b/Start/Ch 2/challenge.py @@ -1,3 +1,6 @@ + +#%% + # Python Object Oriented Programming by Joe Marini course example # Programming challenge: use inheritance and abstract classes @@ -12,14 +15,31 @@ # For stocks: "Ticker: Company -- $Price" # For bonds: "description: duration'yr' : $price : yieldamt%" -class Asset(): - pass +from abc import ABC, abstractmethod +class Asset(ABC): + def __init__(self, price): + self.price = price + @abstractmethod + def get_description(self): + pass + -class Stock(): - pass +class Stock(Asset): + def __init__(self, ticker, price, company): + super().__init__(price) + self.ticker = ticker + self.company = company + def get_description(self): + return f"{self.ticker}: {self.company} -- ${self.price}" -class Bond(): - pass +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 ~~~~~~~~~ @@ -47,3 +67,4 @@ class Bond(): print(us10yr.get_description()) print(us5yr.get_description()) print(us2yr.get_description()) +# %% diff --git a/Start/Ch 3/challenge.py b/Start/Ch 3/challenge.py index 679db2b..7995f90 100644 --- a/Start/Ch 3/challenge.py +++ b/Start/Ch 3/challenge.py @@ -5,6 +5,7 @@ # Stocks should sort from low to high on price # Bonds should sort from low to high on yield +#%% from abc import ABC, abstractmethod @@ -22,7 +23,11 @@ def __init__(self, ticker, price, company): super().__init__(price) self.company = company self.ticker = ticker + def __str__(self): + return f"{self.ticker}: {self.company} -- ${self.price}" + def __lt__(self, other): + return self.price < other.price class Bond(Asset): def __init__(self, price, description, duration, yieldamt): @@ -30,7 +35,11 @@ def __init__(self, price, description, duration, yieldamt): self.description = description 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..b6e8136 100644 --- a/Start/Ch 4/challenge.py +++ b/Start/Ch 4/challenge.py @@ -5,17 +5,43 @@ # The subclasses are required to override the magic method # that makes them sortable -class Asset(): - pass - +#%% + +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): - pass + 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): - pass + 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"), @@ -44,3 +70,5 @@ class Bond(Asset): print("-----------") for bond in bonds: print(bond) + +# %% diff --git a/branch_GitHub_link b/branch_GitHub_link new file mode 100644 index 0000000..4824a0b --- /dev/null +++ b/branch_GitHub_link @@ -0,0 +1,3 @@ +https://github.com/DonArchila/python-object-oriented-programming-4413110/tree/my_course_work + +this is a fork from original LinkedIn course with my changes and course work \ No newline at end of file