Skip to main content

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)

Metadata

Release files for clickhouse-driver-decorators 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for clickhouse-driver-decorators 0.2.0
File Size Uploaded
clickhouse-driver-decorators-0.2.0.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for clickhouse-driver-decorators 0.2.0
File Interpreter ABI Platform
clickhouse_driver_decorators-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / clickhouse-driver-decorators-0.2.0.tar.gz

Download URL clickhouse-driver-decorators-0.2.0.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
32154434cc50d2982f40258501ea02f2e372475a0472cf3cf88ce423f198991a
BLAKE2b-256 checksum
How to use checksums
571fd7b723c0d3a2c15fdb8add0c64c44eb0251e1f19e27cd50a75d9667ca1f1
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / clickhouse_driver_decorators-0.2.0-py3-none-any.whl

Download URL clickhouse_driver_decorators-0.2.0-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
df05e25e7f2428d1ec478324e0fcaddb0d47e52fd0b0a0ec20d21b8013744dee
BLAKE2b-256 checksum
How to use checksums
1e5fd357ca76c9791d495d1e00aff112adc665c845156b3708623f2a409816bd
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page