GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
priceDB_test.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Test file for price database stuff
3 # To update the price database call
4 # $PATH/gnucash --add-price-quotes $PATHTOFILE
5 # before running this.
6 # Adding to a calling bash script would be better
7 # Although calling it from here would be even better!
8 # OR: export PYTHONPATH=$HOME/progs/lib/python2.6/site-packages
9 # Then: gnucash-env ipython
10 # The account file is not saved but always use a disposable copy.
11 # Change, FILE, CURRENCY and STOCK to those defined in your test account.
12 
13 ## @file
14 # @brief Test file for price database stuff
15 # @author Mike Evans
16 # @ingroup python_bindings_examples
17 
18 from gnucash import Session
19 
20 # FILE should be the path to your existing gnucash file/database
21 # For a file, simply pass the pathname, for a database you can use
22 # these forms:
23 # mysql://user:password@host/dbname
24 # postgres://user:password@host[:port]/dbname (the port is optional)
25 #
26 FILE = "PATH_TO_YOUR_TEST_FILE" ## Fail is not saved but use a copy anyway
27 
28 session = Session(FILE, True, False, False)
29 
30 root = session.book.get_root_account()
31 book = session.book
32 pdb = book.get_price_db()
33 comm_table = book.get_table()
34 gbp = comm_table.lookup("CURRENCY", "SOME_CURRENCY")
35 arm = comm_table.lookup("NASDAQ", "SOME_STOCK")
36 latest = pdb.lookup_latest(arm,gbp) # from the table, NOT live data
37 value = latest.get_value()
38 pl = pdb.get_prices(arm,gbp)
39 for pr in pl:
40  source = pr.get_source()
41  time = pr.get_time()
42  v=pr.get_value()
43  price = float(v.num)/v.denom
44  print time, source, price
45 
46 if len(pl) > 0:
47  v0 = pl[0].get_value()
48  print arm.get_fullname(), float(v0.num) / float(v0.denom )
49 
50 session.end()
51 session.destroy()
52 quit()