From 151ef8bbe23d85f74cdab690073afd4a9329e78a Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 27 Oct 2021 10:24:28 +0300 Subject: Remove if clauses: replace with dict Issue: https://github.com/genenetwork/gn-gemtext-threads/blob/main/topics/gn1-migration-to-gn2/partial-correlations.gmi * Remove the if clauses to simplify the code flow: use a dictionary of queries and select the appropriate query from the dictionary instead. --- gn3/db/species.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'gn3') diff --git a/gn3/db/species.py b/gn3/db/species.py index 1e5015f..abcbf64 100644 --- a/gn3/db/species.py +++ b/gn3/db/species.py @@ -47,17 +47,13 @@ def translate_to_mouse_gene_id(species: str, geneid: int, conn: Any) -> int: return geneid with conn.cursor as cursor: - if species == "rat": - cursor.execute( - "SELECT mouse FROM GeneIDXRef WHERE rat = %s", geneid) - rat_geneid = cursor.fetchone() - if rat_geneid: - return rat_geneid[0] - - cursor.execute( - "SELECT mouse FROM GeneIDXRef WHERE human = %s", geneid) - human_geneid = cursor.fetchone() - if human_geneid: - return human_geneid[0] + query = { + "rat": "SELECT mouse FROM GeneIDXRef WHERE rat = %s" + "human": "SELECT mouse FROM GeneIDXRef WHERE human = %s" + } + cursor.execute(query[species], geneid) + translated_gene_id = cursor.fetchone() + if translated_gene_id: + return translated_gene_id[0] return 0 # default if all else fails -- cgit v1.2.3