diff --git a/datamol/data/__init__.py b/datamol/data/__init__.py index 04cbbb37..21255c19 100644 --- a/datamol/data/__init__.py +++ b/datamol/data/__init__.py @@ -13,6 +13,7 @@ import sys import io +import functools try: import importlib.resources as importlib_resources @@ -26,6 +27,15 @@ from ..convert import from_df from ..convert import render_mol_df +@functools.lru_cache() +def datamol_data_file_path(filename: str, dm_module: str = "datamol.data") -> str: + if sys.version_info < (3, 9, 0): + with importlib_resources.path("datamol.data", filename) as p: + data_path = p + else: + data_path = importlib_resources.files(dm_module).joinpath(filename) + + return str(data_path) def open_datamol_data_file( filename: str, diff --git a/datamol/mol.py b/datamol/mol.py index 389b1ece..e0cf6cab 100644 --- a/datamol/mol.py +++ b/datamol/mol.py @@ -45,8 +45,7 @@ AROMATIC_BOND = Chem.rdchem.BondType.AROMATIC DATIVE_BOND = Chem.rdchem.BondType.DATIVE UNSPECIFIED_BOND = Chem.rdchem.BondType.UNSPECIFIED -with datamol.data.open_datamol_data_file("salts_solvents.smi") as file_obj: - SALT_SOLVENT_PATH = file_obj.name +SALT_SOLVENT_PATH = datamol.data.datamol_data_file_path("salts_solvents.smi") SALT_SOLVENT_REMOVER = SaltRemover(defnFilename=SALT_SOLVENT_PATH)