Conditions | 51 |
Total Lines | 332 |
Code Lines | 293 |
Lines | 0 |
Ratio | 0 % |
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_toolcode() 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 -*- |
||
404 | def synoname_toolcode(lname, fname='', qual='', normalize=0): |
||
405 | """Build the Synoname toolcode. |
||
406 | |||
407 | :param lname: last name |
||
408 | :param fname: first name (can be blank) |
||
409 | :param qual: qualifier |
||
410 | :return: |
||
411 | """ |
||
412 | method_dict = {'end': 1, 'middle': 2, 'beginning': 4, |
||
413 | 'beginning_no_space': 8} |
||
414 | special_table = ( |
||
415 | # Roman, string, extra, method |
||
416 | (False, 'NONE', '', 0), |
||
417 | (False, 'aine', '', 3), |
||
418 | (False, 'also erroneously', '', 4), |
||
419 | (False, 'also identified with the', '', 2), |
||
420 | (False, 'also identified with', '', 2), |
||
421 | (False, 'archbishop', '', 7), |
||
422 | (False, 'atelier', '', 7), |
||
423 | (False, 'baron', '', 7), |
||
424 | (False, 'cadet', '', 3), |
||
425 | (False, 'cardinal', '', 7), |
||
426 | (False, 'circle of', '', 5), |
||
427 | (False, 'circle', '', 5), |
||
428 | (False, 'class of', '', 5), |
||
429 | (False, 'conde de', '', 7), |
||
430 | (False, 'countess', '', 7), |
||
431 | (False, 'count', '', 7), |
||
432 | (False, "d'", " d'", 15), |
||
433 | (False, 'dai', '', 15), |
||
434 | (False, "dall'", " dall'", 15), |
||
435 | (False, 'dalla', '', 15), |
||
436 | (False, 'dalle', '', 15), |
||
437 | (False, 'dal', '', 15), |
||
438 | (False, 'da', '', 15), |
||
439 | (False, 'degli', '', 15), |
||
440 | (False, 'della', '', 15), |
||
441 | (False, 'del', '', 15), |
||
442 | (False, 'den', '', 15), |
||
443 | (False, 'der altere', '', 3), |
||
444 | (False, 'der jungere', '', 3), |
||
445 | (False, 'der', '', 15), |
||
446 | (False, 'de la', '', 15), |
||
447 | (False, 'des', '', 15), |
||
448 | (False, "de'", " de'", 15), |
||
449 | (False, 'de', '', 15), |
||
450 | (False, 'di ser', '', 7), |
||
451 | (False, 'di', '', 15), |
||
452 | (False, 'dos', '', 15), |
||
453 | (False, 'du', '', 15), |
||
454 | (False, 'duke of', '', 7), |
||
455 | (False, 'earl of', '', 7), |
||
456 | (False, 'el', '', 15), |
||
457 | (False, 'fils', '', 3), |
||
458 | (False, 'florentine follower of', '', 5), |
||
459 | (False, 'follower of', '', 5), |
||
460 | (False, 'fra', '', 7), |
||
461 | (False, 'freiherr von', '', 7), |
||
462 | (False, 'giovane', '', 7), |
||
463 | (False, 'group', '', 5), |
||
464 | (True, 'iii', '', 3), |
||
465 | (True, 'ii', '', 3), |
||
466 | (False, 'il giovane', '', 7), |
||
467 | (False, 'il vecchio', '', 7), |
||
468 | (False, 'il', '', 15), |
||
469 | (False, "in't", '', 7), |
||
470 | (False, 'in het', '', 7), |
||
471 | (True, 'iv', '', 3), |
||
472 | (True, 'ix', '', 3), |
||
473 | (True, 'i', '', 3), |
||
474 | (False, 'jr.', '', 3), |
||
475 | (False, 'jr', '', 3), |
||
476 | (False, 'juniore', '', 3), |
||
477 | (False, 'junior', '', 3), |
||
478 | (False, 'king of', '', 7), |
||
479 | (False, "l'", " l'", 15), |
||
480 | (False, "l'aine", '', 3), |
||
481 | (False, 'la', '', 15), |
||
482 | (False, 'le jeune', '', 3), |
||
483 | (False, 'le', '', 15), |
||
484 | (False, 'lo', '', 15), |
||
485 | (False, 'maestro', '', 7), |
||
486 | (False, 'maitre', '', 7), |
||
487 | (False, 'marchioness', '', 7), |
||
488 | (False, 'markgrafin von', '', 7), |
||
489 | (False, 'marquess', '', 7), |
||
490 | (False, 'marquis', '', 7), |
||
491 | (False, 'master of the', '', 7), |
||
492 | (False, 'master of', '', 7), |
||
493 | (False, 'master known as the', '', 7), |
||
494 | (False, 'master with the', '', 7), |
||
495 | (False, 'master with', '', 7), |
||
496 | (False, 'masters', '', 7), |
||
497 | (False, 'master', '', 7), |
||
498 | (False, 'meister', '', 7), |
||
499 | (False, 'met de', '', 7), |
||
500 | (False, 'met', '', 7), |
||
501 | (False, 'mlle.', '', 7), |
||
502 | (False, 'mlle', '', 7), |
||
503 | (False, 'monogrammist', '', 7), |
||
504 | (False, 'monsu', '', 7), |
||
505 | (False, 'nee', '', 2), |
||
506 | (False, 'of', '', 3), |
||
507 | (False, 'oncle', '', 3), |
||
508 | (False, 'op den', '', 15), |
||
509 | (False, 'op de', '', 15), |
||
510 | (False, 'or', '', 2), |
||
511 | (False, 'over den', '', 15), |
||
512 | (False, 'over de', '', 15), |
||
513 | (False, 'over', '', 7), |
||
514 | (False, 'p.re', '', 7), |
||
515 | (False, 'p.r.a.', '', 1), |
||
516 | (False, 'padre', '', 7), |
||
517 | (False, 'painter', '', 7), |
||
518 | (False, 'pere', '', 3), |
||
519 | (False, 'possibly identified with', '', 6), |
||
520 | (False, 'possibly', '', 6), |
||
521 | (False, 'pseudo', '', 15), |
||
522 | (False, 'r.a.', '', 1), |
||
523 | (False, 'reichsgraf von', '', 7), |
||
524 | (False, 'ritter von', '', 7), |
||
525 | (False, 'sainte-', ' sainte-', 8), |
||
526 | (False, 'sainte', '', 7), |
||
527 | (False, 'saint-', ' saint-', 8), |
||
528 | (False, 'saint', '', 7), |
||
529 | (False, 'santa', '', 15), |
||
530 | (False, "sant'", " sant'", 15), |
||
531 | (False, 'san', '', 15), |
||
532 | (False, 'ser', '', 7), |
||
533 | (False, 'seniore', '', 3), |
||
534 | (False, 'senior', '', 3), |
||
535 | (False, 'sir', '', 5), |
||
536 | (False, 'sr.', '', 3), |
||
537 | (False, 'sr', '', 3), |
||
538 | (False, 'ss.', ' ss.', 14), |
||
539 | (False, 'ss', '', 6), |
||
540 | (False, 'st-', ' st-', 8), |
||
541 | (False, 'st.', ' st.', 15), |
||
542 | (False, 'ste-', ' ste-', 8), |
||
543 | (False, 'ste.', ' ste.', 15), |
||
544 | (False, 'studio', '', 7), |
||
545 | (False, 'sub-group', '', 5), |
||
546 | (False, 'sultan of', '', 7), |
||
547 | (False, 'ten', '', 15), |
||
548 | (False, 'ter', '', 15), |
||
549 | (False, 'the elder', '', 3), |
||
550 | (False, 'the younger', '', 3), |
||
551 | (False, 'the', '', 7), |
||
552 | (False, 'tot', '', 15), |
||
553 | (False, 'unidentified', '', 1), |
||
554 | (False, 'van den', '', 15), |
||
555 | (False, 'van der', '', 15), |
||
556 | (False, 'van de', '', 15), |
||
557 | (False, 'vanden', '', 15), |
||
558 | (False, 'vander', '', 15), |
||
559 | (False, 'van', '', 15), |
||
560 | (False, 'vecchia', '', 7), |
||
561 | (False, 'vecchio', '', 7), |
||
562 | (True, 'viii', '', 3), |
||
563 | (True, 'vii', '', 3), |
||
564 | (True, 'vi', '', 3), |
||
565 | (True, 'v', '', 3), |
||
566 | (False, 'vom', '', 7), |
||
567 | (False, 'von', '', 15), |
||
568 | (False, 'workshop', '', 7), |
||
569 | (True, 'xiii', '', 3), |
||
570 | (True, 'xii', '', 3), |
||
571 | (True, 'xiv', '', 3), |
||
572 | (True, 'xix', '', 3), |
||
573 | (True, 'xi', '', 3), |
||
574 | (True, 'xviii', '', 3), |
||
575 | (True, 'xvii', '', 3), |
||
576 | (True, 'xvi', '', 3), |
||
577 | (True, 'xv', '', 3), |
||
578 | (True, 'xx', '', 3), |
||
579 | (True, 'x', '', 3), |
||
580 | (False, 'y', '', 7) |
||
581 | ) |
||
582 | |||
583 | # Start with the basic code |
||
584 | toolcode = ['0', '0', '0', '000', '00', '00', '$', '', '$', ''] |
||
585 | |||
586 | full_name = ' '.join((lname, fname)) |
||
587 | |||
588 | # Fill field 0 (qualifier) |
||
589 | qual_3 = {'adaptation after', 'after', 'assistant of', 'assistants of', |
||
590 | 'circle of', 'follower of', 'imitator of', 'in the style of', |
||
591 | 'manner of', 'pupil of', 'school of', 'studio of', |
||
592 | 'style of', 'workshop of'} |
||
593 | qual_2 = {'copy after', 'copy after?', 'copy of'} |
||
594 | qual_1 = {'ascribed to', 'attributed to or copy after', |
||
595 | 'attributed to', 'possibly'} |
||
596 | |||
597 | if qual in qual_3: |
||
598 | toolcode[0] = '3' |
||
599 | elif qual in qual_2: |
||
600 | toolcode[0] = '2' |
||
601 | elif qual in qual_1: |
||
602 | toolcode[0] = '1' |
||
603 | |||
604 | # Fill field 1 (punctuation) |
||
605 | if '.' in full_name: |
||
606 | toolcode[1] = '2' |
||
607 | else: |
||
608 | for punct in ',-/:;"&\'()!{|}?$%*+<=>[\\]^_`~': |
||
609 | if punct in full_name: |
||
610 | toolcode[1] = '1' |
||
611 | break |
||
612 | |||
613 | # Fill field 2 (generation) |
||
614 | gen_1 = ('the elder', ' sr.', ' sr', 'senior', 'der altere', 'il vecchio', |
||
615 | "l'aine", 'p.re', 'padre', 'seniore', 'vecchia', 'vecchio') |
||
616 | gen_2 = (' jr.', ' jr', 'der jungere', 'il giovane', 'giovane', 'juniore', |
||
617 | 'junior', 'le jeune', 'the younger') |
||
618 | |||
619 | elderyounger = '' # save elder/younger for possible movement later |
||
620 | for gen in gen_1: |
||
621 | if gen in full_name: |
||
622 | toolcode[2] = '1' |
||
623 | elderyounger = gen |
||
624 | break |
||
625 | else: |
||
626 | for gen in gen_2: |
||
627 | if gen in full_name: |
||
628 | toolcode[2] = '2' |
||
629 | elderyounger = gen |
||
630 | break |
||
631 | |||
632 | # do comma flip |
||
633 | if normalize: |
||
634 | comma = lname.find(',') |
||
635 | if comma != -1: |
||
636 | lname_end = lname[comma + 1:] |
||
637 | while lname_end[0] in {' ', ','}: |
||
638 | lname_end = lname_end[1:] |
||
639 | fname = lname_end + ' ' + fname |
||
640 | lname = lname[:comma].strip() |
||
641 | |||
642 | # do elder/younger move |
||
643 | if normalize == 2 and elderyounger: |
||
644 | elderyounger_loc = fname.find(elderyounger) |
||
645 | if elderyounger_loc != -1: |
||
646 | lname = lname + ' ' + elderyounger.strip() |
||
647 | fname = (fname[:elderyounger_loc].strip() + ' ' + |
||
648 | fname[elderyounger_loc + len(elderyounger):]) |
||
649 | |||
650 | toolcode[4] = '{:02d}'.format(len(fname)) |
||
651 | toolcode[5] = '{:02d}'.format(len(lname)) |
||
652 | |||
653 | # strip punctuation |
||
654 | for char in ',/:;"&()!{|}?$%*+<=>[\\]^_`~': |
||
655 | full_name = full_name.replace(char, '') |
||
656 | for pos, char in enumerate(full_name): |
||
657 | if char == '-' and full_name[pos - 1:pos + 2] != 'b-g': |
||
658 | full_name = full_name[:pos] + ' ' + full_name[pos + 1:] |
||
659 | |||
660 | # Fill field 9 (search range) |
||
661 | for letter in [_[0] for _ in full_name.split()]: |
||
662 | if letter not in toolcode[9]: |
||
663 | toolcode[9] += letter |
||
664 | if len(toolcode[9]) == 15: |
||
665 | break |
||
666 | |||
667 | def roman_check(numeral, fname, lname): |
||
668 | """Move Roman numerals from first name to last.""" |
||
669 | loc = fname.find(numeral) |
||
670 | if (loc != -1 and |
||
671 | (fname[loc + len(numeral)] in {' ', ','} or |
||
672 | len(fname[loc:]) == len(numeral))): |
||
673 | lname += ' ' + numeral |
||
674 | fname = fname[:loc].strip() |
||
675 | while fname[-1] in {' ', ','}: |
||
676 | fname = fname[:-1] |
||
677 | return fname, lname |
||
678 | |||
679 | # Fill fields 7 (specials) and 3 (roman numerals) |
||
680 | for num, special in enumerate(special_table): |
||
681 | roman, string, extra, method = special |
||
682 | if method & method_dict['end']: |
||
683 | string_context = ' ' + string |
||
684 | loc = full_name.find(string_context) |
||
685 | if ((len(full_name) > len(string_context)) and |
||
686 | (loc == len(full_name) - len(string_context))): |
||
687 | if roman: |
||
688 | if not any(abbr in fname for abbr in ('i.', 'v.', 'x.')): |
||
689 | full_name = full_name[:loc] |
||
690 | toolcode[7] += '{:03d}'.format(num) + 'a' |
||
691 | if not toolcode[3]: |
||
692 | toolcode[3] = '{:03d}'.format(num) |
||
693 | if normalize == 2: |
||
694 | fname, lname = roman_check(string, fname, lname) |
||
695 | else: |
||
696 | full_name = full_name[:loc] |
||
697 | toolcode[7] += '{:03d}'.format(num) + 'a' |
||
698 | if method & method_dict['middle']: |
||
699 | string_context = ' ' + string + ' ' |
||
700 | loc = full_name.find(string_context) |
||
701 | if loc > 0: |
||
702 | if roman: |
||
703 | if not any(abbr in fname for abbr in ('i.', 'v.', 'x.')): |
||
704 | full_name = (full_name[:loc] + |
||
705 | full_name[loc + len(string) + 1:]) |
||
706 | toolcode[7] += '{:03d}'.format(num) + 'b' |
||
707 | if not toolcode[3]: |
||
708 | toolcode[3] = '{:03d}'.format(num) |
||
709 | if normalize == 2: |
||
710 | fname, lname = roman_check(string, fname, lname) |
||
711 | else: |
||
712 | full_name = (full_name[:loc] + |
||
713 | full_name[loc + len(string) + 1:]) |
||
714 | toolcode[7] += '{:03d}'.format(num) + 'b' |
||
715 | if method & method_dict['beginning']: |
||
716 | string_context = string + ' ' |
||
717 | loc = full_name.find(string_context) |
||
718 | if loc == 0: |
||
719 | full_name = full_name[len(string) + 1:] |
||
720 | toolcode[7] += '{:03d}'.format(num) + 'c' |
||
721 | if method & method_dict['beginning_no_space']: |
||
722 | loc = full_name.find(string) |
||
723 | if loc == 0: |
||
724 | toolcode[7] += '{:03d}'.format(num) + 'd' |
||
725 | if full_name[len(string)] not in toolcode[9]: |
||
726 | toolcode[9] += full_name[len(string)] |
||
727 | |||
728 | if extra: |
||
729 | loc = full_name.find(extra) |
||
730 | if loc != -1: |
||
731 | toolcode[7] += '{:03d}'.format(num) + 'X' |
||
732 | if full_name[loc + len(extra)] not in toolcode[9]: |
||
733 | toolcode[9] += full_name[loc + len(string)] |
||
734 | |||
735 | return lname, fname, ''.join(toolcode) |
||
736 | |||
741 |