1
0

Compare commits

...

4 Commits

Author SHA1 Message Date
3a96b50b68 🤖 Bumped version to 0.1.35
Some checks failed
Package Python / Package (push) Successful in 24s
Test Python / Test (push) Failing after 23s
This commit was automatically generated by a script: https://gitfub.space/Jmaa/python-omni
2024-09-01 18:59:38 +02:00
17d9a61bd4 🤖 Repository layout updated to latest version
This commit was automatically generated by a script: https://gitfub.space/Jmaa/python-omni
2024-09-01 18:59:29 +02:00
aefece86d2
Improved tests 2024-09-01 18:05:05 +02:00
74895720d6
Amount prefix 2024-09-01 17:39:22 +02:00
6 changed files with 35 additions and 12 deletions

View File

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

View File

@ -1 +1 @@
__version__ = '0.1.34' __version__ = '0.1.35'

View File

@ -7,6 +7,7 @@ lint.ignore = [
'Q003', 'D205', # Format conflict 'Q003', 'D205', # Format conflict
'TCH', # Microoptimization at the cost of readability 'TCH', # Microoptimization at the cost of readability
'TID252', # I like relative imports
'D407', 'D413', # Weird documentation stuff 'D407', 'D413', # Weird documentation stuff

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

@ -2,15 +2,18 @@ import pytest
import fin_defs import fin_defs
BAD_TICKERS = ['TEST123', '123', 'TEST.EUR', 'TEST:EUR', 'EUR:TEST'] VALID_TICKERS = ['TEST123', '123', 'TEST.EUR']
BAD_TICKERS = ['TEST:EUR', 'EUR:TEST']
@pytest.mark.parametrize('ticker', VALID_TICKERS)
def test_valid_tickers(ticker: str):
fin_defs.Stock(ticker,exchange=fin_defs.EXCHANGES_BY_IDS['NYSE'])
@pytest.mark.parametrize('ticker', BAD_TICKERS) @pytest.mark.parametrize('ticker', BAD_TICKERS)
def test_bad_tickers(ticker): def test_bad_tickers(ticker: str):
try: with pytest.raises(ValueError):
fin_defs.Stock(ticker) fin_defs.Stock(ticker,exchange=fin_defs.EXCHANGES_BY_IDS['NYSE'])
except Exception as e:
assert e
@pytest.mark.parametrize('ticker', BAD_TICKERS) @pytest.mark.parametrize('ticker', BAD_TICKERS)
@ -20,4 +23,4 @@ def test_crypto_tickers(ticker):
def test_str(): def test_str():
NVO = fin_defs.Stock('NVO', fin_defs.EXCHANGES_BY_IDS['NYSE']) NVO = fin_defs.Stock('NVO', fin_defs.EXCHANGES_BY_IDS['NYSE'])
assert str(NVO) == 'NVO.XNYS' assert str(NVO) == 'stock:NVO.XNYS'

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 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()) @pytest.mark.parametrize('asset', fin_defs.WELL_KNOWN_SYMBOLS.values())
def test_to_from_polygon_id(asset: fin_defs.Asset): def test_to_from_polygon_id(asset: fin_defs.Asset):
assert fin_defs.Asset.from_polygon_id(asset.to_polygon_id()) == asset assert fin_defs.Asset.from_polygon_id(asset.to_polygon_id()) == asset