Conditions | 21 |
Total Lines | 78 |
Code Lines | 58 |
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 amd.io._Reader._CIFBlock_to_PeriodicSet() 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 | """Contains I/O tools, including a .CIF reader and CSD reader |
||
145 | |||
146 | if isinstance(refcodes, str) and refcodes.lower() == 'csd': |
||
147 | refcodes = None |
||
148 | |||
149 | if refcodes is None: |
||
150 | families = False |
||
151 | else: |
||
152 | refcodes = [refcodes] if isinstance(refcodes, str) else list(refcodes) |
||
153 | |||
154 | # families parameter reads all crystals with ids starting with passed refcodes |
||
155 | if families: |
||
156 | all_refcodes = [] |
||
157 | for refcode in refcodes: |
||
158 | query = ccdc.search.TextNumericSearch() |
||
159 | query.add_identifier(refcode) |
||
160 | all_refcodes.extend((hit.identifier for hit in query.search())) |
||
161 | |||
162 | # filter to unique refcodes |
||
163 | seen = set() |
||
164 | seen_add = seen.add |
||
165 | refcodes = [ |
||
166 | refcode for refcode in all_refcodes |
||
167 | if not (refcode in seen or seen_add(refcode))] |
||
168 | |||
169 | self._entry_reader = ccdc.io.EntryReader('CSD') |
||
170 | self._generator = self._map( |
||
171 | self._Entry_to_PeriodicSet, |
||
172 | self._ccdc_generator(refcodes)) |
||
173 | |||
174 | def _ccdc_generator(self, refcodes): |
||
175 | """Generates ccdc Entries from CSD refcodes""" |
||
176 | |||
177 | if refcodes is None: |
||
178 | for entry in self._entry_reader: |
||
179 | yield entry |
||
180 | else: |
||
181 | for refcode in refcodes: |
||
182 | try: |
||
183 | entry = self._entry_reader.entry(refcode) |
||
184 | yield entry |
||
185 | except RuntimeError: |
||
186 | warnings.warn( |
||
187 | f'Identifier {refcode} not found in database') |
||
188 | |||
189 | def entry(self, refcode: str) -> PeriodicSet: |
||
190 | """Read a PeriodicSet given any CSD refcode.""" |
||
191 | |||
192 | entry = self._entry_reader.entry(refcode) |
||
193 | periodic_set = self._Entry_to_PeriodicSet(entry) |
||
194 | return periodic_set |
||
195 | |||
196 | |||
197 | def crystal_to_periodicset(crystal): |
||
198 | """ccdc.crystal.Crystal --> amd.periodicset.PeriodicSet. |
||
199 | Ignores disorder, missing sites/coords, checks & no options. |
||
200 | Is a stripped-down version of the function used in CifReader.""" |
||
201 | |||
202 | cell = cellpar_to_cell(*crystal.cell_lengths, *crystal.cell_angles) |
||
203 | |||
204 | # asymmetric unit fractional coordinates |
||
205 | asym_frac_motif = np.array([tuple(a.fractional_coordinates) |
||
206 | for a in crystal.asymmetric_unit_molecule.atoms]) |
||
207 | asym_frac_motif = np.mod(asym_frac_motif, 1) |
||
208 | |||
209 | # if the above removed everything, skip this structure |
||
210 | if asym_frac_motif.shape[0] == 0: |
||
211 | raise ValueError(f'{crystal.identifier} has no coordinates') |
||
212 | |||
213 | sitesym = crystal.symmetry_operators |
||
214 | if not sitesym: |
||
215 | sitesym = ('x,y,z', ) |
||
216 | r = _Reader() |
||
217 | r.current_identifier = crystal.identifier |
||
218 | frac_motif, asym_unit, multiplicities, _ = r.expand(asym_frac_motif, sitesym) |
||
219 | motif = frac_motif @ cell |
||
220 | |||
221 | kwargs = { |
||
222 | 'name': crystal.identifier, |
||
223 | 'asymmetric_unit': asym_unit, |
||
275 |