Skip to main content

A collection of personal scripts for json, File/Folder Operations, String Validation, Custom Errors, Cache and stuff.

Project description

PyPI Supported Python Versions PyPI version GitHub version codecov

Python-Backpack

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

Compatibility

  • Python 3.11+

Installation

pip install python-backpack

Package API Reference

Cache (backpack.cache)

  • timed_lru_cache(seconds: int, maxsize: int = 128)
    • functools.lru_cache decorator with expiration time.
    • Wrapped calls support force_clear=True and show_log=True.

Custom Errors (backpack.custom_errors)

  • EnvironmentVariableNotFoundError(var_name: str)
    • Raised when a required environment variable is missing.
  • ApplicationNotFoundError(app_name: str)
    • Raised when a required application is not found.

File Utils (backpack.file_utils)

  • 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.
  • get_version_from_filename(filename: str) -> str
    • Extracts a numeric version token from a filename.

Folder Utils (backpack.folder_utils)

  • 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 Utils (backpack.json_utils)

  • 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.

JSON Metadata (backpack.json_metadata)

  • JsonMetaFile(name: str, path: str)
    • Manages a metadata JSON file with package/system/time information.
    • Main public methods:
      • has_file() -> bool
      • load() -> None
      • insert(key: str, value: Any) -> None
      • remove(key: str) -> None
      • save() -> None
      • load_as_class() -> type
      • insert_class(_class: type) -> None

JSON User Settings (backpack.json_user_settings)

  • 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 | None
      • load_settings() -> dict | bool

Logger (backpack.logger)

  • get_logger(name: str) -> logging.Logger
    • Returns a configured logger with stream handler.

Patterns (backpack.patterns)

  • Singleton
    • Base class implementing singleton behavior via __new__.

Strings (backpack.strings)

  • 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.

Test Utils (backpack.test_utils)

  • random_string(length: int = 10) -> str
    • Generates a random lowercase string.
  • time_function_decorator(method: type)
    • Decorator that logs execution time.

Quick Example

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')

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

python_backpack-2.0.2.tar.gz (49.9 kB view details)

Uploaded Source

Built Distribution

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

python_backpack-2.0.2-py3-none-any.whl (26.9 kB view details)

Uploaded Python 3

File details

Details for the file python_backpack-2.0.2.tar.gz.

File metadata

  • Download URL: python_backpack-2.0.2.tar.gz
  • Upload date:
  • Size: 49.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.9 {"installer":{"name":"uv","version":"0.11.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for python_backpack-2.0.2.tar.gz
Algorithm Hash digest
SHA256 46ec2d06e603f432b9056e3e26f8b6883803c0858495eb70b9a045b8ad73a36a
MD5 a908cde26a73aa85743436b70186f2ae
BLAKE2b-256 4d56ccc5c19d070e5b62cf916512a4a5b9e8dd2d1156d18b556f982b6c80cd81

See more details on using hashes here.

File details

Details for the file python_backpack-2.0.2-py3-none-any.whl.

File metadata

  • Download URL: python_backpack-2.0.2-py3-none-any.whl
  • Upload date:
  • Size: 26.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.9 {"installer":{"name":"uv","version":"0.11.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for python_backpack-2.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0f79eb363cfc6a6206f408c6b9233fbfd4333c099ebaf62fe5de70b29af708e4
MD5 ff223d34b33d62a3106a2bfbf92b4362
BLAKE2b-256 f621ef2fb76be7161fdde4339774b2b83204c707b98ac3e2dd1aab4af201d26b

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