From ecea606533550d8d53a65daa6eeef583788448f4 Mon Sep 17 00:00:00 2001 From: uvok Date: Thu, 17 Apr 2025 11:20:01 +0200 Subject: Enforce use of Decimal, formatting --- trade.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'trade.py') diff --git a/trade.py b/trade.py index ed646d1..a07efe7 100644 --- a/trade.py +++ b/trade.py @@ -1,6 +1,7 @@ from decimal import Decimal from enum import Enum + class PriceAdaption(Enum): KeepTotalCost = 1 KeepPricePerCoin = 2 @@ -11,7 +12,8 @@ class Trade: Represents a cryptocurrency trade, including the amount traded, total cost, and the date of trade. Provides methods to modify the trade and access various attributes. """ - def __init__(self, amount: float|Decimal, total_cost: float|Decimal, date: str) -> None: + + def __init__(self, amount: Decimal, total_cost: Decimal, date: str) -> None: """ Initialize a new Trade instance. @@ -28,7 +30,11 @@ class Trade: self.__total_cost: Decimal = Decimal(total_cost) self.__date: str = date - def remove_coins(self, amount: float|Decimal, adapt: PriceAdaption = PriceAdaption.KeepPricePerCoin) -> None: + def remove_coins( + self, + amount: float | Decimal, + adapt: PriceAdaption = PriceAdaption.KeepPricePerCoin, + ) -> None: """ Reduce the amount of cryptocurrency in the trade by a specified amount. @@ -42,7 +48,7 @@ class Trade: """ if amount > self.__amount: raise ValueError(f"Can't remove more than {self.__amount}") - + if adapt == PriceAdaption.KeepPricePerCoin: amount = Decimal(amount) self.__total_cost -= amount * self.price_per_coin @@ -95,7 +101,9 @@ class Trade: ZeroDivisionError: If the current amount is zero. """ if self.amount == 0: - raise ZeroDivisionError("Price per coin cannot be calculated when the amount is zero") + raise ZeroDivisionError( + "Price per coin cannot be calculated when the amount is zero" + ) return self.total_cost / self.amount -- cgit v1.2.3