turingdb 0.1.0__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 ADDED
@@ -0,0 +1,150 @@
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: 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)
150
+
@@ -0,0 +1,15 @@
1
+ Metadata-Version: 2.4
2
+ Name: turingdb
3
+ Version: 0.1.0
4
+ Summary: TuringDB python sdk
5
+ Project-URL: Homepage, https://github.com/turing-db/turingdb-sdk-python
6
+ Project-URL: Repository, https://github.com/turing-db/turingdb-sdk-python
7
+ License-File: LICENSE.txt
8
+ Requires-Python: >=3.9
9
+ Requires-Dist: httpx>=0.28.1
10
+ Requires-Dist: orjson>=3.11.1
11
+ Requires-Dist: pandas>=2.3.1
12
+ Description-Content-Type: text/markdown
13
+
14
+ # turingdb-sdk-python
15
+ Python SDK of the TuringDB graph database engine
@@ -0,0 +1,5 @@
1
+ turingdb/__init__.py,sha256=jDI3-GTmSmWvEhFlV4y7l1UTSt9L-6952D5WOzW55cY,4533
2
+ turingdb-0.1.0.dist-info/METADATA,sha256=G_24yqlVrl6DoJ4sZQ_gBSj8jKXgfhrmRN7-uskF42U,476
3
+ turingdb-0.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
4
+ turingdb-0.1.0.dist-info/licenses/LICENSE.txt,sha256=PgwD9hjeoyWO93yZQnxCyt0s-PlhXWLgqdCPpB91GhY,1078
5
+ turingdb-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Turing Biosystems Ltd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.