Added connection getter and setter

This commit is contained in:
danielwilczak101
2020-11-21 23:04:07 -05:00
parent d8974ddb1c
commit 056d67306b
3 changed files with 50 additions and 42 deletions

View File

@ -154,6 +154,25 @@ class Attributes:
# Getter and setters for all required varibles # Getter and setters for all required varibles
@property
def database_name(self):
"""Getter function for the database name"""
return self._database_name
@database_name.setter
def database_name(self, value_input):
"""Setter function with error checking for the database name"""
# Update the database class of the name change
self.database.database_name = value_input
# Set the name in the ga attribute
self._database_name = value_input
@property @property
def chromosome_length(self): def chromosome_length(self):
"""Getter function for chromosome length""" """Getter function for chromosome length"""

View File

@ -12,6 +12,7 @@ class SQL_Database:
def __init__(self): def __init__(self):
self.conn = None self.conn = None
self.config_id = None self.config_id = None
self.database_name = 'database.db'
def get_current_config(self): def get_current_config(self):
@ -30,13 +31,13 @@ class SQL_Database:
return 'TEXT' return 'TEXT'
def create_connection(self, db_file): def create_connection(self):
"""Create a database connection to the SQLite database """Create a database connection to the SQLite database
specified by db_file.""" specified by db_file."""
conn = None conn = None
try: try:
conn = sqlite3.connect(db_file) conn = sqlite3.connect(self.database_name)
return conn return conn
except Error as e: except Error as e:
print(e) print(e)
@ -119,7 +120,7 @@ class SQL_Database:
pass pass
# Create the database connection # Create the database connection
self.conn = self.create_connection(ga.database_name) self.conn = self.create_connection()
if self.conn is not None: if self.conn is not None:
# Create data table # Create data table
@ -197,19 +198,17 @@ class SQL_Database:
return query_data[0] return query_data[0]
def past_runs(self,database_name = 'database.db'): def past_runs(self):
"""Show a summerization of the past runs that the user has done.""" """Show a summerization of the past runs that the user has done."""
self.conn = self.create_connection(database_name)
query_data = self.query_all(f"SELECT id,generation_goal,chromosome_length FROM config;") query_data = self.query_all(f"SELECT id,generation_goal,chromosome_length FROM config;")
print(query_data) print(query_data)
def get_generation_total_fitness(self,config_id = None,database_name = 'database.db'): def get_generation_total_fitness(self,config_id = None):
"""Get each generations total fitness sum from the database """ """Get each generations total fitness sum from the database """
if config_id == None: config_id = self.config_id if config_id == None: config_id = self.config_id
self.conn = self.create_connection(database_name)
query_data = self.query_all(f"SELECT SUM(fitness) FROM data WHERE config_id={config_id} GROUP BY generation;") query_data = self.query_all(f"SELECT SUM(fitness) FROM data WHERE config_id={config_id} GROUP BY generation;")
# Format the fitness data into one list # Format the fitness data into one list
@ -218,22 +217,21 @@ class SQL_Database:
return formated_query_data return formated_query_data
def get_total_generations(self,config_id = None,database_name = 'database.db'): def get_total_generations(self,config_id = None):
"""Get the total generations from the database""" """Get the total generations from the database"""
if config_id == None: config_id = self.config_id if config_id == None: config_id = self.config_id
self.conn = self.create_connection(database_name)
query_data = self.query_one_item(f"SELECT COUNT(DISTINCT generation) FROM data WHERE config_id={config_id};") query_data = self.query_one_item(f"SELECT COUNT(DISTINCT generation) FROM data WHERE config_id={config_id};")
return query_data return query_data
def get_highest_chromosome(self,config_id = None,database_name = 'database.db'): def get_highest_chromosome(self,config_id = None):
"""Get the highest fitness of each generation""" """Get the highest fitness of each generation"""
if config_id == None: config_id = self.config_id if config_id == None: config_id = self.config_id
self.conn = self.create_connection(database_name)
query_data = self.query_all(f"SELECT fitness, max(fitness) FROM data WHERE config_id={config_id} GROUP by generation;") query_data = self.query_all(f"SELECT fitness, max(fitness) FROM data WHERE config_id={config_id} GROUP by generation;")
# Format the fitness data into one list # Format the fitness data into one list
@ -242,12 +240,11 @@ class SQL_Database:
return formated_query_data; return formated_query_data;
def get_lowest_chromosome(self,config_id = None,database_name = 'database.db'): def get_lowest_chromosome(self,config_id = None):
"""Get the lowest fitness of each generation""" """Get the lowest fitness of each generation"""
if config_id == None: config_id = self.config_id if config_id == None: config_id = self.config_id
self.conn = self.create_connection(database_name)
query_data = self.query_all(f"SELECT fitness, min(fitness) FROM data WHERE config_id={config_id} GROUP by generation;") query_data = self.query_all(f"SELECT fitness, min(fitness) FROM data WHERE config_id={config_id} GROUP by generation;")
# Format the fitness data into one list # Format the fitness data into one list
@ -256,35 +253,28 @@ class SQL_Database:
return formated_query_data; return formated_query_data;
# Getters and setter for class @property
def get_config_id(self): def conn(self):
"""Getter function for conn"""
try: # Return if the connection has already been set
# return the config id if its already set if self._conn is not None:
self.config_id = "something" return self._conn
except:
# config_id and config table dont exist: Tell the user
print("""You are required to run a ga before you
can connect to the database. Run ga.evolve()""")
def get_conn(self):
"""Conn """
try:
# Return if the connection has already been set
return self.conn
except:
else:
# If the connection has not been set yet
try: try:
# Check if you can connect to the database named in ga # Check if you can connect to the database
self.conn = self.create_connection(ga.database_name) self._conn = self.create_connection()
return self.conn
except: except:
# if the connection # if the connection doesnt exist then print error
print("""You are required to run a ga before you raise Exception("""You are required to run a ga before you
can connect to the database. Run ga.evolve()""") can connect to the database. Run ga.evolve() or ga.active()""")
return
@conn.setter
def conn(self, value_input):
"""Setter function for conn"""
# Set the name in the ga attribute
self._conn = value_input

View File

@ -9,6 +9,5 @@ ga.evolve()
ga.database.past_runs() ga.database.past_runs()
ga.graph.highest_value_chromosome(1) # Change this so it doesn't make its own figure or show ga.graph.highest_value_chromosome(1) # Change this so it doesn't make its own figure or show
ga.graph.show() ga.graph.show()