diff options
Diffstat (limited to 'python/report_ops/cash.py')
| -rw-r--r-- | python/report_ops/cash.py | 63 |
1 files changed, 28 insertions, 35 deletions
diff --git a/python/report_ops/cash.py b/python/report_ops/cash.py index 552058a8..2742e1f7 100644 --- a/python/report_ops/cash.py +++ b/python/report_ops/cash.py @@ -1,11 +1,10 @@ import datetime from typing import ClassVar -from dataclasses import dataclass, field +from dataclasses import dataclass import re import pandas as pd from serenitas.utils.env import DAILY_DIR -from serenitas.utils.db2 import dbconn from serenitas.analytics.dates import prev_business_day from serenitas.analytics.exceptions import MissingDataError from serenitas.ops.trade_dataclasses import Ccy @@ -16,43 +15,44 @@ from .base import Report @dataclass -class CashReport(Report, table_name="cash_balances"): +class CashReport( + Report, + table_name="cash_balances", + columns=( + "date", + "fund", + "account_name", + "account_number", + "currency_code", + "balance", + ), +): date: datetime.date fund: Fund - account_name: str - account_number: str - currency_code: Ccy - balance: float - custodian: ClassVar[Custodian] = field(metadata={"insert": False}) - _registry: ClassVar = field(default={}, metadata={"insert": False}) + custodian: ClassVar[Custodian] - def __init_subclass__(cls, custodian): + def __init_subclass__(cls, custodian, **kwargs): cls.custodian = custodian cls._registry[custodian] = cls - def __class_getitem__(cls, key): - return cls._registry[key] - - @classmethod - def get_report(cls, knowledge_date, fund): - report_dir = get_dir(knowledge_date) - pattern = f"{cls.custodian}_CASH_{fund}_" + def get_report(self): + report_dir = get_dir(self.date) + pattern = f"{self.custodian}_CASH_{self.fund}_" reports = [ f for f in report_dir.iterdir() - if f.name.startswith(pattern) - and cls.get_ts(f.name).date() == knowledge_date + if f.name.startswith(pattern) and self.get_ts(f.name).date() == self.date ] p = max( reports, - key=lambda f: cls.get_ts(f.name), + key=lambda f: self.get_ts(f.name), default=None, ) if p: return p else: raise MissingDataError( - f"Report not ready {knowledge_date}: {cls.custodian} {fund}" + f"Report not ready {self.date}: {self.custodian} {self.fund}" ) @staticmethod @@ -62,21 +62,14 @@ class CashReport(Report, table_name="cash_balances"): class NTCashReport(CashReport, custodian="NT"): - @classmethod - def yield_rows(cls, knowledge_date, fund): - df = pd.read_csv(cls.get_report(knowledge_date, fund), on_bad_lines="warn") + def __iter__(self): + df = pd.read_csv(self.get_report(), on_bad_lines="warn") df = df[df["T-NARR-LONG"] == "CLOSING BALANCE"] - yield from df.to_dict(orient="records") - - @classmethod - def from_report_line(cls, d): - return cls( - date=prev_business_day(d["knowledge_date"]), - fund=d["fund"], - account_name=d["Consolidation"], - account_number=d["Account Number"], - currency_code=d["Currency code"], - balance=d["A-TRAN-AMT"], + return ( + (prev_business_day(self.date), self.fund, *t) + for t in df[ + ["Consolidation", "Account Number", "Currency code", "A-TRAN-AMT"] + ].itertuples(index=False) ) |
