This is very clever! Maybe there should be like a repo on github with these kinds of advanced tricks. Or at least a section in the manual where these can be gathered
On Wed, Dec 4, 2024 at 7:17 AM Tavis Ormandy <[email protected]> wrote: > Whenever I notice I've made a data entry error, I try to write an > assertion so that I'll catch any similar error in future. > > I recently accidentally wrote something like this: > > 2024/12/01 * Payment > Assets:Credit:Mastercard $10 > Assets:Credit:Mastercard > > That's a perfectly valid transaction, but obviously not what I intended! > > I guess I want to say "all transactions should have at least 2 different > accounts", so how would you encode that into an assertion? > > The obvious solution is post.any(account != post.account), but that > won't work because account will be re-evaluated each time. I thought > maybe you can save it outside the any(), like: > > "saved=account; post.any(account != saved)" > > But that won't work either... it will memoize the assignment and > re-evaluate that too. I did find a solution, you define an expression > and then use that within the any(). This is the final version I ended up > with: > > ; There should be at least two accounts or commodities per post. > = expr "lambda(_c,_n)=any(account != _n or commodity != > _c);!all(lambda(commodity, account)))" > assert has_tag('verified') > > This means that if there are not two accounts in a post, *or* you're > exchanging commodities within the same account, then raise an error > unless you tagged it :verified:... it looks like this: > > $ ledger bal > While applying automated transaction from "assert.ldg", lines 40-41: > > = expr "lambda(_c,_n)=any(account != _n or commodity != > _c);!all(lambda(commodity, account)))" > > assert has_tag('verified') > While extending transaction from "test.ldg", lines 631-633: > > 2024/12/01 * Payment > > Assets:Credit:Mastercard $10 > > Assets:Credit:Mastercard > Error: Transaction assertion failed: has_tag("verified") > > I dunno, just thought it was an unusual enough solution to mention :) > > Tavis. > > -- > _o) $ lynx lock.cmpxchg8b.com > /\\ _o) _o) $ finger [email protected] > _\_V _( ) _( ) @taviso > > -- > > --- > 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 visit > https://groups.google.com/d/msgid/ledger-cli/viprqq%242g0%241%40ciao.gmane.io > . > -- --- 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 visit https://groups.google.com/d/msgid/ledger-cli/CAMM1LKFcCjwzqdqg7YmRX%3D6Jc1ESjbqzFM16dV0Q9Z6%2BCR6bRA%40mail.gmail.com.
