/
analysis.py
61 lines (49 loc) · 1.63 KB
/
analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import sqlite3
import json
from datetime import datetime
from pyexcel_ods import get_data, save_data
def create_db_connection():
return sqlite3.connect(':memory:')
def fill_transactions(db, ods_file):
c = db.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS transactions (
year INTEGER,
month INTEGER,
day INTEGER,
vendor TEXT,
credit REAL,
debit REAL,
account TEXT,
category TEXT
)""")
transactionData = get_data(ods_file)
for _, sheet in transactionData.items():
for row in sheet[1:]:
if len(row) == 0:
continue
d = datetime.strptime(row[0], '%Y-%m-%d')
c.execute('INSERT INTO transactions VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
(d.year, d.month, d.day, row[1], row[2], row[3], row[4], row[5]))
db.commit()
def fill_taxonomy(db, json_file):
c = db.cursor()
c.execute(
"""CREATE TABLE IF NOT EXISTS full_taxonomy (
id TEXT,
parent TEXT,
name TEXT,
spend INTEGER,
budget INTEGER
)""")
c.execute(
'CREATE VIEW IF NOT EXISTS taxonomy AS SELECT * FROM full_taxonomy WHERE spend = 1')
with open(json_file) as json_file:
data = json.load(json_file)
for key, value in data.items():
c.execute('INSERT INTO full_taxonomy VALUES (?, ?, ?, ?, ?)',
(key,
value.get('parent', None),
value.get('name'),
1 if value.get('spend', True) else 0,
value.get('budget', None)))
db.commit()