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