-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
64 lines (51 loc) · 1.69 KB
/
db.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
62
63
64
from sqlalchemy import Column, String, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import config
def make_dict(row):
return {"text": row.text,
"food": row.food,
"service": row.service,
"ambiance": row.ambiance,
"price": row.price,
"location": row.location}
def fill(review, food=0, service=0, ambiance=0, price=0, location=0, marked=0):
s = session()
rows = s.query(Review).filter(Review.text == review).all()
if rows:
row = rows[0]
row.food = food
row.service = service
row.ambiance = ambiance
row.price = price
row.location = location
row.marked = marked
else:
review = Review(text=review,
food=food,
service=service,
ambiance=ambiance,
price=price,
location=location,
marked=marked)
s.add(review)
s.commit()
def load_reviews(labeled=True):
s = session()
rows = s.query(Review).filter(Review.marked == int(labeled)).all()
return [make_dict(r) for r in rows]
Base = declarative_base()
engine = create_engine("sqlite:///" + config.DB_PATH)
session = sessionmaker(bind=engine)
class Review(Base):
__tablename__ = "review"
id = Column(Integer, primary_key=True)
text = Column(String)
food = Column(Integer)
service = Column(Integer)
ambiance = Column(Integer)
price = Column(Integer)
location = Column(Integer)
marked = Column(Integer)
Base.metadata.create_all(bind=engine)