diff options
author | uvok | 2025-04-14 20:37:38 +0200 |
---|---|---|
committer | uvok | 2025-04-14 20:37:38 +0200 |
commit | 8ca506bcf6475e0d0514089d02bb6855f848a908 (patch) | |
tree | 1c7f7e457a19ef197ab13f5cdef20d2fe6df70f8 /trade.py | |
parent | 14b737dc629950a4d9fe30c841c8697c958feff0 (diff) |
Docstrings
Diffstat (limited to 'trade.py')
-rw-r--r-- | trade.py | 58 |
1 files changed, 55 insertions, 3 deletions
@@ -1,7 +1,18 @@ -from decimal import Decimal - 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, total_cost: float, date: str) -> None: + """ + Initialize a new Trade instance. + + Args: + amount (float): The amount of cryptocurrency traded. + total_cost (float): The total cost of the trade. + date (str): The date of the trade, formatted as a string. + """ + if amount <= 0 or total_cost <= 0: raise ValueError("Amount and total cost must be > 0") @@ -10,6 +21,15 @@ class Trade: self.__date: str = date def remove_coins(self, amount: float) -> None: + """ + Reduce the amount of cryptocurrency in the trade by a specified amount. + + Args: + amount (float): The amount of cryptocurrency to remove. + + Raises: + ValueError: If the amount to remove exceeds the current amount in the trade. + """ if amount > self.__amount: raise ValueError(f"Can't remove more than {self.__amount}") @@ -18,22 +38,54 @@ class Trade: @property def amount(self) -> float: + """ + Get the current amount of cryptocurrency in the trade. + + Returns: + float: The amount of cryptocurrency. + """ return self.__amount @property def total_cost(self) -> float: + """ + Get the total cost of the trade. + + Returns: + float: The total cost of the trade. + """ return self.__total_cost @property def date(self) -> str: + """ + Get the date of the trade. + + Returns: + str: The trade date as a string. + """ return self.__date @property def price_per_coin(self) -> float: """ - Calculate the price per coin based on the total cost and the current amount + Calculate the price per coin based on the total cost and current amount. + + Returns: + float: The price per coin. + + Raises: + ZeroDivisionError: If the current amount is zero. """ + if self.amount == 0: + raise ZeroDivisionError("Price per coin cannot be calculated when the amount is zero") return self.total_cost / self.amount def __repr__(self) -> str: + """ + Get a string representation of the Trade instance. + + Returns: + str: A formatted string displaying the trade details. + """ return f"Trade(amount={self.amount}, price_per_coin={self.price_per_coin:.2f}, total_cost={self.total_cost:.2f}, date={self.date})" |