Python Utility library
Introduction
The library provides a useful set of utility functions frequently used when creating Python components for Keboola Connection. The utility library should be used in cooperation with the main Python Component library.
The Python Utility library is developed the Keboola Data Services team and is officially supported by Keboola. The library aims to ease the component creation process by removing the necessity to write frequently used functions all over again.
Links
- API Documentation: API Docs
- Source code: https://github.com/keboola/python-utils
- PYPI project code: https://pypi.org/project/keboola.utils
- Documentation: https://developers.keboola.com/extend/component/python-component-library
Quick start
Installation
The package can be installed via pip using:
pip install keboola.utils
Structure and functionality
The package currently contains one core module:
keboola.utils.date- a set of methods for date manipulation.keboola.utils.helpers- a general helper functions and classes that are relevant in Keboola Connection environment.
keboola.utils.header_normalizer- Different strategies to convert column names to a valid KBC format.
Helpers
The module contains general helper functions and classes that are relevant in Keboola Connection environment.
Date Utilities
The module contains all date related functions, which can be utilized to effective work with dates, when creating components for Keboola Connection.
Initialization
All util functions can be imported from keboola.utils module.
from keboola.utils import *
or
import keboola.utils.date
to import only functions from a certain module.
Getting converted date period from string
The function parse_datetime_interval() allows to parse any string containing date format into a Python datetime; or if strformat parameter is specified, into a datetime formatted string.
The positional arguments period_from and period_to can be specified in relative format (e.g. 3 days ago, 2 months ago, etc.) or in absolute format (e.g. 2020-01-01). For full list of supported formats, please refer to dateparser documentation.
from keboola.utils import *
dt_str_1 = '5 days ago'
dt_str_2 = 'today'
dt_format = '%Y-%m-%d'
start_date, end_date = parse_datetime_interval(dt_str_1, dt_str_2, dt_format)
Generating date period chunks
The function split_dates_to_chunks() allows to split time interval into chunks of specified size.
import keboola.utils.date as dutils
from datetime import date
dt_1 = date(2021, 1, 1)
dt_2 = date(2021, 1, 10)
dt_format = '%Y-%m-%d'
intervals = dutils.split_dates_to_chunks(dt_1, dt_2, intv=2, strformat=dt_format)
for intv in intervals:
print(intv['start_date'], intv['end_date'])
Usage Example
import keboola.utils.date as dutils
dt_str_1 = '5 days ago'
dt_str_2 = 'today'
dt_format = '%Y-%m-%d'
start_date, end_date = dutils.parse_datetime_interval(dt_str_1, dt_str_2)
intervals = dutils.split_dates_to_chunks(start_date, end_date, intv=2, strformat=dt_format)
for intv in intervals:
print(intv['start_date'], intv['end_date'])
Header normalizer
This module provides different strategies to normalize CSV column names to a format supported by the Keboola Connection Storage:
Only alphanumeric characters and underscores are allowed in column name. Underscore is not allowed on the beginning.
Example:
import keboola.utils.header_normalizer as hnorm
head_norm = hnorm.get_normalizer(strategy=hnorm.NormalizerStrategy.ENCODER, char_encoder="unicode")
header = ["dactor#fd", "a*ruas$", "48DHBb#@"]
norm_headers = head_norm.normalize_header(header)
# Results in: ['dactor_35_fd', 'a_42_ruas_36_', '48DHBb_35__64_'])
Metadata
Release files for keboola.utils 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| keboola.utils-1.1.0.tar.gz | 9.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| keboola.utils-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.0 kB
Release files / keboola.utils-1.1.0.tar.gz
| Download URL | keboola.utils-1.1.0.tar.gz |
|---|---|
| Size | 9.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e943dbda932d945bcd5edd51283eea8f7035249c9dac769d3e96d2f507b52f60
|
|
BLAKE2b-256 checksum How to use checksums |
a7b8ccfddc2eb510f7a6ab878ab8a6249a23494194780a436676da6c2f5d23c7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.10
|
Release files / keboola.utils-1.1.0-py3-none-any.whl
| Download URL | keboola.utils-1.1.0-py3-none-any.whl |
|---|---|
| Size | 10.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8c73faa4a81f371a2eecd8465b08a51b3f7608969dd91d38d5b3bcfad7ef0da5
|
|
BLAKE2b-256 checksum How to use checksums |
f9f46697a0c2ff512baa7b84413972e51d5449a0a145f68dc750f05a8b1da39d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.10
|
Security for keboola.utils 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.