How to use the pydsdl.PrimitiveType 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 / _constants.py View on Github external
def _unittest_slow_constants(generated_packages: typing.List[pyuavcan.dsdl.GeneratedPackageInfo]) -> None:
    for info in generated_packages:
        for model in _util.expand_service_types(info.models, keep_services=True):
            dtype = pyuavcan.dsdl.get_class(model)
            for c in model.constants:
                if isinstance(c.data_type, pydsdl.PrimitiveType):  # pragma: no branch
                    reference = c.value
                    generated = pyuavcan.dsdl.get_attribute(dtype, c.name)
                    assert isinstance(reference, pydsdl.Primitive)
                    assert reference.native_value == pytest.approx(generated), \
                        'The generated constant does not compare equal against the DSDL source'
            if issubclass(dtype, pyuavcan.dsdl.FixedPortObject):
                assert issubclass(dtype, pyuavcan.dsdl.CompositeObject) \
                    and issubclass(dtype, pyuavcan.dsdl.FixedPortObject)
                assert pyuavcan.dsdl.get_fixed_port_id(dtype) == pyuavcan.dsdl.get_model(dtype).fixed_port_id
github UAVCAN / pyuavcan / tests / dsdl / _util.py View on Github external
return (a is None) == (b is None)

    elif isinstance(model, pydsdl.CompositeType):
        if type(a) != type(b):  # pragma: no cover
            return False
        for f in pyuavcan.dsdl.get_model(a).fields_except_padding:  # pragma: no cover
            if not are_close(f.data_type,
                             pyuavcan.dsdl.get_attribute(a, f.name),
                             pyuavcan.dsdl.get_attribute(b, f.name)):
                return False
        return True                 # Empty objects of same type compare equal

    elif isinstance(model, pydsdl.ArrayType):
        if len(a) != len(b) or a.dtype != b.dtype:  # pragma: no cover
            return False
        if isinstance(model.element_type, pydsdl.PrimitiveType):
            return bool(numpy.allclose(a, b, equal_nan=True))  # Speedup for large arrays like images or point clouds
        else:
            return all(itertools.starmap(functools.partial(are_close, model.element_type), zip(a, b)))

    elif isinstance(model, pydsdl.FloatType):
        t = {
            16: numpy.float16,
            32: numpy.float32,
            64: numpy.float64,
        }[model.bit_length]
        return bool(numpy.allclose(t(a), t(b), equal_nan=True))

    else:
        return bool(numpy.allclose(a, b))
github UAVCAN / pyuavcan / tests / dsdl / _compiler.py View on Github external
def _test_constants(model: pydsdl.CompositeType) -> None:
    cls = pyuavcan.dsdl.get_generated_class(model)
    for c in model.constants:
        if isinstance(c.data_type, pydsdl.PrimitiveType):
            reference = c.value
            generated = pyuavcan.dsdl.get_attribute(cls, c.name)
            assert isinstance(reference, pydsdl.Primitive)
            assert reference.native_value == pytest.approx(generated), \
                'The generated constant does not compare equal against the DSDL source'
github UAVCAN / pyuavcan / pyuavcan / dsdl / _builtin_form.py View on Github external
field_type = f.data_type
        try:
            value = source.pop(f.name)
        except LookupError:
            continue    # No value specified, keep original value

        if isinstance(field_type, pydsdl.CompositeType):
            field_obj = get_attribute(destination, f.name)
            if field_obj is None:                               # Oh, this is a union
                field_obj = get_class(field_type)()             # The variant was not selected, construct a default
                set_attribute(destination, f.name, field_obj)   # Switch the union to the new variant
            update_from_builtin(field_obj, value)

        elif isinstance(field_type, pydsdl.ArrayType):
            element_type = field_type.element_type
            if isinstance(element_type, pydsdl.PrimitiveType):
                set_attribute(destination, f.name, value)
            elif isinstance(element_type, pydsdl.CompositeType):
                dtype = get_class(element_type)
                set_attribute(destination, f.name, [update_from_builtin(dtype(), s) for s in value])
            else:
                assert False, f'Unexpected array element type: {element_type!r}'

        elif isinstance(field_type, pydsdl.PrimitiveType):
            set_attribute(destination, f.name, value)

        else:
            assert False, f'Unexpected field type: {field_type!r}'

    if source:
        raise ValueError(f'No such fields in {model}: {list(source.keys())}')
github UAVCAN / pyuavcan / pyuavcan / dsdl / _compiler.py View on Github external
def pick_width(w: int) -> int:
        for o in [8, 16, 32, 64]:
            if w <= o:
                return o
        raise ValueError(f'Invalid bit width: {w}')  # pragma: no cover

    if isinstance(t, pydsdl.BooleanType):
        return f'_np_.bool'
    elif isinstance(t, pydsdl.SignedIntegerType):
        return f'_np_.int{pick_width(t.bit_length)}'
    elif isinstance(t, pydsdl.UnsignedIntegerType):
        return f'_np_.uint{pick_width(t.bit_length)}'
    elif isinstance(t, pydsdl.FloatType):
        return f'_np_.float{pick_width(t.bit_length)}'
    else:
        assert not isinstance(t, pydsdl.PrimitiveType), 'Forgot to handle some primitive types'
        return f'_np_.object_'