Commit 1b383f67 authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Optimized the code for handling the lists

parent e20e8c74
Pipeline #21724 passed with stage
in 1 minute and 54 seconds
......@@ -25,8 +25,29 @@ import pandas
logger = logging.getLogger(__name__)
def add_element_and_get_index(element, element_list):
"""
Checks if an element is in a list and adds it to the list if not.
Returns the index of the element in the list.
Args:
element (str):
Element to be added to the list
element_list (list):
List to add the element to
Returns:
Index of inserted element in the list
"""
if element not in element_list:
element_list.append(element)
return element_list.index(element)
class JapanDatabase(SpatialiteDatabase):
"""The JapanDatabase class represents a Spatialite database for the Japan
"""
The JapanDatabase class represents a Spatialite database for the Japan
exposure model. It is derived from the generic Database class.
Args:
......@@ -314,17 +335,13 @@ class JapanDatabase(SpatialiteDatabase):
continue
district_id = result[0]
# Get ID of building type
building_type = row["Type of building"]
if building_type not in building_type_list:
building_type_list.append(building_type)
building_type_id = building_type_list.index(building_type)
# Get ID of number of stories
story_number = row["Stories of building"]
if story_number not in story_number_list:
story_number_list.append(story_number)
story_number_id = story_number_list.index(story_number)
# Get ID of building type and number of stories
building_type_id = add_element_and_get_index(
row["Type of building"], building_type_list
)
story_number_id = add_element_and_get_index(
row["Stories of building"], story_number_list
)
# Insert dwelling numbers for each construction material
for material_id in range(5):
......@@ -362,17 +379,13 @@ class JapanDatabase(SpatialiteDatabase):
continue
district_id = result[0]
# Get ID of building type
building_type = row["Type of building"]
if building_type not in building_type_list:
building_type_list.append(building_type)
building_type_id = building_type_list.index(building_type)
# Get ID of number of stories
story_number = row["Stories of building"]
if story_number not in story_number_list:
story_number_list.append(story_number)
story_number_id = story_number_list.index(story_number)
# Get ID of building type and number of stories
building_type_id = add_element_and_get_index(
row["Type of building"], building_type_list
)
story_number_id = add_element_and_get_index(
row["Stories of building"], story_number_list
)
# Insert building numbers for each construction material
for material_index in range(3):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment