Conditions | 11 |
Total Lines | 61 |
Code Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Tests | 23 |
CRAP Score | 11 |
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._soundex_br.SoundexBR.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 -*- |
||
53 | 1 | def encode(self, word, max_length=4, zero_pad=True): |
|
54 | """Return the SoundexBR encoding of a word. |
||
55 | |||
56 | Parameters |
||
57 | ---------- |
||
58 | word : str |
||
59 | The word to transform |
||
60 | max_length : int |
||
61 | The length of the code returned (defaults to 4) |
||
62 | zero_pad : bool |
||
63 | Pad the end of the return value with 0s to achieve a max_length |
||
64 | string |
||
65 | |||
66 | Returns |
||
67 | ------- |
||
68 | str |
||
69 | The SoundexBR code |
||
70 | |||
71 | Examples |
||
72 | -------- |
||
73 | >>> soundex_br('Oliveira') |
||
74 | 'O416' |
||
75 | >>> soundex_br('Almeida') |
||
76 | 'A453' |
||
77 | >>> soundex_br('Barbosa') |
||
78 | 'B612' |
||
79 | >>> soundex_br('Araújo') |
||
80 | 'A620' |
||
81 | >>> soundex_br('Gonçalves') |
||
82 | 'G524' |
||
83 | >>> soundex_br('Goncalves') |
||
84 | 'G524' |
||
85 | |||
86 | """ |
||
87 | 1 | word = unicode_normalize('NFKD', text_type(word.upper())) |
|
88 | 1 | word = ''.join(c for c in word if c in self._uc_set) |
|
89 | |||
90 | 1 | if word[:2] == 'WA': |
|
91 | 1 | first = 'V' |
|
92 | 1 | elif word[:1] == 'K' and word[1:2] in {'A', 'O', 'U'}: |
|
93 | 1 | first = 'C' |
|
94 | 1 | elif word[:1] == 'C' and word[1:2] in {'I', 'E'}: |
|
95 | 1 | first = 'S' |
|
96 | 1 | elif word[:1] == 'G' and word[1:2] in {'E', 'I'}: |
|
97 | 1 | first = 'J' |
|
98 | 1 | elif word[:1] == 'Y': |
|
99 | 1 | first = 'I' |
|
100 | 1 | elif word[:1] == 'H': |
|
101 | 1 | first = word[1:2] |
|
102 | 1 | word = word[1:] |
|
103 | else: |
||
104 | 1 | first = word[:1] |
|
105 | |||
106 | 1 | sdx = first + word[1:].translate(self._trans) |
|
107 | 1 | sdx = self._delete_consecutive_repeats(sdx) |
|
108 | 1 | sdx = sdx.replace('0', '') |
|
109 | |||
110 | 1 | if zero_pad: |
|
111 | 1 | sdx += '0' * max_length |
|
112 | |||
113 | 1 | return sdx[:max_length] |
|
114 | |||
158 |