Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelsoup42 committed Sep 12, 2023
1 parent 5068dd9 commit a87078d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
8 changes: 1 addition & 7 deletions src/cardano_account_pandas_dumper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
36 changes: 13 additions & 23 deletions src/cardano_account_pandas_dumper/cardano_account_pandas_dumper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Cardano Account To Pandas Dumper."""
import argparse
import datetime
import functools
import itertools
Expand Down Expand Up @@ -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]:
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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]
):
Expand Down Expand Up @@ -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)
Expand All @@ -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

0 comments on commit a87078d

Please sign in to comment.