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:
ConfigParserfromconfigparser,psycopg2,DictCursorfrompsycopg2.extras, andsys. -
The
DatabaseHandlerclass is defined. -
The
__init__method initializes theDatabaseHandlerobject with theconfig_fileandconfig_file_sectionparameters. It also initializesself.connasNone. -
The
read_db_configmethod reads the configuration file specified inself.config_fileusingConfigParser. It retrieves the parameters for the PostgreSQL section specified inself.config_file_sectionand returns them as a dictionary. -
The
connectmethod establishes a connection to the database using the parameters obtained fromread_db_config. -
The
get_cursormethod creates a cursor for executing database queries. It checks if a connection exists or is closed and connects if necessary. It returns a cursor withDictCursoras the cursor factory, which returns query results as dictionaries. -
The
closemethod closes the database connection if it is open. -
The
commitmethod commits the currently open transaction. -
The
rollbackmethod rolls back the currently open transaction. -
The
executemethod 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
fetchonemethod executes a SELECT query that is expected to return a single row. It calls theexecutemethod internally, fetches the first row from the cursor, closes the cursor, and returns the row as apsycopg2 DictRow. -
The
fetchallmethod executes a SELECT query that is expected to return multiple rows. It calls theexecutemethod internally, fetches all rows from the cursor, closes the cursor, and returns the rows as a list ofpsycopg2 DictRowobjects. -
The
copy_tomethod executes a COPY command to copy data from a table to a file. It opens the file specified inpathand usescopy_tomethod of the cursor to perform the copying. If an exception occurs, it closes the cursor and raises the exception. -
The
sql_copy_tomethod executes an SQL COPY command to copy data from a table to a file. It opens the file specified inpathand usescopy_expertmethod of the cursor to perform the copying. If an exception occurs, it closes the cursor and raises the exception. -
The
sql_copy_frommethod executes an SQL COPY command to copy data from a file to a table. It opens the file specified inpathand usescopy_expertmethod of the cursor to perform the copying. If an exception occurs, it closes the cursor and raises the exception. -
The
copy_frommethod executes a COPY command to copy data from a file to a table. It opens the file specified inpathand usescopy_frommethod 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,encodersfromemail.mimeandemail,Helpersfromdatabase_handler,configfromdatabase_handler, andpathfromos. -
It reads the email server configuration parameters from a configuration file using the
CONFIGclass fromdatabase_handlerand assigns them to theparamsvariable. -
The
SendMailclass is defined. -
The
send_emailmethod is defined within theSendMailclass. 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
MIMEMultipartto 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_messageparameter. -
It attaches the HTML content to the email message using
MIMETextand the 'html' subtype. -
If there are file attachments, it iterates over the
file_attachmentslist, opens each file, creates aMIMEBaseobject, 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.
GoogleDrive
This code defines a class worker that provides various functionalities to interact with Google Drive using the Google Drive API. Below is a description of the key components and functionalities of this code:
Class worker
Class Attributes
- scope_readonly: Scope for read-only access to Google Drive.
- scope_write: Scope for read and write access to Google Drive.
- initial_download_path: Default path for downloading files, set to the current working directory.
Constructor
__init__(self, api_name='drive', api_version='v3', key_file_location=''): Initializes the class with API name, version, and the location of the key file.
Methods
-
construct_service(self, scope: str = None):
- Constructs and returns a Google Drive service instance with the specified scope.
-
read_drive_files(self, scope=scope_readonly, file_id: str = None, filename: str = None, ignore_trashed=True):
- Reads files from Google Drive based on file ID or filename, with an option to ignore trashed files.
-
download_drive_file(self, file_id=None, download_path=initial_download_path, filename=None, scope=scope_write):
- Downloads a file from Google Drive based on file ID or filename to a specified download path.
-
upload_file_to_drive(self, scope=scope_write, filename=None, file_path=None, parent_folder_id=None, mimetype=None, coerce=True):
- Uploads a file to Google Drive with the specified parameters.
-
get_file_permissions(self, file_id=None):
- Retrieves and returns the permissions of a specified file on Google Drive.
-
delete_drive_files(self, file_ids: list = [], reset=False):
- Deletes specified files from Google Drive. If
resetis True, it deletes all files.
- Deletes specified files from Google Drive. If
Error Handling
- The code handles errors using
try-exceptblocks and returns appropriate error messages and codes.
Usage
- The class
workercan be instantiated and used to interact with Google Drive, performing operations like reading, downloading, uploading, checking permissions, and deleting files.
This code provides a structured way to interact with Google Drive using the Google Drive API, encapsulating the logic within a class for ease of use and reusability.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file helper_scripts-0.4.3.tar.gz.
File metadata
- Download URL: helper_scripts-0.4.3.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8c8973c34713f56c1459831a807f1b8d4d4638a62740ba7ff0fa85597acff25
|
|
| MD5 |
2ae89ac8a7d4e2ca14872fbac682eafc
|
|
| BLAKE2b-256 |
abdc519fa55464b21e2f0f37bac29421dcb2f711904438f5025220ef07f27220
|
Provenance
The following attestation bundles were made for helper_scripts-0.4.3.tar.gz:
Publisher:
publish.yml on winston1o1/helper-scripts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
helper_scripts-0.4.3.tar.gz -
Subject digest:
f8c8973c34713f56c1459831a807f1b8d4d4638a62740ba7ff0fa85597acff25 - Sigstore transparency entry: 409387309
- Sigstore integration time:
-
Permalink:
winston1o1/helper-scripts@165a0b100b0f89ea9ad414a8ccb4e4d0f2235343 -
Branch / Tag:
refs/tags/0.4.3 - Owner: https://github.com/winston1o1
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@165a0b100b0f89ea9ad414a8ccb4e4d0f2235343 -
Trigger Event:
release
-
Statement type:
File details
Details for the file helper_scripts-0.4.3-py3-none-any.whl.
File metadata
- Download URL: helper_scripts-0.4.3-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5873087a30d10b76733cb0e6ba94892178b3709b7ae2c253bed5eea2edd0057e
|
|
| MD5 |
ab6d7f6f5e42c2b2c270c8d45a4038fc
|
|
| BLAKE2b-256 |
2fe24113da0fb99345e1951d1d1a0665e0b97c45d5ea0ce24b4cfef552c1218a
|
Provenance
The following attestation bundles were made for helper_scripts-0.4.3-py3-none-any.whl:
Publisher:
publish.yml on winston1o1/helper-scripts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
helper_scripts-0.4.3-py3-none-any.whl -
Subject digest:
5873087a30d10b76733cb0e6ba94892178b3709b7ae2c253bed5eea2edd0057e - Sigstore transparency entry: 409387312
- Sigstore integration time:
-
Permalink:
winston1o1/helper-scripts@165a0b100b0f89ea9ad414a8ccb4e4d0f2235343 -
Branch / Tag:
refs/tags/0.4.3 - Owner: https://github.com/winston1o1
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@165a0b100b0f89ea9ad414a8ccb4e4d0f2235343 -
Trigger Event:
release
-
Statement type: