Announce: bean-sort

155 views
Skip to first unread message

Simon Guest

unread,
Jun 16, 2025, 8:24:00 PM6/16/25
to bean...@googlegroups.com
I made a naive sorting utility for Beancount.  I hunted for such a thing and failed to find one, which surprised me.

Anyway, it was quicker to write a few lines of Python than keep wondering whether or why this hadn't been done before.

Here in case it helps anyone else.


cheers,
Simon

Simon Guest

unread,
Jun 16, 2025, 9:56:26 PM6/16/25
to bean...@googlegroups.com
And now the repo is public. 🤭

Paul Walker

unread,
Jun 17, 2025, 1:58:37 AM6/17/25
to Beancount
Interesting! I had my own need for this recently, but with a requirement to "fix" currency columns/spacing. The script gets pretty short if you leverage some internal methods, but this also ends up stripping comments and filling in empty/inferred posting amounts. It looks like yours would preserve more context by more or less splitting on date. I ended up with this plus some messy logic to regextract my fava currency-column option:

import sys
from beancount.loader import load_file
from beancount.parser import printer
from beancount.scripts import format

def main(file):
    entries, errors, options = load_file(file)

    entries_sorted = sorted(entries, key=lambda x: x.date)

    # Or if you don't need formatting, just printer.print_entries(entries_sorted, file=file)
    with open(file, 'w') as f:
        # Enumerate to get "last" entry to skip extra inter-directive newline
        for i, entry in enumerate(entries_sorted):
            printed = printer.format_entry(entry)
            # Align the printed entry with a currency column, set or grab from options (e.g. Fava)
            formatted = format.align_beancount(printed, currency_column=61)
            f.write(formatted)
            if i != len(entries_sorted) - 1:
                f.write('\n')

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python bean-sort.py <beancount-file>")
        sys.exit(1)
    main(sys.argv[1])

Thanks for sharing!
Paul

Brian Lalor

unread,
Jun 17, 2025, 9:11:37 AM6/17/25
to bean...@googlegroups.com
I’ve found autobean-format to be extremely useful. https://github.com/SEIAROTg/autobean-format#examples
— 
Brian Lalor (he/him)

--
You received this message because you are subscribed to the Google Groups "Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beancount+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/beancount/4dc9dbe6-31f8-4ed3-909b-9bf053b48b94n%40googlegroups.com.

Martin Blais

unread,
Jun 17, 2025, 11:46:09 AM6/17/25
to bean...@googlegroups.com
Added to contrib doc. Thanks for sharing!


--
You received this message because you are subscribed to the Google Groups "Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beancount+...@googlegroups.com.

Red S

unread,
Jun 18, 2025, 2:49:45 AM6/18/25
to Beancount
Same here. OP,  wondering if you came across SEIAROTg's repo below, and if you did, if it didn't meet your needs?

Simon Guest

unread,
Jun 19, 2025, 1:43:50 AM6/19/25
to bean...@googlegroups.com
Ah, thanks for the pointers to autobean-format, that does indeed do what I need.  Should have asked first, heh heh.

I will add a deprecation notice to my repo and a reference and recommendation to that instead.

cheers,
Simon

Simon Guest

unread,
Jun 19, 2025, 2:12:20 AM6/19/25
to bean...@googlegroups.com
Also, users of Nix may want to track the outcome of this.
Reply all
Reply to author
Forward
0 new messages