Hello,

I deal with multiple currencies and occasionally I want to do external
processing on my journal as if everything was kept in my native
currency. More specifically, I try to use the Python binding like
this:

    #!/usr/bin/python

    from __future__ import print_function
    import ledger

    journal = ledger.read_journal('test.ledger')
    for post in journal.query('-X RUB'):
        print(post.date, post.account, post.amount)

However, it breaks on this minimal example (test1.ledger attached):

    2019-02-09 payee 1
        Expenses:Foo       10 USD
        Assets:Bank card  -69.54 RUB

    2019-02-10 payee 2
        Expenses:Bar       10 USD
        Assets:Bank card  -70 RUB

Specifically, it prints the two postings of the first transaction,
then errors out with:

RuntimeError: Assertion failed in
"/build/ledger-h93drX/ledger-3.1.2~pre1+g3a00e1c+dfsg1/src/item.h",
line 181:virtual ledger::date_t ledger::item_t::date() const: _date

I tracked it down to there being a dummy transaction between days when
a currency price changes:

```
$ ledger print -f test1.ledger -X RUB
2019-02-09 payee 1
    Expenses:Foo                              10 USD
    Assets:Bank card                      -69.54 RUB

2019-02-10 Commodities revalued

2019-02-10 payee 2
    Expenses:Bar                              10 USD
    Assets:Bank card                      -70.00 RUB
```

That “Commodities revalued” transaction causes the .query iterator to
yield a ledger.JournalItem object instead of a ledger.Posting, and
asking that JournalItem for a date triggers an assertion.

I tried then to ignore any objects other than Posting:

    for post in journal.query('-X RUB'):
        if type(post) is ledger.Posting:
            print(post.date, post.account, post.amount)

It works on the simple two-transaction example, but, as the number of
transactions (or revaluations?) grows, at some point it just
segfaults. Nine transactions trigger the segfault for me (test.ledger
attached).

I also tried to work around the whole Ledger Python binding by
exporting XML. It also breaks on revaluations:

$ ledger xml -X RUB -f test.ledger
While handling posting from "/home/yuri/src/ledger/test.ledger", line 6:
>     Expenses:Bar                              10 USD
Error: Assertion failed in
"/build/ledger-h93drX/ledger-3.1.2~pre1+g3a00e1c+dfsg1/src/ptree.cc",
line 97:virtual void
ledger::format_ptree::operator()(ledger::post_t&):
post.xdata().has_flags(POST_EXT_VISITED)

What else can I try in order to get a stream of postings normalized to
a single currency? (`ledger reg -X RUB` works but I would very much
dislike to scrape its output.)

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"Ledger" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ledger-cli/CAP_d_8WwjLPaNNi5g5p5WDd%2B9JpwwVB28UXwKQu%3DVp%2BTBfbqzw%40mail.gmail.com.

Attachment: test.ledger
Description: Binary data

Attachment: test1.ledger
Description: Binary data

#!/usr/bin/python

from __future__ import print_function
import ledger

journal = ledger.read_journal('test1.ledger')
for post in journal.query('-X RUB'):
    print(post.date, post.account, post.amount)

Reply via email to