Skip to content

Commit c6a24f3

Browse files
authored
Iss499 (#507)
* fixes #499 decode_number now supports floating point value
1 parent ead7445 commit c6a24f3

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/canmatrix/formats/arxml.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -938,8 +938,8 @@ def decode_compu_method(compu_method, ea, float_factory):
938938
# Modification to support sourcing the COMPU_METHOD info from the Vector NETWORK-REPRESENTATION-PROPS
939939
# keyword definition. 06Jun16
940940
#####################################################################################################
941-
942-
if ll is not None and desc is not None and canmatrix.utils.decode_number(ul.text) == canmatrix.utils.decode_number(ll.text):
941+
942+
if ll is not None and desc is not None and canmatrix.utils.decode_number(ul.text, float_factory) == canmatrix.utils.decode_number(ll.text, float_factory):
943943
#####################################################################################################
944944
#####################################################################################################
945945
values[ll.text] = desc
@@ -1213,7 +1213,7 @@ def get_signals(signal_array, frame, ea, multiplex_id, float_factory, bit_offset
12131213
new_signal.initial_value = float_factory(initvalue.text)
12141214

12151215
for key, value in list(values.items()):
1216-
new_signal.add_values(canmatrix.utils.decode_number(key), value)
1216+
new_signal.add_values(canmatrix.utils.decode_number(key, float_factory), value)
12171217
if signal_name is not None:
12181218
new_signal.add_attribute("LongName", signal_name)
12191219
frame.add_signal(new_signal)
@@ -1708,7 +1708,7 @@ def decode_can_helper(ea, float_factory, ignore_cluster_info):
17081708
baudrate_elem = ea.find("BAUDRATE", cc)
17091709
fd_baudrate_elem = ea.find("CAN-FD-BAUDRATE", cc)
17101710

1711-
logger.debug("Busname: " + get_element_name(cc, ns))
1711+
logger.debug("Busname: " + ea.get_element_name(cc))
17121712

17131713
bus_name = ea.get_element_name(cc)
17141714
if speed is not None:

src/canmatrix/tests/test_utils.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44
import canmatrix.utils
5-
5+
import decimal
66

77
def test_utils_guess_value():
88
assert canmatrix.utils.guess_value("true") == "1"
@@ -14,10 +14,10 @@ def test_utils_guess_value():
1414

1515

1616
def test_decode_number():
17-
assert canmatrix.utils.decode_number("0x10") == 16
18-
assert canmatrix.utils.decode_number("0b10") == 2
19-
assert canmatrix.utils.decode_number("10") == 10
20-
17+
assert canmatrix.utils.decode_number("0x10", decimal.Decimal) == 16
18+
assert canmatrix.utils.decode_number("0b10", decimal.Decimal) == 2
19+
assert canmatrix.utils.decode_number("10", decimal.Decimal) == 10
20+
assert canmatrix.utils.decode_number("1023.0", decimal.Decimal) == 1023.0
2121

2222
@pytest.mark.parametrize(
2323
'input_string, expected_list',

src/canmatrix/utils.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,18 @@ def get_gcd(value1, value2): # type (int,int) -> (int)
8787
else:
8888
return fractions.gcd(value1, value2)
8989

90-
def decode_number(value): # type(string) -> (int)
90+
def decode_number(value, float_factory): # type(string) -> (int)
9191
"""
9292
Decode string to integer and guess correct base
9393
:param value: string input value
9494
:return: integer
9595
"""
9696

9797
value = value.strip()
98+
99+
if '.' in value:
100+
return float_factory(value)
101+
98102
base = 10
99103
if len(value) > 1 and value[1] == 'b': # bin coded
100104
base = 2

0 commit comments

Comments
 (0)