Conditions | 5 |
Total Lines | 61 |
Code Lines | 55 |
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 | from __future__ import annotations |
||
122 | @classmethod |
||
123 | def load(cls, data: NestedDotDict) -> Settings: |
||
124 | extra_default_keys = dict(defaults) |
||
125 | |||
126 | def get(s: str, t: Type[T]) -> T: |
||
127 | if s in extra_default_keys: # could be accessed more than once |
||
128 | del extra_default_keys[s] |
||
129 | try: |
||
130 | return data.get_as(s, t, defaults[s]) |
||
131 | except TypeError: |
||
132 | raise ConfigError(f"Key {s}={data.get(s), defaults[s]} is not of type {t}") |
||
133 | |||
134 | _continent = Suretime.Types.NtpContinents.of |
||
135 | _selenium_path = get("query.selenium_driver_path", Path) |
||
136 | if _selenium_path is not None: |
||
137 | _selenium_path = _selenium_path.expanduser() |
||
138 | chembl_delay = get("query.chembl.delay_sec", float) |
||
139 | pubchem_delay = get("query.pubchem.delay_sec", float) |
||
140 | hmdb_delay = get("query.hmdb.delay_sec", float) |
||
141 | data = cls( |
||
142 | is_testing=get("is_testing", bool), |
||
143 | ntp_continent=get("search.ntp_continent_code", _continent), |
||
144 | table_suffix=get("search.default_table_suffix", str), |
||
145 | log_suffix=get("search.default_log_suffix", str), |
||
146 | save_every=get("search.save_every", int), |
||
147 | sanitize_paths=get("search.sanitize_paths", bool), |
||
148 | cache_path=Path(get("cache.path", str)).expanduser(), |
||
149 | chembl_expire_sec=get("cache.chembl.expire_sec", int), |
||
150 | pubchem_expire_sec=get("cache.pubchem.expire_sec", int), |
||
151 | taxon_expire_sec=get("cache.taxa.expire_sec", int), |
||
152 | cache_gzip=get("cache.gzip", bool), |
||
153 | archive_filename_suffix=get("cache.archive_filename_suffix", str), |
||
154 | chembl_n_tries=get("query.chembl.n_tries", int), |
||
155 | chembl_fast_save=get("query.chembl.fast_save", bool), |
||
156 | chembl_timeout_sec=get("query.chembl.timeout_sec", int), |
||
157 | chembl_backoff_factor=get("query.chembl.backoff_factor", float), |
||
158 | chembl_query_delay_min=chembl_delay, |
||
159 | chembl_query_delay_max=chembl_delay * max_coeff, |
||
160 | pubchem_timeout_sec=get("query.pubchem.timeout_sec", int), |
||
161 | hmdb_expire_sec=get("cache.hmdb.expire_sec", int), |
||
162 | pubchem_backoff_factor=get("query.pubchem.backoff_factor", float), |
||
163 | pubchem_query_delay_min=get("query.pubchem.delay_sec", float), |
||
164 | pubchem_query_delay_max=pubchem_delay * max_coeff, |
||
165 | pubchem_n_tries=get("query.pubchem.n_tries", int), |
||
166 | hmdb_timeout_sec=get("query.hmdb.timeout_sec", int), |
||
167 | hmdb_backoff_factor=get("query.hmdb.backoff_factor", float), |
||
168 | hmdb_query_delay_min=hmdb_delay, |
||
169 | hmdb_query_delay_max=hmdb_delay * max_coeff, |
||
170 | selenium_driver=get("query.selenium_driver", str).title(), |
||
171 | selenium_driver_path=_selenium_path, |
||
172 | log_signals=get("cli.log_signals", bool), |
||
173 | log_exit=get("cli.log_exit", bool), |
||
174 | ) |
||
175 | # we got all the required fields |
||
176 | # make sure we don't have extra keys in defaults |
||
177 | if len(extra_default_keys) > 0: |
||
178 | raise AssertionError( |
||
179 | f"There are {len(extra_default_keys)} extra defaults" |
||
180 | + f"in {defaults}: {extra_default_keys}" |
||
181 | ) |
||
182 | return data |
||
183 | |||
244 |