It works well, thank you very much!
Here is an example of code for exploring the full account tree with each
associated balance:
#!/usr/bin/env python3
from beancount import loader
from beancount.core import realization
from beancount.core.realization import compute_balance
from beancount.core.convert import convert_position
from beancount.core.number import ZERO
from beancount.core.prices import build_price_map
# 1. Load your file
file_path = '/opt/hpf/20250726/compta.bean'
entries, errors, options_map = loader.load_file(file_path)
main_currency = options_map['operating_currency'][0]
# 2. Build the realization tree & price map
tree = realization.realize(entries)
price_map = build_price_map(entries)
# 3. Compute the balance for a RealAccount instance
def compute_realacct_balance(racct):
global main_currency
global price_map
inv = compute_balance(racct) # Aggregate leaf and branch balances
:contentReference[oaicite:0]{index=0}
total = ZERO
for pos in inv:
converted = convert_position(pos, main_currency, price_map)
if converted and converted.currency == main_currency:
total += converted.number
print(f"{racct.account}: {total:.2f} {main_currency}")
# Main loop on the full account tree (without recursivity)
for acct in realization.iter_children(tree):
compute_realacct_balance(acct)
Le jeudi 31 juillet 2025 à 01:17:36 UTC+2, Red S a écrit :
> This prints the balance for the five top level accounts. Easily modified
> to print it for all the accounts:
> #!/usr/bin/env python3 from beancount import loader from beancount.core
> import realization from beancount.core.realization import compute_balance
> from beancount.core.convert import convert_position from
> beancount.core.number import ZERO from beancount.core.prices import
> build_price_map # 1. Load your file entries, errors, options_map =
> loader.load_file("main.bc") main_currency =
> options_map['operating_currency'][0] # 2. Build the realization tree &
> price map tree = realization.realize(entries) price_map =
> build_price_map(entries) # 3. For each top‑level account, compute its total
> (including all sub‑accounts) for acct_name, acct in tree.items(): inv =
> compute_balance(acct) # Aggregate leaf and branch balances
> :contentReference[oaicite:0]{index=0} print(f"→ {acct_name!r} has
> {len(inv)} positions: ") total = ZERO for pos in inv: converted =
> convert_position(pos, main_currency, price_map) if converted and
> converted.currency == main_currency: total += converted.number
> print(f"{acct_name}: {total:.2f} {main_currency}")
>
>
>
> On Wednesday, July 30, 2025 at 9:16:45 AM UTC-7 Red S wrote:
>
>> It’s been a while since I looked at this, but IIRC, you’re right in that
>> you’ll have to write a bit of python code. There isn’t a built in function,
>> and what Fava does is the closest reference.
>>
>> But it should be minor: for each account in the realization, you’ll want
>> to sum up inventories of all its children, and then call convert to convert
>> them to the main currency. Which IIRC is exactly what Fava does.
>>
>>
>>
>> On Tuesday, July 29, 2025 at 5:57:59 PM UTC-7 [email protected]
>> wrote:
>>
>>> Hello, sorry if this a trivial question.
>>>
>>> In Python, I am struggling to get the tree of all accounts and their
>>> final balance or position for the main currency, including the open account
>>> without any attached posting, such as the intermediate nodes whose balances
>>> are expected to be the sum of their subaccount balances.
>>>
>>> Realization only provides it for accounts with postings, Claude nor
>>> ChaGPT don’t help to solve this (probably not enough source code available
>>> for beancount v3), and looking at fava code, it seems they are
>>> re-calculating the positions.
>>>
>>> Is it possible to get it from the core data structure or do we have to
>>> use bean-query?
>>>
>>> Claude
>>>
>>
--
You received this message because you are subscribed to the Google Groups
"Beancount" 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/beancount/d36afbde-a557-43d9-81e7-c654d84ef35fn%40googlegroups.com.