1
0

Amount prefix

This commit is contained in:
Jon Michael Aanes 2024-09-01 17:39:04 +02:00
parent d635b6c4b9
commit 74895720d6
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
4 changed files with 24 additions and 7 deletions

View File

@ -258,6 +258,7 @@ Commodity.COFFEE = Commodity('COFFEE')
DKK = FiatCurrency('DKK')
USD = FiatCurrency('USD')
EUR = FiatCurrency('EUR')
GBP = FiatCurrency('GBP')
BTC = CryptoCurrency('BTC', coingecko_id='bitcoin')
MPC = CryptoCurrency('MPC', coingecko_id='partisia-blockchain')
SPX = Index('SPX')
@ -316,6 +317,13 @@ WELL_KNOWN_SYMBOLS = (
| {'SPX500': SPX, 'SP500': SPX, 'Nasdaq 100': NDX}
)
ASSET_PREFIX: dict[Asset, str] = {
USD: '$',
EUR: '',
GBP: '£',
BTC: '',
}
NYSE = StockExchange(
name='New York Stock Exchange',
mic='XNYS',
@ -430,8 +438,14 @@ class AssetAmount:
amount: Decimal
def __str__(self):
return self.human_readable_str()
def human_readable_str(self):
specificity = '2' if self.amount >= 0.10 else '3'
return ('{:.' + specificity + 'f} {}').format(self.amount, self.asset)
prefix = ASSET_PREFIX.get(self.asset, '')
return ('{}{:.' + specificity + 'f} {}').format(
prefix, self.amount, self.asset.raw_short_name()
)
def __mul__(self, other: Decimal):
if not isinstance(other, Decimal):

View File

@ -0,0 +1,8 @@
from decimal import Decimal
import fin_defs
def test_str():
amount = fin_defs.AssetAmount(fin_defs.USD, Decimal(10))
assert str(amount) == '$10.00 USD'

View File

@ -7,10 +7,8 @@ BAD_TICKERS = ['TEST123', '123', 'TEST.EUR', 'TEST:EUR', 'EUR:TEST']
@pytest.mark.parametrize('ticker', BAD_TICKERS)
def test_bad_tickers(ticker):
try:
with pytest.raises(Exception) as e:
fin_defs.Stock(ticker)
except Exception as e:
assert e
@pytest.mark.parametrize('ticker', BAD_TICKERS)

View File

@ -8,9 +8,6 @@ def test_to_from_string_id(asset: fin_defs.Asset):
assert fin_defs.Asset.from_string_id(asset.to_string_id()) == asset
import fin_defs
@pytest.mark.parametrize('asset', fin_defs.WELL_KNOWN_SYMBOLS.values())
def test_to_from_polygon_id(asset: fin_defs.Asset):
assert fin_defs.Asset.from_polygon_id(asset.to_polygon_id()) == asset