Skip to main content

A collection of utility scripts for working with files, Excel, logging, and database connections.

Project description

Python Utils

Welcome to the Python Utils repository! This project contains a collection of utility scripts written in Python for various purposes. Each script focuses on specific functionalities that can be reused across different projects. Below is a detailed description of the project's structure and its components.

Dependancies:

  • pandas
  • xlsxwriter
  • python-dotenv

Requirements

Make sure to install the required packages:

pip install py-utility-scripts

Components

excel_functions.py

This script provides functionalities for reading from and writing to Excel files using the pandas and xlsxwriter libraries.

Classes:

  • ExcelReader: Reads data from an Excel file.
  • WriteToExcel: Writes data to an Excel file.

Usage:

  1. ExcelReader: Instantiate with the path to the Excel file and optional columns to select. Use read_excel() to read the file and iterate_rows() to get the data as a list of dictionaries.
  2. WriteToExcel: Instantiate with the path to the Excel file. Use createWorkbook(), createWorksheet(), defineRowColumn(), and closeWorkbook() to manage and write data to the workbook.

Example

from python_utils import ExcelReader, WriteToExcel

# Write Score data to example.xlsx Excel file

file_path = "example.xlsx"

# Define headers and data
headers = ['name','age', 'score']
scores = [
    ['ankit',12, 1000],
    ['rahul',13, 100],
    ['priya',12, 300],
    ['harshita',12, 50],
]

# Create an instance of WriteToExcel
excel = WriteToExcel(file_path)

# Write data to the Excel file
excel.write_data_to_excel(file_path, "sheet_1", 0, 0, headers, scores)


# Read Score data from example.xlsx Excel file

excel_columns = ExcelReader('example.xlsx', selected_columns=['name', 'score'])
excel_columns.read_excel()
values = excel_columns.iterate_rows()

for row_data in values:
    print(row_data)

file_functions.py

This script renames all files in a specified directory to a sequentially numbered format with a user-defined prefix and format.

Class:

  • FileRenamer: Handles the file renaming process within a specified directory.

Usage:

  • Create an instance of FileRenamer with the desired path, prefix, and format. Use rename_files() to apply the renaming.

Example

from python_utils import FileRenamer

prefix = 'desktop-wallpaper'
count = 0
path = r'D:\images\Walpapers'

file_renamer = FileRenamer(path, prefix=prefix, name_format="{prefix}-{count:03d}")
file_renamer.rename_files()

log_message.py

This script provides a flexible logging mechanism that supports logging messages with dynamic log levels to both the console and a log file in JSON format. It also includes log file rotation. Check if the current log file exceeds the predefined maximum size. If it does, the function renames the current log file to include a timestamp in its name and retains it as an old log file. The timestamp format used is 'YYYYMMDD_HHMMSS' to ensure uniqueness and chronological sorting of old log files.

Class:

  • Logger: Handles logging messages with support for dynamic log levels and file rotation.

Usage:

  • Create an instance of Logger and use log() to log messages. Use add_log_level() and remove_log_level() to manage log levels.

Example

from python_utils import Logger

# Constants for log levels
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"

logger = Logger()
logger.log("This is an info message.", INFO)
logger.log("This is a warning message.", WARNING)
logger.log("This is an error message.", ERROR)

# Add a new log level and log a message with it
logger.add_log_level("DEBUG")
logger.log("This is a debug message.", "DEBUG")

# Remove an existing log level
logger.remove_log_level("DEBUG")

src/project_structure_generator.py

This script provides a project structure generatoe mechanism to print the given path directory structure.

Class:

  • ProjectStructure: Handles generating project structure for a given directory.

Usage:

  • Create an instance of ProjectStructure and use generate() to generate the project structure of a given directory.

Example

from python_utils import ProjectStructure
import os

root_path = r'D:\repos\python-utility-functions'
gitignore_file = os.path.join(root_path, '.gitignore')

project_structure = ProjectStructure(root_path, gitignore_file)
project_structure.generate()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_utility_scripts-1.2.1.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_utility_scripts-1.2.1-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file py_utility_scripts-1.2.1.tar.gz.

File metadata

  • Download URL: py_utility_scripts-1.2.1.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for py_utility_scripts-1.2.1.tar.gz
Algorithm Hash digest
SHA256 7b616915f56383e73cf9175bd79d3755bad01f7fec4f50c0b23ba716188f267f
MD5 5a6287176a700cde3f5f3a91d9ee28ee
BLAKE2b-256 ab13d973caf2a3337edad65aaca7deda666413e48f98981d81c5c3ceebf115c5

See more details on using hashes here.

File details

Details for the file py_utility_scripts-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for py_utility_scripts-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8440da5530437bb404220912db8c6a4daa44d875167ebc68a56f862425d33652
MD5 d295e42dc8f40cc075ad4416f27dd225
BLAKE2b-256 2c0ad1226f35ccf191a3ca0717d72f7722aa818cb9d64bd258ce1eab0b0e5a20

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page