From a87078df42292fda4f3d90d8c9729a75fc7ce6c2 Mon Sep 17 00:00:00 2001 From: pixelsoup42 Date: Tue, 12 Sep 2023 11:14:45 +0200 Subject: [PATCH] more refactoring --- src/cardano_account_pandas_dumper/__main__.py | 8 +---- .../cardano_account_pandas_dumper.py | 36 +++++++------------ 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/src/cardano_account_pandas_dumper/__main__.py b/src/cardano_account_pandas_dumper/__main__.py index 72e7705..c448f15 100644 --- a/src/cardano_account_pandas_dumper/__main__.py +++ b/src/cardano_account_pandas_dumper/__main__.py @@ -157,13 +157,7 @@ def main(): else: parser.exit(status=1, message="Staking address(es) required.") reporter = AccountPandasDumper( - data=data_from_api, - known_dict=known_dict_from_file, - detail_level=args.detail_level, - unmute=args.unmute, - truncate_length=None if args.no_truncate else args.truncate_length, - raw_asset=args.raw_asset or False, - rewards=data_from_api.rewards, + data=data_from_api, known_dict=known_dict_from_file, args=args ) dataframe = reporter.make_transaction_frame() if args.pandas_output: diff --git a/src/cardano_account_pandas_dumper/cardano_account_pandas_dumper.py b/src/cardano_account_pandas_dumper/cardano_account_pandas_dumper.py index 733629d..399c7a3 100644 --- a/src/cardano_account_pandas_dumper/cardano_account_pandas_dumper.py +++ b/src/cardano_account_pandas_dumper/cardano_account_pandas_dumper.py @@ -1,4 +1,5 @@ """ Cardano Account To Pandas Dumper.""" +import argparse import datetime import functools import itertools @@ -210,30 +211,19 @@ class AccountPandasDumper: SCRIPTS_KEY = "scripts" LABELS_KEY = "labels" - def __init__( - self, - data: AccountData, - known_dict: Any, - detail_level: int, - unmute: bool, - truncate_length: Optional[int], - raw_asset: bool, - rewards: bool, - ): - self.known_dict = known_dict + def __init__(self, data: AccountData, known_dict: Any, args: argparse.Namespace): self.data = data - self.truncate_length = truncate_length - self.detail_level = detail_level - self.unmute = unmute - self.raw_asset = raw_asset - self.rewards = rewards + self.known_dict = known_dict + self.args = args def _format_asset(self, asset: str) -> Optional[str]: return self.data.assets[asset].metadata.name def _truncate(self, value: str) -> str: return ( - (value[: self.truncate_length] + "...") if self.truncate_length else value + value + if self.args.no_truncate + else (value[: self.args.truncate_length] + "...") ) def _format_policy(self, policy: str) -> Optional[str]: @@ -259,14 +249,14 @@ def _munge_metadata(self, obj: blockfrost.utils.Namespace) -> Any: hex_name = self._is_hex_number(att) value = getattr(obj, att) hex_value = isinstance(value, str) and self._is_hex_number(value) - if (hex_name and hex_value) and not self.unmute: + if (hex_name and hex_value) and not self.args.unmute: continue out_att = self._truncate(att) if hex_name else att value = self._munge_metadata(value) if value: result[out_att] = value return result - elif isinstance(obj, str) and self._is_hex_number(obj) and not self.unmute: + elif isinstance(obj, str) and self._is_hex_number(obj) and not self.args.unmute: return {} else: return obj @@ -282,7 +272,7 @@ def _format_message(self, tx_obj: blockfrost.utils.Namespace) -> str: if ( self._is_hex_number(label) and (not val or self._is_hex_number(val)) - and not self.unmute + and not self.args.unmute ): continue result.append(label) @@ -396,7 +386,7 @@ def _drop_foreign_assets(self, balance: pd.DataFrame) -> None: def _drop_muted_policies(self, balance: pd.DataFrame) -> None: if ( - (not self.unmute) + (not self.args.unmute) and self.MUTED_POLICIES_KEY in self.known_dict and self.known_dict[self.MUTED_POLICIES_KEY] ): @@ -431,7 +421,7 @@ def make_transaction_frame(self) -> pd.DataFrame: transactions = pd.concat( objs=[ self.data.transactions, - self.data.reward_transactions if self.rewards else pd.Series(), + pd.Series() if self.args.no_rewards else self.data.reward_transactions, ], ).rename("transactions") timestamp = transactions.rename("timestamp").map(self._extract_timestamp) @@ -457,6 +447,6 @@ def make_transaction_frame(self) -> pd.DataFrame: ] ) frame = frame.join(balance) - frame.drop_duplicates(inplace=True) + # frame.drop_duplicates(inplace=True) frame.sort_values(by=frame.columns[0], inplace=True) return frame