Skip to content

Commit 68fa371

Browse files
committed
🐛 fix bug when float is 'round' 1.0 2.0 ...
1 parent b52b739 commit 68fa371

File tree

3 files changed

+72
-58
lines changed

3 files changed

+72
-58
lines changed

ros_typedb/ros_typedb/ros_typedb_interface.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from ros_typedb.typedb_interface import MatchResultDict
2727
from ros_typedb.typedb_interface import TypeDBInterface
28+
from ros_typedb.typedb_interface import convert_query_type_to_py_type
2829
from ros_typedb_msgs.msg import Attribute
2930
from ros_typedb_msgs.msg import QueryResult
3031
from ros_typedb_msgs.srv import Query
@@ -50,20 +51,25 @@ def set_query_result_value(
5051
_type_dict = {
5152
'boolean': (1, 'bool_value'),
5253
'bool': (1, 'bool_value'),
53-
'long': (2, 'integer_value'),
54-
'int': (2, 'integer_value'),
55-
'double': (3, 'double_value'),
56-
'float': (3, 'double_value'),
57-
'string': (4, 'string_value'),
58-
'str': (4, 'string_value'),
59-
'datetime': (4, 'string_value'),
54+
'long': (2, 'integer_value', 'long'),
55+
'int': (2, 'integer_value', 'long'),
56+
'double': (3, 'double_value', 'double'),
57+
'float': (3, 'double_value', 'double'),
58+
'string': (4, 'string_value', 'string'),
59+
'str': (4, 'string_value', 'string'),
60+
'datetime': (4, 'string_value', 'string'),
6061
'boolean_array': (6, 'bool_array_value'),
6162
'long_array': (7, 'integer_array_value'),
6263
'double_array': (8, 'double_array_value'),
6364
'string_array': (9, 'string_array_value'),
6465
}
6566
if value_type in _type_dict:
6667
_param_value.type = _type_dict[value_type][0]
68+
try:
69+
value = convert_query_type_to_py_type(
70+
value=value, value_type=_type_dict[value_type][2])
71+
except IndexError:
72+
pass
6773
setattr(
6874
_param_value,
6975
_type_dict[value_type][1],

ros_typedb/ros_typedb/typedb_interface.py

+57-49
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,54 @@
3030
from typing import TypedDict
3131

3232

33+
def convert_query_type_to_py_type(
34+
value_dict: Optional[dict] = None,
35+
value: Optional[str] = None,
36+
value_type: Optional[str] = None) -> datetime | int | str | float:
37+
"""
38+
Convert typedb 'value_type' to python type.
39+
40+
:param value: Data to be converted.
41+
:param value_type: Data to be converted.
42+
:param value_dict: Typedb value dict to be converted, overrides value and
43+
value_type.
44+
:return: Converted data.
45+
"""
46+
if value_dict is not None:
47+
value_type = value_dict.get('type').get('value_type')
48+
value = value_dict.get('value')
49+
if value_type == 'datetime':
50+
return datetime.fromisoformat(value)
51+
elif value_type == 'long':
52+
return int(value)
53+
elif value_type == 'string':
54+
return str(value)
55+
elif value_type == 'double':
56+
return float(value)
57+
return value
58+
59+
60+
def convert_py_type_to_query_type(
61+
data: datetime | str | bool) -> str:
62+
"""
63+
Convert python type to string.
64+
65+
Convert python type to a properly formatted string to be used with
66+
a typedb query.
67+
68+
:param data: Data to be converted.
69+
:return: Converted data.
70+
"""
71+
if isinstance(data, str):
72+
if len(data) > 0 and data[0] != '$':
73+
return "'{}'".format(data)
74+
elif isinstance(data, datetime):
75+
return data.isoformat(timespec='milliseconds')
76+
elif isinstance(data, bool):
77+
return str(data).lower()
78+
return data
79+
80+
3381
def string_to_string_array(string: str) -> list[str]:
3482
"""
3583
Convert string to string array.
@@ -407,46 +455,6 @@ def get_aggregate_database(self, query: str) -> int | float | None:
407455
return result
408456
# Read/write database end
409457

410-
def convert_query_type_to_py_type(
411-
self, data: str) -> datetime | int | str | float:
412-
"""
413-
Convert typedb 'value_type' to python type.
414-
415-
:param data: Data to be converted.
416-
:return: Converted data.
417-
"""
418-
_value_type = data.get('type').get('value_type')
419-
_value = data.get('value')
420-
if _value_type == 'datetime':
421-
return datetime.fromisoformat(_value)
422-
elif _value_type == 'long':
423-
return int(_value)
424-
elif _value_type == 'string':
425-
return str(_value)
426-
elif _value_type == 'double':
427-
return float(_value)
428-
return _value
429-
430-
def convert_py_type_to_query_type(
431-
self, data: datetime | str | bool) -> str:
432-
"""
433-
Convert python type to string.
434-
435-
Convert python type to a properly formatted string to be used with
436-
a typedb query.
437-
438-
:param data: Data to be converted.
439-
:return: Converted data.
440-
"""
441-
if isinstance(data, str):
442-
if len(data) > 0 and data[0] != '$':
443-
return "'{}'".format(data)
444-
elif isinstance(data, datetime):
445-
return data.isoformat(timespec='milliseconds')
446-
elif isinstance(data, bool):
447-
return str(data).lower()
448-
return data
449-
450458
def attribute_dict_to_query(
451459
self,
452460
attribute_dict: dict[str, str | int | float | bool | datetime]
@@ -492,7 +500,7 @@ def attribute_dict_to_query(
492500
_query += ','
493501
_query += ' has {0} {1}'.format(
494502
attr,
495-
self.convert_py_type_to_query_type(v)
503+
convert_py_type_to_query_type(v)
496504
)
497505
first = False
498506
return _query
@@ -741,7 +749,7 @@ def create_match_query(
741749
match_query += " ${0}_{1} isa {2},".format(
742750
prefix, t_counter, thing[0])
743751
match_query += " has {0} {1};".format(
744-
thing[1], self.convert_py_type_to_query_type(thing[2]))
752+
thing[1], convert_py_type_to_query_type(thing[2]))
745753
prefix_list.append("{0}_{1}".format(prefix, t_counter))
746754
t_counter += 1
747755
return match_query, prefix_list
@@ -802,7 +810,7 @@ def create_relationship_query(
802810
if attribute[0] is not None:
803811
query += ", has {} {} ".format(
804812
attribute[0],
805-
self.convert_py_type_to_query_type(attribute[1])
813+
convert_py_type_to_query_type(attribute[1])
806814
)
807815
query += ";"
808816
return query
@@ -845,7 +853,7 @@ def insert_entity(
845853
"""
846854
for attribute in attribute_list:
847855
if attribute[0] is not None:
848-
value = self.convert_py_type_to_query_type(attribute[1])
856+
value = convert_py_type_to_query_type(attribute[1])
849857
query += f""", has {attribute[0]} {value} """
850858
query += ";"
851859
return self.insert_database(query)
@@ -936,7 +944,7 @@ def fetch_attribute_from_thing_raw(
936944
match $thing isa {thing}
937945
"""
938946
for (key, value) in key_attr_list:
939-
value = self.convert_py_type_to_query_type(value)
947+
value = convert_py_type_to_query_type(value)
940948
query += f""", has {key} {value} """
941949
query += f"""
942950
, has {attr} $attribute;
@@ -960,7 +968,7 @@ def fetch_attribute_from_thing(
960968
"""
961969
result = self.fetch_attribute_from_thing_raw(
962970
thing, key_attr_list, attr)
963-
return [self.convert_query_type_to_py_type(r.get('attribute'))
971+
return [convert_query_type_to_py_type(value_dict=r.get('attribute'))
964972
for r in result]
965973

966974
def delete_attribute_from_thing(
@@ -978,7 +986,7 @@ def delete_attribute_from_thing(
978986
:param attr: attribute name to be deleted
979987
:return: True.
980988
"""
981-
key_value = self.convert_py_type_to_query_type(key_value)
989+
key_value = convert_py_type_to_query_type(key_value)
982990
query = f"""
983991
match $thing isa {thing},
984992
has {key} {key_value},
@@ -1051,8 +1059,8 @@ def insert_attribute_in_thing(
10511059
:param attr_value: attribute value to be inserted
10521060
:return: Insert query result.
10531061
"""
1054-
key_value = self.convert_py_type_to_query_type(key_value)
1055-
attr_value = self.convert_py_type_to_query_type(attr_value)
1062+
key_value = convert_py_type_to_query_type(key_value)
1063+
attr_value = convert_py_type_to_query_type(attr_value)
10561064
query = f"""
10571065
match $thing isa {thing},
10581066
has {key} {key_value};

ros_typedb/test/test_ros_typedb_interface.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def insert_query():
4343
has email "[email protected]",
4444
has nickname "test",
4545
has age 33,
46-
has height 1.75,
46+
has height 1.00,
4747
has alive true,
4848
has birth-date 1990-06-01;
4949
$person2 isa person,
@@ -250,7 +250,7 @@ def test_ros_typedb_fetch_query_attribute(insert_query):
250250
r.value.integer_value == 33:
251251
correct_age = True
252252
if r.name == 'height' and r.label == 'height' and \
253-
r.value.double_value == 1.75:
253+
r.value.double_value == 1.0:
254254
correct_height = True
255255
if r.name == 'alive' and r.label == 'alive' and \
256256
r.value.bool_value is True:

0 commit comments

Comments
 (0)