Added support for unknown assets and special casing of AssetAmounts.
All checks were successful
Test Python / Test (push) Successful in 23s
All checks were successful
Test Python / Test (push) Successful in 23s
This commit is contained in:
parent
c36911d706
commit
6a13f5129f
|
@ -143,6 +143,8 @@ class Asset:
|
|||
return f'index:{self.ticker}'
|
||||
if isinstance(self, Commodity):
|
||||
return f'commodity:{self.alpha_vantage_id}'
|
||||
if isinstance(self, UnknownAsset):
|
||||
return 'unknown:XXX'
|
||||
|
||||
msg = f'Unsupported asset type: {repr(self)}'
|
||||
raise ValueError(msg)
|
||||
|
@ -179,6 +181,8 @@ class Asset:
|
|||
return CryptoCurrency(ticker, **attrs)
|
||||
if prefix == 'commodity':
|
||||
return Commodity(ticker)
|
||||
if prefix == 'unknown':
|
||||
return UnknownAsset()
|
||||
|
||||
msg = f'Unsupported asset format: {asset_id}'
|
||||
raise ValueError(msg)
|
||||
|
@ -192,6 +196,7 @@ class Asset:
|
|||
class UnknownAsset(Asset):
|
||||
pass
|
||||
|
||||
|
||||
@enforce_typing.enforce_types
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Currency(Asset):
|
||||
|
@ -513,6 +518,10 @@ class AssetAmount:
|
|||
if not isinstance(other, AssetAmount):
|
||||
msg = f'other must be AssetAmount, but was {type(other)}'
|
||||
raise TypeError(msg)
|
||||
if self.is_zero():
|
||||
return other
|
||||
if other.is_zero():
|
||||
return self
|
||||
if self.asset != other.asset:
|
||||
msg = (
|
||||
f'AssetAmount must have same asset, but: {self.asset} != {other.asset}'
|
||||
|
@ -525,6 +534,10 @@ class AssetAmount:
|
|||
if not isinstance(other, AssetAmount):
|
||||
msg = f'other must be AssetAmount, but was {type(other)}'
|
||||
raise TypeError(msg)
|
||||
if self.is_zero():
|
||||
return -other
|
||||
if other.is_zero():
|
||||
return self
|
||||
if self.asset != other.asset:
|
||||
msg = (
|
||||
f'AssetAmount must have same asset, but: {self.asset} != {other.asset}'
|
||||
|
|
Loading…
Reference in New Issue
Block a user