helpers

This module contains varous helper functions.

rivalcfg.helpers.REGEXP_PARAM_STRING = re.compile('^\\s*([a-zA-Z0-9_]+)\\s*\\(\\s*(.+?)[\\s;]*\\)[\\s;]*$')

A regual expression that matches the general form of a param string.

rivalcfg.helpers.bytes_to_high_low_nibbles(byte)

Splits bytes into high and low nibbles.

>>> bytes_to_high_low_nibbles(0xAB)
(10, 11)
rivalcfg.helpers.merge_bytes(*args)

Returns a single list of int from given int and list of int.

Parameters:

args (int,list[int]) – Values to merge

Return type:

list[int]

>>> from rivalcfg.helpers import merge_bytes
>>> merge_bytes(1, 2, 3)
[1, 2, 3]
>>> merge_bytes([1, 2], [3, 4])
[1, 2, 3, 4]
>>> merge_bytes(1, [2, 3], 4)
[1, 2, 3, 4]
rivalcfg.helpers.module_ls(module)

List the content of the given Python module, ignoring private elements.

Parameters:

module – The module to list.

Return type:

[str]

rivalcfg.helpers.nibbles_to_byte(nibble1, nibble2)

Converts two nibbles into a single byte.

>>> nibbles_to_byte(0xA, 0xB)
171
rivalcfg.helpers.parse_param_string(paramstr, value_parsers={})

Parses a parameter string (used for rgbgrandiant).

Parameters:
  • paramstr (str) – The parameter string.

  • value_parsers (dict) – Additional parsers to parse values.

Return type:

dict

Example syntax:

myparam(foo=1; bar=hello; baz=a, b, c)

Once parsed, it will return this:

{
    "myparam": {
        "foo": "1",
        "bar": "hello",
        "baz": "a, b, c",
    }
}

Additional parsers can be provided to parse the values, see examples bellow.

>>> parse_param_string("hello(name=world)")
{'hello': {'name': 'world'}}
>>> parse_param_string("hello(name=world;)")
{'hello': {'name': 'world'}}
>>> parse_param_string("hello(name=world ) ;;")
{'hello': {'name': 'world'}}
>>> parse_param_string("foo(a=42; b=3.14)", value_parsers={
...     "foo": {
...         "a": int,
...         "b": float,
...     },
... })
{'foo': {'a': 42, 'b': 3.14}}
>>> parse_param_string("foobar[a=1]")
Traceback (most recent call last):
    ...
ValueError: invalid parameter string 'foobar[a=1]'
rivalcfg.helpers.uint_to_little_endian_bytearray(number, size)

Converts an unsigned interger to a little endian bytearray.

Parameters:
  • number (in) – The number to convert.

  • size (in) – The length of the target bytearray.

Return type:

[int]

>>> uint_to_little_endian_bytearray(0x42, 1)
[66]
>>> uint_to_little_endian_bytearray(0x42, 2)
[66, 0]
>>> uint_to_little_endian_bytearray(0xFF42, 2)
[66, 255]
>>> uint_to_little_endian_bytearray(0xFF42, 4)
[66, 255, 0, 0]
>>> uint_to_little_endian_bytearray(0xFFFFFF, 2)
Traceback (most recent call last):
    ...
ValueError: integer overflow