From c92cb3dacdfb656ec3b8843e2fab59f50e6b763c Mon Sep 17 00:00:00 2001 From: Stephan Philips <s.g.j.philips@tudelft.nl> Date: Sat, 6 Jun 2020 22:36:21 +0200 Subject: [PATCH] testing db stugg --- core_tools.egg-info/SOURCES.txt | 6 +- core_tools/db_tools/db_help_funtions.py | 46 +++++++++++++ core_tools/db_tools/parameter_management.py | 76 +++++++++++++++++++++ 3 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 core_tools/db_tools/db_help_funtions.py create mode 100644 core_tools/db_tools/parameter_management.py diff --git a/core_tools.egg-info/SOURCES.txt b/core_tools.egg-info/SOURCES.txt index 57af58f2..87e3eea8 100644 --- a/core_tools.egg-info/SOURCES.txt +++ b/core_tools.egg-info/SOURCES.txt @@ -1,3 +1,4 @@ +README.md setup.py core_tools/__init__.py core_tools.egg-info/PKG-INFO @@ -34,13 +35,8 @@ core_tools/GUI/param_viewer/param_viewer_GUI_window.py core_tools/HVI/__init__.py core_tools/HVI/charge_stability_diagram/HVI_charge_stability_diagram.py core_tools/HVI/charge_stability_diagram/__init__.py -core_tools/HVI/charge_stability_diagram_fast/HVI_charge_stability_diagram.py -core_tools/HVI/charge_stability_diagram_fast/__init__.py core_tools/HVI/single_shot_exp/HVI_single_shot.py core_tools/HVI/single_shot_exp/__init__.py -core_tools/HVI/single_shot_exp_SD_corr/HVI_single_shot.py -core_tools/HVI/single_shot_exp_SD_corr/__init__.py -core_tools/HVI/single_shot_exp_SD_corr/generate_SD_corr_sequence.py core_tools/sweeps/__init__.py core_tools/sweeps/pulse_lib_sweep.py core_tools/sweeps/Modulated_scans/DEMOD_tests.py diff --git a/core_tools/db_tools/db_help_funtions.py b/core_tools/db_tools/db_help_funtions.py new file mode 100644 index 00000000..c0af38e8 --- /dev/null +++ b/core_tools/db_tools/db_help_funtions.py @@ -0,0 +1,46 @@ +from dataclasses import dataclass + +@dataclass +class column_prop: + field_name : str + data_type : str + null : str + key : str + default : str + extra : str + privileges : str = None + comment : str = None + + __eq__(self, other): + if isinstance(other, string): + if self.field_name==other: + return True + raise ValueError("comparison not type {} not supported.".format(type(other))) + + + +def checkTableExists(cursor, table_name): + ''' + check is a table exists in the db (expected to be selected before) + + Args: + cursor (): + table_name (str) : name of the table to check + ''' + cursor.execute("SELECT COUNT(*) FROM information_schema.tables WHERE table_name = %s", (table_name, )) + + if cursor.fetchone()[0]: + return True + + return False + +def getColumnNames(cursor, table_name): + ''' + get call the column names out of the db + + Args: + cursor (): + table_name (str) : name of the table to check + ''' + cursor.execute("SHOW COLUMNS FROM {}".format(table_name)) + return cursor.fetchall() \ No newline at end of file diff --git a/core_tools/db_tools/parameter_management.py b/core_tools/db_tools/parameter_management.py new file mode 100644 index 00000000..d454f600 --- /dev/null +++ b/core_tools/db_tools/parameter_management.py @@ -0,0 +1,76 @@ +''' +classes that automate the saving an collection of data of a parameter on the database +''' +from qcodes import Parameter, Instrument +from dataclasses import dataclass +from core_tools.db_tools.db_help_funtions import checkTableExists, getColumnNames +import logging + +@dataclass +class db_field: + name : Parameter + field_type : str + value : any = None + +def define_table(cursor, table_name, fields): + ''' + define a paramter in the db, if does not exist, field are the field that are expected to be present. + + Args: + cursor () : db cursor + table_name (str) : name of the table to write to the db (e.g. the insturment names which generaters the paramters) + fields (list<db_field>) : field to write + ''' + # check if instrument present / create db if needed + if not checkTableExists(cursor, table_name): + cursor.execute("CREATE TABLE %s (snapshot_id INT AUTO_INCREMENT PRIMARY KEY,)", (table_name, )) + logging.info('generated a new table in db, {}'.format(table_name)) + + + + + # for every field, check if the column exists + pass + +def remove_table(cursor, param): + ''' + removes a table out of the db (if needed) + ''' + pass +def write_to_db(cursor, param, fields): + ''' + perform a write if fuekd in the db + ''' + pass +def get_snapshot_names(cursor, param, contains, N = 10): + ''' + get names of snapshots + ''' + pass +def get_data(cursor, parameter, snapshot_id=-1): + ''' + get data by primary key, if -1, get latest entry + ''' + pass + + + + + +if __name__ == '__main__': + from qcodes.tests.instrument_mocks import DummyInstrument + import mysql.connector + + dac = DummyInstrument('dac', gates=['ch1', 'ch2']) + # print(dac.print_readable_snapshot()) + + db = mysql.connector.connect(user='stephan', password='magicc', + host='51.89.64.39', + database='qcodes_test') + + ## creating an instance of 'cursor' class which is used to execute the 'SQL' statements in 'Python' + cursor = db.cursor() + cursor.execute("USE testing") + print(checkTableExists(cursor, 'table_prim_key')) + print(getColumnNames(cursor, 'table_prim_key')) + db.close() \ No newline at end of file -- GitLab