evargs package v1.0.0¶
Github code¶
The example is here.
from evargs import TypeCast
TypeCast.to_int(' 1 ')
TypeCast.to_int(' aaa ', raise_error=0)
TypeCast.to_int('', nullable=1)
TypeCast.to_int('', default=123)
TypeCast.to_round_int('1.1')
TypeCast.to_round_int(' 1.2 ')
TypeCast.to_round_int('-10.1')
TypeCast.to_float(' aaa ')
TypeCast.to_float(' aaa ', raise_error=0)
TypeCast.to_float(None, nullable=1)
TypeCast.to_float('', default=1.23)
TypeCast.to_str('ABC')
TypeCast.to_str(None, default='ABC')
TypeCast.to_bool('1')
TypeCast.to_bool('1', default=True)
TypeCast.to_bool('True')
TypeCast.bool_strict(1)
TypeCast.bool_strict('1')
TypeCast.bool_loose('1')
TypeCast.bool_loose('True')
TypeCast.to_enum(Color, 1)
TypeCast.to_enum(Color, 99, default=Color.WHITE)
TypeCast.to_enum(Color, 'RED', is_name=True)
TypeCast.to_enum(Color, 'AAA', default=Color.WHITE)
TypeCast.to_enum_loose(Color, 1)
TypeCast.to_enum_loose(Color, '3.5')
TypeCast.to_enum_loose(Color, 'BLUE', is_name=True, is_value=False)
TypeCast class¶
- class TypeCast¶
Bases:
object
- ERROR_CANCEL = 0¶
- ERROR_DEFAULT_NONE = 1¶
- ERROR_ALL = 2¶
- classmethod to_int(v, default=None, nullable=False, raise_error=1)¶
Cast to int.
- Parameters
v (
any
) – Source value.default (
Optional
[int
]) – The default value to return if casting fails.nullable (
bool
) – Allows None if value is empty.raise_error (
int
) – Raises an error when casting fails.
- Return type
Optional[int]
- classmethod to_round_int(v, default=None, nullable=False, raise_error=1)¶
Cast to int with round.
- Parameters
v (
any
) – Source value.default (
Optional
[int
]) – The default value to return if casting fails.nullable (
bool
) – Allows None if value is empty.raise_error (
int
) – Raises an error when casting fails.
- Return type
Optional[int]
- classmethod to_float(v, default=None, nullable=False, raise_error=1)¶
Cast to float.
- Parameters
v (
any
) – Source value.default (
Optional
[float
]) – The default value to return if casting fails.nullable (
bool
) – Allows None if value is empty.raise_error (
int
) – Raises an error when casting fails.
- Return type
Optional[float]
- classmethod to_complex(v, default=None, nullable=False, raise_error=1)¶
Cast to complex.
- Parameters
v (
any
) – Source value.default (
Optional
[complex
]) – The default value to return if casting fails.nullable (
bool
) – Allows None if value is empty.raise_error (
int
) – Raises an error when casting fails.
- Return type
Optional[complex]
- classmethod to_str(v, default=None, nullable=False, raise_error=1)¶
Cast to str.
- Parameters
v (
any
) – Source value.default (
Optional
[str
]) – The default value to return if casting fails.nullable (
bool
) – Allows None if value is empty.raise_error (
int
) – Raises an error when casting fails.
- Return type
str
- classmethod to_bool(v, default=None, nullable=False, raise_error=1, none_cast=False)¶
Cast to bool.
- Parameters
v (
any
) – Source value.default (
Optional
[bool
]) – The default value to return if casting fails.nullable (
bool
) – Allows None if value is empty.raise_error (
int
) – Raises an error when casting fails.none_cast (
bool
) – Whether to convert None to False.
- Return type
bool
- classmethod bool_strict(v, default=None, nullable=False, raise_error=1, none_cast=False)¶
Cast to bool strictly. Converting only ‘1’, ‘0’, 1, 0.
- Parameters
v (
any
) – Source value.default (
Optional
[bool
]) – The default value to return if casting fails.nullable (
bool
) – Allows None if value is empty.raise_error (
int
) – Raises an error when casting fails.none_cast (
bool
) – Whether to convert None to False.
- Return type
bool
- classmethod bool_loose(v, default=None)¶
Cast to bool loosely. Always return True or False.
- Parameters
v (
any
) – Source value.default (
Optional
[bool
]) – The default value to return if casting fails.
- Return type
bool
- classmethod to_enum(enum_class, v, default=None, is_value=True, is_name=False, nullable=False, raise_error=1)¶
Cast to enum.
- Parameters
enum_class (
Type
[Enum
]) – Enum class.v (
any
) – Source value.default (
Optional
[Enum
]) – The default value to return if casting fails.is_value (
bool
) – Conversion by using the enum’s value.is_name (
bool
) – Conversion by using the enum’s name.nullable (
bool
) – Allows None if value is empty.raise_error (
int
) – Raises an error when casting fails.
- Return type
Enum
- classmethod to_enum_loose(enum_class, v, default=None, is_value=True, is_name=False, nullable=False, raise_error=1)¶
Cast to enum loosely.
- Parameters
enum_class (
Type
[Enum
]) – Enum class.v (
any
) – Source value.default (
Optional
[Enum
]) – The default value to return if casting fails.is_value (
bool
) – Conversion by using the enum’s value.is_name (
bool
) – Conversion by using the enum’s name.nullable (
bool
) – Allows None if value is empty.raise_error (
int
) – Raises an error when casting fails.
- Return type
Enum
- classmethod noop(v, default=None, nullable=False, raise_error=1)¶
Noop.
- Parameters
v (
any
) – Source value.default (
Optional
[any
]) – The default value to return if casting fails.nullable (
bool
) – Allows None if value is empty.raise_error (
int
) – Raises an error when casting fails.
- Return type
any
- classmethod is_empty(v)¶
Whether value is empty value.
- Return type
bool
- Parameters
v (any) –