Conditions | 9 |
Total Lines | 53 |
Code Lines | 39 |
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 | """ |
||
65 | def _search_one(self, search: Search, inchikeys: Sequence[str], path: Path) -> None: |
||
66 | """ |
||
67 | Loops over every compound and calls ``find``. |
||
68 | Comes with better logging. |
||
69 | Writes a logging ERROR for each compound that was not found. |
||
70 | |||
71 | Args: |
||
72 | inchikeys: A list of InChI key strings |
||
73 | path: Path to write to |
||
74 | """ |
||
75 | logger.info(f"Will save every {SETTINGS.save_every} compounds") |
||
76 | logger.info(f"Writing {search.key} to {path}") |
||
77 | annotes = [] |
||
78 | compounds_run = set() |
||
79 | cache = SearchCache(path, inchikeys) |
||
80 | # refresh so we know it's (no longer) complete |
||
81 | Checksums.delete_dir_hashes(Checksums.get_hash_dir(path), [path], missing_ok=True) |
||
82 | self._save_metadata(path, search) |
||
83 | while True: |
||
84 | try: |
||
85 | compound = cache.next() |
||
86 | except StopIteration: |
||
87 | break |
||
88 | try: |
||
89 | with logger.contextualize(compound=compound): |
||
90 | x = search.find(compound) |
||
91 | annotes.extend(x) |
||
92 | except CompoundNotFoundError: |
||
93 | logger.info(f"Compound {compound} not found for {search.key}") |
||
94 | x = [] |
||
95 | except Exception: |
||
96 | raise SearchError( |
||
97 | f"Failed {search.key} [{search.search_class}] on compound {compound}", |
||
98 | compound=compound, |
||
99 | search_key=search.key, |
||
100 | search_class=search.search_class, |
||
101 | ) |
||
102 | compounds_run.add(compound) |
||
103 | logger.debug(f"Found {len(x)} {search.search_name()} annotations for {compound}") |
||
104 | # logging, caching, and such: |
||
105 | on_nth = cache.at % SETTINGS.save_every == SETTINGS.save_every - 1 |
||
106 | is_last = cache.at == len(inchikeys) - 1 |
||
107 | if on_nth or is_last: |
||
108 | logger.log( |
||
109 | "NOTICE" if is_last else "INFO", |
||
110 | f"Found {len(annotes)} {search.search_name()} annotations" |
||
111 | + f" for {cache.at} of {len(inchikeys)} compounds", |
||
112 | ) |
||
113 | self._save_annotations(annotes, path, done=is_last) |
||
114 | cache.save(*compounds_run) # CRITICAL -- do this AFTER saving |
||
115 | # done! |
||
116 | cache.kill() |
||
117 | logger.info(f"Wrote {search.key} to {path}") |
||
118 | |||
138 |