| Conditions | 21 |
| Total Lines | 98 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 50 |
| CRAP Score | 21 |
| 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._lc_cutter.LCCutter.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 -*- |
||
| 72 | 1 | def fingerprint(self, word): |
|
| 73 | """Return the Library of Congress Cutter table encoding of a word. |
||
| 74 | |||
| 75 | Parameters |
||
| 76 | ---------- |
||
| 77 | word : str |
||
| 78 | The word to fingerprint |
||
| 79 | |||
| 80 | Returns |
||
| 81 | ------- |
||
| 82 | str |
||
| 83 | The Library of Congress Cutter table encoding |
||
| 84 | |||
| 85 | Examples |
||
| 86 | -------- |
||
| 87 | >>> cf = LCCutter() |
||
| 88 | >>> cf.fingerprint('hat') |
||
| 89 | 'H38' |
||
| 90 | >>> cf.fingerprint('niall') |
||
| 91 | 'N5355' |
||
| 92 | >>> cf.fingerprint('colin') |
||
| 93 | 'C6556' |
||
| 94 | >>> cf.fingerprint('atcg') |
||
| 95 | 'A834' |
||
| 96 | >>> cf.fingerprint('entreatment') |
||
| 97 | 'E5874386468' |
||
| 98 | |||
| 99 | |||
| 100 | .. versionadded:: 0.4.1 |
||
| 101 | |||
| 102 | """ |
||
| 103 | # uppercase |
||
| 104 | 1 | uc = ''.join(letter for letter in word.upper() if letter.isalpha()) |
|
| 105 | |||
| 106 | 1 | if not uc: |
|
| 107 | 1 | return '' |
|
| 108 | |||
| 109 | 1 | code = uc[0] |
|
| 110 | |||
| 111 | # length 1 |
||
| 112 | 1 | if len(uc) == 1: |
|
| 113 | 1 | return code |
|
| 114 | |||
| 115 | # length 2+ |
||
| 116 | 1 | code = [code] |
|
| 117 | |||
| 118 | # first cutter |
||
| 119 | 1 | pos = 1 |
|
| 120 | 1 | if uc[0] in self._vowels: |
|
| 121 | 1 | cval = 2 |
|
| 122 | 1 | for letter in self._after_initial_vowel: |
|
| 123 | 1 | if uc[1] > letter: |
|
| 124 | 1 | cval += 1 |
|
| 125 | else: |
||
| 126 | 1 | break |
|
| 127 | 1 | elif uc[0] == 'S': |
|
| 128 | 1 | cval = 2 |
|
| 129 | 1 | for letter in self._after_initial_s: |
|
| 130 | 1 | if uc[1] > letter: |
|
| 131 | 1 | cval += 1 |
|
| 132 | 1 | elif uc[1] == 'C' and uc[1:3] < 'CI': |
|
| 133 | 1 | cval += 1 |
|
| 134 | 1 | pos += 1 |
|
| 135 | 1 | break |
|
| 136 | else: |
||
| 137 | 1 | break |
|
| 138 | 1 | elif uc[0:2] == 'QU': |
|
| 139 | 1 | cval = 3 |
|
| 140 | 1 | pos += 1 |
|
| 141 | 1 | for letter in self._after_initial_qu: |
|
| 142 | 1 | if uc[2:3] > letter: |
|
| 143 | 1 | cval += 1 |
|
| 144 | else: |
||
| 145 | 1 | break |
|
| 146 | 1 | elif 'QA' <= uc[0:2] <= 'QT': |
|
| 147 | 1 | cval = 2 |
|
| 148 | else: |
||
| 149 | 1 | cval = 3 |
|
| 150 | 1 | for letter in self._after_initial_cons: |
|
| 151 | 1 | if uc[1] > letter: |
|
| 152 | 1 | cval += 1 |
|
| 153 | else: |
||
| 154 | 1 | break |
|
| 155 | 1 | code.append(str(cval)) |
|
| 156 | |||
| 157 | # length 3+ |
||
| 158 | 1 | for ch in uc[pos + 1 :]: |
|
| 159 | 1 | if len(code) >= self._max_length: |
|
| 160 | 1 | break |
|
| 161 | 1 | cval = 3 |
|
| 162 | 1 | for letter in self._expansions: |
|
| 163 | 1 | if ch > letter: |
|
| 164 | 1 | cval += 1 |
|
| 165 | else: |
||
| 166 | 1 | break |
|
| 167 | 1 | code.append(str(cval)) |
|
| 168 | |||
| 169 | 1 | return ''.join(code[: self._max_length]) |
|
| 170 | |||
| 176 |