Skip to content

Parse

Provides functions for parsing and exporting beancont types/models.

These functions are stricly helper functions in that parsing/exporting can be done directly by importing the appropriate model and accessing its parse/export method. However, most of these functions take any supported type/model as an input which results in less imports in your code.

export(model)

Exports a Model to its respective BeancountType.

Parameters:

Name Type Description Default
model Union[bdantic.models.directives.Balance, bdantic.models.directives.Close, bdantic.models.directives.Commodity, bdantic.models.directives.Custom, bdantic.models.directives.Document, bdantic.models.directives.Event, bdantic.models.directives.Note, bdantic.models.directives.Open, bdantic.models.directives.Pad, bdantic.models.directives.Price, bdantic.models.directives.Query, bdantic.models.directives.Transaction, bdantic.models.data.Amount, bdantic.models.data.Cost, bdantic.models.data.CostSpec, bdantic.models.data.Position, bdantic.models.directives.Posting, bdantic.models.directives.TxnPosting, bdantic.models.display.CurrencyContext, bdantic.models.display.DisplayContext, bdantic.models.display.Distribution, bdantic.models.data.Inventory, bdantic.models.realize.RealAccount]

A valid Model

required

Returns:

Type Description
Union[beancount.core.amount.Amount, beancount.core.data.Balance, beancount.core.data.Close, beancount.core.data.Open, beancount.core.position.Cost, beancount.core.position.CostSpec, beancount.core.data.Commodity, beancount.core.data.Custom, beancount.core.data.Document, beancount.core.data.Event, beancount.core.data.Note, beancount.core.data.Pad, beancount.core.position.Position, beancount.core.data.Posting, beancount.core.data.Price, beancount.core.data.Query, beancount.core.realization.RealAccount, beancount.core.data.Transaction, beancount.core.data.TxnPosting, beancount.core.display_context._CurrencyContext, beancount.core.display_context.DisplayContext, beancount.core.distribution.Distribution, beancount.core.inventory.Inventory]

The associated BeancountType for this Model

Source code in bdantic/parse.py
def export(model: Model) -> BeancountType:
    """Exports a Model to its respective BeancountType.

    Args:
        model: A valid Model

    Returns:
        The associated BeancountType for this Model
    """
    return model.export()

export_all(models)

Exports a list of Models into a list of their respective BeancountType.

Parameters:

Name Type Description Default
models Sequence[Union[bdantic.models.directives.Balance, bdantic.models.directives.Close, bdantic.models.directives.Commodity, bdantic.models.directives.Custom, bdantic.models.directives.Document, bdantic.models.directives.Event, bdantic.models.directives.Note, bdantic.models.directives.Open, bdantic.models.directives.Pad, bdantic.models.directives.Price, bdantic.models.directives.Query, bdantic.models.directives.Transaction, bdantic.models.data.Amount, bdantic.models.data.Cost, bdantic.models.data.CostSpec, bdantic.models.data.Position, bdantic.models.directives.Posting, bdantic.models.directives.TxnPosting, bdantic.models.display.CurrencyContext, bdantic.models.display.DisplayContext, bdantic.models.display.Distribution, bdantic.models.data.Inventory, bdantic.models.realize.RealAccount]]

A list of Models

required

Returns:

Type Description
List[Union[beancount.core.amount.Amount, beancount.core.data.Balance, beancount.core.data.Close, beancount.core.data.Open, beancount.core.position.Cost, beancount.core.position.CostSpec, beancount.core.data.Commodity, beancount.core.data.Custom, beancount.core.data.Document, beancount.core.data.Event, beancount.core.data.Note, beancount.core.data.Pad, beancount.core.position.Position, beancount.core.data.Posting, beancount.core.data.Price, beancount.core.data.Query, beancount.core.realization.RealAccount, beancount.core.data.Transaction, beancount.core.data.TxnPosting, beancount.core.display_context._CurrencyContext, beancount.core.display_context.DisplayContext, beancount.core.distribution.Distribution, beancount.core.inventory.Inventory]]

A list of associated BeancountType's for each model

Source code in bdantic/parse.py
def export_all(models: Sequence[Model]) -> List[BeancountType]:
    """Exports a list of Models into a list of their respective BeancountType.

    Args:
        models: A list of Models

    Returns:
        A list of associated BeancountType's for each model
    """
    return [export(model) for model in models]

parse(obj)

Parses a BeancountType into it's respective Model.

Parameters:

Name Type Description Default
obj Union[beancount.core.amount.Amount, beancount.core.data.Balance, beancount.core.data.Close, beancount.core.data.Open, beancount.core.position.Cost, beancount.core.position.CostSpec, beancount.core.data.Commodity, beancount.core.data.Custom, beancount.core.data.Document, beancount.core.data.Event, beancount.core.data.Note, beancount.core.data.Pad, beancount.core.position.Position, beancount.core.data.Posting, beancount.core.data.Price, beancount.core.data.Query, beancount.core.realization.RealAccount, beancount.core.data.Transaction, beancount.core.data.TxnPosting, beancount.core.display_context._CurrencyContext, beancount.core.display_context.DisplayContext, beancount.core.distribution.Distribution, beancount.core.inventory.Inventory]

A valid BeancountType

required

Returns:

Type Description
Union[bdantic.models.directives.Balance, bdantic.models.directives.Close, bdantic.models.directives.Commodity, bdantic.models.directives.Custom, bdantic.models.directives.Document, bdantic.models.directives.Event, bdantic.models.directives.Note, bdantic.models.directives.Open, bdantic.models.directives.Pad, bdantic.models.directives.Price, bdantic.models.directives.Query, bdantic.models.directives.Transaction, bdantic.models.data.Amount, bdantic.models.data.Cost, bdantic.models.data.CostSpec, bdantic.models.data.Position, bdantic.models.directives.Posting, bdantic.models.directives.TxnPosting, bdantic.models.display.CurrencyContext, bdantic.models.display.DisplayContext, bdantic.models.display.Distribution, bdantic.models.data.Inventory, bdantic.models.realize.RealAccount]

The associated Model for the given BeancountType

Source code in bdantic/parse.py
def parse(obj: BeancountType) -> Model:
    """Parses a BeancountType into it's respective Model.

    Args:
        obj: A valid BeancountType

    Returns:
        The associated Model for the given BeancountType
    """
    return type_map[type(obj)].parse(obj)  # type: ignore

parse_all(objs)

Parses a list of BeancountTypes's into a list of their respective Models.

Parameters:

Name Type Description Default
objs Sequence[Union[beancount.core.amount.Amount, beancount.core.data.Balance, beancount.core.data.Close, beancount.core.data.Open, beancount.core.position.Cost, beancount.core.position.CostSpec, beancount.core.data.Commodity, beancount.core.data.Custom, beancount.core.data.Document, beancount.core.data.Event, beancount.core.data.Note, beancount.core.data.Pad, beancount.core.position.Position, beancount.core.data.Posting, beancount.core.data.Price, beancount.core.data.Query, beancount.core.realization.RealAccount, beancount.core.data.Transaction, beancount.core.data.TxnPosting, beancount.core.display_context._CurrencyContext, beancount.core.display_context.DisplayContext, beancount.core.distribution.Distribution, beancount.core.inventory.Inventory]]

A list of valid BeancountType's

required

Returns:

Type Description
List[Union[bdantic.models.directives.Balance, bdantic.models.directives.Close, bdantic.models.directives.Commodity, bdantic.models.directives.Custom, bdantic.models.directives.Document, bdantic.models.directives.Event, bdantic.models.directives.Note, bdantic.models.directives.Open, bdantic.models.directives.Pad, bdantic.models.directives.Price, bdantic.models.directives.Query, bdantic.models.directives.Transaction, bdantic.models.data.Amount, bdantic.models.data.Cost, bdantic.models.data.CostSpec, bdantic.models.data.Position, bdantic.models.directives.Posting, bdantic.models.directives.TxnPosting, bdantic.models.display.CurrencyContext, bdantic.models.display.DisplayContext, bdantic.models.display.Distribution, bdantic.models.data.Inventory, bdantic.models.realize.RealAccount]]

A list of associated Models for each BeancountType

Source code in bdantic/parse.py
def parse_all(
    objs: Sequence[BeancountType],
) -> List[Model]:
    """Parses a list of BeancountTypes's into a list of their respective
    Models.

    Args:
        objs: A list of valid BeancountType's

    Returns:
        A list of associated Models for each BeancountType
    """
    return [parse(obj) for obj in objs]

parse_directives(entries)

Parses a list of directives into a Directives model.

Parameters:

Name Type Description Default
entries List[Union[beancount.core.data.Open, beancount.core.data.Close, beancount.core.data.Commodity, beancount.core.data.Pad, beancount.core.data.Balance, beancount.core.data.Transaction, beancount.core.data.Note, beancount.core.data.Event, beancount.core.data.Query, beancount.core.data.Price, beancount.core.data.Document, beancount.core.data.Custom]]

The list of directives as returned by the parser

required

Returns:

Type Description
Directives

A Directives instance

Source code in bdantic/parse.py
def parse_directives(entries: List[data.Directive]) -> Directives:
    """Parses a list of directives into a Directives model.

    Args:
        entries: The list of directives as returned by the parser

    Returns:
        A Directives instance
    """
    return Directives.parse(entries)

parse_loader(entries, errors, options)

Parses the result from calling the beancount loader to a BeancountFile.

Parameters:

Name Type Description Default
entries List[Union[beancount.core.data.Open, beancount.core.data.Close, beancount.core.data.Commodity, beancount.core.data.Pad, beancount.core.data.Balance, beancount.core.data.Transaction, beancount.core.data.Note, beancount.core.data.Event, beancount.core.data.Query, beancount.core.data.Price, beancount.core.data.Document, beancount.core.data.Custom]]

The entries return from the loader

required
errors List[Any]

The errors returned from a loader

required
options Dict[str, Any]

The options returned from a loder

required

Returns:

Type Description
BeancountFile

A BeancountFile model

Source code in bdantic/parse.py
def parse_loader(
    entries: List[data.Directive], errors: List[Any], options: Dict[str, Any]
) -> BeancountFile:
    """Parses the result from calling the beancount loader to a BeancountFile.

    Args:
        entries: The entries return from the loader
        errors: The errors returned from a loader
        options: The options returned from a loder

    Returns:
        A BeancountFile model
    """
    return BeancountFile.parse((entries, errors, options))

parse_query(query_result)

Parses the response from running query.run_query() on a list of entries.

Parameters:

Name Type Description Default
query_result Tuple[List[Tuple[str, Type]], List[Any]]

The query result to parse

required

Returns:

Type Description
QueryResult

A QueryResult model

Source code in bdantic/parse.py
def parse_query(
    query_result: Tuple[List[Tuple[str, Type]], List[Any]]
) -> QueryResult:
    """Parses the response from running query.run_query() on a list of entries.

    Args:
        query_result: The query result to parse

    Returns:
        A QueryResult model
    """
    return QueryResult.parse(query_result)