diff --git a/exposurejapan/database.py b/exposurejapan/database.py index ed9f6cff29075524455ff8a60f9f5e01cbdf3730..914a11918285909e6eebf4093265ecd2fad288da 100644 --- a/exposurejapan/database.py +++ b/exposurejapan/database.py @@ -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):