Package: custom-utils
custom-utilsPip Package for Database Connectors, Alerter, Log Formatter etcTable of Contentscustom-utilsTable of Contents1.  Installation2. Connector1. S3 Connector2. MySQL Connector3.  MongoDB Connector4.  BigQuery Connector3. Configurer1. Profile Decorator2.  Log Formatter4. Alerter1.  Slack Alerter1.  InstallationInstallation from Pypi repository(Any one)pip install custom-utils--> minimal installationpip install custom-utils[full]--> full installation (linux/windows)pip install "custom-utils[full]"--> full installation (mac)pip install custom-utils[s3,mysql,bigquery,mongodb,slack]--> selective installationInstallation from github repository(Any one)pip install git+https://github.com/rahulnkumar/custom-utils.gitpip install git+https://github.com/rahulnkumar/custom-utils.git@<tag_no>pip install git+https://github.com/rahulnkumar/custom-utils.git@<branch_name>2. Connector1. S3 ConnectorCode Snippet Sample :## To use S3 either setup AWS CLI or set the credentials from pip package only## Setup AWS CLIsudoaptinstallawscli&&awsconfigure## Setup Credentials from pip packagefromcustom_utils.configurer.utilsimportCredentialCredential.set(ACCESS_KEY="*********",SECRET_KEY="*********")fromcustom_utils.connector.s3importS3# Upload python object to S3S3.push_python_object(python_object,s3_uri)# Pull python object from S3obj=S3.pull_python_object(s3_uri)# AWS CLI setup is must to use the following methods# Uplaoding local data to S3S3.push_local_data(file_path,s3_uri)# Downloading data from S3S3.pull_s3_data(file_path,s3_uri)S3 Connector Documentationclass S3(builtins.object)
 |  AWS S3 utility functions
 |  
 |  Static methods defined here:
 |  
 |  pull_python_object(s3_uri)
 |      read python object stored in S3 bucket
 |      :param string s3_uri: s3 uri of the object
 |      :return python_object : python object stored in the S3
 |  
 |  pull_s3_data(file_path, s3_uri)
 |      write data stored in local machine into S3 bucket from
 |      :param string s3_uri: target s3 uri
 |      :param string file_path:  local path of the file 
 |      :return None
 |  
 |  push_local_data(file_path, s3_uri)
 |      write data stored in local machine into S3 bucket from
 |      :param string s3_uri: target s3 uri
 |      :param string file_path:  local path of the file 
 |      :return None
 |  
 |  push_python_object(python_object, s3_uri)
 |      write python objects/variables etc  into S3 bucket
 |      :param string bucket: bucket name
 |      :param string sub_bucket: sub-bucket name
 |      :param string file_name: name of the file to be written
 |      :return None
 |  
 |  read_csv(s3_uri)
 |      write data stored in local machine into S3 bucket from
 |      :param string s3_uri: csv file S3 URI
 |      :return df : pandas dataframe
 |  
 |  ----------------------------------------------------------------------2. MySQL ConnectorCode Snippet Sample :# Query from Custom MySQL Databasefromcustom_utils.connector.mysqlimportMySQLuser="***"password="***"host="***"port="***"database="***"db_string=f"mysql+mysqlconnector://{user}:{password}@{host}:{port}/{database}"query="select * from table_name limit 10"mysql=MySQL(db_string=db_string)df=mysql.pull_data(query=query)MySQL Connector Documentaionclass MySQL(builtins.object)
 |  MySQL(db_string)
 |  
 |  MySQL database utility functions
 |  
 |  Methods defined here:
 |  
 |  __init__(self, db_string)
 |      initialisation method for MySQL Connector
 |      :param string db_string: mysql database connection string
 |  
 |  push_data(self, data, table_name, mode='append')
 |      Execute a query in the mysql table
 |      :param pd.DataFrame data: dataframe to be appended or replaced
 |      :param string table_name: name of the the target table
 |      :param string mode: it can be either replace or append
 |      :return None
 |  
 |  execute_query(self, query)
 |      Execute a query in the mysql table
 |      :param string query: query for execution in the table
 |      :return :
 |  
 |  pull_data(self, query)
 |      Fetch data from mysql as a dataframe.
 |      :param string query: query for fetching data from table
 |      :return pd.DataFrame data3.  MongoDB ConnectorCode Snippet Sample :fromcustom_utils.connector.mongodbimportMongoDBuri="****"db="***"collection="****"mongo=MongoDb(uri=uri,db=db)#Reading with pull methoddata=mongo.pull_data(collection=collection,list_dict=list_dict)# Reading with fetch methodquery={'id':{'$in':[1,2]}}data=mongo.fetch_data(collection,query=query,only_include_keys=["name"])#Writing into MongoDBmongo.push_data(collection=collection,data=data)#Updating valueid_dict={"id":"2"}set_dict={"$set":{"name":"ram"}}mongo.update_value(id_dict,set_dict,collection=collection,upsert=True)# Deleting datamongo.delete_data(collection=collection,overall=False,condition_dict={"id":None})MongoDB Connector Documentaionclass MongoDB(builtins.object)
 |  MongoDB(db=None, uri=None)
 |  
 |  MongoDB utility functions.
 |  
 |  Methods defined here:
 |  
 |  __init__(self, db=None, uri=None)
 |      initialisation method for MongoDB connector
 |      :param str db: database name
 |      :param str uri: mongo uri string for establishing connection
 |  
 |  delete_data(self, collection, db=None, overall=False, condition_dict=None)
 |      Function for inserting data into db
 |      :param str db : database name
 |      :param str collection : collection name
 |      :param bool overall : delete whole collection if True
 |      :param dict condition_dict : query for deletion
 |      :return:
 |  
 |  fetch_data(self, collection, db=None, query={}, only_include_keys=[])
 |      function to fetch data from the given database and collection on given query
 |      :param str db : db_name in mongo
 |      :param str collection: collection name in mongo for database db
 |      :param dict query : execution query statement; default is {} which means fetch
 |                         all without any filters
 |      :param list only_include_keys : list of keys to be included while fetching rows
 |      :return: pd.DataFrame
 |  
 |  fetch_data_sorted(self, collection, db=None, pipeline=[])
 |      function to fetch data from the given database and collection on given query
 |      :param str db: db_name in mongo
 |      :param str collection: collection name in mongo for database db
 |      :param list pipeline: pipeline required to aggregate
 |      :return: pd.DataFrame
 |  
 |  pull_data(self, list_dict, collection, db=None)
 |      Function for inserting data into db
 |      :param str db : database name
 |      :param str collection : collection name
 |      :param list list_dict : query for fetching data
 |      :return: pd.DataFrame
 |  
 |  push_data(self, data, collection, db=None)
 |      Function for inserting data into db
 |      :param str db : database name
 |      :param str collection : collection name
 |      :param list/pd.DataFrame data : data to be inserted in the form of
 |                                      dataframe or list of dictionaries
 |      :return:
 |  
 |  update_value(self, id_dict, set_dict, collection, db=None, upsert=None)
 |      Function for updating data into db
 |      :param str db : database name
 |      :param str collection : collection name
 |      :param dict id_dict : query for updation
 |      :param dict set_dict : key and value dictionary to be updated
 |      :param bool upsert : whether to upsert or just update
 |      :return:
 |  
 |  upsert_json(self, output_json, upsert_keys, collection, db=None)
 |      Function for inserting data into db
 |      :param str db : database name
 |      :param str collection : collection name
 |      :param dict output_json : list of dictionaries where each dictionary is
 |                                a row with keys as column names
 |      :param list upsert_keys : keys to be upserted
 |      :return:
 |  
 |  ----------------------------------------------------------------------4.  BigQuery ConnectorCode Snippet Sample :# Fetching data from BigQueryfromcustom_utils.connector.bigqueryimportBigQuerybq=BigQuery(read_big_query_project="****",service_account_file_path="***.json")query="select * from table_name"df=bq.pull_data(query)# Dumping Dataframe in BigQuerybq.push_data(database="rahul_temp",table="demo",dataframe=df,mode="append")# Executing any query in BigQueryquery="INSERT rahul_temp.Demo (id, userId) VALUES(1,1),(1,1)"BigQuery.execute_query(query)# Streaming insert in BigQueryrow_to_insert=[{"id":1,"userid":1,"languageId":58,"mode":0,"active":1}]BigQuery.insert_rows_in_bigquery(dataset="rahul_temp",table="Demo",rows_to_insert=row_to_insert)BigQuery Connector Documentaionclass BigQuery(builtins.object)
 |  BigQuery(read_big_query_project, write_big_query_project, service_account_file_path)
 |  
 |  BigQuery database utility functions
 |  
 |  Methods defined here:
 |  
 |  __init__(self, read_big_query_project, write_big_query_project, service_account_file_path)
 |      initialisation method for BigQuery Connector
 |      :param str read_big_query_project : project used while reading from BigQuery
 |      :param str write_big_query_project: project used while writing into BigQuery
 |      :param str service_account_file_path: project specific BigQuery Credential
 |  
 |  push_data(self, database=None, table=None, dataframe=None, mode='append')
 |      Dumps data into from BigQuery
 |      :param string database: target bigquery database
 |      :param string table: target table name
 |      :param pd.DataFrame dataframe: pandas dataframe for dumping into bigquery
 |      :param string mode: it can be either append or replace
 |  
 |  execute_query(self, query, job_config=None, timeout=900, max_retries=0, time_interval=5)
 |      Executes query from from BigQuery table
 |      :param string query: query for execution
 |      :param string query_cofig: config for parameterised query
 |      :param integer timeout : maximum bigquery execution timeout
 |      :param string max_retries: maximum retries if data is not fetched
 |      :param integer time_interval : time interval between retries
 |  
 |  pull_data(self, query=None, job_config=None, max_retries=0, time_interval=5)
 |      Fetches data from from BigQuery
 |      :param string query: query for fetching data from table
 |      :param string query_cofig: config for parameterised query
 |      :param string max_retries: maximum retries if data is not fetched
 |      :param integer time_interval : time interval between retries
 |  
 |  insert_rows_array(self, dataset=None, table=None, rows_to_insert=None)
 |      Streaming insert into from BigQuery
 |      :param string dataset: target bigquery database
 |      :param string table: target table name
 |      :param list rows_to_insert: list of dictionaries where each dictionary is a
 |                                  row with keys as column names
 |  
 |  insert_rows_in_bigquery(self, dataset=None, table=None, rows_to_insert=None)
 |      Streaming insert into from BigQuery
 |      :param string dataset: target bigquery database
 |      :param string table: target table name
 |      :param  rows_to_insert: list of dictionaries where each dictionary is a
 |                              row with keys as column names3. Configurer1. Profile DecoratorCode Snippet Sample :fromcustom_utils.configurer.profilerimportprofiler@profiler(sort_by='cumulative',lines_to_print=10,strip_dirs=True)defproduct_counter_v3():return[1,2,3,4,5]Profiler Documentaiondefprofiler(output_file=None,sort_by='cumulative',lines_to_print=None,strip_dirs=False):"""A time profiler decorator:param str output_file: Path of the output file. If only name of the file is given, it's saved in the currentdirectory. If it's None, the name of the decorated function is used.:param str sort_by: SortKey enum or tuple/list of str/SortKey enum Sorting criteria for the Stats object. For a listof valid string and SortKey refer to: https://docs.python.org/3/library/profile.html#pstats.Stats.sort_stats:param int lines_to_print: Number of lines to print. Default (None) is for all the lines. This is useful in reducingthe size of the printout, especially that sorting by 'cumulative', the time consuming operations are printed towardthe top of the file.:param bool strip_dirs: Whether to remove the leading path info from file names. This is also useful in reducing thesize of the printout:return: Profile of the decorated function"""2.  Log FormatterCode Snippet Sample :fromcustom_utils.configurer.utilsimportLogFormatterLogFormatter.apply()Log Formatter Documentationclass LogFormatter(logging.Formatter):
    """Log Formatter class for custom-utils package"""

    __date_format = '%d/%b/%Y:%H:%M:%S %Z'

    @staticmethod
    def apply(level=logging.INFO):
        """
        Start logging in json format.
        :return:4. Alerter1.  Slack AlerterCode Snippet Sample :fromcustom_utils.alerter.slackimportSlackslack=Slack(token=SLACK_BOT_TOKEN)# OR Slack() with  SLACK_BOT_TOKEN as env variablechannel="#shield"message="testing"uids=["U03PAP8C1RC","U03PAP8C1RC"]slack.send_alert(channel,message,uids)Alerter Documentation :classSlack(builtins.object)|Slack(token=None)||Classforsendingalertsandmonitoringstatstoaslackchannel||Methodsdefinedhere:||__init__(self,token=None)|Initializeself.Seehelp(type(self))foraccuratesignature.||send_alert(self,channel:str,message:str,uids:list=[])|Thisfunctionsendalerttoatargetchanneltaggingauser|withaalertmessageandtracebackerror.|args:|channel:Nameofthechannel(ex:#channel_name)|message:Passthemessagetobedisplayedinthechannel|uids:ListofSlackuser_idswhoneedstobetagged||returns:Nothing||----------------------------------------------------------------------