I've been trying out the Python interface to ledger.

My goal is to build something that worked like the "balance" cli
command, to get the total of a set of accounts.

First, it appears that journal.find_account('') doesn't find all
accounts available (does work when you specify an exact account name).
 However, journal.find_account_re('') does, and lets you iterate over
them.

account.amount doesn't act the same as post.amount - printing it
results in a '<bound method Account.amount of <ledger.Account object
at 0x108898830>>' rather than an amount.  I can't appear to add this
to a Balance object to convert it, and I'm not well versed enough in
the python internals to know how to proceed here.

Attempting to add amounts with different commodities results in an
Arithmetic Error.   Using a Balance to add multiple amounts together
works fine, but to get it to print out in a specific commodity type,
apparently you need to do something like this per the demo.py and docs
(much of what's in demo.py would be valid to put in the docs, if
revised/tested slightly):

--
com = ledger.commodities
usd = comms.find_or_create('$')

total = ledger.Balance
for post in journal.query(""):
    total += post.amount

print total.value(usd)
--

And, an extremely hokey and reverse order (totals on bottom) version
of 'balance':

--
def print_bal_tree(account, parent = ""):
    name = parent + ":" + account.name
    total = ledger.Balance()
    for post in account.posts():
        total += post.amount
    for subacct in account.accounts():
        total += print_bal_tree(subacct, name)
    print "%s > %s" % (name, total.value(usd))
    return total

for account in journal.find_account_re(''):
    print_bal_tree(account)
--

Overall, it's quite useful, if a bit rough around the edges.

- Zack

-- 

--- 
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].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to