From 25b6c1bf35ccb590f8728295059d39db77e7422f Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Wed, 2 Mar 2022 16:05:36 +0300 Subject: Make `_map` a constant * gn3/db/sample_data.py: Now constant, `_MAP`. (delete_sample_data)[__delete_data]: Replace `_map` with `_MAP`. (insert_sample_data)[__insert_data]: Ditto. --- gn3/db/sample_data.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/gn3/db/sample_data.py b/gn3/db/sample_data.py index 80f8292..89e95fc 100644 --- a/gn3/db/sample_data.py +++ b/gn3/db/sample_data.py @@ -3,6 +3,14 @@ from typing import Any, Tuple, Union import MySQLdb + +_MAP = { + "PublishData": ("StrainId", "Id", "value"), + "PublishSE": ("StrainId", "DataId", "error"), + "NStrain": ("StrainId", "DataId", "count"), +} + + def get_trait_csv_sample_data(conn: Any, trait_name: int, phenotype_id: int) -> str: """Fetch a trait and return it as a csv string""" @@ -180,14 +188,10 @@ def delete_sample_data(conn: Any, tables.""" def __delete_data(conn, table): if value and value != "x": - _map = { - "PublishData": "StrainId = %s AND Id = %s", - "PublishSE": "StrainId = %s AND DataId = %s", - "NStrain": "StrainId = %s AND DataId = %s", - } + sub_query = (" = %s AND ".join(_MAP.get(table)[:2]) + " = %s") with conn.cursor() as cursor: cursor.execute((f"DELETE FROM {table} " - f"WHERE {_map.get(table)}"), + f"WHERE {sub_query}"), (strain_id, data_id)) return cursor.rowcount return 0 @@ -248,19 +252,16 @@ def insert_sample_data(conn: Any, # pylint: disable=[R0913] """ def __insert_data(conn, table, value): if value and value != "x": - _map = { - "PublishData": "(StrainId, Id, value)", - "PublishSE": "(StrainId, DataId, error)", - "NStrain": "(StrainId, DataId, count)", - } + with conn.cursor() as cursor: cursor.execute( "SELECT Id FROM PublishData where Id = %s " "AND StrainId = %s", (data_id, strain_id)) if not cursor.fetchone(): + columns = ", ".join(_MAP.get(table)) cursor.execute((f"INSERT INTO {table} " - f"{_map.get(table)} " + f"({columns}) " f"VALUES (%s, %s, %s)"), (strain_id, data_id, value)) return cursor.rowcount -- cgit v1.2.3