GnuCash  4.11-243-g1cac132214+
priceDB_test.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
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=<path-to-gnucash-inst-dir>/lib/python3.7/site-packages:$PYTHONPATH
9 # You may have to adjust the above path to your local system (lib->lib64, python3.7->...)
10 # Then: ipython3
11 # The account file is not saved but always use a disposable copy.
12 # Change, FILE, CURRENCY and STOCK to those defined in your test account.
13 
14 
18 
19 from gnucash import Session
20 
21 # FILE should be the path to your existing gnucash file/database
22 # For a file, simply pass the pathname, for a database you can use
23 # these forms:
24 # mysql://user:password@host/dbname
25 # postgres://user:password@host[:port]/dbname (the port is optional)
26 #
27 FILE = "PATH_TO_YOUR_TEST_FILE"
28 
29 session = Session(FILE, True, False, False)
30 
31 root = session.book.get_root_account()
32 book = session.book
33 pdb = book.get_price_db()
34 comm_table = book.get_table()
35 gbp = comm_table.lookup("CURRENCY", "SOME_CURRENCY")
36 arm = comm_table.lookup("NASDAQ", "SOME_STOCK")
37 latest = pdb.lookup_latest(arm,gbp) # from the table, NOT live data
38 value = latest.get_value()
39 pl = pdb.get_prices(arm,gbp)
40 for pr in pl:
41  source = pr.get_source()
42  time = pr.get_time64()
43  v=pr.get_value()
44  price = float(v.num)/v.denom
45  print(time, source, price)
46 
47 if len(pl) > 0:
48  v0 = pl[0].get_value()
49  print(arm.get_fullname(), float(v0.num) / float(v0.denom ))
50 
51 session.end()
52 session.destroy()
53 quit()