Conditions | 32 |
Total Lines | 193 |
Code Lines | 94 |
Lines | 0 |
Ratio | 0 % |
Tests | 81 |
CRAP Score | 32 |
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._Dolby.Dolby.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 -*- |
||
47 | 1 | def encode(self, word, max_length=-1, keep_vowels=False, vowel_char='*'): |
|
48 | r"""Return the Dolby Code of a name. |
||
49 | |||
50 | Args: |
||
51 | word (str): The word to transform |
||
52 | max_length (int): Maximum length of the returned Dolby code -- this |
||
53 | also activates the fixed-length code mode if it is greater than |
||
54 | 0 |
||
55 | keep_vowels (bool): If True, retains all vowel markers |
||
56 | vowel_char (str): The vowel marker character (default to \*) |
||
57 | |||
58 | Returns: |
||
59 | str: The Dolby Code |
||
60 | |||
61 | Examples: |
||
62 | >>> pe = Dolby() |
||
63 | >>> pe.encode('Hansen') |
||
64 | 'H*NSN' |
||
65 | >>> pe.encode('Larsen') |
||
66 | 'L*RSN' |
||
67 | >>> pe.encode('Aagaard') |
||
68 | '*GR' |
||
69 | >>> pe.encode('Braaten') |
||
70 | 'BR*DN' |
||
71 | >>> pe.encode('Sandvik') |
||
72 | 'S*NVK' |
||
73 | >>> pe.encode('Hansen', max_length=6) |
||
74 | 'H*NS*N' |
||
75 | >>> pe.encode('Larsen', max_length=6) |
||
76 | 'L*RS*N' |
||
77 | >>> pe.encode('Aagaard', max_length=6) |
||
78 | '*G*R ' |
||
79 | >>> pe.encode('Braaten', max_length=6) |
||
80 | 'BR*D*N' |
||
81 | >>> pe.encode('Sandvik', max_length=6) |
||
82 | 'S*NF*K' |
||
83 | |||
84 | >>> pe.encode('Smith') |
||
85 | 'SM*D' |
||
86 | >>> pe.encode('Waters') |
||
87 | 'W*DRS' |
||
88 | >>> pe.encode('James') |
||
89 | 'J*MS' |
||
90 | >>> pe.encode('Schmidt') |
||
91 | 'SM*D' |
||
92 | >>> pe.encode('Ashcroft') |
||
93 | '*SKRFD' |
||
94 | >>> pe.encode('Smith', max_length=6) |
||
95 | 'SM*D ' |
||
96 | >>> pe.encode('Waters', max_length=6) |
||
97 | 'W*D*RS' |
||
98 | >>> pe.encode('James', max_length=6) |
||
99 | 'J*M*S ' |
||
100 | >>> pe.encode('Schmidt', max_length=6) |
||
101 | 'SM*D ' |
||
102 | >>> pe.encode('Ashcroft', max_length=6) |
||
103 | '*SKRFD' |
||
104 | |||
105 | """ |
||
106 | # uppercase, normalize, decompose, and filter non-A-Z out |
||
107 | 1 | word = unicode_normalize('NFKD', text_type(word.upper())) |
|
108 | 1 | word = word.replace('ß', 'SS') |
|
109 | 1 | word = ''.join(c for c in word if c in self._uc_set) |
|
110 | |||
111 | # Rule 1 (FL2) |
||
112 | 1 | if word[:3] in {'MCG', 'MAG', 'MAC'}: |
|
113 | 1 | word = 'MK' + word[3:] |
|
114 | 1 | elif word[:2] == 'MC': |
|
115 | 1 | word = 'MK' + word[2:] |
|
116 | |||
117 | # Rule 2 (FL3) |
||
118 | 1 | pos = len(word) - 2 |
|
119 | 1 | while pos > -1: |
|
120 | 1 | if word[pos : pos + 2] in { |
|
121 | 'DT', |
||
122 | 'LD', |
||
123 | 'ND', |
||
124 | 'NT', |
||
125 | 'RC', |
||
126 | 'RD', |
||
127 | 'RT', |
||
128 | 'SC', |
||
129 | 'SK', |
||
130 | 'ST', |
||
131 | }: |
||
132 | 1 | word = word[: pos + 1] + word[pos + 2 :] |
|
133 | 1 | pos += 1 |
|
134 | 1 | pos -= 1 |
|
135 | |||
136 | # Rule 3 (FL4) |
||
137 | # Although the rule indicates "after the first letter", the test cases |
||
138 | # make it clear that these apply to the first letter also. |
||
139 | 1 | word = word.replace('X', 'KS') |
|
140 | 1 | word = word.replace('CE', 'SE') |
|
141 | 1 | word = word.replace('CI', 'SI') |
|
142 | 1 | word = word.replace('CY', 'SI') |
|
143 | |||
144 | # not in the rule set, but they seem to have intended it |
||
145 | 1 | word = word.replace('TCH', 'CH') |
|
146 | |||
147 | 1 | pos = word.find('CH', 1) |
|
148 | 1 | while pos != -1: |
|
149 | 1 | if word[pos - 1 : pos] not in self._uc_vy_set: |
|
150 | 1 | word = word[:pos] + 'S' + word[pos + 1 :] |
|
151 | 1 | pos = word.find('CH', pos + 1) |
|
152 | |||
153 | 1 | word = word.replace('C', 'K') |
|
154 | 1 | word = word.replace('Z', 'S') |
|
155 | |||
156 | 1 | word = word.replace('WR', 'R') |
|
157 | 1 | word = word.replace('DG', 'G') |
|
158 | 1 | word = word.replace('QU', 'K') |
|
159 | 1 | word = word.replace('T', 'D') |
|
160 | 1 | word = word.replace('PH', 'F') |
|
161 | |||
162 | # Rule 4 (FL5) |
||
163 | # Although the rule indicates "after the first letter", the test cases |
||
164 | # make it clear that these apply to the first letter also. |
||
165 | 1 | pos = word.find('K', 0) |
|
166 | 1 | while pos != -1: |
|
167 | 1 | if pos > 1 and word[pos - 1 : pos] not in self._uc_vy_set | { |
|
168 | 'L', |
||
169 | 'N', |
||
170 | 'R', |
||
171 | }: |
||
172 | 1 | word = word[: pos - 1] + word[pos:] |
|
173 | 1 | pos -= 1 |
|
174 | 1 | pos = word.find('K', pos + 1) |
|
175 | |||
176 | # Rule FL6 |
||
177 | 1 | if max_length > 0 and word[-1:] == 'E': |
|
178 | 1 | word = word[:-1] |
|
179 | |||
180 | # Rule 5 (FL7) |
||
181 | 1 | word = self._delete_consecutive_repeats(word) |
|
182 | |||
183 | # Rule 6 (FL8) |
||
184 | 1 | if word[:2] == 'PF': |
|
185 | 1 | word = word[1:] |
|
186 | 1 | if word[-2:] == 'PF': |
|
187 | 1 | word = word[:-1] |
|
188 | 1 | elif word[-2:] == 'GH': |
|
189 | 1 | if word[-3:-2] in self._uc_vy_set: |
|
190 | 1 | word = word[:-2] + 'F' |
|
191 | else: |
||
192 | 1 | word = word[:-2] + 'G' |
|
193 | 1 | word = word.replace('GH', '') |
|
194 | |||
195 | # Rule FL9 |
||
196 | 1 | if max_length > 0: |
|
197 | 1 | word = word.replace('V', 'F') |
|
198 | |||
199 | # Rules 7-9 (FL10-FL12) |
||
200 | 1 | first = 1 + (1 if max_length > 0 else 0) |
|
201 | 1 | code = '' |
|
202 | 1 | for pos, char in enumerate(word): |
|
203 | 1 | if char in self._uc_vy_set: |
|
204 | 1 | if first or keep_vowels: |
|
205 | 1 | code += vowel_char |
|
206 | 1 | first -= 1 |
|
207 | 1 | elif pos > 0 and char in {'W', 'H'}: |
|
208 | 1 | continue |
|
209 | else: |
||
210 | 1 | code += char |
|
211 | |||
212 | 1 | if max_length > 0: |
|
213 | # Rule FL13 |
||
214 | 1 | if len(code) > max_length and code[-1:] == 'S': |
|
215 | 1 | code = code[:-1] |
|
216 | 1 | if keep_vowels: |
|
217 | 1 | code = code[:max_length] |
|
218 | else: |
||
219 | # Rule FL14 |
||
220 | 1 | code = code[: max_length + 2] |
|
221 | # Rule FL15 |
||
222 | 1 | while len(code) > max_length: |
|
223 | 1 | vowels = len(code) - max_length |
|
224 | 1 | excess = vowels - 1 |
|
225 | 1 | word = code |
|
226 | 1 | code = '' |
|
227 | 1 | for char in word: |
|
228 | 1 | if char == vowel_char: |
|
229 | 1 | if vowels: |
|
230 | 1 | code += char |
|
231 | 1 | vowels -= 1 |
|
232 | else: |
||
233 | 1 | code += char |
|
234 | 1 | code = code[: max_length + excess] |
|
235 | |||
236 | # Rule FL16 |
||
237 | 1 | code += ' ' * (max_length - len(code)) |
|
238 | |||
239 | 1 | return code |
|
240 | |||
309 |
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.