Package: bdantic
bdanticA package for extendingbeancountwithpydanticSee thedocsfor more details.InstallationpipinstallbdanticUsageParsingA handful of functions are provided for parsing Beancount types, but the primary
method supports parsing most core types:importbdanticfrombeancount.coreimportamountfromdecimalimportDecimalamt=amount.Amount(number=Decimal(1.50),currency="USD"))model=bdantic.parse(amt)# Produces a bdantic.models.AmountExportingAll models can be directly exported back to their native Beancount types by
using their bult-inexportmethod:amt_export=model.export()assertamt==amt_exportIngestingFunctions are available for parsing common responses from interacting with the
Beancount package. You can parse an entire Beancount file with the following:importbdanticfrombeancountimportloader# A bdantic.models.BeancountFile instancebfile=bdantic.parse_loader(*loader.load_file("ledger.beancount"))print(len(bfile.entries))You can also parse the response from executing a query:importbdanticfrombeancountimportloaderfrombeancount.queryimportqueryentries,_,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:importbdanticfrombeancount.coreimportrealizationentries,_,options=loader.load_file("ledger.beancount")real=realization.realize(entries)parsed_real=bdantic.parse(real)RenderingPerhaps the most powerful usage ofbdanticis rendering beancount data into a
more universal format like JSON. Since all models inherit fromPydanticthey
include full support for rendering their contents as JSON:importbdanticfrombeancountimportloaderbfile=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:frombdantic.modelsimportBeancountFilebfile=BeancountFile.parse_raw(js)In additiona to JSON, the directive models can be rendered as valid Beancount
syntax using the built-insyntaxmethod:frombdantic.modelsimportAmount,Posting,TransactionfromdatetimeimportdatefromdecimalimportDecimaltxn=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())Testingpytest.Most tests make heavy use ofhypothesisfor generating test data to be
used in the tests. Hypothesis automatically keeps a cache to speed up subsequent
testing, however, the first time you runpytestyou may experience longer than
normal run times.Additionally, many tests pull from thestatic.beancountfile found in the
testing folder. This was generated using thebean-exampleCLI tool and is used
to verify models with a realistic ledger.ContributingCheck out theissuesfor items needing attention or submit your own and
then:Fork the repoCreate your feature branch (git checkout -b feature/fooBar)Commit your changes (git commit -am 'Add some fooBar')Push to the branch (git push origin feature/fooBar)Create a new Pull Request