autobean.refactor gist

117 views
Skip to first unread message

Red S

unread,
Jun 29, 2023, 6:11:52 PM6/29/23
to Beancount
Vanguard at some point started repeating their memo strings in some of their ofx files, so they look like this: 'DIVIDEND REINVESTMENTDIVIDEND REINVESTMENT'.

I added a fix in beancount_reds_importers. However, this has been going on for a while, and I wanted to go fix my beancount files from prior imports. While there are several simple ways to accomplish this, I used autobean.refactor since it has the highest level understanding of beancount files, and because I've been using it for other things.

Note that I don't use a single beancount file, but one per per account.

Since this is a good hello-world-ish example, I thought I'd share my code to help others kickstart their reformats/refactors. Here's the gist. Most of it is stolen from autobean.format here.

Archimedes Smith

unread,
Jun 30, 2023, 9:40:00 AM6/30/23
to Beancount
Author of autobean-refactor here. Glad to see it being useful!

Besides, there is now some documentation here: https://autobean-refactor.readthedocs.io/.

Red S

unread,
Jun 30, 2023, 3:42:17 PM6/30/23
to Beancount

I noticed already and I’ve been using that documentation :). I’m finding both the documentation and code to be great: clean, easily understood, well thought out, and behaves exactly as expected. Thank you for sharing!

A couple thoughts:

  • are you open to accepting minor fixes to the documentation? Eg: _print_model(file) wasn’t defined in the examples page
  • an idea to consider: turning class _FilesFormatter into an iterator and/or a context manager and including it in the base project (autobean_refactor) might allow people to avoid having to write boilerplate code, and simply write a couple lines of python code to accomplish what they want quickly

Archimedes Smith

unread,
Jun 30, 2023, 5:26:12 PM6/30/23
to Beancount
> are you open to accepting minor fixes to the documentation? Eg: _print_model(file) wasn’t defined in the examples page

Fixed in 3b31cf47. Issues and PRs are also very welcomed!

> an idea to consider: turning class _FilesFormatter into an iterator and/or a context manager and including it in the base project (autobean_refactor) might allow people to avoid having to write boilerplate code, and simply write a couple lines of python code to accomplish what they want quickly

That's a very good idea. Added in c308958b and documented here. There was actually an undocumented parse_file_recursive created for autobean-format but ended up not being used as the latter needs the original text to support the diff function.

Red S

unread,
Jun 30, 2023, 8:37:22 PM6/30/23
to Beancount
That was quick, and looks great, thank you :). Will send PRs for these types of things henceforth.

Red S

unread,
Jul 1, 2023, 4:36:41 AM7/1/23
to Beancount

For the benefit of anyone following along: with the new context manager, the example gist above becomes nice and simple:

#!/usr/bin/env python3 from autobean_refactor import editor, models e = editor.Editor() def format_double_payee(file): """ A brokerage at some point started repeating their memo strings in some of their ofx files, so they look like this: 'DIVIDEND REINVESTMENTDIVIDEND REINVESTMENT'. This function fixes that. """ for d in file.directives: if isinstance(d, models.Transaction): if d.payee: if d.payee[:int(len(d.payee)/2)] == d.payee[int(len(d.payee)/2):]: d.payee = d.payee[:int(len(d.payee)/2)] with e.edit_file_recursive('/home/user/accounts/main.bc') as files: for f in files: print(f"Processing: {f}") format_double_payee(files[f])
Reply all
Reply to author
Forward
0 new messages