How to use the pydsdl.UnionType 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 _test_textual_representations(model: pydsdl.CompositeType, obj: pyuavcan.dsdl.CompositeObject) -> None:
    for fn in [str, repr]:
        assert callable(fn)
        s = fn(obj)
        for f in model.fields_except_padding:
            field_present = (f'{f.name}=' in s) or (f'{f.name}_=' in s)
            if isinstance(model, pydsdl.UnionType):
                # In unions only the active field is printed. The active field may contain nested fields which
                # may be named similarly to other fields in the current union, so we can't easily ensure lack of
                # non-active fields in the output.
                field_active = pyuavcan.dsdl.get_attribute(obj, f.name) is not None
                if field_active:
                    assert field_present, f'{f.name}: {s}'
            else:
                # In structures all fields are printed always.
                assert field_present, f'{f.name}: {s}'
github UAVCAN / pyuavcan / tests / dsdl / _textual.py View on Github external
def validate(obj: pyuavcan.dsdl.CompositeObject, s: str) -> None:
        for f in model.fields_except_padding:
            field_present = (f'{f.name}=' in s) or (f'{f.name}_=' in s)
            if isinstance(model, pydsdl.UnionType):
                # In unions only the active field is printed.
                # The active field may contain nested fields which  may be named similarly to other fields
                # in the current union, so we can't easily ensure lack of non-active fields in the output.
                field_active = pyuavcan.dsdl.get_attribute(obj, f.name) is not None
                if field_active:
                    assert field_present, f'{f.name}: {s}'
            else:
                # In structures all fields are printed always.
                assert field_present, f'{f.name}: {s}'
github UAVCAN / pyuavcan / tests / dsdl_compiler.py View on Github external
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

    else:   # pragma: no cover
        raise TypeError(f'Unsupported type: {type(data_type)}')
github UAVCAN / pyuavcan / tests / dsdl / _util.py View on Github external
out = bytes(out)
            if model.string_like and fifty_fifty():
                try:
                    out = bytes(out).decode()
                except ValueError:
                    pass
        return out

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

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

    else:   # pragma: no cover
        raise TypeError(f'Unsupported type: {type(model)}')