Minimal ledger implementation

I've used PTA to model simple examples in a micro economy with a Python/Streamlit application:

Intro video:

Central bank example 1:

Central bank example 2:

Minimal ledger

I've factored out the essence of the minimal ledger I used to model the above. It's now available here:

Core : the essence of the ledger

The core of the implementation is only 44 lines:

@dataclass
class Entry:
    description: str
    amount: Decimal

    def clone(self) -> 'Entry':
        return Entry(self.description, self.amount)

@dataclass
class Transaction:
    date: str
    description: str
    entries: list[Entry]

    def clone(self) -> 'Transaction':
        return Transaction(self.date, self.description, [entry.clone() for entry in self.entries])

@dataclass
class Ledger:
    transactions: List[Transaction] = field(default_factory=list)
    
    def entries_gen(self) -> Generator[Entry, None, None]:
        return (
            entry 
            for transaction in self.transactions 
            for entry       in transaction.entries
        )

    def entries(self) -> List[Entry]:
        return list(self.entries_gen())

    def clone(self) -> 'Ledger':
        return Ledger([transaction.clone() for transaction in self.transactions])
    
def add_transaction(ledger, date, description, *entries):

    entries_ = [Entry(acc, amt) for acc, amt in zip(entries[::2], entries[1::2])]

    ledger.transactions.append(Transaction(date, description, entries_))

It would be interesting to see how much more complex it would be if support for ledger-style commodities were added.

history of balances

The history_of_balances function shows you how the balance sheets change over time.

Notes

If anyone knows of any other "minimal" PTA implementations, I'd love to hear about them.

Thanks!

1 Like

Here's another very small implementation of PTA:

Announced here:

https://www.reddit.com/r/plaintextaccounting/s/by6XO6xrtV