From fb0fffb7a3470c648cef8effeb1792e6ac3c7504 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Thu, 1 Dec 2022 19:34:33 +0300 Subject: Fix Exception that's raised when query_sql returns None * wqflask/base/data_set/dataset.py: Delete itertools, DictCursor. (DataSet.get_accession_id): Handle a case where query_sql returns a None value. --- wqflask/base/data_set/dataset.py | 67 +++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/wqflask/base/data_set/dataset.py b/wqflask/base/data_set/dataset.py index d34a3d85..feab2b23 100644 --- a/wqflask/base/data_set/dataset.py +++ b/wqflask/base/data_set/dataset.py @@ -2,11 +2,9 @@ import math import collections -import itertools from redis import Redis -from MySQLdb.cursors import DictCursor from base import species from utility import chunks from gn3.monads import MonadicDict, query_sql @@ -16,6 +14,7 @@ from wqflask.database import database_connection from utility.db_tools import escape, mescape, create_in_clause from .utils import fetch_cached_results, cache_dataset_results + class DataSet: """ DataSet class defines a dataset in webqtl, can be either Microarray, @@ -64,42 +63,46 @@ class DataSet: def get_accession_id(self) -> Maybe[str]: """Get the accession_id of this dataset depending on the dataset type.""" - __accession_id_dict = MonadicDict() + __query = "" with database_connection() as conn: if self.type == "Publish": - __accession_id_dict, = itertools.islice( - query_sql(conn, - ("SELECT InfoFiles.GN_AccesionId AS accession_id FROM " - "InfoFiles, PublishFreeze, InbredSet " - f"WHERE InbredSet.Name = '{conn.escape_string(self.group.name).decode()}' " - "AND PublishFreeze.InbredSetId = InbredSet.Id " - "AND InfoFiles.InfoPageName = PublishFreeze.Name " - "AND PublishFreeze.public > 0 AND " - "PublishFreeze.confidentiality < 1 " - "ORDER BY PublishFreeze.CreateTime DESC") - ), 1) + __query = ( + "SELECT InfoFiles.GN_AccesionId AS accession_id FROM " + "InfoFiles, PublishFreeze, InbredSet " + "WHERE InbredSet.Name = " + f"'{conn.escape_string(self.group.name).decode()}' " + "AND PublishFreeze.InbredSetId = InbredSet.Id " + "AND InfoFiles.InfoPageName = PublishFreeze.Name " + "AND PublishFreeze.public > 0 AND " + "PublishFreeze.confidentiality < 1 " + "ORDER BY PublishFreeze.CreateTime DESC" + ) elif self.type == "Geno": - __accession_id_dict, = itertools.islice( - query_sql(conn, - ("SELECT InfoFiles.GN_AccesionId AS accession_id FROM " - "InfoFiles, GenoFreeze, InbredSet " - f"WHERE InbredSet.Name = '{conn.escape_string(self.group.name).decode()}' AND " - "GenoFreeze.InbredSetId = InbredSet.Id " - "AND InfoFiles.InfoPageName = GenoFreeze.ShortName " - "AND GenoFreeze.public > 0 AND " - "GenoFreeze.confidentiality < 1 " - "ORDER BY GenoFreeze.CreateTime DESC") - ), 1) + __query = ( + "SELECT InfoFiles.GN_AccesionId AS accession_id FROM " + "InfoFiles, GenoFreeze, InbredSet WHERE InbredSet.Name = " + f"'{conn.escape_string(self.group.name).decode()}' AND " + "GenoFreeze.InbredSetId = InbredSet.Id " + "AND InfoFiles.InfoPageName = GenoFreeze.ShortName " + "AND GenoFreeze.public > 0 AND " + "GenoFreeze.confidentiality < 1 " + "ORDER BY GenoFreeze.CreateTime DESC" + ) elif self.type == "ProbeSet": - __accession_id_dict, = itertools.islice( - query_sql(conn, - ("SELECT InfoFiles.GN_AccesionId AS accession_id " - "FROM InfoFiles WHERE InfoFiles.InfoPageName = " - f"'{conn.escape_string(self.name).decode()}'") - ), 1) + __query = ( + "SELECT InfoFiles.GN_AccesionId AS accession_id " + "FROM InfoFiles WHERE InfoFiles.InfoPageName = " + f"'{conn.escape_string(self.name).decode()}'" + ) else: # The Value passed is not present raise LookupError - return __accession_id_dict["accession_id"] + + # Should there be an empty row, query_sql returns a None + # value instead of yielding a value; this block + # accomodates this non-intuitive edge-case + for result in query_sql(conn, __query) or (): + return result["accession_id"] + return Nothing def retrieve_other_names(self): """This method fetches the the dataset names in search_result. -- cgit v1.2.3