Skip to main content

Bunch of decorators to decorate clickhouse-driver's execute method

Project description

clickhouse-driver-decorators

license python coverage

Bunch of decorators to decorate clickhouse-driver's execute method

Available decorators:

  1. add_column_names -- converts each row to dict or namedtuple with column names as keys;
  2. apply_callback -- applies a given callback to each row of result;
  3. 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;
  4. convert_timestamp_to_datetime -- converts timestamp to python datetime object in a needed timezone for a given column.
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clickhouse-driver-decorators-0.2.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

clickhouse_driver_decorators-0.2.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file clickhouse-driver-decorators-0.2.0.tar.gz.

File metadata

  • Download URL: clickhouse-driver-decorators-0.2.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.3

File hashes

Hashes for clickhouse-driver-decorators-0.2.0.tar.gz
Algorithm Hash digest
SHA256 32154434cc50d2982f40258501ea02f2e372475a0472cf3cf88ce423f198991a
MD5 128f5517545fe841be5d0a23e5e90c0e
BLAKE2b-256 571fd7b723c0d3a2c15fdb8add0c64c44eb0251e1f19e27cd50a75d9667ca1f1

See more details on using hashes here.

File details

Details for the file clickhouse_driver_decorators-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: clickhouse_driver_decorators-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.3

File hashes

Hashes for clickhouse_driver_decorators-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df05e25e7f2428d1ec478324e0fcaddb0d47e52fd0b0a0ec20d21b8013744dee
MD5 2a636e93fb6ec12de591a3463c300548
BLAKE2b-256 1e5fd357ca76c9791d495d1e00aff112adc665c845156b3708623f2a409816bd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page