Package: keboola.utils
Python Utility libraryIntroductionThe 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 mainPython Componentlibrary.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.LinksAPI Documentation:API DocsSource code:https://github.com/keboola/python-utilsPYPI project code:https://pypi.org/project/keboola.utilsDocumentation:https://developers.keboola.com/extend/component/python-component-libraryQuick startInstallationThe package can be installed viapipusing:pip install keboola.utilsStructure and functionalityThe package currently contains one core module:keboola.utils.date- a set of methods for date manipulation.keboola.utils.helpersa general helper functions and classes that are relevant in Keboola Connection environment.keboola.utils.header_normalizerDifferent strategies to convert column names to a valid KBC format.HelpersThe module contains general helper functions and classes that are relevant in Keboola Connection environment.Date UtilitiesThe module contains all date related functions, which can be utilized to effective work with dates, when creating components for Keboola Connection.InitializationAll util functions can be imported fromkeboola.utilsmodule.fromkeboola.utilsimport*orimportkeboola.utils.dateto import only functions from a certain module.Getting converted date period from stringThe functionparse_datetime_interval()allows to parse any string containing date format into a Python datetime; or ifstrformatparameter is specified, into a datetime formatted string.The positional argumentsperiod_fromandperiod_tocan 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 todateparserdocumentation.fromkeboola.utilsimport*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 chunksThe functionsplit_dates_to_chunks()allows to split time interval into chunks of specified size.importkeboola.utils.dateasdutilsfromdatetimeimportdatedt_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)forintvinintervals:print(intv['start_date'],intv['end_date'])Usage Exampleimportkeboola.utils.dateasdutilsdt_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)forintvinintervals:print(intv['start_date'],intv['end_date'])Header normalizerThis module provides different strategies to normalize CSV column names
to a format supported by theKeboola Connection Storage:Only alphanumeric characters and underscores are allowed in column name. Underscore is not allowed on the beginning.Example:importkeboola.utils.header_normalizerashnormhead_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_'])