-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
57 lines (45 loc) · 1.57 KB
/
Copy pathmodels.py
File metadata and controls
57 lines (45 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from babel.numbers import format_currency
from dataclasses import dataclass
@dataclass(frozen=True)
class Stock:
symbol: str
shares: int
purchase_price: float
def __post_init__(self):
if not self.symbol or not isinstance(self.symbol, str):
raise ValueError("Symbol must be a non-empty string")
if self.shares <= 0:
raise ValueError("Shares must be positive")
if self.purchase_price <= 0:
raise ValueError("Purchase price must be positive")
@dataclass(frozen=True)
class StockData:
symbol: str
shares: int
purchase_price: float
current_price: float
currency: str
language: str
@property
def __holdings(self) -> float:
return self.current_price * self.shares
@property
def holdings(self) -> str:
return self.__formatNumber(self.__holdings)
@property
def __profit(self) -> float:
return self.__holdings - (self.purchase_price * self.shares)
@property
def profit(self) -> str:
return self.__formatNumber(self.__profit)
@property
def profit_per_stock(self) -> str:
return self.__formatNumber(self.__profit / self.shares)
@property
def profit_percentage(self) -> float:
return ((self.current_price - self.purchase_price) / self.purchase_price) * 100
@property
def is_profit(self) -> bool:
return self.__profit > 0
def __formatNumber(self, number: float) -> str:
return format_currency(round(number, 2), self.currency, locale=self.language.replace('-', '_'))