Skip to content

Commit 841b64a

Browse files
committed
FIX: Create function to add indexes. Add additional indexes to sample db.
1 parent b054f89 commit 841b64a

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

faslr/samples/db/generate_sample_db.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
)
2828

2929
from faslr.utilities.sample import (
30-
XYZ_RATE_INDEX
30+
XYZ_RATE_INDEX,
31+
XYZ_TREND_INDEX,
32+
XYZ_TORT_INDEX
3133
)
3234

3335
from sqlalchemy.orm import sessionmaker
@@ -131,29 +133,44 @@
131133

132134
session.add_all(obj_list)
133135

134-
new_index = IndexTable(
135-
description=XYZ_RATE_INDEX['Name'][0],
136-
scope='Global'
137-
)
136+
def insert_index(
137+
index: dict,
138+
scope: str # 'Global' or 'Project'
139+
) -> None:
138140

139-
session.add(new_index)
140-
session.flush()
141+
new_index = IndexTable(
142+
description=index['Name'][0],
143+
scope=scope
144+
)
141145

142-
df_rate_changes = pd.DataFrame(data={'year': XYZ_RATE_INDEX['Origin'], 'change': XYZ_RATE_INDEX['Change']})
143-
df_rate_changes['index_id'] = new_index.index_id
146+
session.add(new_index)
147+
session.flush()
144148

145-
rate_change_list = df_rate_changes.to_dict('records')
149+
df_index = pd.DataFrame(data={'year': index['Origin'], 'change': index['Change']})
150+
df_index['index_id'] = new_index.index_id
146151

147-
obj_list = []
148-
for record in rate_change_list:
149-
data_obj = IndexValuesTable(**record)
150-
obj_list.append(data_obj)
152+
change_list = df_index.to_dict('records')
151153

152-
session.add_all(obj_list)
154+
obj_list = []
155+
for record in change_list:
156+
data_obj = IndexValuesTable(**record)
157+
obj_list.append(data_obj)
158+
159+
session.add_all(obj_list)
153160

154-
session.commit()
155161

162+
indexes_to_add = [
163+
XYZ_RATE_INDEX,
164+
XYZ_TREND_INDEX,
165+
XYZ_TORT_INDEX
166+
]
156167

168+
for index in indexes_to_add:
169+
insert_index(
170+
index=index,
171+
scope='Global'
172+
)
157173

174+
session.commit()
158175

159176
session.close()

0 commit comments

Comments
 (0)