Error calling account() in a define statement

26 views
Skip to first unread message

Michael Bryant

unread,
Oct 17, 2019, 7:02:27 PM10/17/19
to Ledger
Hi all,

I recently discovered that the version of ledger distributed on Termux for Android does not support Python 2 and they do not intend to add support until ledger supports Python 3.

Because of this, I decided to try replacing the one Python function I had with ledger built-in define statements. It's a little bit of a mess, but I'm finding that trying to use the account() function inside a define statement causes ledger to exit with "Error: Too few arguments to function."

Attached is a file with a minimal version of what I am trying to accomplish. Due to using the account() function, I am using the latest master branch version of ledger.

Is this a bug, or am I doing something wrong?

Michael Bryant
example.ledger

John Wiegley

unread,
Oct 18, 2019, 12:04:41 AM10/18/19
to Michael Bryant, Ledger
>>>>> "MB" == Michael Bryant <mbrya...@gmail.com> writes:

MB> Attached is a file with a minimal version of what I am trying to
MB> accomplish. Due to using the account() function, I am using the latest
MB> master branch version of ledger.

MB> Is this a bug, or am I doing something wrong?

The problem here, which ought to be considered a bug, is that "define" cannot
override the definition of "max", which is a built-in function taking two
arguments.

Once you change the name of max to something else, you'll hit another problem,
but maybe you'll know the answer to that one...

John

Michael Bryant

unread,
Oct 18, 2019, 4:23:05 AM10/18/19
to Ledger
That was indeed the problem. And you were right about me running into another issue.

In case it helps anyone else, the issue was getting an error like "Cannot multiply
a balance with an amount" and it was solved by simplifying my defined functions.
Turns out ledger supports ternary conditionals, so my defines went from

define bktmax = tag('BucketMax')
define pct = tag('IncomePercent')
define tax = tag('TaxRatio')
define budget_val = (a * -1 * (1 - tax) * pct * 0.8)
define bucket_remaining(act) = bktmax - account(act).total
define bucket_diff(act) = bucket_remaining(act) - budget_val
define bucket_filled(act) = (((abs(bucket_diff(act)) + bucket_diff(act)) / (2*(bucket_diff(act)))) * -1) + 1
define bucket_not_filled(act) = (bucket_filled(act) * -1) + 1
define post_amount(act) = (bucket_filled(act) * bucket_remaining(act)) + (bucket_not_filled(act) * budget_val)

To the following:

define bktmax = tag('BucketMax')
define pct = tag('IncomePercent')
define tax = tag('TaxRatio')
define budget_val = (a * -1 * (1 - tax) * pct * 0.8)
define bucket_remaining(act) = bktmax - account(act).total
define bucket_diff(act) = bucket_remaining(act) - budget_val
define post_amount(act) = (bucket_diff(act) < 0 ? bucket_remaining(act) : budget_val)

And the issue went away.
Reply all
Reply to author
Forward
0 new messages