In [1]:
import piecash
import datetime
In [2]:
# open a SQLAlchemy session linked to the test.gnucash file (as sqlite3 saved Book)
s = piecash.connect_to_gnucash_book("test.gnucash", readonly=False)
In [3]:
# retrieve the single Book object from the session (this is a sqlalchemy standard call)
book = s.query(piecash.Book).one()
# retrieve the EUR currency
EUR = s.query(piecash.Commodity).one()
In [4]:
# from the book, retrieve the root account and display its children accounts
book.root_account.children
Out[4]:
[Account<Assets>,
 Account<Liabilities>,
 Account<Income>,
 Account<Expenses>,
 Account<Equity>]
In [5]:
# retrieve the standard 3 default assets accounts (checking account, saving account, cash in wallet)
curacc, savacc, cash = book.root_account.children[0].children[0].children
In [6]:
# check splits (they should be empty if the GnuCash book was an empty Book)
savacc.splits, curacc.splits
Out[6]:
([], [])
In [7]:
# create a transaction of 45 € from the saving  account to the checking account
tr = piecash.Transaction.single_transaction(datetime.date.today(),datetime.date.today(), "transfer of money", 45, EUR, savacc, curacc)
In [8]:
# check some attributes of the transaction
tr.description, tr.splits
Out[8]:
('transfer of money',
 [<Split Account<Assets:Current Assets:Savings Account> -45>,
  <Split Account<Assets:Current Assets:Checking Account> 45>])
In [9]:
# check the splits from the accounts point of view
savacc.splits, curacc.splits
Out[9]:
([<Split Account<Assets:Current Assets:Savings Account> -45>],
 [<Split Account<Assets:Current Assets:Checking Account> 45>])
In [10]:
# rollback the session (i.e. undo all changes)
s.rollback()
In [11]:
# check splits after the rollback (they should be unchanged)
savacc.splits, curacc.splits
Out[11]:
([], [])