This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Utility functions for process_urls.py | |
| import pkgutil | |
| import encodings | |
| import codecs | |
| ############################################################################ | |
| # Codec related utilities | |
| ############################################################################ | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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: | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ########################################################################################## | |
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ########################################################################################## | |
| # 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 | |
| # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| def str_to_filename( | |
| text_string: str = "", | |
| suffix: str = "", | |
| append_suffix: bool = False, | |
| lower: bool = False, | |
| upper: bool = False, | |
| ): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ############################################################################# | |
| # 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. | |
| # |