Skip to main content

bdantic

A package for extending beancount with pydantic

See the docs for more details.

Installation

pip install bdantic

Usage

Parsing

A handful of functions are provided for parsing Beancount types, but the primary method supports parsing most core types:

import bdantic

from beancount.core import amount
from decimal import Decimal

amt = amount.Amount(number=Decimal(1.50), currency="USD"))
model = bdantic.parse(amt) # Produces a bdantic.models.Amount

Exporting

All models can be directly exported back to their native Beancount types by using their bult-in export method:

amt_export = model.export()
assert amt == amt_export

Ingesting

Functions are available for parsing common responses from interacting with the Beancount package. You can parse an entire Beancount file with the following:

import bdantic

from beancount import loader

# A bdantic.models.BeancountFile instance
bfile = bdantic.parse_loader(*loader.load_file("ledger.beancount"))
print(len(bfile.entries))

You can also parse the response from executing a query:

import bdantic

from beancount import loader
from beancount.query import query

entries, _, options = loader.load_file("ledger.beancount")

query = "SELECT date, narration, account, position"
result = query.run_query(entries, options, query)
parsed_result = bdantic.parse_query(result)

Or the result of running a realization:

import bdantic

from beancount.core import realization

entries, _, options = loader.load_file("ledger.beancount")

real = realization.realize(entries)
parsed_real = bdantic.parse(real)

Rendering

Perhaps the most powerful usage of bdantic is rendering beancount data into a more universal format like JSON. Since all models inherit from Pydantic they include full support for rendering their contents as JSON:

import bdantic

from beancount import loader

bfile = bdantic.parse_loader(*loader.load_file("ledger.beancount"))
js = bfile.json()
print(js) # Look ma, my beancount data in JSON!

The rendered JSON can be parsed back into the Beancount model that generated it:

from bdantic.models import BeancountFile

bfile = BeancountFile.parse_raw(js)

In additiona to JSON, the directive models can be rendered as valid Beancount syntax using the built-in syntax method:

from bdantic.models import Amount, Posting, Transaction
from datetime import date
from decimal import Decimal

txn = Transaction(
    date=date.today(),
    meta={},
    flag="*",
    payee="Home Depot",
    narration="Tools n stuff",
    tags=None,
    links=None,
    postings=[
        Posting(
            account="Assets:Bank:Cash",
            units=Amount(number=Decimal(-142.32), currency="USD"),
            cost=None,
            CostSpec=None,
            flag=None,
            meta={},
        ),
        Posting(
            account="Expenses:HomeDepot",
            units=Amount(number=Decimal(142.32), currency="USD"),
            cost=None,
            CostSpec=None,
            flag=None,
            meta={},
        ),
    ],
)

print(txn.syntax())

Testing

pytest .

Most tests make heavy use of hypothesis for generating test data to be used in the tests. Hypothesis automatically keeps a cache to speed up subsequent testing, however, the first time you run pytest you may experience longer than normal run times.

Additionally, many tests pull from the static.beancount file found in the testing folder. This was generated using the bean-example CLI tool and is used to verify models with a realistic ledger.

Contributing

Check out the issues for items needing attention or submit your own and then:

  1. Fork the repo
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Metadata

Release files for bdantic 0.2.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bdantic 0.2.6
File Size Uploaded Provenance Actions
bdantic-0.2.6.tar.gz 24.3 kB Without provenance Details for bdantic-0.2.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bdantic 0.2.6
File Interpreter ABI Platform Actions
bdantic-0.2.6-py3-none-any.whl Python 3 none any Details for bdantic-0.2.6-py3-none-any.whl

Total release size: 53.7 kB

Release files / bdantic-0.2.6.tar.gz

Download URL bdantic-0.2.6.tar.gz
Size 24.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b7a5bf98b5a14851d9c54c844abdd5042772501f32261cfecb8b87c3f9adeb6f
BLAKE2b-256 checksum
How to use checksums
acdc32af58ee8d0c5d66863b0d2e2a97a39fb00106a3204b1163e6c4c38144db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

Release files / bdantic-0.2.6-py3-none-any.whl

Download URL bdantic-0.2.6-py3-none-any.whl
Size 29.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
139b3ccb6c9ab64c3d1bb80c5eb32ea1ce07e90194876e93e7007fe7d7339c63
BLAKE2b-256 checksum
How to use checksums
0f0ee5034eff433eab1180027b6cbdbb1c0a237620aa84bc104f918374db938b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

Security for bdantic 0.2.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page