rwskit.numeric

Utilities for numeric operations.

Attributes

int_8_max

int_16_max

int_32_max

int_64_max

Functions

int_(→ Optional[int])

Convert a string s to an integer. If s is None return None

float_(→ Optional[float])

Convert a string s to a float. If s is None return None

to_signed(→ int)

If the value is positive, this will return a signed version of the number

Module Contents

rwskit.numeric.int_8_max = 255[source]
rwskit.numeric.int_16_max = 65535[source]
rwskit.numeric.int_32_max = 4294967295[source]
rwskit.numeric.int_64_max = 18446744073709551615[source]
rwskit.numeric.int_(s: str | None, falsy_as_none: bool = True) int | None[source]

Convert a string s to an integer. If s is None return None instead of raising an exception.

Parameters:
  • s (str, optional) – The string to convert.

  • falsy_as_none (bool, default=True) – If True all falsy values of s (other than 0) will return None, otherwise only None values will be passed through and an exception will be raised.

Returns:

None if s is None, otherwise an integer if s is not None and is parseable.

Return type:

int, optional

Raises:

ValueError – If the string cannot be parsed.

rwskit.numeric.float_(s: str | None, falsy_as_none: bool = True) float | None[source]

Convert a string s to a float. If s is None return None instead of raising an exception.

Parameters:
  • s (str, optional) – The string to convert.

  • falsy_as_none (bool, default=True) – If True all falsy values of s will return None (other than 0), otherwise only None values will be passed through and an exception will be raised..

Returns:

None if s is None, otherwise a float if s is not None and is parseable.

Return type:

float, optional

Raises:

ValueError – If the string cannot be parsed.

rwskit.numeric.to_signed(value: int) int[source]

If the value is positive, this will return a signed version of the number that fits in the same size integer type. For example, 129 is a valid 8-bit signed integer, but a 16-bit unsigned integer. This method will convert the number to the corresponding signed integer that still fits in 8-bits. In this case 127.

Parameters:

value (int) – The value to convert.

Returns:

The signed version.

Return type:

int

Raises:

ValueError – If the value is more than 64-bits.