Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def expand_service_types(models: typing.Iterable[pydsdl.CompositeType], keep_services: bool = False) \
-> typing.Iterator[pydsdl.CompositeType]:
"""
Iterates all types in the provided list, expanding each ServiceType into a pair of CompositeType: one for
request, one for response.
"""
for m in models:
if isinstance(m, pydsdl.ServiceType):
yield m.request_type
yield m.response_type
if keep_services:
yield m
else:
yield m
def _test_package(info: pyuavcan.dsdl.GeneratedPackageInfo) -> None:
performance: typing.Dict[pydsdl.CompositeType, numpy.ndarray] = {}
def once(t: pydsdl.CompositeType) -> None:
performance[t] = _test_type(t)
for dsdl_type in info.types:
if isinstance(dsdl_type, pydsdl.ServiceType):
once(dsdl_type.request_type)
once(dsdl_type.response_type)
else:
once(dsdl_type)
_logger.info('Tested types ordered by serialization/deserialization speed in microseconds, '
'%d random samples per type:', _NUM_RANDOM_SAMPLES)
max_name_len = max(map(lambda t: len(str(t)), performance.keys()))
for ty, sample in sorted(performance.items(), key=lambda kv: -max(kv[1])):
suffix = '' if max(sample) < 1e-3 else '\tSLOW!'
sample *= 1e6
_logger.info(f'%-{max_name_len}s %6.0f %6.0f%s', ty, sample[0], sample[1], suffix)
def _test_non_serialization_related_behaviors(info: pyuavcan.dsdl.GeneratedPackageInfo) -> None:
for model in info.types:
if isinstance(model, pydsdl.ServiceType):
_test_type(model.request_type)
_test_type(model.response_type)
else:
_test_type(model)
def _unittest_slow_random(generated_packages: typing.List[pyuavcan.dsdl.GeneratedPackageInfo],
caplog: typing.Any) -> None:
_logger.info(f'Number of random samples: {_NUM_RANDOM_SAMPLES}. '
f'Set the environment variable PYUAVCAN_TEST_NUM_RANDOM_SAMPLES to override.')
# The random test intentionally generates a lot of faulty data, which generates a lot of log messages.
# We don't want them to clutter the test output, so we raise the logging level temporarily.
caplog.set_level(logging.WARNING, logger='pyuavcan.dsdl')
performance: typing.Dict[pydsdl.CompositeType, _TypeTestStatistics] = {}
for info in generated_packages:
for model in _util.expand_service_types(info.models, keep_services=True):
if not isinstance(model, pydsdl.ServiceType):
performance[model] = _test_type(model, _NUM_RANDOM_SAMPLES)
else:
dtype = pyuavcan.dsdl.get_class(model)
with pytest.raises(TypeError):
assert list(pyuavcan.dsdl.serialize(dtype()))
with pytest.raises(TypeError):
pyuavcan.dsdl.deserialize(dtype, [memoryview(b'')])
_logger.info('Tested types ordered by serialization speed, %d random samples per type', _NUM_RANDOM_SAMPLES)
_logger.info('Columns: random SR correctness ratio; '
'mean serialization time [us]; mean deserialization time [us]')
for ty, stat in sorted(performance.items(), key=lambda kv: -kv[1].worst_time): # pragma: no branch
assert isinstance(stat, _TypeTestStatistics)
_logger.info(f'%-60s %3.0f%% %6.0f %6.0f%s', ty,
stat.random_serialized_representation_correctness_ratio * 100,
def _raise_if_service_type(model: pydsdl.SerializableType) -> None:
if isinstance(model, pydsdl.ServiceType): # pragma: no cover
raise TypeError(f'Built-in form is not defined for service types. '
f'Did you mean to use Request or Response? Input type: {model}')