Conditions | 10 |
Total Lines | 66 |
Code Lines | 54 |
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.entry.tools.searchers.Searcher.search() 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 | """ |
||
60 | def search(self) -> SearchReturnInfo: |
||
61 | """ |
||
62 | Performs the search, and writes data. |
||
63 | """ |
||
64 | inchikeys = self.input_df["inchikey"].unique() |
||
65 | if self.is_complete: |
||
66 | logger.info(f"{self.to} already complete") |
||
67 | return SearchReturnInfo( |
||
68 | n_kept=len(inchikeys), n_processed=0, n_errored=0, time_taken=timedelta(seconds=0) |
||
69 | ) |
||
70 | logger.info(f"Will save every {SETTINGS.save_every} compounds") |
||
71 | logger.info(f"Writing {self.what.key} to {self.to}") |
||
72 | annotes = [] |
||
73 | compounds_run = set() |
||
74 | cache = SearchCache(self.to, inchikeys, restart=self.restart, proceed=self.proceed) |
||
75 | # refresh so we know it's (no longer) complete |
||
76 | # this would only happen if we're forcing this -- which is not currently allowed |
||
77 | ( |
||
78 | Checksums() |
||
79 | .load_dirsum_of_file(self.to, missing_ok=True) |
||
80 | .remove(self.to, missing_ok=True) |
||
81 | .write(rm_if_empty=True) |
||
82 | ) |
||
83 | t0, n0, n_proc, n_err, n_annot = time.monotonic(), cache.at, 0, 0, 0 |
||
84 | while True: |
||
85 | try: |
||
86 | compound = cache.next() |
||
87 | except StopIteration: |
||
88 | break |
||
89 | try: |
||
90 | with logger.contextualize(compound=compound): |
||
91 | x = self.what.find(compound) |
||
92 | annotes.extend(x) |
||
93 | except CompoundNotFoundError: |
||
94 | logger.info(f"Compound {compound} not found for {self.what.key}") |
||
95 | x = [] |
||
96 | n_err += 1 |
||
97 | except Exception: |
||
98 | raise SearchError( |
||
99 | f"Failed {self.what.key} [{self.what.search_class}] on compound {compound}", |
||
100 | compound=compound, |
||
101 | search_key=self.what.key, |
||
102 | search_class=self.what.search_class, |
||
103 | ) |
||
104 | compounds_run.add(compound) |
||
105 | logger.debug(f"Found {len(x)} {self.what.search_name()} annotations for {compound}") |
||
106 | n_annot += len(x) |
||
107 | n_proc += 1 |
||
108 | # logging, caching, and such: |
||
109 | on_nth = cache.at % SETTINGS.save_every == SETTINGS.save_every - 1 |
||
110 | is_last = cache.at == len(inchikeys) - 1 |
||
111 | if on_nth or is_last: |
||
112 | logger.log( |
||
113 | "NOTICE" if is_last else "INFO", |
||
114 | f"Found {len(annotes)} {self.what.search_name()} annotations" |
||
115 | + f" for {cache.at} of {len(inchikeys)} compounds", |
||
116 | ) |
||
117 | self._save(annotes, done=is_last) |
||
118 | cache.save(*compounds_run) # CRITICAL -- do this AFTER saving |
||
119 | # done! |
||
120 | i1, t1 = cache.at, time.monotonic() |
||
121 | assert i1 == len(inchikeys) |
||
122 | cache.kill() |
||
123 | logger.success(f"Wrote {self.what.key} to {self.to}") |
||
124 | return SearchReturnInfo( |
||
125 | n_kept=n0, n_processed=n_proc, n_errored=n_err, time_taken=timedelta(seconds=t1 - t0) |
||
126 | ) |
||
155 |