From eeffb23c9838b4292291f5ea5080ff0af89576da Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 1 Sep 2024 19:37:28 +0200 Subject: [PATCH] Ruff --- fin_defs/__init__.py | 11 ++++++++--- test/test_data.py | 5 +++-- test/test_ids.py | 22 ++++++++++++++++------ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/fin_defs/__init__.py b/fin_defs/__init__.py index 2c68402..c155c8b 100644 --- a/fin_defs/__init__.py +++ b/fin_defs/__init__.py @@ -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): diff --git a/test/test_data.py b/test/test_data.py index 1de3225..f4447d7 100644 --- a/test/test_data.py +++ b/test/test_data.py @@ -8,12 +8,13 @@ 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']) + 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): - fin_defs.Stock(ticker,exchange=fin_defs.EXCHANGES_BY_IDS['NYSE']) + fin_defs.Stock(ticker, exchange=fin_defs.EXCHANGES_BY_IDS['NYSE']) @pytest.mark.parametrize('ticker', BAD_TICKERS) diff --git a/test/test_ids.py b/test/test_ids.py index 0b56752..87b4d06 100644 --- a/test/test_ids.py +++ b/test/test_ids.py @@ -6,10 +6,14 @@ import fin_defs def test_parse_attr(): assert fin_defs.parse_attr_data('') == {} assert fin_defs.parse_attr_data(' ') == {} - 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=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', + } + 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())