Database Helper Package
Project description
HELPER SCRIPTS
Built for data professionals by data professionals.
Note: Despite the tagline, this package can be used by anyone
PACKAGE DESCRIPTION
DatabaseHandler
This code defines a class called DatabaseHandler
that provides methods for interacting with a PostgreSQL database.
-
It imports necessary modules:
ConfigParser
fromconfigparser
,psycopg2
,DictCursor
frompsycopg2.extras
, andsys
. -
The
DatabaseHandler
class is defined. -
The
__init__
method initializes theDatabaseHandler
object with theconfig_file
andconfig_file_section
parameters. It also initializesself.conn
asNone
. -
The
read_db_config
method reads the configuration file specified inself.config_file
usingConfigParser
. It retrieves the parameters for the PostgreSQL section specified inself.config_file_section
and returns them as a dictionary. -
The
connect
method establishes a connection to the database using the parameters obtained fromread_db_config
. -
The
get_cursor
method creates a cursor for executing database queries. It checks if a connection exists or is closed and connects if necessary. It returns a cursor withDictCursor
as the cursor factory, which returns query results as dictionaries. -
The
close
method closes the database connection if it is open. -
The
commit
method commits the currently open transaction. -
The
rollback
method rolls back the currently open transaction. -
The
execute
method creates a cursor, executes a query with optional arguments, and returns the cursor. If an exception occurs during query execution, it rolls back the transaction, closes the cursor, and raises the exception. -
The
fetchone
method executes a SELECT query that is expected to return a single row. It calls theexecute
method internally, fetches the first row from the cursor, closes the cursor, and returns the row as apsycopg2 DictRow
. -
The
fetchall
method executes a SELECT query that is expected to return multiple rows. It calls theexecute
method internally, fetches all rows from the cursor, closes the cursor, and returns the rows as a list ofpsycopg2 DictRow
objects. -
The
copy_to
method executes a COPY command to copy data from a table to a file. It opens the file specified inpath
and usescopy_to
method of the cursor to perform the copying. If an exception occurs, it closes the cursor and raises the exception. -
The
sql_copy_to
method executes an SQL COPY command to copy data from a table to a file. It opens the file specified inpath
and usescopy_expert
method of the cursor to perform the copying. If an exception occurs, it closes the cursor and raises the exception. -
The
sql_copy_from
method executes an SQL COPY command to copy data from a file to a table. It opens the file specified inpath
and usescopy_expert
method of the cursor to perform the copying. If an exception occurs, it closes the cursor and raises the exception. -
The
copy_from
method executes a COPY command to copy data from a file to a table. It opens the file specified inpath
and usescopy_from
method of the cursor to perform the copying. If an exception occurs, it closes the cursor and raises the exception.
Overall, this code provides a set of methods to interact with a PostgreSQL database, including connecting to the database, executing queries, fetching results, and performing data copying operations using the psycopg2
library.
SendMail
This code defines a class called SendMail
that is responsible for sending emails using the smtplib
library. Here's a breakdown of what the code is doing:
-
It imports necessary modules:
MIMEBase
,smtplib
,ssl
,MIMEText
,MIMEMultipart
,encoders
fromemail.mime
andemail
,Helpers
fromdatabase_handler
,config
fromdatabase_handler
, andpath
fromos
. -
It reads the email server configuration parameters from a configuration file using the
CONFIG
class fromdatabase_handler
and assigns them to theparams
variable. -
The
SendMail
class is defined. -
The
send_email
method is defined within theSendMail
class. It takes several parameters:email_message
(the body of the email),subject
(the subject of the email),email_recepients
(a list of recipient email addresses), andfile_attachments
(a list of file paths for attachments, optional). -
Inside the method, it retrieves the necessary email server configuration parameters from
params
. -
It creates an instance of
MIMEMultipart
to construct the email message. -
It sets the email subject, sender, recipient(s), and Cc fields of the message.
-
It constructs the HTML content of the email using the
email_message
parameter. -
It attaches the HTML content to the email message using
MIMEText
and the 'html' subtype. -
If there are file attachments, it iterates over the
file_attachments
list, opens each file, creates aMIMEBase
object, reads the file content, encodes it using Base64, and adds it as an attachment to the email message. -
It creates a SSL context using
ssl.create_default_context()
. -
It establishes a connection to the SMTP server specified in the email server configuration using
smtplib.SMTP()
. -
It starts a TLS connection with the server using
server.starttls()
. -
It authenticates with the server using the login email and password specified in the email server configuration.
-
It sends the email message using
server.sendmail()
. -
If an exception occurs during the process, it is raised.
Overall, this code provides a class that encapsulates the functionality to send emails with attachments using an email server specified in a configuration file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for helper_scripts-0.3.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a1db9bec2050ce1e937e93dbd31d994da3344fcbedcb7e9c394cfb6277c34a1 |
|
MD5 | 140b12b44c87b2dd954f83079105ee35 |
|
BLAKE2b-256 | 50969ecd273291443272eff8067235fe041d8849775a324d648d3800b19d4ac5 |