Using EvArgs🔌
You can easily perform type casting using "EvArgs".
"EvArgs" library is a handy tool for Python users that makes handling values much easier. It focuses on type casting, allowing you to convert data types smoothly and reliably. With built-in validation features, it ensures that the data you work with is correct and clean. This means you can spend less time worrying about type management.
Install EvArgs
$ pip3 install evargs
Type Casting
The basic syntax for type casting with EvArgs is straightforward:
from evargs import EvArgs
evargs = EvArgs()
value = evargs.assign(' 1 ', cast=str, trim=True)
In this example:
- An EvArgs instance is created
- The
assign()
method is called with a string value (' 1 ') -
cast=str
specifies the target type (though redundant in this case since the input is already a string) -
trim=True
removes leading and trailing whitespace
Casting to int
Casting str value to int value. Minor casting problems are automatically resolved.
value = evargs.assign(' 1 ', cast=int, trim=True)
Value:
1: int
The above code converts the string ' 1 ' to an integer value 1 after trimming whitespace.
You can also add validation constraints:
value = evargs.assign('1', cast=int, validation=('range', 1, 10))
Value:
1: int
This ensures the integer falls within the specified range (1-10), raising an exception if the value is outside this range.
Casting to rounded int
Casting round-int easily.
value = evargs.assign(' 1.5 ', cast='round_int', trim=True)
Value:
2: int
The 'round_int' casting type automatically rounds 1.5 to 2.
value = evargs.assign('2.5', cast='round_int')
Value:
3: int
Similarly, 2.5 is rounded up to 3.
Casting to list
In casting to list, It's available for various types.
values = evargs.assign([' 1 ', 2, ' 3', 4.0], cast=int, trim=True, list=True)
Value:
[1, 2, 3, 4]
This handles a mixed list of strings, integers, and floats, converting all to integers.
values = evargs.assign([' a ', ' b', ' c '], trim=True, cast=str, list=True)
Value:
['a', 'b', 'c']
Here, whitespace is trimmed from each string in the list.
Casting to Enum
EvArgs support Enum casting. EvArgs automatically determines whether to match by name or value.
Enum class
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
EMERALD_GREEN = 2.5
Enum casting
evargs = EvArgs()
value = evargs.assign('RED', cast=Color)
Value:
Color.RED
value = evargs.assign(3, cast=Color)
Value:
Color.BLUE
value = evargs.assign('3', cast=Color)
Value:
Color.BLUE
Using TypeCast class🔌
For simpler use cases, the standalone TypeCast
class provides direct type conversion functions.
The TypeCast class provides a more direct approach when you don't need the additional validation features of the full EvArgs class.
from evargs import TypeCast
Integer conversion:
value = TypeCast.to_int('1')
Value:
1: int
Rounded integer conversion:
value = TypeCast.to_round_int('1.5')
Value:
2: int
Boolean conversion:
value = TypeCast.to_bool('True')
Value:
True: bool
Enum conversion:
value = TypeCast.to_enum(Color, 1)
Value:
Color.RED
More...🔌
"EvArgs" provides features for casting, value validation, and value conversion, allowing for complex usage as well. For more details, please refer to the documentation and sample code.