-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_oxalate_db.py
More file actions
81 lines (72 loc) · 2.36 KB
/
Copy pathbuild_oxalate_db.py
File metadata and controls
81 lines (72 loc) · 2.36 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Populate oxalate.db from oxalate_source_data.py.
Run once (or to refresh after updating source data):
python build_oxalate_db.py
The generated oxalate.db is committed to the repo so end users
do not need to run this script.
"""
import pathlib
import sqlite3
import sys
from oxalate_source_data import OXALATE_FOODS, SOURCE_URL, SOURCE_CREDIT, SOURCE_DATE
DB_PATH = pathlib.Path(__file__).parent / "oxalate.db"
def build() -> None:
DB_PATH.unlink(missing_ok=True)
conn = sqlite3.connect(DB_PATH)
try:
conn.execute("""
CREATE TABLE oxalate_foods (
id INTEGER PRIMARY KEY AUTOINCREMENT,
food_group TEXT NOT NULL,
food_name TEXT NOT NULL,
serving_size TEXT,
oxalate_mg_per_serving REAL,
oxalate_mg_per_100g REAL,
category TEXT,
directly_measured INTEGER DEFAULT 0,
source_note TEXT
)
""")
conn.execute("""
CREATE TABLE source_info (
key TEXT PRIMARY KEY,
value TEXT
)
""")
conn.executemany(
"INSERT INTO source_info VALUES (?, ?)",
[
("url", SOURCE_URL),
("credit", SOURCE_CREDIT),
("date", SOURCE_DATE),
],
)
rows = [
(
f["group"],
f["food_name"],
f["serving_size"],
f["oxalate_mg_per_serving"],
f["oxalate_mg_per_100g"],
f["category"],
1 if f["directly_measured"] else 0,
SOURCE_CREDIT,
)
for f in OXALATE_FOODS
]
conn.executemany(
"""INSERT INTO oxalate_foods
(food_group, food_name, serving_size, oxalate_mg_per_serving,
oxalate_mg_per_100g, category, directly_measured, source_note)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
rows,
)
conn.commit()
print(f"Built {DB_PATH} — {len(rows)} foods inserted.")
except Exception:
conn.rollback()
raise
finally:
conn.close()
if __name__ == "__main__":
build()
sys.exit(0)