Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_num(self):
self.assertEqual(cantools.database.can.formats.utils.num('1'), 1)
self.assertEqual(cantools.database.can.formats.utils.num('1.0'), 1.0)
with self.assertRaises(ValueError):
cantools.database.can.formats.utils.num('x')
def test_num(self):
self.assertEqual(cantools.database.can.formats.utils.num('1'), 1)
self.assertEqual(cantools.database.can.formats.utils.num('1.0'), 1.0)
with self.assertRaises(ValueError):
cantools.database.can.formats.utils.num('x')
# Default values.
factor = 1
offset = 0
unit = None
decimal.scale = Decimal(factor)
decimal.offset = Decimal(offset)
for item in tokens:
if isinstance(item, list):
key, value = item
if key == '/f:':
factor = num(value)
decimal.scale = Decimal(value)
elif key == '/o:':
offset = num(value)
decimal.offset = Decimal(value)
elif key == '/min:':
minimum = num(value)
decimal.minimum = Decimal(value)
elif key == '/max:':
maximum = num(value)
decimal.maximum = Decimal(value)
elif key == '/e:':
enum = _get_enum(enums, value)
else:
LOGGER.debug("Ignoring unsupported message attribute '%s'.", key)
elif item.startswith('/u:"'):
unit = item[4:-1]
elif item.startswith('/u:'):
unit = item[3:]
else:
def _load_enums(tokens):
section = _get_section_tokens(tokens, '{ENUMS}')
enums = {}
for _, _, name, _, values, _, _ in section:
if values:
values = values[0]
enums[name] = odict((num(v[0]), v[2]) for v in values)
return enums
elif key == 'offset':
offset = int(value)
elif key == 'length':
length = int(value)
elif key == 'endianess':
byte_order = '{}_endian'.format(value)
else:
LOGGER.debug("Ignoring unsupported signal attribute '%s'.", key)
# Value XML element.
value = signal.find('ns:Value', NAMESPACES)
if value is not None:
for key, value in value.attrib.items():
if key == 'min':
minimum = num(value)
decimal.minimum = Decimal(value)
elif key == 'max':
maximum = num(value)
decimal.maximum = Decimal(value)
elif key == 'slope':
slope = num(value)
decimal.scale = Decimal(value)
elif key == 'intercept':
intercept = num(value)
decimal.offset = Decimal(value)
elif key == 'unit':
unit = value
elif key == 'type':
is_signed = (value == 'signed')
is_float = (value in ['single', 'double'])
else:
length = int(value)
elif key == 'endianess':
byte_order = '{}_endian'.format(value)
else:
LOGGER.debug("Ignoring unsupported signal attribute '%s'.", key)
# Value XML element.
value = signal.find('ns:Value', NAMESPACES)
if value is not None:
for key, value in value.attrib.items():
if key == 'min':
minimum = num(value)
decimal.minimum = Decimal(value)
elif key == 'max':
maximum = num(value)
decimal.maximum = Decimal(value)
elif key == 'slope':
slope = num(value)
decimal.scale = Decimal(value)
elif key == 'intercept':
intercept = num(value)
decimal.offset = Decimal(value)
elif key == 'unit':
unit = value
elif key == 'type':
is_signed = (value == 'signed')
is_float = (value in ['single', 'double'])
else:
LOGGER.debug("Ignoring unsupported signal value attribute '%s'.",
key)
def _load_environment_variables(tokens, comments):
environment_variables = odict()
for env_var in tokens.get('EV_', []):
name = env_var[1]
environment_variables[name] = EnvironmentVariable(
name=name,
env_type=int(env_var[3]),
minimum=num(env_var[5]),
maximum=num(env_var[7]),
unit=env_var[9],
initial_value=num(env_var[10]),
env_id=int(env_var[11]),
access_type=env_var[12],
access_node=env_var[13],
comment=comments.get(env_var[1], None))
return environment_variables
def get_maximum(minimum, maximum):
if minimum == maximum == '0':
return None
else:
return num(maximum)