| Conditions | 24 |
| Total Lines | 149 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 58 |
| CRAP Score | 24 |
| 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._Phonix.Phonix.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 -*- |
||
| 182 | 1 | def encode(self, word, max_length=4, zero_pad=True): |
|
| 183 | """Return the Phonix code for a word. |
||
| 184 | |||
| 185 | Args: |
||
| 186 | word (str): The word to transform |
||
| 187 | max_length (int): The length of the code returned (defaults to 4) |
||
| 188 | zero_pad (bool): pad the end of the return value with 0s to achieve |
||
| 189 | a max_length string |
||
| 190 | |||
| 191 | Returns: |
||
| 192 | str: The Phonix value |
||
| 193 | |||
| 194 | Examples: |
||
| 195 | >>> pe = Phonix() |
||
| 196 | >>> pe.encode('Christopher') |
||
| 197 | 'K683' |
||
| 198 | >>> pe.encode('Niall') |
||
| 199 | 'N400' |
||
| 200 | >>> pe.encode('Smith') |
||
| 201 | 'S530' |
||
| 202 | >>> pe.encode('Schmidt') |
||
| 203 | 'S530' |
||
| 204 | |||
| 205 | """ |
||
| 206 | |||
| 207 | 1 | def _start_repl(word, src, tar, post=None): |
|
| 208 | """Replace src with tar at the start of word. |
||
| 209 | |||
| 210 | Args: |
||
| 211 | word (str): The word to modify |
||
| 212 | src (str): Substring to match |
||
| 213 | tar (str): Substring to substitute |
||
| 214 | post (set): Following characters |
||
| 215 | |||
| 216 | Returns: |
||
| 217 | str: Modified string |
||
| 218 | |||
| 219 | """ |
||
| 220 | 1 | if post: |
|
| 221 | 1 | for i in post: |
|
| 222 | 1 | if word.startswith(src + i): |
|
| 223 | 1 | return tar + word[len(src) :] |
|
| 224 | 1 | elif word.startswith(src): |
|
| 225 | 1 | return tar + word[len(src) :] |
|
| 226 | 1 | return word |
|
| 227 | |||
| 228 | 1 | def _end_repl(word, src, tar, pre=None): |
|
| 229 | """Replace src with tar at the end of word. |
||
| 230 | |||
| 231 | Args: |
||
| 232 | word (str): The word to modify |
||
| 233 | src (str): Substring to match |
||
| 234 | tar (str): Substring to substitute |
||
| 235 | pre (set): Preceding characters |
||
| 236 | |||
| 237 | Returns: |
||
| 238 | str: Modified string |
||
| 239 | |||
| 240 | """ |
||
| 241 | 1 | if pre: |
|
| 242 | 1 | for i in pre: |
|
| 243 | 1 | if word.endswith(i + src): |
|
| 244 | 1 | return word[: -len(src)] + tar |
|
| 245 | 1 | elif word.endswith(src): |
|
| 246 | 1 | return word[: -len(src)] + tar |
|
| 247 | 1 | return word |
|
| 248 | |||
| 249 | 1 | def _mid_repl(word, src, tar, pre=None, post=None): |
|
| 250 | """Replace src with tar in the middle of word. |
||
| 251 | |||
| 252 | Args: |
||
| 253 | word (str): The word to modify |
||
| 254 | src (str): Substring to match |
||
| 255 | tar (str): Substring to substitute |
||
| 256 | pre (set): Preceding characters |
||
| 257 | post (set): Following characters |
||
| 258 | |||
| 259 | Returns: |
||
| 260 | str: Modified string |
||
| 261 | |||
| 262 | """ |
||
| 263 | 1 | if pre or post: |
|
| 264 | 1 | if not pre: |
|
| 265 | 1 | return word[0] + _all_repl(word[1:], src, tar, pre, post) |
|
| 266 | 1 | elif not post: |
|
| 267 | 1 | return _all_repl(word[:-1], src, tar, pre, post) + word[-1] |
|
| 268 | 1 | return _all_repl(word, src, tar, pre, post) |
|
| 269 | 1 | return ( |
|
| 270 | word[0] + _all_repl(word[1:-1], src, tar, pre, post) + word[-1] |
||
| 271 | ) |
||
| 272 | |||
| 273 | 1 | def _all_repl(word, src, tar, pre=None, post=None): |
|
| 274 | """Replace src with tar anywhere in word. |
||
| 275 | |||
| 276 | Args: |
||
| 277 | word (str): The word to modify |
||
| 278 | src (str): Substring to match |
||
| 279 | tar (str): Substring to substitute |
||
| 280 | pre (set): Preceding characters |
||
| 281 | post (set): Following characters |
||
| 282 | |||
| 283 | Returns: |
||
| 284 | str: Modified string |
||
| 285 | |||
| 286 | """ |
||
| 287 | 1 | if pre or post: |
|
| 288 | 1 | if post: |
|
| 289 | 1 | post = post |
|
| 290 | else: |
||
| 291 | 1 | post = frozenset(('',)) |
|
| 292 | 1 | if pre: |
|
| 293 | 1 | pre = pre |
|
| 294 | else: |
||
| 295 | 1 | pre = frozenset(('',)) |
|
| 296 | |||
| 297 | 1 | for i, j in ((i, j) for i in pre for j in post): |
|
| 298 | 1 | word = word.replace(i + src + j, i + tar + j) |
|
| 299 | 1 | return word |
|
| 300 | else: |
||
| 301 | 1 | return word.replace(src, tar) |
|
| 302 | |||
| 303 | 1 | repl_at = (_start_repl, _end_repl, _mid_repl, _all_repl) |
|
| 304 | |||
| 305 | 1 | sdx = '' |
|
| 306 | |||
| 307 | 1 | word = unicode_normalize('NFKD', text_type(word.upper())) |
|
| 308 | 1 | word = word.replace('ß', 'SS') |
|
| 309 | 1 | word = ''.join(c for c in word if c in self._uc_set) |
|
| 310 | 1 | if word: |
|
| 311 | 1 | for trans in self._substitutions: |
|
| 312 | 1 | word = repl_at[trans[0]](word, *trans[1:]) |
|
| 313 | 1 | if word[0] in self._uc_vy_set: |
|
| 314 | 1 | sdx = 'v' + word[1:].translate(self._trans) |
|
| 315 | else: |
||
| 316 | 1 | sdx = word[0] + word[1:].translate(self._trans) |
|
| 317 | 1 | sdx = self._delete_consecutive_repeats(sdx) |
|
| 318 | 1 | sdx = sdx.replace('0', '') |
|
| 319 | |||
| 320 | # Clamp max_length to [4, 64] |
||
| 321 | 1 | if max_length != -1: |
|
| 322 | 1 | max_length = min(max(4, max_length), 64) |
|
| 323 | else: |
||
| 324 | 1 | max_length = 64 |
|
| 325 | |||
| 326 | 1 | if zero_pad: |
|
| 327 | 1 | sdx += '0' * max_length |
|
| 328 | 1 | if not sdx: |
|
| 329 | 1 | sdx = '0' |
|
| 330 | 1 | return sdx[:max_length] |
|
| 331 | |||
| 365 |
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.