| Conditions | 25 | 
| Total Lines | 100 | 
| Code Lines | 64 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Tests | 59 | 
| CRAP Score | 25 | 
| 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.distance._sift4.Sift4.dist_abs() 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 -*-  | 
            ||
| 48 | 1 | def dist_abs(self, src, tar, max_offset=5, max_distance=0):  | 
            |
| 49 | """Return the "common" Sift4 distance between two terms.  | 
            ||
| 50 | |||
| 51 | :param str src: source string for comparison  | 
            ||
| 52 | :param str tar: target string for comparison  | 
            ||
| 53 | :param max_offset: the number of characters to search for matching  | 
            ||
| 54 | letters  | 
            ||
| 55 | :param max_distance: the distance at which to stop and exit  | 
            ||
| 56 | :returns: the Sift4 distance according to the common formula  | 
            ||
| 57 | :rtype: int  | 
            ||
| 58 | |||
| 59 | >>> cmp = Sift4()  | 
            ||
| 60 |         >>> cmp.dist_abs('cat', 'hat') | 
            ||
| 61 | 1  | 
            ||
| 62 |         >>> cmp.dist_abs('Niall', 'Neil') | 
            ||
| 63 | 2  | 
            ||
| 64 |         >>> cmp.dist_abs('Colin', 'Cuilen') | 
            ||
| 65 | 3  | 
            ||
| 66 |         >>> cmp.dist_abs('ATCG', 'TAGC') | 
            ||
| 67 | 2  | 
            ||
| 68 | """  | 
            ||
| 69 | 1 | if not src:  | 
            |
| 70 | 1 | return len(tar)  | 
            |
| 71 | |||
| 72 | 1 | if not tar:  | 
            |
| 73 | 1 | return len(src)  | 
            |
| 74 | |||
| 75 | 1 | src_len = len(src)  | 
            |
| 76 | 1 | tar_len = len(tar)  | 
            |
| 77 | |||
| 78 | 1 | src_cur = 0  | 
            |
| 79 | 1 | tar_cur = 0  | 
            |
| 80 | 1 | lcss = 0  | 
            |
| 81 | 1 | local_cs = 0  | 
            |
| 82 | 1 | trans = 0  | 
            |
| 83 | 1 | offset_arr = []  | 
            |
| 84 | |||
| 85 | 1 | while (src_cur < src_len) and (tar_cur < tar_len):  | 
            |
| 86 | 1 | if src[src_cur] == tar[tar_cur]:  | 
            |
| 87 | 1 | local_cs += 1  | 
            |
| 88 | 1 | is_trans = False  | 
            |
| 89 | 1 | i = 0  | 
            |
| 90 | 1 | while i < len(offset_arr):  | 
            |
| 91 | 1 | ofs = offset_arr[i]  | 
            |
| 92 | 1 | if src_cur <= ofs['src_cur'] or tar_cur <= ofs['tar_cur']:  | 
            |
| 93 | 1 | is_trans = abs(tar_cur - src_cur) >= abs(  | 
            |
| 94 | ofs['tar_cur'] - ofs['src_cur']  | 
            ||
| 95 | )  | 
            ||
| 96 | 1 | if is_trans:  | 
            |
| 97 | 1 | trans += 1  | 
            |
| 98 | 1 | elif not ofs['trans']:  | 
            |
| 99 | 1 | ofs['trans'] = True  | 
            |
| 100 | 1 | trans += 1  | 
            |
| 101 | 1 | break  | 
            |
| 102 | 1 | elif src_cur > ofs['tar_cur'] and tar_cur > ofs['src_cur']:  | 
            |
| 103 | 1 | del offset_arr[i]  | 
            |
| 104 | else:  | 
            ||
| 105 | 1 | i += 1  | 
            |
| 106 | |||
| 107 | 1 | offset_arr.append(  | 
            |
| 108 |                     {'src_cur': src_cur, 'tar_cur': tar_cur, 'trans': is_trans} | 
            ||
| 109 | )  | 
            ||
| 110 | else:  | 
            ||
| 111 | 1 | lcss += local_cs  | 
            |
| 112 | 1 | local_cs = 0  | 
            |
| 113 | 1 | if src_cur != tar_cur:  | 
            |
| 114 | 1 | src_cur = tar_cur = min(src_cur, tar_cur)  | 
            |
| 115 | 1 | for i in range(max_offset):  | 
            |
| 116 | 1 | if not (  | 
            |
| 117 | (src_cur + i < src_len) or (tar_cur + i < tar_len)  | 
            ||
| 118 | ):  | 
            ||
| 119 | 1 | break  | 
            |
| 120 | 1 | if (src_cur + i < src_len) and (  | 
            |
| 121 | src[src_cur + i] == tar[tar_cur]  | 
            ||
| 122 | ):  | 
            ||
| 123 | 1 | src_cur += i - 1  | 
            |
| 124 | 1 | tar_cur -= 1  | 
            |
| 125 | 1 | break  | 
            |
| 126 | 1 | if (tar_cur + i < tar_len) and (  | 
            |
| 127 | src[src_cur] == tar[tar_cur + i]  | 
            ||
| 128 | ):  | 
            ||
| 129 | 1 | src_cur -= 1  | 
            |
| 130 | 1 | tar_cur += i - 1  | 
            |
| 131 | 1 | break  | 
            |
| 132 | |||
| 133 | 1 | src_cur += 1  | 
            |
| 134 | 1 | tar_cur += 1  | 
            |
| 135 | |||
| 136 | 1 | if max_distance:  | 
            |
| 137 | 1 | temporary_distance = max(src_cur, tar_cur) - lcss + trans  | 
            |
| 138 | 1 | if temporary_distance >= max_distance:  | 
            |
| 139 | 1 | return round(temporary_distance)  | 
            |
| 140 | |||
| 141 | 1 | if (src_cur >= src_len) or (tar_cur >= tar_len):  | 
            |
| 142 | 1 | lcss += local_cs  | 
            |
| 143 | 1 | local_cs = 0  | 
            |
| 144 | 1 | src_cur = tar_cur = min(src_cur, tar_cur)  | 
            |
| 145 | |||
| 146 | 1 | lcss += local_cs  | 
            |
| 147 | 1 | return round(max(src_len, tar_len) - lcss + trans)  | 
            |
| 148 | |||
| 350 |