Conditions | 14 |
Total Lines | 79 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Tests | 19 |
CRAP Score | 14 |
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._needleman_wunsch.NeedlemanWunsch.sim_matrix() 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 -*- |
||
49 | 1 | @staticmethod |
|
50 | 1 | def sim_matrix( |
|
51 | src, |
||
52 | tar, |
||
53 | mat=None, |
||
54 | mismatch_cost=0, |
||
55 | match_cost=1, |
||
56 | symmetric=True, |
||
57 | alphabet=None, |
||
58 | ): |
||
59 | """Return the matrix similarity of two strings. |
||
60 | |||
61 | With the default parameters, this is identical to sim_ident. |
||
62 | It is possible for sim_matrix to return values outside of the range |
||
63 | :math:`[0, 1]`, if values outside that range are present in mat, |
||
64 | mismatch_cost, or match_cost. |
||
65 | |||
66 | Parameters |
||
67 | ---------- |
||
68 | src : str |
||
69 | Source string for comparison |
||
70 | tar : str |
||
71 | Target string for comparison |
||
72 | mat : dict |
||
73 | A dict mapping tuples to costs; the tuples are (src, tar) pairs of |
||
74 | symbols from the alphabet parameter |
||
75 | mismatch_cost : float |
||
76 | The value returned if (src, tar) is absent from mat when src does |
||
77 | not equal tar |
||
78 | match_cost : float |
||
79 | The value returned if (src, tar) is absent from mat when src equals |
||
80 | tar |
||
81 | symmetric : bool |
||
82 | True if the cost of src not matching tar is identical to the cost |
||
83 | of tar not matching src; in this case, the values in mat need only |
||
84 | contain (src, tar) or (tar, src), not both |
||
85 | alphabet : str |
||
86 | A collection of tokens from which src and tar are drawn; if this is |
||
87 | defined a ValueError is raised if either tar or src is not found in |
||
88 | alphabet |
||
89 | |||
90 | Returns |
||
91 | ------- |
||
92 | float |
||
93 | Matrix similarity |
||
94 | |||
95 | Raises |
||
96 | ------ |
||
97 | ValueError |
||
98 | src value not in alphabet |
||
99 | ValueError |
||
100 | tar value not in alphabet |
||
101 | |||
102 | Examples |
||
103 | -------- |
||
104 | >>> NeedlemanWunsch.sim_matrix('cat', 'hat') |
||
105 | 0 |
||
106 | >>> NeedlemanWunsch.sim_matrix('hat', 'hat') |
||
107 | 1 |
||
108 | |||
109 | """ |
||
110 | 1 | if alphabet: |
|
111 | 1 | alphabet = tuple(alphabet) |
|
112 | 1 | for i in src: |
|
113 | 1 | if i not in alphabet: |
|
114 | 1 | raise ValueError('src value not in alphabet') |
|
115 | 1 | for i in tar: |
|
116 | 1 | if i not in alphabet: |
|
117 | 1 | raise ValueError('tar value not in alphabet') |
|
118 | |||
119 | 1 | if src == tar: |
|
120 | 1 | if mat and (src, src) in mat: |
|
121 | 1 | return mat[(src, src)] |
|
122 | 1 | return match_cost |
|
123 | 1 | if mat and (src, tar) in mat: |
|
124 | 1 | return mat[(src, tar)] |
|
125 | 1 | elif symmetric and mat and (tar, src) in mat: |
|
126 | 1 | return mat[(tar, src)] |
|
127 | 1 | return mismatch_cost |
|
128 | |||
218 |