/
seed.py
128 lines (86 loc) · 3.4 KB
/
seed.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import datetime
from sqlalchemy import func
from model import User, Food, Meal, connect_to_db, db
from server import app
def load_users(user_filename):
"""Load users from u.user into database."""
print("Users")
for i, row in enumerate(open(user_filename)):
row = row.rstrip()
user_id, email, password = row.split("|")
user = User(email=email,
password=password)
# We need to add to the session or it won't ever be stored
db.session.add(user)
# provide some sense of progress
if i % 100 == 0:
print(i)
# Once we're done, we should commit our work
db.session.commit()
def load_food(food_filename):
"""Load movies from u.item into database."""
print("Food")
for i, row in enumerate(open(food_filename)):
row = row.rstrip()
# clever -- we can unpack part of the row!
# movie_id, title, released_str, junk, imdb_url = row.split("|")[:5]
# The date is in the file as daynum-month_abbreviation-year;
# we need to convert it to an actual datetime object.
# if released_str:
# released_at = datetime.datetime.strptime(released_str, "%d-%b-%Y")
# else:
# released_at = None
# Remove the (YEAR) from the end of the title.
# title = title[:-7] # " (YEAR)" == 7
food = Food(name=name,
carbs=carbs)
# We need to add to the session or it won't ever be stored
db.session.add(food)
# provide some sense of progress
if i % 100 == 0:
print(i)
# Once we're done, we should commit our work
db.session.commit()
def load_meals(meals_filename):
"""Load ratings from u.data into database."""
print("Meals")
for i, row in enumerate(open(meals_filename)):
row = row.rstrip()
# user_id, movie_id, score, timestamp = row.split("\t")
# user_id = int(user_id)
# movie_id = int(movie_id)
# score = int(score)
# We don't care about the timestamp, so we'll ignore this
meal = Meal(user_id=user_id,)
# We need to add to the session or it won't ever be stored
db.session.add(meal)
# provide some sense of progress
if i % 1000 == 0:
print(i)
# An optimization: if we commit after every add, the database
# will do a lot of work committing each record. However, if we
# wait until the end, on computers with smaller amounts of
# memory, it might thrash around. By committing every 1,000th
# add, we'll strike a good balance.
db.session.commit()
# Once we're done, we should commit our work
db.session.commit()
def set_val_user_id():
"""Set value for the next user_id after seeding database"""
# Get the Max user_id in the database
result = db.session.query(func.max(User.user_id)).one()
max_id = int(result[0])
# Set the value for the next user_id to be max_id + 1
query = "SELECT setval('users_user_id_seq', :new_id)"
db.session.execute(query, {'new_id': max_id + 1})
db.session.commit()
if __name__ == "__main__":
connect_to_db(app)
db.create_all()
user_filename = "seed_data/u.user"
food_filename = ""
meal_filename = ""
load_users(user_filename)
load_foods(food_filename)
load_meals(meal_filename)
set_val_user_id()