Skip to content

Commit

Permalink
move transaction creation out to main, add flags for cols
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelsoup42 committed Sep 16, 2023
1 parent 279700a commit 810c496
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
9 changes: 8 additions & 1 deletion src/cardano_account_pandas_dumper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from json import JSONDecodeError

import jstyleson
import pandas as pd
from blockfrost import ApiError, BlockFrostApi

from .cardano_account_pandas_dumper import AccountData, AccountPandasDumper
Expand Down Expand Up @@ -159,7 +160,13 @@ def main():
reporter = AccountPandasDumper(
data=data_from_api, known_dict=known_dict_from_file, args=args
)
dataframe = reporter.make_transaction_frame()
transactions = pd.concat(
objs=[
data_from_api.transactions,
pd.Series() if args.no_rewards else data_from_api.reward_transactions,
],
).rename("transactions")
dataframe = reporter.make_transaction_frame(transactions)
if args.pandas_output:
try:
dataframe.to_pickle(args.pandas_output)
Expand Down
24 changes: 13 additions & 11 deletions src/cardano_account_pandas_dumper/cardano_account_pandas_dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,12 @@ def _drop_muted_policies(self, balance: pd.DataFrame) -> None:
balance.sort_index(inplace=True, axis=1)
balance.drop(policies_to_drop, axis=1, inplace=True)

def make_transaction_frame(self) -> pd.DataFrame:
def make_transaction_frame(
self,
transactions: pd.Series,
with_tx_hash: bool = True,
with_tx_message: bool = True,
) -> pd.DataFrame:
"""Build a transaction spreadsheet."""

# Add total line at the bottom
Expand All @@ -451,17 +456,13 @@ def make_transaction_frame(self) -> pd.DataFrame:
# )
# )
# outputs.loc["Total"] = total
transactions = pd.concat(
objs=[
self.data.transactions,
pd.Series() if self.args.no_rewards else self.data.reward_transactions,
],
).rename("transactions")
timestamp = transactions.rename("timestamp").map(self._extract_timestamp)
tx_hash = transactions.rename("hash").map(lambda x: x.hash)
message = transactions.rename("message").map(self._format_message)
columns = [transactions.rename("timestamp").map(self._extract_timestamp)]
if with_tx_hash:
columns.append(transactions.rename("hash").map(lambda x: x.hash))
if with_tx_message:
columns.append(transactions.rename("message").map(self._format_message))
balance = self.make_balance_frame(transactions)
frame = pd.concat([timestamp, tx_hash, message], axis=1)
frame = pd.concat(columns, axis=1)
frame.reset_index(drop=True, inplace=True)
frame.columns = pd.MultiIndex.from_tuples(
[
Expand All @@ -474,6 +475,7 @@ def make_transaction_frame(self) -> pd.DataFrame:
return frame

def make_balance_frame(self, transactions):
"""Make DataFrame with transaction balances."""
balance = pd.DataFrame(
data=[self._transaction_balance(x) for x in transactions],
dtype="Int64",
Expand Down

0 comments on commit 810c496

Please sign in to comment.