ascetic_ddd.seedwork.domain.values.money

Money pattern implementation.

This is the final version of the Money pattern from Kent Beck’s “Test Driven Development By Example” book.

The pattern provides: - Multi-currency support - Expression-based arithmetic operations - Currency conversion through a Bank - Composite pattern for complex expressions (Sum)

Example usage:
>>> from money import Money, Bank
>>>
>>> # Create money objects
>>> five_dollars = Money.dollar(5)
>>> ten_francs = Money.franc(10)
>>>
>>> # Set up a bank with exchange rates
>>> bank = Bank()
>>> bank.add_rate("CHF", "USD", 2)  # 2 CHF = 1 USD
>>>
>>> # Add different currencies
>>> sum_expr = five_dollars.plus(ten_francs)
>>> result = bank.reduce(sum_expr, "USD")
>>> print(result)  # 10 USD
>>>
>>> # Multiply
>>> result = Money.dollar(5).times(2)
>>> print(result)  # 10 USD
class ascetic_ddd.seedwork.domain.values.money.Expression[source]

Bases: ABC

Interface for monetary expressions.

This is the core abstraction that allows Money and Sum to be treated uniformly, enabling complex operations like adding different currencies.

abstractmethod plus(addend)[source]

Add another expression to this one.

Parameters:
addend

The expression to add

Return type:

Expression

Returns:

A new Expression representing the sum

abstractmethod reduce(bank, to)[source]

Reduce this expression to a Money object in the target currency.

Parameters:
bank

The bank to use for currency conversion

to

The target currency code

Return type:

Money

Returns:

Money object in the target currency

abstractmethod times(multiplier)[source]

Multiply this expression by a scalar.

Parameters:
multiplier

The multiplier

Return type:

Expression

Returns:

A new Expression representing the product

class ascetic_ddd.seedwork.domain.values.money.Money(amount, currency)[source]

Bases: Expression

Represents a monetary amount in a specific currency.

This is the fundamental class in the Money pattern, representing an amount with its associated currency.

Parameters:
amount : int

currency : Currency

property amount : int

Get the amount.

currency()[source]

Get the currency code.

Return type:

str

static dollar(amount)[source]

Create a Money object in USD.

Return type:

Money

Parameters:
amount : int

export(exporter)[source]
Return type:

None

Parameters:
exporter : IMoneyExporter

plus(addend)[source]

Add another expression to this Money.

Parameters:
addend

The expression to add

Return type:

Expression

Returns:

A Sum expression representing the addition

reduce(bank, to)[source]

Reduce this Money to the target currency.

Parameters:
bank

The bank to use for currency conversion

to

The target currency code

Return type:

Money

Returns:

Money object in the target currency

times(multiplier)[source]

Multiply this Money by a scalar.

Parameters:
multiplier

The multiplier

Return type:

Expression

Returns:

A new Money object with the multiplied amount

class ascetic_ddd.seedwork.domain.values.money.IMoneyExporter[source]

Bases: object

set_amount(value)[source]
Return type:

None

Parameters:
value : int

set_currency(value)[source]
Return type:

None

Parameters:
value : Currency

class ascetic_ddd.seedwork.domain.values.money.MoneyExporter[source]

Bases: IMoneyExporter

set_amount(value)[source]
Return type:

None

Parameters:
value : int

set_currency(value)[source]
Return type:

None

Parameters:
value : Currency

class ascetic_ddd.seedwork.domain.values.money.Sum(augend, addend)[source]

Bases: Expression

Represents the sum of two monetary expressions.

This class enables complex expressions like (5 USD + 10 CHF) + 20 EUR to be built up and then reduced to a single currency when needed.

Parameters:
augend : Expression

addend : Expression

plus(addend)[source]

Add another expression to this Sum.

Parameters:
addend

The expression to add

Return type:

Expression

Returns:

A new Sum expression

reduce(bank, to)[source]

Reduce this Sum to a Money object in the target currency.

This reduces both operands to the target currency and adds them.

Parameters:
bank

The bank to use for currency conversion

to

The target currency code

Return type:

Money

Returns:

Money object representing the sum in the target currency

times(multiplier)[source]

Multiply this Sum by a scalar.

This distributes the multiplication: (a + b) * n = a*n + b*n

Parameters:
multiplier

The multiplier

Return type:

Expression

Returns:

A new Sum expression with both operands multiplied

class ascetic_ddd.seedwork.domain.values.money.Bank[source]

Bases: object

Handles currency conversion using exchange rates.

The Bank maintains a table of exchange rates and provides the reduce operation to convert expressions to a target currency.

add_rate(from_currency, to_currency, rate)[source]

Add an exchange rate.

Parameters:
from_currency

The source currency code

to_currency

The target currency code

rate

The exchange rate (how many from units equal one to unit)

Return type:

None

rate(from_currency, to_currency)[source]

Get the exchange rate between two currencies.

Parameters:
from_currency

The source currency code

to_currency

The target currency code

Return type:

int

Returns:

The exchange rate. Returns 1 if converting to the same currency.

reduce(source, to)[source]

Reduce an expression to a Money object in the target currency.

Parameters:
source

The expression to reduce

to

The target currency code

Return type:

Money

Returns:

Money object in the target currency

class ascetic_ddd.seedwork.domain.values.money.Currency(value)[source]

Bases: str, Enum

to_symbol()[source]

Get the currency symbol

Return type:

str

export(setter)[source]
Return type:

None

Parameters:
setter : Callable[[str], None]

USD = 'USD'
EUR = 'EUR'
RUB = 'RUB'
GBP = 'GBP'

Modules

bank

currency

expression

money

money_exporter

sum