Conditions | 17 |
Total Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 2 | 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.""" |
||
184 | def typify(value, type_hint=None): |
||
185 | """Take a primitive value, usually a string, and try to make a more relevant type out of it. |
||
186 | An optional type_hint will try to coerce the value to that type. |
||
187 | |||
188 | Args: |
||
189 | value (Any): Usually a string, not a sequence |
||
190 | type_hint (type or Tuple[type]): |
||
191 | |||
192 | Examples: |
||
193 | >>> typify('32') |
||
194 | 32 |
||
195 | >>> typify('32', float) |
||
196 | 32.0 |
||
197 | >>> typify('32.0') |
||
198 | 32.0 |
||
199 | >>> typify('32.0.0') |
||
200 | '32.0.0' |
||
201 | >>> [typify(x) for x in ('true', 'yes', 'on')] |
||
202 | [True, True, True] |
||
203 | >>> [typify(x) for x in ('no', 'FALSe', 'off')] |
||
204 | [False, False, False] |
||
205 | >>> [typify(x) for x in ('none', 'None', None)] |
||
206 | [None, None, None] |
||
207 | |||
208 | """ |
||
209 | # value must be a string, or there at least needs to be a type hint |
||
210 | if isinstance(value, string_types): |
||
211 | value = value.strip() |
||
212 | elif type_hint is None: |
||
213 | # can't do anything because value isn't a string and there's no type hint |
||
214 | return value |
||
215 | |||
216 | # now we either have a stripped string, a type hint, or both |
||
217 | # use the hint if it exists |
||
218 | if isiterable(type_hint): |
||
219 | if isinstance(type_hint, type) and issubclass(type_hint, Enum): |
||
220 | try: |
||
221 | return type_hint(value) |
||
222 | except ValueError: |
||
223 | return type_hint[value] |
||
224 | type_hint = set(type_hint) |
||
225 | if not (type_hint - NUMBER_TYPES_SET): |
||
226 | return numberify(value) |
||
227 | elif not (type_hint - STRING_TYPES_SET): |
||
228 | return text_type(value) |
||
229 | elif not (type_hint - {bool, NoneType}): |
||
230 | return boolify(value, nullable=True) |
||
231 | elif not (type_hint - (STRING_TYPES_SET | {bool})): |
||
232 | return boolify(value, return_string=True) |
||
233 | elif not (type_hint - (STRING_TYPES_SET | {NoneType})): |
||
234 | value = text_type(value) |
||
235 | return None if value.lower() == 'none' else value |
||
236 | elif not (type_hint - {bool, int}): |
||
237 | return typify_str_no_hint(text_type(value)) |
||
238 | else: |
||
239 | raise NotImplementedError() |
||
240 | elif type_hint is not None: |
||
241 | # coerce using the type hint, or use boolify for bool |
||
242 | try: |
||
243 | return boolify(value) if type_hint == bool else type_hint(value) |
||
244 | except ValueError as e: |
||
245 | # ValueError: invalid literal for int() with base 10: 'nope' |
||
246 | raise TypeCoercionError(value, text_type(e)) |
||
247 | else: |
||
248 | # no type hint, but we know value is a string, so try to match with the regex patterns |
||
249 | # if there's still no match, `typify_str_no_hint` will return `value` |
||
250 | return typify_str_no_hint(value) |
||
251 | |||
285 |