turingdb 0.2.2__py3-none-any.whl → 0.2.3__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 CHANGED
@@ -1,150 +1,3 @@
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)
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,151 @@
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
+ raise TuringDBException(err)
117
+
118
+ return json
119
+
120
+ def _parse_chunks(self, json: dict):
121
+ import pandas as pd
122
+ import numpy as np
123
+
124
+ data_header = json["header"]
125
+ column_names: list[str] = data_header["column_names"]
126
+ column_types: list[str] = data_header["column_types"]
127
+ dtypes: list[np.dtype] = []
128
+ columns: list[list] = []
129
+
130
+ for chunk in json["data"]:
131
+ for i, col in enumerate(chunk):
132
+ if i >= len(columns):
133
+ match column_types[i]:
134
+ case "String":
135
+ dtypes.append(np.dtype(np.dtypes.StringDType))
136
+ case "Int64":
137
+ dtypes.append(np.dtype(np.dtypes.Int64DType))
138
+ case "UInt64":
139
+ dtypes.append(np.dtype(np.dtypes.UInt64DType))
140
+ case "Double":
141
+ dtypes.append(np.dtype(np.dtypes.Float64DType))
142
+ case "Boolean":
143
+ dtypes.append(np.dtype(np.dtypes.BoolDType))
144
+
145
+ columns.append([])
146
+
147
+ columns[i] = columns[i] + col
148
+
149
+ arrays = [np.array(columns[i], dtype=dtypes[i]) for i in range(len(columns))]
150
+
151
+ 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.2
3
+ Version: 0.2.3
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=zT1ms_avuTmTA7xg8DfOQbGnGQ4p0awuQXcu43ItPhc,4615
5
+ turingdb-0.2.3.dist-info/METADATA,sha256=vtyv4ImbtOrYk1GuQnVi-46D3s2Nng8WP0l7cm9hRII,519
6
+ turingdb-0.2.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
+ turingdb-0.2.3.dist-info/licenses/LICENSE.txt,sha256=PgwD9hjeoyWO93yZQnxCyt0s-PlhXWLgqdCPpB91GhY,1078
8
+ turingdb-0.2.3.dist-info/RECORD,,
@@ -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,,