Conditions | 10 |
Total Lines | 57 |
Code Lines | 43 |
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:
Complex classes like mandos.entries.searcher.SearcherUtils.dl() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """ |
||
39 | @classmethod |
||
40 | def dl( |
||
41 | cls, |
||
42 | inchikeys: Sequence[str], |
||
43 | pubchem: bool = True, |
||
44 | chembl: bool = True, |
||
45 | hmdb: bool = True, |
||
46 | quiet: bool = False, |
||
47 | ) -> IdMatchFrame: |
||
48 | # we actually cache the results, even though the underlying APIs cache |
||
49 | # the reasons for this are a little obscure -- |
||
50 | # when running a Searcher, we want to run before the FIRST search |
||
51 | # for the typer commands to be replicas of the ``Entry.run`` methods, Searcher fetches before running a search |
||
52 | # but if we have multiple searches (as in ``mandos search --config``), we only want that at the beginning |
||
53 | # the alternative was having ``mandos search`` dynamically subclass each ``Entry`` -- which was really hard |
||
54 | # this is much cleaner, even though it's redundant |
||
55 | # if the cached results under /pubchem and /chembl are deleted, we unfortunately won't cache the results |
||
56 | # when running this command |
||
57 | # to fix that, we need to delete the cached /match dataframes |
||
58 | # now that I'm writing this down, I realize this is pretty bad |
||
59 | # TODO |
||
60 | # noinspection PyPep8Naming |
||
61 | Chembl, Pubchem = Apis.Chembl, Apis.Pubchem |
||
62 | logger.info(f"Using {Chembl}, {Pubchem}") |
||
63 | key = hash(",".join(inchikeys)) |
||
64 | cached_path = (MANDOS_SETTINGS.match_cache_path / str(key)).with_suffix(".feather") |
||
65 | if cached_path.exists(): |
||
66 | logger.info(f"Found ID matching results at {cached_path}") |
||
67 | return IdMatchFrame.read_feather(cached_path) |
||
68 | found_chembl: Dict[str, str] = {} |
||
69 | found_pubchem: Dict[str, str] = {} |
||
70 | if pubchem: |
||
71 | for inchikey in inchikeys: |
||
72 | try: |
||
73 | cid = Pubchem.fetch_data(inchikey).cid |
||
74 | found_pubchem[inchikey] = str(cid) |
||
75 | if not quiet: |
||
76 | logger.info(f"Found: PubChem {inchikey} ({cid})") |
||
77 | except CompoundNotFoundError: |
||
78 | logger.info(f"NOT FOUND: PubChem {inchikey}") |
||
79 | logger.trace(f"Did not find PubChem {inchikey}", exc_info=True) |
||
80 | if chembl: |
||
81 | for inchikey in inchikeys: |
||
82 | try: |
||
83 | chid = ChemblUtils(Chembl).get_compound(inchikey).chid |
||
84 | found_chembl[inchikey] = chid |
||
85 | if not quiet: |
||
86 | logger.info(f"Found: ChEMBL {inchikey} ({chid})") |
||
87 | except CompoundNotFoundError: |
||
88 | logger.info(f"NOT FOUND: ChEMBL {inchikey}") |
||
89 | logger.trace(f"Did not find ChEMBL {inchikey}", exc_info=True) |
||
90 | df = pd.DataFrame([pd.Series(dict(inchikey=c)) for c in inchikeys]) |
||
91 | df["chembl_id"] = df["inchikey"].map(found_chembl.get) |
||
92 | df["pubchem_id"] = df["inchikey"].map(found_pubchem.get) |
||
93 | df = IdMatchFrame(df) |
||
94 | df.to_feather(cached_path) |
||
95 | logger.info(f"Wrote {cached_path}") |
||
96 | |||
182 |