Creating a price conversion with total price, in code

66 views
Skip to first unread message

Red S

unread,
Jan 21, 2023, 5:21:50 AM1/21/23
to Beancount
I'm probably missing it, but how can I create via code (eg: an importer), a posting with a price conversion based on a total price like so: "20 EUR @@ 10 USD" ?

The definition for Posting here has a cost, where where a CostSpec can be passed in with the cost total. Perhaps I'm missing it, but I'm not seeing an analogue for a price total?

Daniele Nicolodi

unread,
Jan 21, 2023, 5:45:19 AM1/21/23
to bean...@googlegroups.com
On 21/01/2023 11:21, Red S wrote:
> I'm probably missing it, but how can I create via code (eg: an
> importer), a posting with a price conversion based on a total price like
> so: "20 EUR @@ 10 USD" ?

It cannot be done.

It is one of the things that will be resolved in Beancount 3 so there
hasn't been much pressure to fix it in Beancount 2. However, with the
work on version 3 stalled, I'm starting to think to resume actively
working on version 2.

Cheers,
Dan

Red S

unread,
Jan 21, 2023, 6:29:56 AM1/21/23
to Beancount
Good to know, thanks! 

Archimedes Smith

unread,
Jan 23, 2023, 7:33:46 PM1/23/23
to Beancount
As Dan said, this is not possible with the beancount v2 data model. It is however possible with autobean.refactor, which is not integrated with `beancount.ingest` or `beangulp` but is ready to use if all you are looking for is a way to structurally constructing beancount files and programmatically serializing them into text. Example:

```
import io
from autobean_refactor import models, printer

total_price = models.TotalPrice.from_value(decimal.Decimal(100), 'USD')
posting = models.Posting.from_value('Assets:Foo', decimal.Decimal(75), 'GBP', price=total_price)
txn = models.Transaction.from_value(datetime.date(2000, 1, 1), payee=None, narration=None, postings=[posting])
file = models.File.from_value([txn])

text = printer.print_model(file, io.StringIO()).getvalue()
```

I still haven't got around with the documentation so feel free to ping me for any questions.

Red S

unread,
Jan 23, 2023, 8:10:06 PM1/23/23
to Beancount
Excellent! The case I had in mind is in an importer, and thus wouldn't work because beancount.ingest doesn't support it, but nevertheless, it's great to know of a way to  programmatically construct and serialize. Thank you!
Reply all
Reply to author
Forward
0 new messages