Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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
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'
def _numpy_scalar_type(t: pydsdl.Any) -> str:
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_'