forked from pianiel/food-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
42 lines (29 loc) · 775 Bytes
/
main.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
#!/usr/bin/env python
from sqlalchemy import *
import json
datafile = "debug20.json"
user = 'postgres'
# password = ''
host = 'localhost'
dbname = 'food'
dbstring = 'postgresql://' + user + ':' + password + '@' + host + '/' + dbname
def fetchjson():
with open(datafile) as f:
lines = [json.loads(line) for line in f]
# print lines[0]['ingredients']
print len(lines), 'records read'
print lines[0]
return lines
def populate(records):
print len(records), 'records to populate'
db = create_engine(dbstring)
metadata = MetaData(db)
recipes = Table("recipes", metadata, autoload=True)
run(recipes.select())
def run(stmt):
rs = stmt.execute()
for row in rs:
print row
if __name__ == '__main__':
records = fetchjson()
populate(records)