auxlib.type_coercion¶
Collection of functions to coerce conversion of types with an intelligent guess.
-
auxlib.type_coercion.
boolify
(value, nullable=False, return_string=False)[source]¶ Convert a number, string, or sequence type into a pure boolean.
Parameters: value (number, string, sequence) – pretty much anything Returns: boolean representation of the given value Return type: bool Examples
>>> [boolify(x) for x in ('yes', 'no')] [True, False] >>> [boolify(x) for x in (0.1, 0+0j, True, '0', '0.0', '0.1', '2')] [True, False, True, False, False, True, True] >>> [boolify(x) for x in ("true", "yes", "on", "y")] [True, True, True, True] >>> [boolify(x) for x in ("no", "non", "none", "off", "")] [False, False, False, False, False] >>> [boolify(x) for x in ([], set(), dict(), tuple())] [False, False, False, False] >>> [boolify(x) for x in ([1], set([False]), dict({'a': 1}), tuple([2]))] [True, True, True, True]
-
auxlib.type_coercion.
typify
(*args, **kwargs)[source]¶ Take a primitive value, usually a string, and try to make a more relevant type out of it. An optional type_hint will try to coerce the value to that type.
Parameters: - value (Any) – Usually a string, not a sequence
- type_hint (type or Tuple[type]) –
Examples
>>> typify('32') 32 >>> typify('32', float) 32.0 >>> typify('32.0') 32.0 >>> typify('32.0.0') '32.0.0' >>> [typify(x) for x in ('true', 'yes', 'on')] [True, True, True] >>> [typify(x) for x in ('no', 'FALSe', 'off')] [False, False, False] >>> [typify(x) for x in ('none', 'None', None)] [None, None, None]