-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
56 lines (52 loc) · 1.79 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from clickhouse_driver import Client as CHClient
from IPython.display import display
class Client():
def __init__(
self,
host,
port,
user,
password,
notebook=True
):
self.host = host
self.port = port
self.user = user
self.password = password
self.notebook = notebook
self._client = CHClient(
host=self.host,
port=self.port,
user=self.user,
password=self.password,
# settings={'use_numpy': True}
)
def execute(self, input):
return self._client.execute(input)
def query_dataframe(self, input):
return self._client.query_dataframe(input)
def run(self, queries, verbose=True, pandas=True, skip=False,just_output=False,title=None):
if skip: return
if title is not None and title != "":
print(f"<< {title} >>")
queries = [query.strip() for query in queries.split(';') if query.strip() != '']
if verbose and not just_output: print(f"Running {len(queries)} commands")
for i, query in enumerate(queries):
if verbose and not just_output:
print(f"Running: (order={i})")
print(query)
if pandas:
output = self._client.query_dataframe(query)
if verbose:
if not output.empty:
print("Output:")
if self.notebook:
display(output)
else:
print(output)
else:
output = self._client.execute(query)
if verbose:
if output:
print("Output:")
print(output)