Skip to content

Instantly share code, notes, and snippets.

@devinci-it
Last active May 7, 2024 01:38
Show Gist options
  • Select an option

  • Save devinci-it/d432708e796d6e6160efe13ee6cc7bbc to your computer and use it in GitHub Desktop.

Select an option

Save devinci-it/d432708e796d6e6160efe13ee6cc7bbc to your computer and use it in GitHub Desktop.
This is a collection of Python utility modules aimed at streamlining development workflows and making the development process more enjoyable.
from .writer import write_file_with_prompt as write_cautious
from .banner import print_banner as banner
from .file_utils import copy_file_with_prompt as copy

Python Dev Utility Modules

This is a collection of Python utility modules aimed at streamlining development workflows and making the development process more enjoyable.

Overview

This collection includes various utility modules designed to perform common development tasks, such as file manipulation, text formatting, and command-line interface (CLI) handling.

Modules

Module Description
writer.py A module providing utility functions for writing files.
Functions:
- write_file_with_prompt: Write content to a file, prompting the user if the file already exists.
banner.py A module for creating formatted banners for CLI output.
Functions:
- print_banner: Print a formatted banner to the console.

Got it! Here's the updated README section:


Setup

To use these utility modules in your Python projects, follow these steps:

  1. Clone this repository to your local machine:

    git clone https://gist.github.com/devinci-it/d432708e796d6e6160efe13ee6cc7bbc/
  2. Rename and navigate to the cloned directory:

    mv ./d432708e796d6e6160efe13ee6cc7bbc ./dev_utils
    cd dev_utils
  3. Install the modules using pip:

    pip install .
  4. The main functions from some modules are automatically imported in the __init__.py file for convenience. You can modify these aliases as needed or use them directly in your scripts.

  5. Start using the utility functions in your projects!

Note

This collection of utility modules is an ongoing project, and additional modules will be published as they are developed. These modules are personally used and developed to streamline and enhance the development experience.

"""
banner: A utility module for printing formatted banners in the console.
This module provides a function for printing banners with customizable text, width, border character, alignment,
and newline options.
Example:
```
from banner import print_banner
# Print a centered banner with default width and border character
print_banner("Hello, World!")
# Print a left-aligned banner with custom width and border character
print_banner("Welcome", width=60, border_char="-", alignment="left")
# Print a right-aligned banner without newline
print_banner("Goodbye", width=50, border_char="*", alignment="right", newline=False)
```
"""
def print_banner(text: str, width: int = 80, border_char: str = "#", alignment: str = 'center', newline: bool = True):
"""
Print a formatted banner with the specified text.
Args:
text (str): The text to display in the banner.
width (int): The width of the banner. Default is 80 characters.
border_char (str): The character used for the border of the banner. Default is "#".
alignment (str): The alignment of the text within the banner. Options: 'left', 'center', 'right'. Default is 'center'.
newline (bool): Whether to print a newline before and after the banner. Default is True.
"""
border = border_char * width
if alignment == 'center':
padding = (width - len(text) - 2) // 2
banner_text = f"{border_char}{' ' * padding}{text}{' ' * padding}{border_char}"
elif alignment == 'left':
banner_text = f"{border_char} {text}{' ' * (width - len(text) - 2)} {border_char}"
elif alignment == 'right':
banner_text = f"{border_char}{' ' * (width - len(text) - 2)}{text} {border_char}"
else:
raise ValueError("Invalid alignment. Options: 'left', 'center', 'right'.")
if newline:
print(border)
print(banner_text)
if newline:
print(border)
"""
file_utils.py: A module providing utility functions for file operations.
This module provides functions for performing various file operations with additional features such as prompting the user before overwriting existing files.
Functions:
- copy_file_with_prompt: Copy a file from source to destination, prompting the user if the destination file already exists.
"""
import os
import shutil
def copy_file_with_prompt(src_file, dst_file, force=False):
"""
Copy a file from source to destination, prompting the user if the destination file already exists.
Args:
src_file (str): The path to the source file.
dst_file (str): The path to the destination file.
force (bool, optional): If True, overwrite the destination file without prompting. Defaults to False.
Returns:
bool: True if the file was successfully copied, False otherwise.
"""
if os.path.exists(dst_file) and not force:
overwrite = input(f"The file '{dst_file}' already exists. Do you want to overwrite it? (yes/no): ")
if overwrite.lower() != 'yes':
print("Operation canceled.")
return False
try:
shutil.copy(src_file, dst_file)
print(f"File '{src_file}' copied to '{dst_file}' successfully.")
return True
except Exception as e:
print(f"Error copying file '{src_file}' to '{dst_file}': {e}")
return False
[metadata]
name = dev-utils
version = 0.1
author = devinci-it
description = Collection of Python utility modules for developers.
long_description = file: README.md
long_description_content_type = text/markdown
url = https://gist.github.com/devinci-it/d432708e796d6e6160efe13ee6cc7bbc/
license = MIT
[options]
packages = find:
python_requires = >=3.6
"""
writer.py: A module providing utility functions for writing files.
This module provides functions to write content to files with additional features such as prompting the user before overwriting existing files.
Functions:
- write_file_with_prompt: Write content to a file, prompting the user if the file already exists.
"""
import os
def write_file_with_prompt(file_path, content, force=False):
"""
Write content to a file, prompting the user if the file already exists.
Args:
file_path (str): The path to the file.
content (str): The content to write to the file.
force (bool, optional): If True, overwrite the file without prompting. Defaults to False.
Returns:
bool: True if the file was successfully written, False otherwise.
"""
if os.path.exists(file_path) and not force:
overwrite = input(f"The file '{file_path}' already exists. Do you want to overwrite it? (yes/no): ")
if overwrite.lower() != 'yes':
print("Operation canceled.")
return False
try:
with open(file_path, 'w') as file:
file.write(content)
print(f"File '{file_path}' successfully written.")
return True
except Exception as e:
print(f"Error writing to file '{file_path}': {e}")
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment