On Monday, July 13, 2015 at 1:38:44 AM UTC+5:30, Martin Blais wrote:
>
> 2015-07-11
>
>   - WARNING: API CHANGES AHEAD. If you wrote plugins, you will need to 
> remove
>     the 'entry' parameter to constructors of Posting().
>
>     I removed the Posting.entry back-reference of Postings to their owning
>     Transaction objects. This makes programmatically creating our data
>     structures much more straight-forward and avoids us having to run 
> pirouettes
>     around which object to create first. It also removes all manners of 
> "fixing
>     up" transactions and removes one ultimatley unnecessary degree of 
> freedom
>     from plugins outputs: we don't have to check that the plugins generate
>     objects with correct back-references. It also makes the documentation
>     simpler: I won't have to explain backreferences and how to deal with 
> them.
>     Finally, it avoids the circular reference which could potentially make
>     Python's GC do some work for no other reason.
>
>     This happened because it occurred to me that the only really important 
> place
>     where back-references were used were in the beancount.core.realization 
> code,
>     and that in the RealAccount objects I could easily replace the Posting
>     instances in lists of "postings or entries" into a new tuple object
>     "TxnPosting" that contains a reference to the parent transaction and a
>     reference to the posting. This voids the need for a back-reference, and
>     makes the API really straightforward.
>


Hi Martin,
Quick question: so now, it is best practice to keep track of the 
transaction when iterating through postings? In other words:
---------------------

Old code:
---------------
# replace the account on a given posting with a new account                 
                
def account_replace(posting, new_account):                                 
                                                       
    # create a new posting with the new account, then remove old and add 
new                
    # from parent transaction                                               
                
    new_posting = posting._replace(account=new_account)                     
                
    new_posting.entry.postings.remove(posting)                             
              
    new_posting.entry.postings.append(new_posting)   

matches = [p for t in zerosum_txns for p in t.postings if p.account == 
"Expenses:Blah"]
account_replace(matches[0], new_account)


New code:
---------------
def account_replace(txn, posting, new_account):                             
             
    new_posting = posting._replace(account=new_account)                     
             
    txn.postings.remove(posting)                                           
                                                       
    txn.postings.append(new_posting)                                       
              
                                     
matches = [(p, t) for t in zerosum_txns for p in t.postings if p.account == 
"Expenses:Blah"]
account_replace(matches[0][1], matches[0][0], new_account)

---------------------

Does this look right, or should I be iterating through transactions in some 
other way (using TxnPosting, perhaps)?

Thanks

-- 

--- 
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/d/optout.

Reply via email to