-
Notifications
You must be signed in to change notification settings - Fork 2
/
seed.py
176 lines (124 loc) · 4.03 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from model import Move, Type_, Chain, Progression, Title
from model import connect_to_db, db
import doctest
def add_types(type_data):
"""Seed types from csv to database"""
print("Types")
Type_.query.delete()
for row in open(type_data):
row = row.rstrip()
row = row.split(",")
type_code = row[0]
min_repeats = row[1]
max_repeats = row[2]
type_ = Type_(type_code=type_code,
min_repeats=min_repeats,
max_repeats=max_repeats)
db.session.add(type_)
db.session.commit()
def add_moves(move_data):
"""Seed moves from csv to database"""
print("Moves")
Move.query.delete()
for row in open(move_data):
row = row.rstrip()
row = row.split(",")
move_code = row[0]
type_code = row[1]
move_name = row[2]
beats = row[3]
follows_move = row[6]
leads_move = row[7]
same_side = row[8]
move = Move(move_code=move_code,
type_code=type_code,
move_name=move_name,
beats=beats,
follows_move=follows_move,
leads_move=leads_move,
same_side=same_side
)
db.session.add(move)
db.session.commit()
def parse_csv(dance_data):
"""Parse csv (from Google Sheets).
>>> parse_csv("seed_data/dances.txt")[-1]
('0', ['nmrm', 'nswg', 'nswg', 'llfb', 'lal6', 'pbal', 'pswg', 'pswg', 'pswg', 'pprm', 'crl3'])
"""
dance_list = []
for row in (open(dance_data)):
row = row.split(",")
start = row[2]
dance = row[3]
dance = dance.replace("|", "")
dance = dance.rstrip()
dance = dance.split()
dance_list.append((start, dance))
return dance_list
def seed_prog(dance):
"""Takes dance as a list of moves and seeds progressions table.
"""
print("Progressions")
start = dance[0]
last = dance[1][-1]
first = dance[1][0]
try:
db.session.query(Progression).filter_by(last=last, first=first).one()
except:
progression = Progression(last=last, first=first, start=start)
db.session.add(progression)
db.session.commit()
def seed_chains(dance):
"""Takes dance as a list of moves and seeds chains table.
"""
print("Chains")
dance = dance[1]
print dance
i = 0
while i < (len(dance)-1):
key_ = dance[i]
value = dance[i+1]
try:
db.session.query(Chain).filter_by(key_=key_, value=value).one()
except:
chain = Chain(key_=key_, value=value)
db.session.add(chain)
db.session.commit()
i += 1
def seed_dances(dance_file):
"""Seed data from dances into progressions and chains tables.
Ensures that both processes are seeded when data is passed in.
TODO add tests!
"""
Progression.query.delete()
Chain.query.delete()
dances = parse_csv(dance_file)
for dance in dances:
seed_prog(dance)
seed_chains(dance)
def seed_titles(title_file):
"""Seed words from dance titles into Title table.
Very slow with lots of data - only run with good reason."""
for row in open(title_file):
row = row.strip()
if row.endswith(">"):
row = row.replace("<", ">")
row = row.split(">")
title = row[2]
title = title.split(" ")
for word in title:
try:
db.session.query(Title).filter_by(word=word).one()
except:
new_word = Title(word=word)
print new_word
db.session.add(new_word)
db.session.commit()
if __name__ == "__main__":
from server import app
connect_to_db(app)
doctest.testmod(verbose=False)
add_types("seed_data/types.txt")
add_moves("seed_data/moves.txt")
seed_dances("seed_data/dances.txt")
# seed_titles("seed_data/dance_names.txt")