On Thu, May 2, 2019 at 4:26 AM Florian Lindner <[email protected]> wrote:
> Hello, > > I would like to create a balance assertion > > YYYY-MM-DD balance Account Amount > > program,atically: > https://aumayr.github.io/beancount-docs-static/api_reference/beancount.core.html#beancount.core.data.Balance > > The parameter description in the API docs is only "Alias for field X". The > source code reveals some more information: > > # Attributes: > # meta: See above. > # date: See above. > # account: A string, the account whose balance to check at the given > date. > # amount: An Amount, the number of units of the given currency you're > # expecting 'account' to have at this date. > # diff_amount: None if the balance check succeeds. This value is set to > # an Amount instance if the balance fails, the amount of the > difference. > # tolerance: A Decimal object, the amount of tolerance to use in the > # verification. > Balance = new_directive('Balance', [ > ('account', Account), > ('amount', Amount), > ('tolerance', Optional[Decimal]), > ('diff_amount', Optional[Amount])]) > > > Just omitting the optional arguments doesn't work and gives a TypeError. > Python Namedtyples don't come with defaults in their constructor. I've been meaning to add this eventually - by inheriting and providing such a constructor. It could make the entire codebase a little simpler and less error-prone, it's a bit annoying to always use the full set of positional arguments. The tags and links empty sets could be handled there automatically as well. Using this piece of code: > > value = desc.split(" ")[-1] # value = "1.522,43H" > sign = 1 if value[-1] == "H" else -1 > units = sign * amount.Amount(D(value[:-1].replace(".", "").replace(",", > ".")), "EUR") > print(units) > txn = data.Balance(meta, > datetime.datetime.strptime(row["Datum/Zeit"], "%d.%m.%Y > %H:%M").date(), > account = self.file_account(), > amount = units, > tolerance = D(0), diff_amount = amount > ) > > gives: > > (Decimal('12415.79'), 'EUR') > > Traceback (most recent call last): > File "/usr/bin/bean-extract", line 4, in <module> > from beancount.ingest.extract import main; main() > File "/usr/lib/python3.7/site-packages/beancount/ingest/extract.py", > line 257, in main > return scripts_utils.trampoline_to_ingest(sys.modules[__name__]) > File > "/usr/lib/python3.7/site-packages/beancount/ingest/scripts_utils.py", line > 174, in trampoline_to_ingest > return run_import_script_and_ingest(parser) > File > "/usr/lib/python3.7/site-packages/beancount/ingest/scripts_utils.py", line > 224, in run_import_script_and_ingest > return ingest(importers_list) > File > "/usr/lib/python3.7/site-packages/beancount/ingest/scripts_utils.py", line > 116, in ingest > detect_duplicates_func=detect_duplicates_func) > File "/usr/lib/python3.7/site-packages/beancount/ingest/extract.py", > line 252, in run > detect_duplicates_func=detect_duplicates_func) > File "/usr/lib/python3.7/site-packages/beancount/ingest/extract.py", > line 218, in extract > print_extracted_entries(new_entries, output) > File "/usr/lib/python3.7/site-packages/beancount/ingest/extract.py", > line 140, in print_extracted_entries > entry_string = printer.format_entry(entry) > File "/usr/lib/python3.7/site-packages/beancount/parser/printer.py", > line 339, in format_entry > return EntryPrinter(dcontext, render_weights)(entry) > File "/usr/lib/python3.7/site-packages/beancount/parser/printer.py", > line 117, in __call__ > method(obj, oss) > File "/usr/lib/python3.7/site-packages/beancount/parser/printer.py", > line 254, in Balance > amount=entry.amount.to_string(self.dformat), > AttributeError: 'tuple' object has no attribute 'to_string' > > when attached to the list of statements that is returned from the > extract() function. > > Also, I am obviosuly still having some problems crasping beancounts type > system, esp. with optional parameters, like creating a transaction: > > txn = data.Transaction(meta, date, "*", payee, desc, set(), set(), [ > data.Posting("Assets:Checking", units, None, None, > None, None), > data.Posting("Expenses:Unknown", None, None, None, > None, None)]) > > took me some time to find out, that I need to pass set() to some empty > arguments, None to others. > > My questions: > > * How can I programmatically create a balance assertion? > Provide all the position arguments. > > > * How do the parameters diff_amount and tolerance reflect in the produced > statements? The balance statement does not seem to have these fields. > They're only rendered in journals in bean-web. > > * What is the meaning of the diff_amount? > If the balance fails, that's the difference between the computed and expected amount (enabled reporting errors without having to recompute the balance - not sure I like this design choice much). * And finally: Is there something I don't understand regarding beancount's > API? So far, it feels a bit unpythonic. > Python provides a class construct, but it's not without problems. Many experienced programmers don't use them (and many junior programmers insist on using them when they really don't need them). Classes encourage mutable state and side-effects, and that makes code harder to maintain. You don't need classes. I avoid them almost always. I prefer to use tuples and as a matter of convention avoid mutating them. It's definitely not unPythonic to do that. Thanks a lot, > Florian > > -- > 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 post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/beancount/f2123ceb-8a2a-40ca-ae56-f7b0527e413b%40googlegroups.com > <https://groups.google.com/d/msgid/beancount/f2123ceb-8a2a-40ca-ae56-f7b0527e413b%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- 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 post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/CAK21%2BhPM2fc9%3DiwOj42ySUt3Gi%3DwbF3-Kt3oNk3bf%3DSsCnREKA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
