GnuCash c935c2f+
Loading...
Searching...
No Matches
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-cli --quotes get $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
19from 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#
27FILE = "PATH_TO_YOUR_TEST_FILE"
28
29session = Session(FILE, True, False, False)
30
31root = session.book.get_root_account()
32book = session.book
33pdb = book.get_price_db()
34comm_table = book.get_table()
35gbp = comm_table.lookup("CURRENCY", "SOME_CURRENCY")
36arm = comm_table.lookup("NASDAQ", "SOME_STOCK")
37latest = pdb.lookup_latest(arm,gbp) # from the table, NOT live data
38value = latest.get_value()
39pl = pdb.get_prices(arm,gbp)
40for 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
47if len(pl) > 0:
48 v0 = pl[0].get_value()
49 print(arm.get_fullname(), float(v0.num) / float(v0.denom ))
50
51session.end()
52session.destroy()
53quit()