1
0

Depo methods
Some checks failed
Test Python / Test (push) Failing after 19s

This commit is contained in:
Jon Michael Aanes 2024-06-02 17:24:53 +02:00
parent 71f767e2ed
commit f74945d6bf
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
4 changed files with 40 additions and 5 deletions

View File

@ -1 +1 @@
__version__ = '0.1.2'
__version__ = '0.1.2'

View File

@ -1,6 +1,7 @@
import abc
import dataclasses
import datetime
from collections.abc import Mapping
from collections.abc import Iterable, Mapping
from decimal import Decimal
import enforce_typing
@ -9,18 +10,48 @@ from fin_defs import Asset
@enforce_typing.enforce_types
@dataclasses.dataclass
class Depo:
class Depo(abc.ABC):
name: str
updated_time: datetime.datetime
@abc.abstractmethod
def assets(self) -> Iterable[Asset]:
"""Returns the different assets managed by this depo."""
@abc.abstractmethod
def get_amount_of_asset(self, asset: Asset) -> Decimal:
"""Returns the amount of owned assets for all nested depos.
Must return 0 if depo does not contain the given asset.
"""
@enforce_typing.enforce_types
@dataclasses.dataclass
class DepoSingle(Depo):
assets: Mapping[Asset, Decimal]
_assets: Mapping[Asset, Decimal]
def assets(self) -> Iterable[Asset]:
return self._assets
def get_amount_of_asset(self, asset: Asset) -> Decimal:
return self._assets.get(asset, Decimal(0))
@enforce_typing.enforce_types
@dataclasses.dataclass
class DepoGroup(Depo):
nested: list[Depo]
def assets(self) -> Iterable[Asset]:
assets: list[Asset] = []
for nested_depo in self.nested:
assets.extend(nested_depo.assets())
return assets
def get_amount_of_asset(self, asset: Asset) -> Decimal:
summed: Decimal = Decimal(0)
for nested_depo in self.nested:
summed += nested_depo.get_amount_of_asset(asset)
del nested_depo
return summed

View File

@ -25,7 +25,8 @@ class KucoinDepoFetcher:
asset = fin_defs.WELL_KNOWN_SYMBOLS[account_data['currency']]
balance = Decimal(account_data['balance'])
assets_for_account_type = assets_by_account_type.get(
account_data['type'], {},
account_data['type'],
{},
)
assets_for_account_type[asset] = (
assets_for_account_type.get(asset, Decimal(0)) + balance

View File

@ -15,6 +15,7 @@ PACKAGE_NAME = 'fin_depo'
with open('README.md') as f:
readme = f.read()
def parse_version_file(text: str) -> str:
match = re.match(r'^__version__\s*=\s*(["\'])([\d\.]+)\1$', text)
if match is None:
@ -22,9 +23,11 @@ def parse_version_file(text: str) -> str:
raise Exception(msg)
return match.group(2)
with open(PACKAGE_NAME + '/_version.py') as f:
version = parse_version_file(f.read())
def parse_requirements(text: str) -> list[str]:
return text.strip().split('\n')