1
0

Ruff
All checks were successful
Test Python / Test (push) Successful in 25s

This commit is contained in:
Jon Michael Aanes 2024-09-01 19:37:28 +02:00
parent fb07c495fa
commit eeffb23c98
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
3 changed files with 27 additions and 11 deletions

View File

@ -26,19 +26,22 @@ import enforce_typing
from ._version import __version__ # noqa: F401
def parse_attr_data(attr_data: str) -> dict[str, str | int]:
d = {}
if attr_data is None:
return d
for s in attr_data.split(','):
s = s.strip()
if s == '': continue
if s == '':
continue
(attr_key, attr_value) = s.split('=')
if re.match(r'^\d+$', attr_value):
attr_value = int(attr_value)
d[attr_key] = attr_value
return d
## Ids
RE_TICKER_FORMAT = r'^[A-Z0-9_]+$'
@ -132,7 +135,9 @@ class Asset:
if isinstance(self, FiatCurrency):
return f'fiat:{self.iso_code}'
if isinstance(self, CryptoCurrency):
attrs_str = f'{{coingecko_id={self.coingecko_id}}}' if self.coingecko_id else ''
attrs_str = (
f'{{coingecko_id={self.coingecko_id}}}' if self.coingecko_id else ''
)
return f'crypto:{self.ccxt_symbol}{attrs_str}'
if isinstance(self, Index):
return f'index:{self.ticker}'
@ -466,7 +471,7 @@ class AssetAmount:
specificity = '2' if self.amount >= 0.10 else '3'
prefix = ASSET_PREFIX.get(self.asset, '')
return ('{}{:.' + specificity + 'f} {}').format(
prefix, self.amount, self.asset.raw_short_name()
prefix, self.amount, self.asset.raw_short_name(),
)
def __mul__(self, other: Decimal):

View File

@ -10,6 +10,7 @@ BAD_TICKERS = ['TEST:EUR', 'EUR:TEST']
def test_valid_tickers(ticker: str):
fin_defs.Stock(ticker, exchange=fin_defs.EXCHANGES_BY_IDS['NYSE'])
@pytest.mark.parametrize('ticker', BAD_TICKERS)
def test_bad_tickers(ticker: str):
with pytest.raises(ValueError):

View File

@ -9,7 +9,11 @@ def test_parse_attr():
assert fin_defs.parse_attr_data('abc=abc') == {'abc': 'abc'}
assert fin_defs.parse_attr_data('abc=123') == {'abc': 123}
assert fin_defs.parse_attr_data('abc=123,xyz=abc') == {'abc': 123, 'xyz': 'abc'}
assert fin_defs.parse_attr_data(' abc=123 , xyz=abc ') == {'abc':123, 'xyz': 'abc'}
assert fin_defs.parse_attr_data(' abc=123 , xyz=abc ') == {
'abc': 123,
'xyz': 'abc',
}
def test_from_nordnet():
derp = fin_defs.Asset.from_string_id('stock:NVO.NYSE{nordnet_id=123}')
@ -20,12 +24,18 @@ def test_from_nordnet():
@pytest.mark.parametrize('asset', fin_defs.WELL_KNOWN_SYMBOLS.values())
def test_to_from_string_id_shortcut(asset: fin_defs.Asset):
assert fin_defs.Asset.from_string_id(asset.to_string_id(), shortcut_well_known=True) == asset
assert (
fin_defs.Asset.from_string_id(asset.to_string_id(), shortcut_well_known=True)
== asset
)
@pytest.mark.parametrize('asset', fin_defs.WELL_KNOWN_SYMBOLS.values())
def test_to_from_string_id(asset: fin_defs.Asset):
assert fin_defs.Asset.from_string_id(asset.to_string_id(), shortcut_well_known=False) == asset
assert (
fin_defs.Asset.from_string_id(asset.to_string_id(), shortcut_well_known=False)
== asset
)
@pytest.mark.parametrize('asset', fin_defs.WELL_KNOWN_SYMBOLS.values())