Bunch of decorators to decorate clickhouse-driver's execute method
Project description
clickhouse-driver-decorators
Bunch of decorators to decorate clickhouse-driver's execute
method
Available decorators:
add_column_names
-- converts each row to dict or namedtuple with column names as keys;apply_callback
-- applies a given callback to each row of result;convert_string_to_datetime
-- converts string datetime to python datetime object in a needed timezone for a given column. String datetime have to be in UTC;convert_timestamp_to_datetime
-- converts timestamp to python datetime object in a needed timezone for a given column.transform_to_pandas_dataframe
-- creates pandas frame based on result from db
Examples
For a closer look, check out the tests
from typing import Generator
from pandas import DataFrame
from clickhouse_driver import Client
from clickhouse_driver_decorators import *
def replace_empty_with_none(row: tuple) -> Generator:
yield tuple([None if v == '' else v for v in row])
class A:
def __init__(self, client: Client):
self.__client = client
def get_data(self) -> Generator[]:
return self.__decorated_execute("""
select
toUnixTimestamp(now()) as now_ts,
formatDateTime(now(), '%F %T', 'UTC') as now_str,
'' as empty
""")
@add_column_names()
@apply_callback(on_row_callable=replace_empty_with_none)
@convert_timestamp_to_datetime(columns_to_convert={'now_ts': 'Europe/Moscow'})
@convert_string_to_datetime(date_format='%Y-%m-%d %H:%M:%S', columns_to_convert={'now_str': 'Europe/Moscow'})
def __decorated_execute(self, query: str):
return self.__client.execute(query, with_column_types=True)
class B:
def __init__(self, client: Client):
self.__client = client
def get_pandas_df(self) -> DataFrame:
return self.__decorated_execute("select 'val1' as col1, 'val2' as col2")
@transform_to_pandas_dataframe()
def __decorated_execute(self, query: str):
return self.__client.execute(query, with_column_types=True)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for clickhouse-driver-decorators-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ba8b91981f65d984435840c30c245b84435b4cc6e595b1a85b04d1b6b5664bc |
|
MD5 | fed41d5a24967701f9c2c4ced35e9fa8 |
|
BLAKE2b-256 | 90a2df56848702846957829a44bc89755deb22ba9fb4390999e4a950af2b7bfc |
Close
Hashes for clickhouse_driver_decorators-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fec3958332b8a3156749f664ec1f2d32ea453fff11bbd85f94918b9387ebfdd0 |
|
MD5 | 00c077709379a998cd2947977f4a21f9 |
|
BLAKE2b-256 | ba7e2cdc3c5cfbf8cf36fab0f6822b1d39495236dd06d4a4b563d4a445577364 |