How to use the pydsdl.IntegerType function in pydsdl

To help you get started, we’ve selected a few pydsdl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github UAVCAN / pyuavcan / tests / dsdl_compiler.py View on Github external
def _make_random_object(data_type: pydsdl.SerializableType) -> typing.Any:
    if isinstance(data_type, pydsdl.BooleanType):
        return random.random() >= 0.5

    elif isinstance(data_type, pydsdl.IntegerType):  # noinspection PyTypeChecker
        return random.randint(int(data_type.inclusive_value_range.min),
                              int(data_type.inclusive_value_range.max))

    elif isinstance(data_type, pydsdl.FloatType):   # We want inf/nan as well, so we generate int and then reinterpret
        int_value = random.randrange(0, 2 ** data_type.bit_length)
        unpack_fmt, pack_fmt = {
            16: ('e', 'H'),
            32: ('f', 'I'),
            64: ('d', 'Q'),
        }[data_type.bit_length]
        fmt_prefix = '<'
        out, = struct.unpack(fmt_prefix + unpack_fmt, struct.pack(fmt_prefix + pack_fmt, int_value))
        return out

    elif isinstance(data_type, pydsdl.FixedLengthArrayType):
        return [_make_random_object(data_type.element_type) for _ in range(data_type.capacity)]
github UAVCAN / pyuavcan / tests / dsdl / _util.py View on Github external
def make_random_object(model: pydsdl.SerializableType) -> typing.Any:
    """
    Returns an object of the specified DSDL type populated with random data.
    """
    def fifty_fifty() -> bool:
        return random.random() >= 0.5

    if isinstance(model, pydsdl.BooleanType):
        return fifty_fifty()

    elif isinstance(model, pydsdl.IntegerType):  # noinspection PyTypeChecker
        return random.randint(int(model.inclusive_value_range.min),
                              int(model.inclusive_value_range.max))

    elif isinstance(model, pydsdl.FloatType):   # We want inf/nan as well, so we generate int and then reinterpret
        int_value = random.randrange(0, 2 ** model.bit_length)
        unpack_fmt, pack_fmt = {
            16: ('e', 'H'),
            32: ('f', 'I'),
            64: ('d', 'Q'),
        }[model.bit_length]
        fmt_prefix = '<'
        out, = struct.unpack(fmt_prefix + unpack_fmt, struct.pack(fmt_prefix + pack_fmt, int_value))
        return out

    elif isinstance(model, pydsdl.FixedLengthArrayType):
        et = model.element_type
github UAVCAN / pyuavcan / pyuavcan / dsdl / _builtin_form.py View on Github external
if get_attribute(obj, f.name) is not None  # The check is to hide inactive union variants.
        }

    elif isinstance(model, pydsdl.ArrayType):
        assert isinstance(obj, numpy.ndarray)
        if model.string_like:  # TODO: drop this special case when strings are natively supported in DSDL.
            try:
                return bytes(e for e in obj).decode()
            except UnicodeError:
                return list(map(int, obj))
        else:
            return [_to_builtin_impl(e, model.element_type) for e in obj]

    elif isinstance(model, pydsdl.PrimitiveType):
        # The explicit conversions are needed to get rid of NumPy scalar types.
        if isinstance(model, pydsdl.IntegerType):
            return int(obj)                         # type: ignore
        elif isinstance(model, pydsdl.FloatType):
            return float(obj)                       # type: ignore
        elif isinstance(model, pydsdl.BooleanType):
            return bool(obj)
        else:
            assert isinstance(obj, str)
            return obj

    else:
        assert False, 'Unexpected inputs'