DEV Community

Deer hunt
Deer hunt

Posted on

How to cast value easily in Python - Type casting🔌

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The 'round_int' casting type automatically rounds 1.5 to 2.

value = evargs.assign('2.5', cast='round_int')

Value:
3: int
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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']
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Integer conversion:

value = TypeCast.to_int('1')

Value:
1: int
Enter fullscreen mode Exit fullscreen mode

Rounded integer conversion:

value = TypeCast.to_round_int('1.5')

Value:
2: int
Enter fullscreen mode Exit fullscreen mode

Boolean conversion:

value = TypeCast.to_bool('True')

Value:
True: bool
Enter fullscreen mode Exit fullscreen mode

Enum conversion:

value = TypeCast.to_enum(Color, 1)

Value:
Color.RED
Enter fullscreen mode Exit fullscreen mode

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.

Github:
https://github.com/deer-hunt/evargs/

Top comments (0)