Python-Backpack is a lightweight utility collection for common scripting tasks:
- JSON load/save helpers with validation
- user settings persistence under the OS user directory
- metadata export/import to JSON files
- file and folder operations
- string normalization and case conversion
- cache decorator with expiration support
- custom exceptions, logging helper, singleton pattern, and testing helpers
- Python 3.11+
pip install python-backpacktimed_lru_cache(seconds: int, maxsize: int = 128)functools.lru_cachedecorator with expiration time.- Wrapped calls support
force_clear=Trueandshow_log=True.
EnvironmentVariableNotFoundError(var_name: str)- Raised when a required environment variable is missing.
ApplicationNotFoundError(app_name: str)- Raised when a required application is not found.
replace_strings_in_file(ascii_file: str, strings: list, new_string: str) -> None- Replaces multiple string occurrences in a text file.
remove_line_from_file(ascii_file: str, strings: list, verbose: bool = False) -> None- Removes exact matching lines from a text file.
file_is_writeable(filepath: str) -> bool- Checks whether a file can be opened for read/write.
browse_folder(folder: str) -> bool- Opens a folder in Windows Explorer.
create_folders(folders: list, force_empty: bool = False, verbose: bool = False)- Creates multiple folders.
create_folder(path: str, force_empty: bool = False, verbose: bool = True)- Creates a folder and optionally clears it if it already exists.
remove_files_in_dir(path: str)- Removes all files and subdirectories inside a directory.
recursive_dir_copy(source_path: str, target_path: str)- Recursively copies files and subfolders from source to target.
json_load(json_file: str) -> dict- Loads JSON data from file with validation and error handling.
json_save(data: dict, json_file: str) -> bool- Saves a dictionary to JSON file.
JsonMetaFile(name: str, path: str)- Manages a metadata JSON file with package/system/time information.
- Main public methods:
has_file() -> boolload() -> Noneinsert(key: str, value: Any) -> Noneremove(key: str) -> Nonesave() -> Noneload_as_class() -> typeinsert_class(_class: type) -> None
JsonUserSettings(folder: str, name: str)- Saves and loads JSON settings in the current user's home directory.
- Main public methods:
save_settings(data: dict | None = None) -> bool | Noneload_settings() -> dict | bool
get_logger(name: str) -> logging.Logger- Returns a configured logger with stream handler.
Singleton- Base class implementing singleton behavior via
__new__.
- Base class implementing singleton behavior via
normalize_input_string(input_string: str, under_spaces: bool = True, under_hyphen: bool = True, replacer: str = '_') -> str- Keeps alphanumeric/space/hyphen characters and normalizes separators.
begin_or_end_with_numbers(input_string: str) -> bool- Checks whether first or last character is numeric.
begin_with_number(input_string: str) -> bool- Checks whether the first character is numeric.
has_numbers(input_string: str) -> bool- Checks whether any character is numeric.
camelcase_to_snakecase(input_string: str) -> str- Converts CamelCase to snake_case.
- Handles acronym prefixes, for example
HTTPServer -> http_server.
random_string(length: int = 10) -> str- Generates a random lowercase string.
time_function_decorator(method: type)- Decorator that logs execution time.
from backpack.cache import timed_lru_cache
from backpack.strings import camelcase_to_snakecase, normalize_input_string
from backpack.json_utils import json_save, json_load
@timed_lru_cache(seconds=60)
def expensive_call():
return {'ok': True}
value = camelcase_to_snakecase('HTTPServer')
clean = normalize_input_string('Blade Runner-2049')
json_save({'value': value, 'clean': clean}, 'data.json')
data = json_load('data.json')