Skip to content

Instantly share code, notes, and snippets.

View mpmansell's full-sized avatar

Mark Peter Mansell mpmansell

  • Bratislava, Slovakia
View GitHub Profile
@mpmansell
mpmansell / is_package_installed.py
Created January 26, 2026 02:30
Checks to see if a Python module is installed and available
# Check if a specified package is installed and available to Python.
def is_package_installed(package_name: str) -> bool:
"""Check if a Python package is installed."""
try:
__import__(package_name)
return True
except ImportError:
print(f"The required package '{package_name}' is not installed. Please install it to proceed.", file=sys.stderr)
return False
@mpmansell
mpmansell / codec_utils.py
Last active January 24, 2026 12:29
A few simple utilities to validate file encoding.
# Utility functions for process_urls.py
import pkgutil
import encodings
import codecs
############################################################################
# Codec related utilities
############################################################################
@mpmansell
mpmansell / generate_password.py
Last active November 29, 2025 04:14
Python function to generate secure passwords with some flexibility
# Generate an alphanumeric password with at least one lowercase character,
# at least one uppercase character, at least one digits, and one special symbol.
# See docstring for more information
from typing import List
import secrets
import string
def generate_password(min_len:int = 16, max_len: int = 16, allow_short: bool = False, safe: bool = True, risky: str = "") -> str:
"""
@mpmansell
mpmansell / dict_execute.py
Created November 3, 2022 15:21
Returns the result of a SQLite query as an iterator of dictionaries for each row, with each column name being used as a key for the column value.
import sqlite3
from typing import List
def dict_execute(con: sqlite3.Connection, query: str, **kwargs):
"""
Returns the result of a SQLite query as an iterator of dictionaries for each row, with each column name
being used as a key for the column value.
@mpmansell
mpmansell / remove_lines_by_step.py
Last active May 7, 2022 02:23
Copies a file of data rows, removing the lines specified in the user supplied pattern
##########################################################################################
# Purpose: Copies a file of data rows, removing the lines specified in the
# user supplied pattern
#
# Repository: https://gist.github.com/mpmansell/ad82a2cb7d0022e7239381e33b3eebf1
#
# Author: Mark Peter Mansell
# Github: https://github.com/mpmansell
#
# Copyright (c) 2022 Mark Peter Mansell
@mpmansell
mpmansell / random_remove_lines.py
Last active May 6, 2022 22:05
Removes random lines from a data file while copying it
##########################################################################################
# Purpose: Copies a file of data rows, randomly removing a specified number of them.
#
# Repository: https://gist.github.com/mpmansell/1479e309db5d0b354d082dc00a299683
#
# Author: Mark Peter Mansell
# Github: https://github.com/mpmansell
#
# Copyright (c) 2022 Mark Peter Mansell
#
@mpmansell
mpmansell / str_to_filename.py
Created March 14, 2022 16:15
Converts an arbitrary string into a filename, 'normalising' characters and optionally adding a default suffix.
import re
def str_to_filename(
text_string: str = "",
suffix: str = "",
append_suffix: bool = False,
lower: bool = False,
upper: bool = False,
):
@mpmansell
mpmansell / deep_set_dict.py
Created March 9, 2022 02:39
Set value in multiply nested dictionaries, creating keys and sub-dictionaries as required. 'Path' specified by key list or JSON path
#TODO: extend documentation for both methods and provide deeper explanations with examples
from typing import List, Set, Dict, SupportsFloat, Tuple, Optional
def deep_set_dict(dict_obj:dict, keys:list, value:any) -> None :
"""Sets a value in multiply nested dictionaries, creating keys and sub-dictionaries as required.
Args:
dict_obj (dict): The dictionary to add values to
@mpmansell
mpmansell / frange.py
Created December 2, 2021 07:12
Floating point implementation of the range() built-in function for embedded applications (as well as desktop/server)
#############################################################################
# Floating point implementation of the range() built-in function
# ==============================================================
#
# Primarily written for use with embedded Pythons such as micropython as a
# lighter implementation than importing NumPy and using numpy.arange()
#
# Implementation can take integers as parameters as they are all internally
# coerced to floats to avoid Type Errors during the calculations.
#