Cleaned up sql config
This commit is contained in:
@ -7,6 +7,7 @@ class SQL_Database:
|
|||||||
"""Main database class that controls all the functionality for input /
|
"""Main database class that controls all the functionality for input /
|
||||||
out of the database using SQLite3."""
|
out of the database using SQLite3."""
|
||||||
|
|
||||||
|
sql_types_dict = [int, float, str]
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.conn = None
|
self.conn = None
|
||||||
@ -92,19 +93,9 @@ class SQL_Database:
|
|||||||
else:
|
else:
|
||||||
print("Error! cannot create the database connection.")
|
print("Error! cannot create the database connection.")
|
||||||
|
|
||||||
|
|
||||||
def insert_config(self,ga):
|
def insert_config(self,ga):
|
||||||
"""Insert the configuration attributes into the config """
|
"""Insert the configuration attributes into the config."""
|
||||||
|
|
||||||
# If either function is None we handle the error
|
|
||||||
try:
|
|
||||||
chromosome_impl = ga.chromosome_impl.__name__
|
|
||||||
except:
|
|
||||||
chromosome_impl = 'None'
|
|
||||||
|
|
||||||
try:
|
|
||||||
gene_impl = ga.chromosome_impl.__name__
|
|
||||||
except:
|
|
||||||
gene_impl = 'None'
|
|
||||||
|
|
||||||
# Structure the insert data
|
# Structure the insert data
|
||||||
db_config_list = [
|
db_config_list = [
|
||||||
@ -125,33 +116,30 @@ class SQL_Database:
|
|||||||
ga.percent_converged,
|
ga.percent_converged,
|
||||||
ga.chromosome_mutation_rate,
|
ga.chromosome_mutation_rate,
|
||||||
ga.gene_mutation_rate,
|
ga.gene_mutation_rate,
|
||||||
ga.initialization_impl.__name__,
|
ga.initialization_impl,
|
||||||
ga.fitness_function_impl.__name__,
|
ga.fitness_function_impl,
|
||||||
ga.parent_selection_impl.__name__,
|
ga.parent_selection_impl,
|
||||||
ga.crossover_individual_impl.__name__,
|
ga.crossover_individual_impl,
|
||||||
ga.crossover_population_impl.__name__,
|
ga.crossover_population_impl,
|
||||||
ga.survivor_selection_impl.__name__,
|
ga.survivor_selection_impl,
|
||||||
ga.mutation_individual_impl.__name__,
|
ga.mutation_individual_impl,
|
||||||
ga.mutation_population_impl.__name__,
|
ga.mutation_population_impl,
|
||||||
ga.termination_impl.__name__,
|
ga.termination_impl,
|
||||||
ga.database_name
|
ga.database_name
|
||||||
]
|
]
|
||||||
|
|
||||||
# Clean up so the sqlite database accepts the data structure
|
# Clean up so the sqlite database accepts the data structure
|
||||||
for i in range(len(db_config_list)):
|
for i in range(len(db_config_list)):
|
||||||
if db_config_list[i] == None:
|
if callable(db_config_list[i]):
|
||||||
db_config_list[i] = "None"
|
db_config_list[i] = db_config_list[i].__name__
|
||||||
if db_config_list[i] == True:
|
elif type(db_config_list[i]) not in self.sql_types_dict:
|
||||||
db_config_list[i] = "True"
|
db_config_list[i] = str(db_config_list[i])
|
||||||
if db_config_list [i] == False:
|
|
||||||
db_config_list[i] = "False"
|
|
||||||
|
|
||||||
# For some reason it has to be in var = array(tuple()) form
|
# For some reason it has to be in var = array(tuple()) form
|
||||||
db_config_list = tuple(db_config_list)
|
db_config_list = [tuple(db_config_list)]
|
||||||
db_config_list = [db_config_list]
|
|
||||||
|
|
||||||
# Create sql query structure
|
# Create sql query structure
|
||||||
sql_start = '''INSERT INTO config (chromosome_length,
|
sql = f'''INSERT INTO config (chromosome_length,
|
||||||
population_size,
|
population_size,
|
||||||
chromosome_impl,
|
chromosome_impl,
|
||||||
gene_impl,
|
gene_impl,
|
||||||
@ -177,12 +165,7 @@ class SQL_Database:
|
|||||||
mutation_individual_impl,
|
mutation_individual_impl,
|
||||||
mutation_population_impl,
|
mutation_population_impl,
|
||||||
termination_impl,
|
termination_impl,
|
||||||
database_name) VALUES('''
|
database_name) VALUES({(',?'*len(db_config_list))[1:]}) '''
|
||||||
|
|
||||||
# Automate the value inputs
|
|
||||||
sql_middle = '?,' * (27 - 1)
|
|
||||||
sql_end = '?) '
|
|
||||||
sql = sql_start + sql_middle + sql_end
|
|
||||||
|
|
||||||
# Execute sql query
|
# Execute sql query
|
||||||
cur = self.conn.cursor()
|
cur = self.conn.cursor()
|
||||||
@ -190,6 +173,7 @@ class SQL_Database:
|
|||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
return cur.lastrowid
|
return cur.lastrowid
|
||||||
|
|
||||||
|
|
||||||
def query_all(self, query):
|
def query_all(self, query):
|
||||||
"""Query for muliple rows of data"""
|
"""Query for muliple rows of data"""
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user