Transaction: A Balanced Entry#

class means.types.Transaction.Transaction(*, date: date, memo: str, posts: list[Post] = [])#

Represents a financial transaction, which consists of two or more financial ‘posts’.

I Construction and Serialization#

classmethod Transaction.from_match(match: MatchData) → Transaction#

Create a transaction from a regex match.

Parameters:

match – A regex match with groups: date (or y/m/d), memo, and post (multiple).

Returns:

A Transaction object with the parsed data.

classmethod Transaction.new(**kwargs: Any) → Transaction#

Build a transaction, coercing raw string or dict posts into Post objects.

Parameters:

**kwargs – Field values; posts entries may be strings, dicts, or Post objects.

Returns:

The constructed transaction.

Transaction.serialize_bilateral() → dict[str, Timestamp | float | str]#

Build a dataframe row from a transaction, assuming it is well-formed and bilateral.

This is meant to be used for building a dataframe with one row per transaction, where each transaction is a transfer of money between two accounts. If the transaction has more than 2 posts, or if the transaction is not balanced (i.e. the sum of the posts is not zero), then this method will raise an assertion error.

Transaction.serialize_posts() → Iterator[dict]#

Build a dataframe row from a transaction post, assuming it is well-formed.

II Splitting#

Transaction.split() → Iterable[Transaction]#

Split a transaction with more than 2 posts into multiple transactions with 2 posts each.

If this transaction already has 2 posts, it is returned as-is.

Yields:

A series of new 2-post transactions which sum to the original amnt.

III Derived Views#

Transaction.type#

The transaction type inferred from the account types of the posts.

Transaction.types#

The set of account types involved in this transaction.

Transaction.absolute#

The total positive value moved by this transaction.

Transaction.remainder#

The sum of all post amounts; a nonzero value means the transaction is unbalanced.

Transaction.accounts#

The account of each post, in post order.

Transaction.users#

The set of (non-empty) user uids involved in this transaction.

Transaction.uid#

A stable identifier built from the date, memo, accounts, and absolute value.

Transaction.idxs#

A string representing the types of accounts involved in the transaction, in order.

Transaction.is_unknown#

Whether the transaction is likely to be an unknown transfer.

IV Mutation Helpers#

Transaction.sort() → Self#

Sort the posts in the transaction by type and amount (descending).

Transaction.clear() → None#

Clear all cached properties; called after any mutation of the transaction.

Transaction.set_user(i: int, user: str) → None#

Set the user of the account in the post at index i.

Transaction.set_acct(i: int, acct: str | Account) → None#

Set the account of the post at index i.