"""Lambda shared module.""" from contextlib import contextmanager import pymysql @contextmanager def mysql_connection( host, user, password, database, connect_timeout=5, port=3306): """Context manager for mysql connection objects. Args: host (str): hostname of database server user (str): user name password (str): password database (str): database name connect_timeout (int): time to wait for db connection port (int): the port of the sql connection Yields: pymysql.connections.Connection: connection to direct delivery db host """ conn = pymysql.connect( host, user=user, passwd=password, db=database, connect_timeout=connect_timeout, port=port, cursorclass=pymysql.cursors.DictCursor, autocommit=True) yield conn