Skip to content

Commit

Permalink
fix bugs introduced in PR enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
yoelcortes committed May 30, 2022
1 parent 0363d94 commit bdb4e45
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
28 changes: 13 additions & 15 deletions chemicals/data_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,20 @@ def list_available_methods_from_df(df, index, keys_by_method):

DATABASE_CONSTANTS_CACHE = {}
def cached_constant_lookup(CASi, prop):
if CONSTANTS_CURSOR is None:
init_constants_db()
prop_idx = CONSTANT_DATABASE_NAME_TO_IDX[prop]
if CONSTANTS_CURSOR is None: init_constants_db()
if CASi in DATABASE_CONSTANTS_CACHE:
return DATABASE_CONSTANTS_CACHE[CASi][prop_idx], True

# Fetch and store the whole row
CONSTANTS_CURSOR.execute("SELECT * FROM constants WHERE `index`=?", (str(CASi),))
result = CONSTANTS_CURSOR.fetchone()
DATABASE_CONSTANTS_CACHE[CASi] = result
if result is not None:
result = DATABASE_CONSTANTS_CACHE[CASi]
else:
# Fetch and store the whole row
CONSTANTS_CURSOR.execute("SELECT * FROM constants WHERE `index`=?", (str(CASi),))
result = CONSTANTS_CURSOR.fetchone()
DATABASE_CONSTANTS_CACHE[CASi] = result
if result is None:
# Result the value, and whether the compound was in the index
return result, False
else:
prop_idx = CONSTANT_DATABASE_NAME_TO_IDX[prop]
return result[prop_idx], True

# Result the value, and whether the compound was in the index
return result, False

def init_constants_db():
global CONSTANTS_CURSOR
Expand All @@ -271,8 +270,7 @@ def database_constant_lookup(CASi, prop):
raise e from None
except Exception:
# Prevent database lookup after first failure considering it should work everytime.
# This fails on Yoel's machine due to "OperationalError: no such table: constants".
# It might possibly fail for other users every time (and maybe for other reasons).
# It will possibly fail for users every time if database has not been created.
global USE_CONSTANTS_DATABASE
USE_CONSTANTS_DATABASE = False
return None, False
2 changes: 1 addition & 1 deletion chemicals/molecular_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def linear(CASRN, method=None):
'''
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'linear')
if found: return bool(found)
if found: return bool(val)
if not _RG_data_loaded: _load_RG_data()
if method:
value = retrieve_from_df_dict(linear_sources, CASRN, 'linear', method)
Expand Down
2 changes: 1 addition & 1 deletion chemicals/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def S0s(CASRN, method=None):
NIST WebBook, NIST, http://doi.org/10.18434/T4M88Q
'''
if dr.USE_CONSTANTS_DATABASE and method is None:
val, found = database_constant_lookup(CASRN, 'S02')
val, found = database_constant_lookup(CASRN, 'S0s')
if found: return val
if not _reaction_data_loaded: _load_reaction_data()
if method:
Expand Down
4 changes: 2 additions & 2 deletions chemicals/refractivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ def RI(CASRN, method=None):
.. [2] Wikidata. Wikidata. Accessed via API. https://www.wikidata.org/
'''
if dr.USE_CONSTANTS_DATABASE and method is None:
RI, found = database_constant_lookup(CASRN, 'dipole_moment')
RIT, _ = database_constant_lookup(CASRN, 'dipole_moment')
RI, found = database_constant_lookup(CASRN, 'RI')
RIT, _ = database_constant_lookup(CASRN, 'RIT')
if found: return (RI, RIT)
if not _RI_data_loaded: _load_RI_data()
key = ('RI', 'RIT')
Expand Down

0 comments on commit bdb4e45

Please sign in to comment.