Skip to content

Data

Provides models for the core beancount data types.

Amount (Base) pydantic-model

A model representing a beancount.core.amount.Amount.

Attributes:

Name Type Description
ty Literal['Amount']

A string literal identifying this model.

number Optional[decimal.Decimal]

The value of the amount.

currency Optional[str]

The amount currency.

Source code in bdantic/models/data.py
class Amount(Base):
    """A model representing a `beancount.core.amount.Amount`.

    Attributes:
        ty: A string literal identifying this model.
        number: The value of the amount.
        currency: The amount currency.
    """

    _sibling = amount.Amount

    ty: Literal["Amount"] = "Amount"
    number: Optional[Decimal]
    currency: Optional[Currency]

Cost (Base) pydantic-model

A model representing a beancount.core.position.Cost.

Attributes:

Name Type Description
ty Literal['Cost']

A string literal identifying this model.

number Decimal

The per-unit cost.

currency str

The cost currency.

date date

A date that the lot was created at.

label Optional[str]

An optional label for the lot.

Source code in bdantic/models/data.py
class Cost(Base):
    """A model representing a `beancount.core.position.Cost`.

    Attributes:
        ty: A string literal identifying this model.
        number: The per-unit cost.
        currency: The cost currency.
        date: A date that the lot was created at.
        label: An optional label for the lot.
    """

    _sibling = position.Cost

    ty: Literal["Cost"] = "Cost"
    number: Decimal
    currency: Currency
    date: datetime.date
    label: Optional[str]

CostSpec (Base) pydantic-model

A model representing a beancount.core.position.CostSpec.

Attributes:

Name Type Description
ty Literal['CostSpec']

A string literal identifying this model.

number_per Optional[decimal.Decimal]

The cost/price per unit.

number_total Optional[decimal.Decimal]

The total cost/price, or None if unspecified.

currency Optional[str]

The commodity of the amount.

date Optional[datetime.date]

A date for the lot.

label Optional[str]

An optional label for the lot.

merge Optional[bool]

True if this specification calls for averaging the units of this lot's currency, or False if unspecified.

Source code in bdantic/models/data.py
class CostSpec(Base):
    """A model representing a `beancount.core.position.CostSpec`.

    Attributes:
        ty: A string literal identifying this model.
        number_per: The cost/price per unit.
        number_total: The total cost/price, or None if unspecified.
        currency: The commodity of the amount.
        date: A date for the lot.
        label: An optional label for the lot.
        merge: True if this specification calls for averaging the units of this
            lot's currency, or False if unspecified.
    """

    _sibling = position.CostSpec

    ty: Literal["CostSpec"] = "CostSpec"
    number_per: Optional[Decimal]
    number_total: Optional[Decimal]
    currency: Optional[Currency]
    date: Optional[datetime.date]
    label: Optional[str]
    merge: Optional[bool]

Inventory (BaseList) pydantic-model

A model representing a beancount.core.inventory.Inventory.

A beancount inventory mimics a dictionary, but ultimately the data underlying it is a list of Positions. This model represents this fact by wrapping a list of Position models. It inherits basic list functionality and can be indexed/iterated over.

Source code in bdantic/models/data.py
class Inventory(BaseList):
    """A model representing a `beancount.core.inventory.Inventory`.

    A beancount inventory mimics a dictionary, but ultimately the data
    underlying it is a list of Positions. This model represents this fact by
    wrapping a list of [Position][bdantic.models.data.Position] models. It
    inherits basic list functionality and can be indexed/iterated over."""

    __root__: List[Position]

    @classmethod
    def parse(cls, obj: inventory.Inventory) -> Inventory:
        positions = [
            Position.parse(position) for position in obj.get_positions()
        ]
        return Inventory(__root__=positions)

    def export(self) -> inventory.Inventory:
        positions = [position.export() for position in self.__root__]
        return inventory.Inventory(positions=positions)

export(self)

Exports this model into it's associated beancount type

Returns:

Type Description
inventory.Inventory

A new instance of the beancount type

Source code in bdantic/models/data.py
def export(self) -> inventory.Inventory:
    positions = [position.export() for position in self.__root__]
    return inventory.Inventory(positions=positions)

parse(obj) classmethod

Parses a beancount type into this model

Parameters:

Name Type Description Default
obj inventory.Inventory

The Beancount type to parse

required

Returns:

Type Description
Inventory

A new instance of this model

Source code in bdantic/models/data.py
@classmethod
def parse(cls, obj: inventory.Inventory) -> Inventory:
    positions = [
        Position.parse(position) for position in obj.get_positions()
    ]
    return Inventory(__root__=positions)

Position (Base) pydantic-model

A model representing a beancount.core.position.Position.

Attributes:

Name Type Description
ty Literal['Position']

A string literal identifying this model.

units Amount

The number of units and its currency.

cost Optional[bdantic.models.data.Cost]

A Cost that represents the lot.

Source code in bdantic/models/data.py
class Position(Base):
    """A model representing a `beancount.core.position.Position`.

    Attributes:
        ty: A string literal identifying this model.
        units: The number of units and its currency.
        cost: A Cost that represents the lot.
    """

    _sibling = position.Position

    ty: Literal["Position"] = "Position"
    units: Amount
    cost: Optional[Cost]