How to use the pydsdl.VariableLengthArrayType 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 / _util.py View on Github external
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
        if isinstance(et, pydsdl.UnsignedIntegerType) and et.bit_length == 8:   # Special case for faster testing
            out = numpy.random.randint(0, 256, size=model.capacity, dtype=numpy.uint8)
        else:
            out = [make_random_object(model.element_type) for _ in range(model.capacity)]
        if model.capacity < 10000:
            if isinstance(et, pydsdl.UnsignedIntegerType) and et.bit_length <= 8 and fifty_fifty():
                out = bytes(out)
        return out

    elif isinstance(model, pydsdl.VariableLengthArrayType):
        length = random.randint(0, model.capacity)
        et = model.element_type
        if isinstance(et, pydsdl.UnsignedIntegerType) and et.bit_length == 8:   # Special case for faster testing
            out = numpy.random.randint(0, 256, size=length, dtype=numpy.uint8)
        else:
            out = [make_random_object(model.element_type) for _ in range(length)]
        if length < 10000:  # pragma: no branch
            if isinstance(et, pydsdl.UnsignedIntegerType) and et.bit_length <= 8 and fifty_fifty():
                out = bytes(out)
            if model.string_like and fifty_fifty():
                try:
                    out = bytes(out).decode()
                except ValueError:
                    pass
        return out
github UAVCAN / pyuavcan / tests / dsdl_compiler.py View on Github external
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)]

    elif isinstance(data_type, pydsdl.VariableLengthArrayType):
        length = random.randint(0, data_type.capacity)
        return [_make_random_object(data_type.element_type) for _ in range(length)]

    elif isinstance(data_type, pydsdl.StructureType):
        o = pyuavcan.dsdl.get_generated_class(data_type)()
        for f in data_type.fields_except_padding:
            v = _make_random_object(f.data_type)
            pyuavcan.dsdl.set_attribute(o, f.name, v)
        return o

    elif isinstance(data_type, pydsdl.UnionType):
        f = random.choice(data_type.fields)
        v = _make_random_object(f.data_type)
        o = pyuavcan.dsdl.get_generated_class(data_type)()
        pyuavcan.dsdl.set_attribute(o, f.name, v)
        return o