| Conditions | 8 |
| Total Lines | 54 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | """ |
||
| 38 | @classmethod |
||
| 39 | def dl( |
||
| 40 | cls, |
||
| 41 | inchikeys: Sequence[str], |
||
| 42 | pubchem: bool = True, |
||
| 43 | chembl: bool = True, |
||
| 44 | hmdb: bool = True, |
||
| 45 | ) -> IdMatchFrame: |
||
| 46 | # we actually cache the results, even though the underlying APIs cache |
||
| 47 | # the reasons for this are a little obscure -- |
||
| 48 | # when running a Searcher, we want to run before the FIRST search |
||
| 49 | # for the typer commands to be replicas of the ``Entry.run`` methods, Searcher fetches before running a search |
||
| 50 | # but if we have multiple searches (as in ``mandos search --config``), we only want that at the beginning |
||
| 51 | # the alternative was having ``mandos search`` dynamically subclass each ``Entry`` -- which was really hard |
||
| 52 | # this is much cleaner, even though it's redundant |
||
| 53 | # if the cached results under /pubchem and /chembl are deleted, we unfortunately won't cache the results |
||
| 54 | # when running this command |
||
| 55 | # to fix that, we need to delete the cached /match dataframes |
||
| 56 | # now that I'm writing this down, I realize this is pretty bad |
||
| 57 | # TODO |
||
| 58 | # noinspection PyPep8Naming |
||
| 59 | Chembl, Pubchem = Apis.Chembl, Apis.Pubchem |
||
| 60 | logger.notice(f"Using {Chembl}, {Pubchem}") |
||
| 61 | key = hash(",".join(inchikeys)) |
||
| 62 | cached_path = (MANDOS_SETTINGS.match_cache_path / str(key)).with_suffix(".feather") |
||
| 63 | if cached_path.exists(): |
||
| 64 | logger.info(f"Found ID matching results at {cached_path}") |
||
| 65 | return IdMatchFrame.read_feather(cached_path) |
||
| 66 | found_chembl: Dict[str, str] = {} |
||
| 67 | found_pubchem: Dict[str, str] = {} |
||
| 68 | if pubchem: |
||
| 69 | for inchikey in inchikeys: |
||
| 70 | try: |
||
| 71 | cid = Pubchem.fetch_data(inchikey).cid |
||
| 72 | found_pubchem[inchikey] = str(cid) |
||
| 73 | logger.info(f"Found: PubChem {inchikey} ({cid})") |
||
| 74 | except CompoundNotFoundError: |
||
| 75 | logger.info(f"NOT FOUND: PubChem {inchikey}") |
||
| 76 | logger.debug(f"Did not find PubChem {inchikey}", exc_info=True) |
||
| 77 | if chembl: |
||
| 78 | for inchikey in inchikeys: |
||
| 79 | try: |
||
| 80 | chid = ChemblUtils(Chembl).get_compound(inchikey).chid |
||
| 81 | found_chembl[inchikey] = chid |
||
| 82 | logger.info(f"Found: ChEMBL {inchikey} ({chid})") |
||
| 83 | except CompoundNotFoundError: |
||
| 84 | logger.info(f"NOT FOUND: ChEMBL {inchikey}") |
||
| 85 | logger.debug(f"Did not find ChEMBL {inchikey}", exc_info=True) |
||
| 86 | df = pd.DataFrame([pd.Series(dict(inchikey=c)) for c in inchikeys]) |
||
| 87 | df["chembl_id"] = df["inchikey"].map(found_chembl.get) |
||
| 88 | df["pubchem_id"] = df["inchikey"].map(found_pubchem.get) |
||
| 89 | df = IdMatchFrame(df) |
||
| 90 | df.to_feather(cached_path) |
||
| 91 | logger.info(f"Wrote {cached_path}") |
||
| 92 | |||
| 194 |