Skip to content

journal

Journal entries and journal for recording accounting transactions.

Classes:

  • CompoundEntry

    A compound journal entry that can affect multiple accounts.

  • Journal

    Class to record all journal entries.

  • JournalEntry

    Abstract base class for a journal entry.

  • SimpleEntry

    A simple journal entry affecting exactly two accounts.

CompoundEntry dataclass

CompoundEntry(
    debit_accounts: dict[TAccount, float],
    credit_accounts: dict[TAccount, float],
    date: date | None = None,
    description: str = "",
)

Bases: JournalEntry

A compound journal entry that can affect multiple accounts.

Used when a transaction touches more than two accounts. The total debits must equal total credits (validated on creation).

Example: Closing a trading income account with sub-accounts::

+----------------------------------------------+
| Description: Closing Trading Income          |
+----------------------------------------------+
| Debit:  Unrealized Gain ......... $5,000     |
| Debit:  Realized Gain ........... $3,000     |
| Credit: Income Summary .......... $6,000     |
| Credit: Unrealized Loss ......... $2,000     |
+----------------------------------------------+

Methods:

  • credit_account_value_pairs

    Return an iterator over credit account and value pairs.

  • debit_account_value_pairs

    Return an iterator over debit account and value pairs.

  • involves_account

    Check if the journal entry involves a specific account.

  • is_balanced

    Check if the compound entry is balanced.

  • reversed

    Return a new CompoundEntry with debit and credit accounts swapped.

  • total_credits

    Calculate the total credits for the compound entry.

  • total_debits

    Calculate the total debits for the compound entry.

  • validate

    Assert sum of debit entries equals sum of credit entries.

credit_account_value_pairs

credit_account_value_pairs() -> (
    Iterator[tuple[TAccount, float]]
)

Return an iterator over credit account and value pairs.

debit_account_value_pairs

debit_account_value_pairs() -> (
    Iterator[tuple[TAccount, float]]
)

Return an iterator over debit account and value pairs.

involves_account

involves_account(account: TAccount) -> bool

Check if the journal entry involves a specific account.

is_balanced

is_balanced() -> bool

Check if the compound entry is balanced.

Using 1e-6 as a tolerance level to account for floating-point inaccuracies.

reversed

reversed() -> CompoundEntry

Return a new CompoundEntry with debit and credit accounts swapped.

total_credits

total_credits() -> float

Calculate the total credits for the compound entry.

total_debits

total_debits() -> float

Calculate the total debits for the compound entry.

validate

validate() -> None

Assert sum of debit entries equals sum of credit entries.

Journal dataclass

Journal(entries: list[JournalEntry] = list())

Class to record all journal entries.

Methods:

add_entry

add_entry(entry: JournalEntry) -> None

Add a journal entry to the journal.

get_entries_by_account

get_entries_by_account(
    account: TAccount,
) -> list[JournalEntry]

Get all journal entries involving a specific account.

get_entries_by_date

get_entries_by_date(date: date) -> list[JournalEntry]

Get all journal entries for a specific date.

get_entries_within_date_range

get_entries_within_date_range(
    start_date: date, end_date: date
) -> list[JournalEntry]

Get all journal entries within a specific date range.

JournalEntry

Bases: ABC

Abstract base class for a journal entry.

Methods:

credit_account_value_pairs

credit_account_value_pairs() -> (
    Iterator[tuple[TAccount, float]]
)

Return an iterator over credit account and value pairs.

debit_account_value_pairs

debit_account_value_pairs() -> (
    Iterator[tuple[TAccount, float]]
)

Return an iterator over debit account and value pairs.

involves_account abstractmethod

involves_account(account: TAccount) -> bool

Check if the journal entry involves a specific account.

reversed abstractmethod

reversed() -> JournalEntry

Return a new entry with debits and credits swapped (reversing entry).

SimpleEntry dataclass

SimpleEntry(
    debit_account: TAccount,
    credit_account: TAccount,
    value: float,
    date: date | None = None,
    description: str = "",
)

Bases: JournalEntry

A simple journal entry affecting exactly two accounts.

Example: Receiving a $10,000 deposit::

+----------------------------------------------+
| Date: 2024-01-15                             |
| Description: Customer deposit received       |
+----------------------------------------------+
| Debit:  Cash ..................... $10,000    |
| Credit: Deposits ................ $10,000    |
+----------------------------------------------+

Methods:

credit_account_value_pairs

credit_account_value_pairs() -> (
    Iterator[tuple[TAccount, float]]
)

Return an iterator over credit account and value pairs.

debit_account_value_pairs

debit_account_value_pairs() -> (
    Iterator[tuple[TAccount, float]]
)

Return an iterator over debit account and value pairs.

involves_account

involves_account(account: TAccount) -> bool

Check if the journal entry involves a specific account.

reversed

reversed() -> SimpleEntry

Return a new SimpleEntry with debit and credit accounts swapped.