Conditions | 14 |
Total Lines | 88 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Tests | 40 |
CRAP Score | 14 |
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 abydos.phonetic._alpha_sis.AlphaSIS.encode() 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 | # -*- coding: utf-8 -*- |
||
157 | 1 | def encode(self, word, max_length=14): |
|
158 | """Return the IBM Alpha Search Inquiry System code for a word. |
||
159 | |||
160 | A collection is necessary as the return type since there can be |
||
161 | multiple values for a single word. But the collection must be ordered |
||
162 | since the first value is the primary coding. |
||
163 | |||
164 | Parameters |
||
165 | ---------- |
||
166 | word : str |
||
167 | The word to transform |
||
168 | max_length : int |
||
169 | The length of the code returned (defaults to 14) |
||
170 | |||
171 | Returns |
||
172 | ------- |
||
173 | tuple |
||
174 | The Alpha-SIS value |
||
175 | |||
176 | Examples |
||
177 | -------- |
||
178 | >>> pe = AlphaSIS() |
||
179 | >>> pe.encode('Christopher') |
||
180 | ('06401840000000', '07040184000000', '04018400000000') |
||
181 | >>> pe.encode('Niall') |
||
182 | ('02500000000000',) |
||
183 | >>> pe.encode('Smith') |
||
184 | ('03100000000000',) |
||
185 | >>> pe.encode('Schmidt') |
||
186 | ('06310000000000',) |
||
187 | |||
188 | """ |
||
189 | 1 | alpha = [''] |
|
190 | 1 | pos = 0 |
|
191 | 1 | word = unicode_normalize('NFKD', text_type(word.upper())) |
|
192 | 1 | word = word.replace('ß', 'SS') |
|
193 | 1 | word = ''.join(c for c in word if c in self._uc_set) |
|
194 | |||
195 | # Clamp max_length to [4, 64] |
||
196 | 1 | if max_length != -1: |
|
197 | 1 | max_length = min(max(4, max_length), 64) |
|
198 | else: |
||
199 | 1 | max_length = 64 |
|
200 | |||
201 | # Do special processing for initial substrings |
||
202 | 1 | for k in self._alpha_sis_initials_order: |
|
203 | 1 | if word.startswith(k): |
|
204 | 1 | alpha[0] += self._alpha_sis_initials[k] |
|
205 | 1 | pos += len(k) |
|
206 | 1 | break |
|
207 | |||
208 | # Add a '0' if alpha is still empty |
||
209 | 1 | if not alpha[0]: |
|
210 | 1 | alpha[0] += '0' |
|
211 | |||
212 | # Whether or not any special initial codes were encoded, iterate |
||
213 | # through the length of the word in the main encoding loop |
||
214 | 1 | while pos < len(word): |
|
215 | 1 | orig_pos = pos |
|
216 | 1 | for k in self._alpha_sis_basic_order: |
|
217 | 1 | if word[pos:].startswith(k): |
|
218 | 1 | if isinstance(self._alpha_sis_basic[k], tuple): |
|
219 | 1 | newalpha = [] |
|
220 | 1 | for i in range(len(self._alpha_sis_basic[k])): |
|
221 | 1 | newalpha += [ |
|
222 | _ + self._alpha_sis_basic[k][i] for _ in alpha |
||
223 | ] |
||
224 | 1 | alpha = newalpha |
|
225 | else: |
||
226 | 1 | alpha = [_ + self._alpha_sis_basic[k] for _ in alpha] |
|
227 | 1 | pos += len(k) |
|
228 | 1 | break |
|
229 | 1 | if pos == orig_pos: |
|
230 | 1 | alpha = [_ + '_' for _ in alpha] |
|
231 | 1 | pos += 1 |
|
232 | |||
233 | # Trim doublets and placeholders |
||
234 | 1 | for i in range(len(alpha)): |
|
235 | 1 | pos = 1 |
|
236 | 1 | while pos < len(alpha[i]): |
|
237 | 1 | if alpha[i][pos] == alpha[i][pos - 1]: |
|
238 | 1 | alpha[i] = alpha[i][:pos] + alpha[i][pos + 1 :] |
|
239 | 1 | pos += 1 |
|
240 | 1 | alpha = (_.replace('_', '') for _ in alpha) |
|
|
|||
241 | |||
242 | # Trim codes and return tuple |
||
243 | 1 | alpha = ((_ + ('0' * max_length))[:max_length] for _ in alpha) |
|
244 | 1 | return tuple(alpha) |
|
245 | |||
283 |