turingdb 0.2.2__py3-none-any.whl → 0.2.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of turingdb might be problematic. Click here for more details.
- turingdb/__init__.py +2 -149
- turingdb/py.typed +0 -0
- turingdb/turingdb.py +154 -0
- {turingdb-0.2.2.dist-info → turingdb-0.2.4.dist-info}/METADATA +2 -1
- turingdb-0.2.4.dist-info/RECORD +8 -0
- turingdb-0.2.2.dist-info/RECORD +0 -6
- {turingdb-0.2.2.dist-info → turingdb-0.2.4.dist-info}/WHEEL +0 -0
- {turingdb-0.2.2.dist-info → turingdb-0.2.4.dist-info}/licenses/LICENSE.txt +0 -0
turingdb/__init__.py
CHANGED
|
@@ -1,150 +1,3 @@
|
|
|
1
|
-
from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
class TuringDBException(Exception):
|
|
5
|
-
def __init__(self, message: str):
|
|
6
|
-
super().__init__(message)
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class TuringDB:
|
|
10
|
-
DEFAULT_HEADERS = {
|
|
11
|
-
"Accept": "application/json",
|
|
12
|
-
"Content-Type": "application/json",
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
def __init__(
|
|
16
|
-
self,
|
|
17
|
-
instance_id: str = "",
|
|
18
|
-
auth_token: str = "",
|
|
19
|
-
host: str = "https://engines.turingdb.ai/sdk",
|
|
20
|
-
):
|
|
21
|
-
import httpx
|
|
22
|
-
import copy
|
|
23
|
-
|
|
24
|
-
self.host = host
|
|
25
|
-
|
|
26
|
-
self._client = httpx.Client(auth=None, verify=False)
|
|
27
|
-
|
|
28
|
-
self._params = {
|
|
29
|
-
"graph": "default",
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
self._headers = copy.deepcopy(TuringDB.DEFAULT_HEADERS)
|
|
33
|
-
|
|
34
|
-
if instance_id != "":
|
|
35
|
-
self._headers["Turing-Instance-Id"] = instance_id
|
|
36
|
-
|
|
37
|
-
if auth_token != "":
|
|
38
|
-
self._headers["Authorization"] = f"Bearer {auth_token}"
|
|
39
|
-
|
|
40
|
-
def list_available_graphs(self) -> list[str]:
|
|
41
|
-
return self._send_request("list_avail_graphs")["data"]
|
|
42
|
-
|
|
43
|
-
def list_loaded_graphs(self) -> list[str]:
|
|
44
|
-
return self._send_request("list_loaded_graphs")["data"][0][0]
|
|
45
|
-
|
|
46
|
-
def load_graph(self, graph_name: str, raise_if_loaded: bool = True):
|
|
47
|
-
try:
|
|
48
|
-
return self._send_request("load_graph", params={"graph": graph_name})
|
|
49
|
-
except TuringDBException as e:
|
|
50
|
-
if raise_if_loaded or e.__str__() != "GRAPH_ALREADY_EXISTS":
|
|
51
|
-
raise e
|
|
52
|
-
|
|
53
|
-
def create_graph(self, graph_name: str):
|
|
54
|
-
return self.query(f"create graph {graph_name}")
|
|
55
|
-
|
|
56
|
-
def query(self, query: str):
|
|
57
|
-
json = self._send_request("query", data=query, params=self._params)
|
|
58
|
-
|
|
59
|
-
if not isinstance(json, dict):
|
|
60
|
-
raise TuringDBException("Invalid response from the server")
|
|
61
|
-
|
|
62
|
-
return self._parse_chunks(json)
|
|
63
|
-
|
|
64
|
-
def set_commit(self, commit: str):
|
|
65
|
-
self._params["commit"] = commit
|
|
66
|
-
|
|
67
|
-
def set_change(self, change: str):
|
|
68
|
-
self._params["change"] = change
|
|
69
|
-
|
|
70
|
-
def checkout(self, change: str = "main", commit: str = "HEAD"):
|
|
71
|
-
if change == "main":
|
|
72
|
-
if "change" in self._params:
|
|
73
|
-
del self._params["change"]
|
|
74
|
-
else:
|
|
75
|
-
self.set_change(change)
|
|
76
|
-
|
|
77
|
-
if commit == "HEAD":
|
|
78
|
-
if "commit" in self._params:
|
|
79
|
-
del self._params["commit"]
|
|
80
|
-
else:
|
|
81
|
-
self.set_commit(commit)
|
|
82
|
-
|
|
83
|
-
def set_graph(self, graph_name: str):
|
|
84
|
-
self._params["graph"] = graph_name
|
|
85
|
-
|
|
86
|
-
def _send_request(
|
|
87
|
-
self,
|
|
88
|
-
path: str,
|
|
89
|
-
data: Optional[dict | str] = None,
|
|
90
|
-
params: Optional[dict] = None,
|
|
91
|
-
):
|
|
92
|
-
import orjson
|
|
93
|
-
|
|
94
|
-
if data is None:
|
|
95
|
-
data = ""
|
|
96
|
-
|
|
97
|
-
url = f"{self.host}/{path}"
|
|
98
|
-
|
|
99
|
-
if isinstance(data, dict):
|
|
100
|
-
response = self._client.post(
|
|
101
|
-
url, json=data, params=params, headers=self._headers
|
|
102
|
-
)
|
|
103
|
-
else:
|
|
104
|
-
response = self._client.post(
|
|
105
|
-
url, content=data, params=params, headers=self._headers
|
|
106
|
-
)
|
|
107
|
-
response.raise_for_status()
|
|
108
|
-
|
|
109
|
-
json = orjson.loads(response.text)
|
|
110
|
-
|
|
111
|
-
if isinstance(json, dict):
|
|
112
|
-
err = json.get("error")
|
|
113
|
-
if err is not None:
|
|
114
|
-
raise TuringDBException(err)
|
|
115
|
-
|
|
116
|
-
return json
|
|
117
|
-
|
|
118
|
-
def _parse_chunks(self, json: dict):
|
|
119
|
-
import pandas as pd
|
|
120
|
-
import numpy as np
|
|
121
|
-
|
|
122
|
-
data_header = json["header"]
|
|
123
|
-
column_names: list[str] = data_header["column_names"]
|
|
124
|
-
column_types: list[str] = data_header["column_types"]
|
|
125
|
-
dtypes: list[np.dtype] = []
|
|
126
|
-
columns: list[list] = []
|
|
127
|
-
|
|
128
|
-
for chunk in json["data"]:
|
|
129
|
-
for i, col in enumerate(chunk):
|
|
130
|
-
if i >= len(columns):
|
|
131
|
-
match column_types[i]:
|
|
132
|
-
case "String":
|
|
133
|
-
dtypes.append(np.dtype(np.dtypes.StringDType))
|
|
134
|
-
case "Int64":
|
|
135
|
-
dtypes.append(np.dtype(np.dtypes.Int64DType))
|
|
136
|
-
case "UInt64":
|
|
137
|
-
dtypes.append(np.dtype(np.dtypes.UInt64DType))
|
|
138
|
-
case "Double":
|
|
139
|
-
dtypes.append(np.dtype(np.dtypes.Float64DType))
|
|
140
|
-
case "Boolean":
|
|
141
|
-
dtypes.append(np.dtype(np.dtypes.BoolDType))
|
|
142
|
-
|
|
143
|
-
columns.append([])
|
|
144
|
-
|
|
145
|
-
columns[i] = columns[i] + col
|
|
146
|
-
|
|
147
|
-
arrays = [np.array(columns[i], dtype=dtypes[i]) for i in range(len(columns))]
|
|
148
|
-
|
|
149
|
-
return pd.DataFrame(dict(zip(column_names, arrays)), index=None)
|
|
1
|
+
from .turingdb import TuringDB, TuringDBException
|
|
150
2
|
|
|
3
|
+
__all__ = ["TuringDB", "TuringDBException"]
|
turingdb/py.typed
ADDED
|
File without changes
|
turingdb/turingdb.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class TuringDBException(Exception):
|
|
5
|
+
def __init__(self, message: str):
|
|
6
|
+
super().__init__(message)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TuringDB:
|
|
10
|
+
DEFAULT_HEADERS = {
|
|
11
|
+
"Accept": "application/json",
|
|
12
|
+
"Content-Type": "application/json",
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
def __init__(
|
|
16
|
+
self,
|
|
17
|
+
instance_id: str = "",
|
|
18
|
+
auth_token: str = "",
|
|
19
|
+
host: str = "https://engines.turingdb.ai/sdk",
|
|
20
|
+
):
|
|
21
|
+
import httpx
|
|
22
|
+
import copy
|
|
23
|
+
|
|
24
|
+
self.host = host
|
|
25
|
+
|
|
26
|
+
self._client = httpx.Client(auth=None, verify=False)
|
|
27
|
+
|
|
28
|
+
self._params = {
|
|
29
|
+
"graph": "default",
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
self._headers = copy.deepcopy(TuringDB.DEFAULT_HEADERS)
|
|
33
|
+
|
|
34
|
+
if instance_id != "":
|
|
35
|
+
self._headers["Turing-Instance-Id"] = instance_id
|
|
36
|
+
|
|
37
|
+
if auth_token != "":
|
|
38
|
+
self._headers["Authorization"] = f"Bearer {auth_token}"
|
|
39
|
+
|
|
40
|
+
def list_available_graphs(self) -> list[str]:
|
|
41
|
+
return self._send_request("list_avail_graphs")["data"]
|
|
42
|
+
|
|
43
|
+
def list_loaded_graphs(self) -> list[str]:
|
|
44
|
+
return self._send_request("list_loaded_graphs")["data"][0][0]
|
|
45
|
+
|
|
46
|
+
def load_graph(self, graph_name: str, raise_if_loaded: bool = True):
|
|
47
|
+
try:
|
|
48
|
+
return self._send_request("load_graph", params={"graph": graph_name})
|
|
49
|
+
except TuringDBException as e:
|
|
50
|
+
if raise_if_loaded or e.__str__() != "GRAPH_ALREADY_EXISTS":
|
|
51
|
+
raise e
|
|
52
|
+
|
|
53
|
+
def create_graph(self, graph_name: str):
|
|
54
|
+
return self.query(f"create graph {graph_name}")
|
|
55
|
+
|
|
56
|
+
def query(self, query: str):
|
|
57
|
+
json = self._send_request("query", data=query, params=self._params)
|
|
58
|
+
|
|
59
|
+
if not isinstance(json, dict):
|
|
60
|
+
raise TuringDBException("Invalid response from the server")
|
|
61
|
+
|
|
62
|
+
return self._parse_chunks(json)
|
|
63
|
+
|
|
64
|
+
def set_commit(self, commit: str):
|
|
65
|
+
self._params["commit"] = commit
|
|
66
|
+
|
|
67
|
+
def set_change(self, change: int | str):
|
|
68
|
+
if isinstance(change, int):
|
|
69
|
+
change = f"{change:x}"
|
|
70
|
+
self._params["change"] = change
|
|
71
|
+
|
|
72
|
+
def checkout(self, change: int | str = "main", commit: str = "HEAD"):
|
|
73
|
+
if change == "main":
|
|
74
|
+
if "change" in self._params:
|
|
75
|
+
del self._params["change"]
|
|
76
|
+
else:
|
|
77
|
+
self.set_change(change)
|
|
78
|
+
|
|
79
|
+
if commit == "HEAD":
|
|
80
|
+
if "commit" in self._params:
|
|
81
|
+
del self._params["commit"]
|
|
82
|
+
else:
|
|
83
|
+
self.set_commit(commit)
|
|
84
|
+
|
|
85
|
+
def set_graph(self, graph_name: str):
|
|
86
|
+
self._params["graph"] = graph_name
|
|
87
|
+
|
|
88
|
+
def _send_request(
|
|
89
|
+
self,
|
|
90
|
+
path: str,
|
|
91
|
+
data: Optional[dict | str] = None,
|
|
92
|
+
params: Optional[dict] = None,
|
|
93
|
+
):
|
|
94
|
+
import orjson
|
|
95
|
+
|
|
96
|
+
if data is None:
|
|
97
|
+
data = ""
|
|
98
|
+
|
|
99
|
+
url = f"{self.host}/{path}"
|
|
100
|
+
|
|
101
|
+
if isinstance(data, dict):
|
|
102
|
+
response = self._client.post(
|
|
103
|
+
url, json=data, params=params, headers=self._headers
|
|
104
|
+
)
|
|
105
|
+
else:
|
|
106
|
+
response = self._client.post(
|
|
107
|
+
url, content=data, params=params, headers=self._headers
|
|
108
|
+
)
|
|
109
|
+
response.raise_for_status()
|
|
110
|
+
|
|
111
|
+
json = orjson.loads(response.text)
|
|
112
|
+
|
|
113
|
+
if isinstance(json, dict):
|
|
114
|
+
err = json.get("error")
|
|
115
|
+
if err is not None:
|
|
116
|
+
details = json.get("error_details")
|
|
117
|
+
if details is not None:
|
|
118
|
+
err = f"{err}: {details}"
|
|
119
|
+
raise TuringDBException(err)
|
|
120
|
+
|
|
121
|
+
return json
|
|
122
|
+
|
|
123
|
+
def _parse_chunks(self, json: dict):
|
|
124
|
+
import pandas as pd
|
|
125
|
+
import numpy as np
|
|
126
|
+
|
|
127
|
+
data_header = json["header"]
|
|
128
|
+
column_names: list[str] = data_header["column_names"]
|
|
129
|
+
column_types: list[str] = data_header["column_types"]
|
|
130
|
+
dtypes: list[np.dtype] = []
|
|
131
|
+
columns: list[list] = []
|
|
132
|
+
|
|
133
|
+
for chunk in json["data"]:
|
|
134
|
+
for i, col in enumerate(chunk):
|
|
135
|
+
if i >= len(columns):
|
|
136
|
+
match column_types[i]:
|
|
137
|
+
case "String":
|
|
138
|
+
dtypes.append(np.dtype(np.dtypes.StringDType))
|
|
139
|
+
case "Int64":
|
|
140
|
+
dtypes.append(np.dtype(np.dtypes.Int64DType))
|
|
141
|
+
case "UInt64":
|
|
142
|
+
dtypes.append(np.dtype(np.dtypes.UInt64DType))
|
|
143
|
+
case "Double":
|
|
144
|
+
dtypes.append(np.dtype(np.dtypes.Float64DType))
|
|
145
|
+
case "Boolean":
|
|
146
|
+
dtypes.append(np.dtype(np.dtypes.BoolDType))
|
|
147
|
+
|
|
148
|
+
columns.append([])
|
|
149
|
+
|
|
150
|
+
columns[i] = columns[i] + col
|
|
151
|
+
|
|
152
|
+
arrays = [np.array(columns[i], dtype=dtypes[i]) for i in range(len(columns))]
|
|
153
|
+
|
|
154
|
+
return pd.DataFrame(dict(zip(column_names, arrays)), index=None)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: turingdb
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.4
|
|
4
4
|
Summary: TuringDB python sdk
|
|
5
5
|
Project-URL: Homepage, https://github.com/turing-db/turingdb-sdk-python
|
|
6
6
|
Project-URL: Repository, https://github.com/turing-db/turingdb-sdk-python
|
|
@@ -8,6 +8,7 @@ License-File: LICENSE.txt
|
|
|
8
8
|
Requires-Python: >=3.10
|
|
9
9
|
Requires-Dist: httpx>=0.28.1
|
|
10
10
|
Requires-Dist: orjson>=3.11.1
|
|
11
|
+
Requires-Dist: pandas-stubs>=2.3.0.250703
|
|
11
12
|
Requires-Dist: pandas>=2.3.1
|
|
12
13
|
Description-Content-Type: text/markdown
|
|
13
14
|
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
turingdb/__init__.py,sha256=zOhbnDfi_6Zf2aLn7mZho-xHIydeLd8-1TIT2_046Kg,95
|
|
2
|
+
turingdb/__init__.pyi,sha256=O6ZMBGuBgXInR2anKZI3ycMZECIuhlsTUanZiHjZEqs,790
|
|
3
|
+
turingdb/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
turingdb/turingdb.py,sha256=79NqNyjjY9uzbH6Qw6zGAKdLi9zuMn5S9M_FW6jUhXw,4753
|
|
5
|
+
turingdb-0.2.4.dist-info/METADATA,sha256=1UxFMqOGgJBYHIji3o9FScPNqOqt5s0WWuBnl-c6eic,519
|
|
6
|
+
turingdb-0.2.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
7
|
+
turingdb-0.2.4.dist-info/licenses/LICENSE.txt,sha256=PgwD9hjeoyWO93yZQnxCyt0s-PlhXWLgqdCPpB91GhY,1078
|
|
8
|
+
turingdb-0.2.4.dist-info/RECORD,,
|
turingdb-0.2.2.dist-info/RECORD
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
turingdb/__init__.py,sha256=jDI3-GTmSmWvEhFlV4y7l1UTSt9L-6952D5WOzW55cY,4533
|
|
2
|
-
turingdb/__init__.pyi,sha256=O6ZMBGuBgXInR2anKZI3ycMZECIuhlsTUanZiHjZEqs,790
|
|
3
|
-
turingdb-0.2.2.dist-info/METADATA,sha256=wMW-ulQiFRq9wOCMNbMErxdkZfzjdGdLJwrCPfO-QVw,477
|
|
4
|
-
turingdb-0.2.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
5
|
-
turingdb-0.2.2.dist-info/licenses/LICENSE.txt,sha256=PgwD9hjeoyWO93yZQnxCyt0s-PlhXWLgqdCPpB91GhY,1078
|
|
6
|
-
turingdb-0.2.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|