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] 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 = [