Conditions | 10 |
Total Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 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 typify() 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 | """Collection of functions to coerce conversion of types with an intelligent guess.""" |
||
167 | @memoize |
||
168 | def typify(value, type_hint=None): |
||
169 | """Take a primitive value, usually a string, and try to make a more relevant type out of it. |
||
170 | An optional type_hint will try to coerce the value to that type. |
||
171 | |||
172 | Args: |
||
173 | value (Any): Usually a string, not a sequence |
||
174 | type_hint (type or Tuple[type]): |
||
175 | |||
176 | Examples: |
||
177 | >>> typify('32') |
||
178 | 32 |
||
179 | >>> typify('32', float) |
||
180 | 32.0 |
||
181 | >>> typify('32.0') |
||
182 | 32.0 |
||
183 | >>> typify('32.0.0') |
||
184 | '32.0.0' |
||
185 | >>> [typify(x) for x in ('true', 'yes', 'on')] |
||
186 | [True, True, True] |
||
187 | >>> [typify(x) for x in ('no', 'FALSe', 'off')] |
||
188 | [False, False, False] |
||
189 | >>> [typify(x) for x in ('none', 'None', None)] |
||
190 | [None, None, None] |
||
191 | |||
192 | """ |
||
193 | # value must be a string, or there at least needs to be a type hint |
||
194 | if isinstance(value, string_types): |
||
195 | value = value.strip() |
||
196 | elif type_hint is None: |
||
197 | # can't do anything because value isn't a string and there's no type hint |
||
198 | return value |
||
199 | |||
200 | # now we either have a stripped string, a type hint, or both |
||
201 | # use the hint if it exists |
||
202 | if isiterable(type_hint): |
||
203 | type_hint = set(type_hint) |
||
204 | if not (type_hint - NUMBER_TYPES_SET): |
||
205 | return numberify(value) |
||
206 | elif not (type_hint - STRING_TYPES_SET): |
||
207 | return text_type(value) |
||
208 | elif not (type_hint - BOOLNULL_TYPE_SET): |
||
209 | return boolify(value, nullable=True) |
||
210 | raise NotImplementedError() |
||
211 | elif type_hint is not None: |
||
212 | # coerce using the type hint, or use boolify for bool |
||
213 | return boolify(value) if type_hint == bool else type_hint(value) |
||
214 | else: |
||
215 | # no type hint, but we know value is a string, so try to match with the regex patterns |
||
216 | candidate = _REGEX.convert(value) |
||
217 | if candidate is not NO_MATCH: |
||
218 | return candidate |
||
219 | |||
220 | # nothing has caught so far; give up, and return the value that was given |
||
221 | return value |
||
222 | |||
256 |