about summary refs log tree commit diff
path: root/gn3/db
diff options
context:
space:
mode:
authorBonfaceKilz2022-03-02 16:05:36 +0300
committerBonfaceKilz2022-03-12 15:33:09 +0300
commit25b6c1bf35ccb590f8728295059d39db77e7422f (patch)
tree331a30c90e2eb4fa0aeb246e683e5484272d00da /gn3/db
parentaf1d36e0847d2bcd878ea7343bd1c858ae4fa91f (diff)
downloadgenenetwork3-25b6c1bf35ccb590f8728295059d39db77e7422f.tar.gz
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.
Diffstat (limited to 'gn3/db')
-rw-r--r--gn3/db/sample_data.py25
1 files 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