Conditions | 17 |
Total Lines | 73 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Tests | 31 |
CRAP Score | 17 |
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._phonetic_distance.PhoneticDistance.__init__() 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 -*- |
||
58 | 1 | def __init__( |
|
59 | self, transforms=None, metric=None, encode_alpha=False, **kwargs |
||
60 | ): |
||
61 | """Initialize PhoneticDistance instance. |
||
62 | |||
63 | Parameters |
||
64 | ---------- |
||
65 | transforms : list or _Phonetic or _Stemmer or _Fingerprint or type |
||
66 | An instance of a subclass of _Phonetic, _Stemmer, or _Fingerprint, |
||
67 | or a list (or other iterable) of such instances to apply to each |
||
68 | input word before computing their distance or similarity. If |
||
69 | omitted, no transformations will be performed. |
||
70 | metric : _Distance or type |
||
71 | An instance of a subclass of _Distance, used for computing the |
||
72 | inputs' distance or similarity after being transformed. If omitted, |
||
73 | the strings will be compared for identify (returning 0.0 if |
||
74 | identical, otherwise 1.0, when distance is computed). |
||
75 | encode_alpha : bool |
||
76 | Set to true to use the encode_alpha method of phonetic algoritms |
||
77 | whenever possible. |
||
78 | **kwargs |
||
79 | Arbitrary keyword arguments |
||
80 | |||
81 | |||
82 | .. versionadded:: 0.4.1 |
||
83 | |||
84 | """ |
||
85 | 1 | super(PhoneticDistance, self).__init__(**kwargs) |
|
86 | 1 | self.transforms = transforms |
|
87 | 1 | if self.transforms: |
|
88 | 1 | if isinstance(self.transforms, (list, tuple)): |
|
89 | 1 | self.transforms = list(self.transforms) |
|
90 | else: |
||
91 | 1 | self.transforms = [self.transforms] |
|
92 | |||
93 | 1 | for i, trans in enumerate(self.transforms): |
|
94 | 1 | if isinstance(trans, (_Phonetic, _Fingerprint, _Stemmer)): |
|
95 | 1 | continue |
|
96 | 1 | elif isinstance(trans, type) and issubclass( |
|
97 | trans, (_Phonetic, _Fingerprint, _Stemmer) |
||
98 | ): |
||
99 | 1 | self.transforms[i] = trans() |
|
100 | 1 | elif callable(trans): |
|
101 | 1 | continue |
|
102 | else: |
||
103 | 1 | raise TypeError( |
|
104 | '{} has unknown type {}'.format(trans, type(trans)) |
||
105 | ) |
||
106 | |||
107 | 1 | for i, trans in enumerate(self.transforms): |
|
108 | 1 | if isinstance(trans, _Phonetic): |
|
109 | 1 | if encode_alpha: |
|
110 | 1 | self.transforms[i] = self.transforms[i].encode_alpha |
|
111 | else: |
||
112 | 1 | self.transforms[i] = self.transforms[i].encode |
|
113 | 1 | elif isinstance(trans, _Fingerprint): |
|
114 | 1 | self.transforms[i] = self.transforms[i].fingerprint |
|
115 | 1 | elif isinstance(trans, _Stemmer): |
|
116 | 1 | self.transforms[i] = self.transforms[i].stem |
|
117 | |||
118 | else: |
||
119 | 1 | self.transforms = [] |
|
120 | |||
121 | 1 | self.metric = metric |
|
122 | 1 | if self.metric: |
|
123 | 1 | if isinstance(self.metric, type) and issubclass( |
|
124 | self.metric, _Distance |
||
125 | ): |
||
126 | 1 | self.metric = self.metric() |
|
127 | 1 | elif not isinstance(self.metric, _Distance): |
|
128 | 1 | raise TypeError( |
|
129 | '{} has unknown type {}'.format( |
||
130 | self.metric, type(self.metric) |
||
131 | ) |
||
241 |