| Conditions | 51 |
| Total Lines | 209 |
| Code Lines | 128 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 110 |
| CRAP Score | 51 |
| 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.fingerprint._Synoname.SynonameToolcode.fingerprint() 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 -*- |
||
| 270 | 1 | def fingerprint(self, lname, fname='', qual='', normalize=0): |
|
| 271 | """Build the Synoname toolcode. |
||
| 272 | |||
| 273 | Args: |
||
| 274 | lname (str): Last name |
||
| 275 | fname (str): First name (can be blank) |
||
| 276 | qual (str): Qualifier |
||
| 277 | normalize (int): Normalization mode (0, 1, or 2) |
||
| 278 | |||
| 279 | Returns: |
||
| 280 | tuple: The transformed names and the synoname toolcode |
||
| 281 | |||
| 282 | Examples: |
||
| 283 | >>> st = SynonameToolcode() |
||
| 284 | >>> st.fingerprint('hat') |
||
| 285 | ('hat', '', '0000000003$$h') |
||
| 286 | >>> st.fingerprint('niall') |
||
| 287 | ('niall', '', '0000000005$$n') |
||
| 288 | >>> st.fingerprint('colin') |
||
| 289 | ('colin', '', '0000000005$$c') |
||
| 290 | >>> st.fingerprint('atcg') |
||
| 291 | ('atcg', '', '0000000004$$a') |
||
| 292 | >>> st.fingerprint('entreatment') |
||
| 293 | ('entreatment', '', '0000000011$$e') |
||
| 294 | |||
| 295 | >>> st.fingerprint('Ste.-Marie', 'Count John II', normalize=2) |
||
| 296 | ('ste.-marie ii', 'count john', '0200491310$015b049a127c$smcji') |
||
| 297 | >>> st.fingerprint('Michelangelo IV', '', 'Workshop of') |
||
| 298 | ('michelangelo iv', '', '3000550015$055b$mi') |
||
| 299 | |||
| 300 | """ |
||
| 301 | 1 | lname = lname.lower() |
|
| 302 | 1 | fname = fname.lower() |
|
| 303 | 1 | qual = qual.lower() |
|
| 304 | |||
| 305 | # Start with the basic code |
||
| 306 | 1 | toolcode = ['0', '0', '0', '000', '00', '00', '$', '', '$', ''] |
|
| 307 | |||
| 308 | 1 | full_name = ' '.join((lname, fname)) |
|
| 309 | |||
| 310 | 1 | if qual in self.qual_3: |
|
| 311 | 1 | toolcode[0] = '3' |
|
| 312 | 1 | elif qual in self.qual_2: |
|
| 313 | 1 | toolcode[0] = '2' |
|
| 314 | 1 | elif qual in self.qual_1: |
|
| 315 | 1 | toolcode[0] = '1' |
|
| 316 | |||
| 317 | # Fill field 1 (punctuation) |
||
| 318 | 1 | if '.' in full_name: |
|
| 319 | 1 | toolcode[1] = '2' |
|
| 320 | else: |
||
| 321 | 1 | for punct in ',-/:;"&\'()!{|}?$%*+<=>[\\]^_`~': |
|
| 322 | 1 | if punct in full_name: |
|
| 323 | 1 | toolcode[1] = '1' |
|
| 324 | 1 | break |
|
| 325 | |||
| 326 | 1 | elderyounger = '' # save elder/younger for possible movement later |
|
| 327 | 1 | for gen in self.gen_1: |
|
| 328 | 1 | if gen in full_name: |
|
| 329 | 1 | toolcode[2] = '1' |
|
| 330 | 1 | elderyounger = gen |
|
| 331 | 1 | break |
|
| 332 | else: |
||
| 333 | 1 | for gen in self.gen_2: |
|
| 334 | 1 | if gen in full_name: |
|
| 335 | 1 | toolcode[2] = '2' |
|
| 336 | 1 | elderyounger = gen |
|
| 337 | 1 | break |
|
| 338 | |||
| 339 | # do comma flip |
||
| 340 | 1 | if normalize: |
|
| 341 | 1 | comma = lname.find(',') |
|
| 342 | 1 | if comma != -1: |
|
| 343 | 1 | lname_end = lname[comma + 1 :] |
|
| 344 | 1 | while lname_end[0] in {' ', ','}: |
|
| 345 | 1 | lname_end = lname_end[1:] |
|
| 346 | 1 | fname = lname_end + ' ' + fname |
|
| 347 | 1 | lname = lname[:comma].strip() |
|
| 348 | |||
| 349 | # do elder/younger move |
||
| 350 | 1 | if normalize == 2 and elderyounger: |
|
| 351 | 1 | elderyounger_loc = fname.find(elderyounger) |
|
| 352 | 1 | if elderyounger_loc != -1: |
|
| 353 | 1 | lname = ' '.join((lname, elderyounger.strip())) |
|
| 354 | 1 | fname = ' '.join( |
|
| 355 | ( |
||
| 356 | fname[:elderyounger_loc].strip(), |
||
| 357 | fname[elderyounger_loc + len(elderyounger) :], |
||
| 358 | ) |
||
| 359 | ).strip() |
||
| 360 | |||
| 361 | 1 | toolcode[4] = '{:02d}'.format(len(fname)) |
|
| 362 | 1 | toolcode[5] = '{:02d}'.format(len(lname)) |
|
| 363 | |||
| 364 | # strip punctuation |
||
| 365 | 1 | for char in ',/:;"&()!{|}?$%*+<=>[\\]^_`~': |
|
| 366 | 1 | full_name = full_name.replace(char, '') |
|
| 367 | 1 | for pos, char in enumerate(full_name): |
|
| 368 | 1 | if char == '-' and full_name[pos - 1 : pos + 2] != 'b-g': |
|
| 369 | 1 | full_name = full_name[:pos] + ' ' + full_name[pos + 1 :] |
|
| 370 | |||
| 371 | # Fill field 9 (search range) |
||
| 372 | 1 | for letter in [_[0] for _ in full_name.split()]: |
|
| 373 | 1 | if letter not in toolcode[9]: |
|
| 374 | 1 | toolcode[9] += letter |
|
| 375 | 1 | if len(toolcode[9]) == 15: |
|
| 376 | 1 | break |
|
| 377 | |||
| 378 | 1 | def roman_check(numeral, fname, lname): |
|
| 379 | """Move Roman numerals from first name to last. |
||
| 380 | |||
| 381 | Args: |
||
| 382 | numeral (str): Roman numeral |
||
| 383 | fname (str): First name |
||
| 384 | lname (str): Last name |
||
| 385 | |||
| 386 | Returns: |
||
| 387 | tuple: First and last names with Roman numeral moved |
||
| 388 | |||
| 389 | """ |
||
| 390 | 1 | loc = fname.find(numeral) |
|
| 391 | 1 | if fname and ( |
|
| 392 | loc != -1 |
||
| 393 | and (len(fname[loc:]) == len(numeral)) |
||
| 394 | or fname[loc + len(numeral)] in {' ', ','} |
||
| 395 | ): |
||
| 396 | 1 | lname = ' '.join((lname, numeral)) |
|
| 397 | 1 | fname = ' '.join( |
|
| 398 | ( |
||
| 399 | fname[:loc].strip(), |
||
| 400 | fname[loc + len(numeral) :].lstrip(' ,'), |
||
| 401 | ) |
||
| 402 | ) |
||
| 403 | 1 | return fname.strip(), lname.strip() |
|
| 404 | |||
| 405 | # Fill fields 7 (specials) and 3 (roman numerals) |
||
| 406 | 1 | for num, special in enumerate(self.synoname_special_table): |
|
| 407 | 1 | roman, match, extra, method = special |
|
| 408 | 1 | if method & self.method_dict['end']: |
|
| 409 | 1 | match_context = ' ' + match |
|
| 410 | 1 | loc = full_name.find(match_context) |
|
| 411 | 1 | if (len(full_name) > len(match_context)) and ( |
|
| 412 | loc == len(full_name) - len(match_context) |
||
| 413 | ): |
||
| 414 | 1 | if roman: |
|
| 415 | 1 | if not any( |
|
| 416 | abbr in fname for abbr in ('i.', 'v.', 'x.') |
||
| 417 | ): |
||
| 418 | 1 | full_name = full_name[:loc] |
|
| 419 | 1 | toolcode[7] += '{:03d}'.format(num) + 'a' |
|
| 420 | 1 | if toolcode[3] == '000': |
|
| 421 | 1 | toolcode[3] = '{:03d}'.format(num) |
|
| 422 | 1 | if normalize == 2: |
|
| 423 | 1 | fname, lname = roman_check(match, fname, lname) |
|
| 424 | else: |
||
| 425 | 1 | full_name = full_name[:loc] |
|
| 426 | 1 | toolcode[7] += '{:03d}'.format(num) + 'a' |
|
| 427 | 1 | if method & self.method_dict['middle']: |
|
| 428 | 1 | match_context = ' ' + match + ' ' |
|
| 429 | 1 | loc = 0 |
|
| 430 | 1 | while loc != -1: |
|
| 431 | 1 | loc = full_name.find(match_context, loc + 1) |
|
| 432 | 1 | if loc > 0: |
|
| 433 | 1 | if roman: |
|
| 434 | 1 | if not any( |
|
| 435 | abbr in fname for abbr in ('i.', 'v.', 'x.') |
||
| 436 | ): |
||
| 437 | 1 | full_name = ( |
|
| 438 | full_name[:loc] |
||
| 439 | + full_name[loc + len(match) + 1 :] |
||
| 440 | ) |
||
| 441 | 1 | toolcode[7] += '{:03d}'.format(num) + 'b' |
|
| 442 | 1 | if toolcode[3] == '000': |
|
| 443 | 1 | toolcode[3] = '{:03d}'.format(num) |
|
| 444 | 1 | if normalize == 2: |
|
| 445 | 1 | fname, lname = roman_check( |
|
| 446 | match, fname, lname |
||
| 447 | ) |
||
| 448 | else: |
||
| 449 | 1 | full_name = ( |
|
| 450 | full_name[:loc] |
||
| 451 | + full_name[loc + len(match) + 1 :] |
||
| 452 | ) |
||
| 453 | 1 | toolcode[7] += '{:03d}'.format(num) + 'b' |
|
| 454 | 1 | if method & self.method_dict['beginning']: |
|
| 455 | 1 | match_context = match + ' ' |
|
| 456 | 1 | loc = full_name.find(match_context) |
|
| 457 | 1 | if loc == 0: |
|
| 458 | 1 | full_name = full_name[len(match) + 1 :] |
|
| 459 | 1 | toolcode[7] += '{:03d}'.format(num) + 'c' |
|
| 460 | 1 | if method & self.method_dict['beginning_no_space']: |
|
| 461 | 1 | loc = full_name.find(match) |
|
| 462 | 1 | if loc == 0: |
|
| 463 | 1 | toolcode[7] += '{:03d}'.format(num) + 'd' |
|
| 464 | 1 | if full_name[: len(match)] not in toolcode[9]: |
|
| 465 | 1 | toolcode[9] += full_name[: len(match)] |
|
| 466 | |||
| 467 | 1 | if extra: |
|
| 468 | 1 | loc = full_name.find(extra) |
|
| 469 | 1 | if loc != -1: |
|
| 470 | 1 | toolcode[7] += '{:03d}'.format(num) + 'X' |
|
| 471 | # Since extras are unique, we only look for each of them |
||
| 472 | # once, and they include otherwise impossible characters |
||
| 473 | # for this field, it's not possible for the following line |
||
| 474 | # to have ever been false. |
||
| 475 | # if full_name[loc:loc+len(extra)] not in toolcode[9]: |
||
| 476 | 1 | toolcode[9] += full_name[loc : loc + len(match)] |
|
| 477 | |||
| 478 | 1 | return lname, fname, ''.join(toolcode) |
|
| 479 | |||
| 520 |
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.