30
30
from typing import TypedDict
31
31
32
32
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
+
33
81
def string_to_string_array (string : str ) -> list [str ]:
34
82
"""
35
83
Convert string to string array.
@@ -407,46 +455,6 @@ def get_aggregate_database(self, query: str) -> int | float | None:
407
455
return result
408
456
# Read/write database end
409
457
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
-
450
458
def attribute_dict_to_query (
451
459
self ,
452
460
attribute_dict : dict [str , str | int | float | bool | datetime ]
@@ -492,7 +500,7 @@ def attribute_dict_to_query(
492
500
_query += ','
493
501
_query += ' has {0} {1}' .format (
494
502
attr ,
495
- self . convert_py_type_to_query_type (v )
503
+ convert_py_type_to_query_type (v )
496
504
)
497
505
first = False
498
506
return _query
@@ -741,7 +749,7 @@ def create_match_query(
741
749
match_query += " ${0}_{1} isa {2}," .format (
742
750
prefix , t_counter , thing [0 ])
743
751
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 ]))
745
753
prefix_list .append ("{0}_{1}" .format (prefix , t_counter ))
746
754
t_counter += 1
747
755
return match_query , prefix_list
@@ -802,7 +810,7 @@ def create_relationship_query(
802
810
if attribute [0 ] is not None :
803
811
query += ", has {} {} " .format (
804
812
attribute [0 ],
805
- self . convert_py_type_to_query_type (attribute [1 ])
813
+ convert_py_type_to_query_type (attribute [1 ])
806
814
)
807
815
query += ";"
808
816
return query
@@ -845,7 +853,7 @@ def insert_entity(
845
853
"""
846
854
for attribute in attribute_list :
847
855
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 ])
849
857
query += f""", has { attribute [0 ]} { value } """
850
858
query += ";"
851
859
return self .insert_database (query )
@@ -936,7 +944,7 @@ def fetch_attribute_from_thing_raw(
936
944
match $thing isa { thing }
937
945
"""
938
946
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 )
940
948
query += f""", has { key } { value } """
941
949
query += f"""
942
950
, has { attr } $attribute;
@@ -960,7 +968,7 @@ def fetch_attribute_from_thing(
960
968
"""
961
969
result = self .fetch_attribute_from_thing_raw (
962
970
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' ))
964
972
for r in result ]
965
973
966
974
def delete_attribute_from_thing (
@@ -978,7 +986,7 @@ def delete_attribute_from_thing(
978
986
:param attr: attribute name to be deleted
979
987
:return: True.
980
988
"""
981
- key_value = self . convert_py_type_to_query_type (key_value )
989
+ key_value = convert_py_type_to_query_type (key_value )
982
990
query = f"""
983
991
match $thing isa { thing } ,
984
992
has { key } { key_value } ,
@@ -1051,8 +1059,8 @@ def insert_attribute_in_thing(
1051
1059
:param attr_value: attribute value to be inserted
1052
1060
:return: Insert query result.
1053
1061
"""
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 )
1056
1064
query = f"""
1057
1065
match $thing isa { thing } ,
1058
1066
has { key } { key_value } ;
0 commit comments