piecash.core.book module¶
-
class
piecash.core.book.
Book
(root_account=None, root_template=None)[source]¶ Bases:
piecash._declbase.DeclarativeBaseGuid
A Book represents a GnuCash document. It is created through one of the two factory functions
create_book()
andopen_book()
.Canonical use is as a context manager like (the book is automatically closed at the end of the with block):
with create_book() as book: ...
Note
If you do not use the context manager, do not forget to close the session explicitly (
book.close()
) to release any lock on the file/DB.The book puts at disposal several attributes to access the main objects of the GnuCash document:
# to get the book and the root_account ra = book.root_account # to get the list of accounts, commodities or transactions for acc in book.accounts: # or book.commodities or book.transactions # do something with acc # to get a specific element of these lists EUR = book.commodities(namespace="CURRENCY", mnemonic="EUR") # to get a list of all objects of some class (even non core classes) budgets = book.get(Budget) # or a specific object budget = book.get(Budget, name="my first budget")
You can check a session has changes (new, deleted, changed objects) by getting the
book.is_saved
property. To save or cancel changes, usebook.save()
orbook.cancel()
:# save a session if it is no saved (saving a unchanged session is a no-op) if not book.is_saved: book.save()
-
root_account
¶ the root account of the book
-
root_template
¶ the root template of the book (usage not yet clear…)
-
default_currency
¶ the currency of the root account (=default currency of the book)
-
session
¶ the sqlalchemy session encapsulating the book
- Type
sqlalchemy.orm.session.Session
-
control_mode
¶ list of allowed non-standard operations like : “allow-root-subaccounts”
-
counter_customer
¶ counter for
piecash.business.person.Customer
id (link to slot “counters/gncCustomer”)- Type
-
counter_vendor
¶ counter for
piecash.business.person.Vendor
id (link to slot “counters/gncVendor”)- Type
-
counter_employee
¶ counter for
piecash.business.person.Employee
id (link to slot “counters/gncEmployee”)- Type
-
counter_invoice
¶ counter for
piecash.business.invoice.Invoice
id (link to slot “counters/gncInvoice”)- Type
-
counter_bill
¶ counter for
piecash.business.invoice.Bill
id (link to slot “counters/gncBill”)- Type
-
counter_exp_voucher
¶ counter for
piecash.business.invoice.Invoice
id (link to slot “counters/gncExpVoucher”)- Type
-
counter_order
¶ counter for
piecash.business.invoice.Order
id (link to slot “counters/gncOrder”)- Type
-
business_company_phone
¶ phone number of book company (link to slit “options/Business/Company Phone Number”)
- Type
-
business_company_email
¶ email of book company (link to slit “options/Business/Company Email Address”)
- Type
-
business_company_contact
¶ contact person of book company (link to slit “options/Business/Company Contact Person”)
- Type
-
business_company_address
¶ address of book company (link to slit “options/Business/Company Address”)
- Type
-
business_company_website
¶ website URL of book company (link to slit “options/Business/Company Website URL”)
- Type
-
static
track_dirty
(session, flush_context, instances)[source]¶ Record in session._all_changes the objects that have been modified before each flush
-
trading_account
(cdty)[source]¶ Return the trading account related to the commodity. If it does not exist and the option “Use Trading Accounts” is enabled, create it on the fly
-
add
(obj)[source]¶ Add an object to the book (to be used if object not linked in any way to the book)
-
property
is_saved
¶ Are all the changes saved to the file/DB?
You can check a session has changes (new, deleted, changed objects) by getting the
book.is_saved
property.
-
close
()[source]¶ Close a session. Any changes not yet saved are rolled back. Any lock on the file/DB is released.
-
get
(cls, **kwargs)[source]¶ Generic getter for a GnuCash object in the GncSession. If no kwargs is given, it returns the list of all objects of type cls (uses the sqlalchemy session.query(cls).all()). Otherwise, it gets the unique object which attributes match the kwargs (uses the sqlalchemy session.query(cls).filter_by(**kwargs).one() underneath):
# to get the first account with name="Income" inc_account = session.get(Account, name="Income") # to get all accounts accs = session.get(Account)
-
property
transactions
¶ gives easy access to all transactions in the book through a
piecash.model_common.CallableList
ofpiecash.core.transaction.Transaction
-
property
splits
¶ gives easy access to all splits in the book through a
piecash.model_common.CallableList
ofpiecash.core.transaction.Split
-
property
accounts
¶ gives easy access to all accounts in the book through a
piecash.model_common.CallableList
ofpiecash.core.account.Account
-
property
commodities
¶ gives easy access to all commodities in the book through a
piecash.model_common.CallableList
ofpiecash.core.commodity.Commodity
-
property
invoices
¶ gives easy access to all commodities in the book through a
piecash.model_common.CallableList
ofpiecash.core.commodity.Commodity
-
property
currencies
¶ gives easy access to all currencies in the book through a
piecash.model_common.CallableList
ofpiecash.core.commodity.Commodity
-
property
prices
¶ gives easy access to all prices in the book through a
piecash.model_common.CallableList
ofpiecash.core.commodity.Price
-
property
customers
¶ gives easy access to all commodities in the book through a
piecash.model_common.CallableList
ofpiecash.business.people.Customer
-
property
vendors
¶ gives easy access to all commodities in the book through a
piecash.model_common.CallableList
ofpiecash.business.people.Vendor
-
property
employees
¶ gives easy access to all commodities in the book through a
piecash.model_common.CallableList
ofpiecash.business.people.Employee
-
property
taxtables
¶ gives easy access to all commodities in the book through a
piecash.model_common.CallableList
ofpiecash.business.tax.Taxtable
-
property
query
¶ proxy for the query function of the underlying sqlalchemy session
-
splits_df
(additional_fields=None)[source]¶ Return a pandas DataFrame with all splits (
piecash.core.commodity.Split
) from the book- Parameters
- Returns
pandas.DataFrame
-
prices_df
()[source]¶ Return a pandas DataFrame with all prices (
piecash.core.commodity.Price
) from the book- Returns
pandas.DataFrame
-