Creating a new transaction programmatically

192 views
Skip to first unread message

joshua...@gmail.com

unread,
Sep 4, 2016, 11:58:48 AM9/4/16
to Beancount
Looking to get feedback on the proper way to create new transactions through a Python script. I'm working on the beginnings of a paycheck parser and am starting by making myself some useful wrappers, but I don't want to reinvent the wheel unnecessarily. Here is my sample code for making a new transaction and printing it:

import beancount.core.amount as amount

from beancount.core.data import Posting, Transaction
from beancount.parser import printer
from datetime import date

def new_posting(account, units=None, cost=None, price=None, flag=None, meta=None):
 
if units:
   units
= amount.from_string(units)

 
return Posting(account=account,
   units
=units,
   cost
=cost,
   price
=price,
   flag
=flag,
   meta
=meta)

def new_transaction(date, flag, postings, payee='', narration='', tags=None, links=None, meta=None):
 
return Transaction(date=date,
   flag
=flag,
   postings
=postings,
   payee
=payee,
   narration
=narration,
   tags
=tags,
   links
=links,
   meta
=meta)

postings
= []
postings
.append(new_posting('Assets:Some:Account', '100.00 USD'))
postings
.append(new_posting('Expenses:Some:Account'))

trans
= new_transaction(date.today(), '*', postings, narration='Test transaction')
print(printer.format_entry(trans))


There's some acrobatics required to make a new transaction, so the objective above was to simplify the process by providing defaults for the mostly optional parameters. Also, I hid the units magic when it comes to making new postings.

Anyways, I've spent some time combing through the source code, but I haven't given an extensive review of every file. With that being said, I just want to make sure I'm not reinventing helper functions that already exist. Any pointers?
Reply all
Reply to author
Forward
0 new messages