1
0

Added support for unknown assets and special casing of AssetAmounts.
All checks were successful
Test Python / Test (push) Successful in 23s

This commit is contained in:
Jon Michael Aanes 2024-10-20 18:00:15 +02:00
parent c36911d706
commit 6a13f5129f
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA

View File

@ -143,6 +143,8 @@ class Asset:
return f'index:{self.ticker}' return f'index:{self.ticker}'
if isinstance(self, Commodity): if isinstance(self, Commodity):
return f'commodity:{self.alpha_vantage_id}' return f'commodity:{self.alpha_vantage_id}'
if isinstance(self, UnknownAsset):
return 'unknown:XXX'
msg = f'Unsupported asset type: {repr(self)}' msg = f'Unsupported asset type: {repr(self)}'
raise ValueError(msg) raise ValueError(msg)
@ -179,6 +181,8 @@ class Asset:
return CryptoCurrency(ticker, **attrs) return CryptoCurrency(ticker, **attrs)
if prefix == 'commodity': if prefix == 'commodity':
return Commodity(ticker) return Commodity(ticker)
if prefix == 'unknown':
return UnknownAsset()
msg = f'Unsupported asset format: {asset_id}' msg = f'Unsupported asset format: {asset_id}'
raise ValueError(msg) raise ValueError(msg)
@ -192,6 +196,7 @@ class Asset:
class UnknownAsset(Asset): class UnknownAsset(Asset):
pass pass
@enforce_typing.enforce_types @enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True) @dataclasses.dataclass(frozen=True)
class Currency(Asset): class Currency(Asset):
@ -513,6 +518,10 @@ class AssetAmount:
if not isinstance(other, AssetAmount): if not isinstance(other, AssetAmount):
msg = f'other must be AssetAmount, but was {type(other)}' msg = f'other must be AssetAmount, but was {type(other)}'
raise TypeError(msg) raise TypeError(msg)
if self.is_zero():
return other
if other.is_zero():
return self
if self.asset != other.asset: if self.asset != other.asset:
msg = ( msg = (
f'AssetAmount must have same asset, but: {self.asset} != {other.asset}' f'AssetAmount must have same asset, but: {self.asset} != {other.asset}'
@ -525,6 +534,10 @@ class AssetAmount:
if not isinstance(other, AssetAmount): if not isinstance(other, AssetAmount):
msg = f'other must be AssetAmount, but was {type(other)}' msg = f'other must be AssetAmount, but was {type(other)}'
raise TypeError(msg) raise TypeError(msg)
if self.is_zero():
return -other
if other.is_zero():
return self
if self.asset != other.asset: if self.asset != other.asset:
msg = ( msg = (
f'AssetAmount must have same asset, but: {self.asset} != {other.asset}' f'AssetAmount must have same asset, but: {self.asset} != {other.asset}'