Conditions | 12 |
Total Lines | 66 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Tests | 33 |
CRAP Score | 12 |
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._mra.MRA.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 -*- |
||
46 | 1 | def dist_abs(self, src, tar): |
|
47 | """Return the MRA comparison rating of two strings. |
||
48 | |||
49 | Parameters |
||
50 | ---------- |
||
51 | src : str |
||
52 | Source string for comparison |
||
53 | tar : str |
||
54 | Target string for comparison |
||
55 | |||
56 | Returns |
||
57 | ------- |
||
58 | int |
||
59 | MRA comparison rating |
||
60 | |||
61 | Examples |
||
62 | -------- |
||
63 | >>> cmp = MRA() |
||
64 | >>> cmp.dist_abs('cat', 'hat') |
||
65 | 5 |
||
66 | >>> cmp.dist_abs('Niall', 'Neil') |
||
67 | 6 |
||
68 | >>> cmp.dist_abs('aluminum', 'Catalan') |
||
69 | 0 |
||
70 | >>> cmp.dist_abs('ATCG', 'TAGC') |
||
71 | 5 |
||
72 | |||
73 | """ |
||
74 | 1 | if src == tar: |
|
75 | 1 | return 6 |
|
76 | 1 | if src == '' or tar == '': |
|
77 | 1 | return 0 |
|
78 | 1 | src = list(mra(src)) |
|
79 | 1 | tar = list(mra(tar)) |
|
80 | |||
81 | 1 | if abs(len(src) - len(tar)) > 2: |
|
82 | 1 | return 0 |
|
83 | |||
84 | 1 | length_sum = len(src) + len(tar) |
|
85 | 1 | if length_sum < 5: |
|
86 | 1 | min_rating = 5 |
|
87 | 1 | elif length_sum < 8: |
|
88 | 1 | min_rating = 4 |
|
89 | 1 | elif length_sum < 12: |
|
90 | 1 | min_rating = 3 |
|
91 | else: |
||
92 | 1 | min_rating = 2 |
|
93 | |||
94 | 1 | for _ in range(2): |
|
95 | 1 | new_src = [] |
|
96 | 1 | new_tar = [] |
|
97 | 1 | minlen = min(len(src), len(tar)) |
|
98 | 1 | for i in range(minlen): |
|
99 | 1 | if src[i] != tar[i]: |
|
100 | 1 | new_src.append(src[i]) |
|
101 | 1 | new_tar.append(tar[i]) |
|
102 | 1 | src = new_src + src[minlen:] |
|
103 | 1 | tar = new_tar + tar[minlen:] |
|
104 | 1 | src.reverse() |
|
105 | 1 | tar.reverse() |
|
106 | |||
107 | 1 | similarity = 6 - max(len(src), len(tar)) |
|
108 | |||
109 | 1 | if similarity >= min_rating: |
|
110 | 1 | return similarity |
|
111 | 1 | return 0 |
|
112 | |||
247 |