python-backpack 2.0.0__tar.gz → 2.0.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_backpack-2.0.2/.github/workflows/codecov_tests.yml +51 -0
- python_backpack-2.0.2/.gitignore +131 -0
- python_backpack-2.0.2/.vscode/settings.json +9 -0
- python_backpack-2.0.2/PKG-INFO +155 -0
- python_backpack-2.0.2/README.md +146 -0
- python_backpack-2.0.2/backpack/__init__.py +0 -0
- {python_backpack-2.0.0 → python_backpack-2.0.2}/backpack/file_utils.py +41 -0
- {python_backpack-2.0.0 → python_backpack-2.0.2}/backpack/json_utils.py +1 -1
- {python_backpack-2.0.0 → python_backpack-2.0.2}/backpack/version.py +3 -2
- python_backpack-2.0.2/backpack.code-workspace +63 -0
- python_backpack-2.0.2/codecov.yml +2 -0
- python_backpack-2.0.2/coverage.bat +9 -0
- python_backpack-2.0.2/pyproject.toml +30 -0
- python_backpack-2.0.2/ruff.toml +26 -0
- python_backpack-2.0.2/tests/__init__.py +0 -0
- python_backpack-2.0.2/tests/test_cache.py +20 -0
- python_backpack-2.0.2/tests/test_errors.py +47 -0
- python_backpack-2.0.2/tests/test_file_utils.py +71 -0
- python_backpack-2.0.2/tests/test_files/edited.txt +9 -0
- python_backpack-2.0.2/tests/test_files/edited_remove.txt +5 -0
- python_backpack-2.0.2/tests/test_files/locked_file.txt +1 -0
- python_backpack-2.0.2/tests/test_files/origin.txt +9 -0
- python_backpack-2.0.2/tests/test_files/origin_remove.txt +8 -0
- python_backpack-2.0.2/tests/test_files/unlocked_file.txt +0 -0
- python_backpack-2.0.2/tests/test_folder_utils.py +105 -0
- python_backpack-2.0.2/tests/test_json/data.json +13 -0
- python_backpack-2.0.2/tests/test_json/data_broken.json +13 -0
- python_backpack-2.0.2/tests/test_json/save/data_saved.json +0 -0
- python_backpack-2.0.2/tests/test_json_user_settings.py +90 -0
- python_backpack-2.0.2/tests/test_json_utils.py +46 -0
- python_backpack-2.0.2/tests/test_jsonmd.py +88 -0
- python_backpack-2.0.2/tests/test_misc.py +47 -0
- python_backpack-2.0.2/tests/test_strings.py +125 -0
- python_backpack-2.0.2/tests/test_test_utils.py +35 -0
- python_backpack-2.0.2/uv.lock +295 -0
- python_backpack-2.0.0/PKG-INFO +0 -12
- python_backpack-2.0.0/pyproject.toml +0 -22
- {python_backpack-2.0.0 → python_backpack-2.0.2}/LICENSE +0 -0
- {python_backpack-2.0.0/backpack → python_backpack-2.0.2}/__init__.py +0 -0
- {python_backpack-2.0.0 → python_backpack-2.0.2}/backpack/cache.py +0 -0
- {python_backpack-2.0.0 → python_backpack-2.0.2}/backpack/custom_errors.py +0 -0
- {python_backpack-2.0.0 → python_backpack-2.0.2}/backpack/folder_utils.py +0 -0
- {python_backpack-2.0.0 → python_backpack-2.0.2}/backpack/json_metadata.py +0 -0
- {python_backpack-2.0.0 → python_backpack-2.0.2}/backpack/json_user_settings.py +0 -0
- {python_backpack-2.0.0 → python_backpack-2.0.2}/backpack/logger.py +0 -0
- {python_backpack-2.0.0 → python_backpack-2.0.2}/backpack/patterns.py +0 -0
- {python_backpack-2.0.0 → python_backpack-2.0.2}/backpack/strings.py +0 -0
- {python_backpack-2.0.0 → python_backpack-2.0.2}/backpack/test_utils.py +0 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
name: Python Tests & Coverage
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
- push
|
|
5
|
+
- pull_request
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
build:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
strategy:
|
|
11
|
+
matrix:
|
|
12
|
+
python-version: ['3.11']
|
|
13
|
+
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v6
|
|
16
|
+
|
|
17
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
18
|
+
uses: actions/setup-python@v6
|
|
19
|
+
with:
|
|
20
|
+
python-version: ${{ matrix.python-version }}
|
|
21
|
+
|
|
22
|
+
- name: Add path to PYTHONPATH
|
|
23
|
+
run: |
|
|
24
|
+
echo "PYTHONPATH=$GITHUB_WORKSPACE" >> $GITHUB_ENV
|
|
25
|
+
|
|
26
|
+
- name: Install dependencies
|
|
27
|
+
run: |
|
|
28
|
+
python -m pip install --upgrade pip
|
|
29
|
+
pip install uv
|
|
30
|
+
uv sync --dev
|
|
31
|
+
|
|
32
|
+
- name: Run tests with coverage
|
|
33
|
+
run: |
|
|
34
|
+
uv run coverage run --source=backpack -m pytest
|
|
35
|
+
uv run coverage report -m --fail-under=70
|
|
36
|
+
uv run coverage xml
|
|
37
|
+
|
|
38
|
+
- name: Upload coverage.xml
|
|
39
|
+
if: ${{ matrix.python-version == '3.11' }}
|
|
40
|
+
uses: actions/upload-artifact@v7
|
|
41
|
+
with:
|
|
42
|
+
name: upload coverage
|
|
43
|
+
path: ${{ github.workspace }}/coverage.xml
|
|
44
|
+
if-no-files-found: warn
|
|
45
|
+
|
|
46
|
+
# upload coverage.xml to codecov
|
|
47
|
+
- name: Upload coverage.xml to codecov
|
|
48
|
+
if: ${{ matrix.python-version == '3.11' }}
|
|
49
|
+
uses: codecov/codecov-action@v6
|
|
50
|
+
with:
|
|
51
|
+
token: ${{ secrets.CODECOV_TOKEN }}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
pip-wheel-metadata/
|
|
24
|
+
share/python-wheels/
|
|
25
|
+
*.egg-info/
|
|
26
|
+
.installed.cfg
|
|
27
|
+
*.egg
|
|
28
|
+
MANIFEST
|
|
29
|
+
|
|
30
|
+
# PyInstaller
|
|
31
|
+
# Usually these files are written by a python script from a template
|
|
32
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
33
|
+
*.manifest
|
|
34
|
+
*.spec
|
|
35
|
+
|
|
36
|
+
# Installer logs
|
|
37
|
+
pip-log.txt
|
|
38
|
+
pip-delete-this-directory.txt
|
|
39
|
+
|
|
40
|
+
# Unit test / coverage reports
|
|
41
|
+
htmlcov/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
|
|
53
|
+
# Translations
|
|
54
|
+
*.mo
|
|
55
|
+
*.pot
|
|
56
|
+
|
|
57
|
+
# Django stuff:
|
|
58
|
+
*.log
|
|
59
|
+
local_settings.py
|
|
60
|
+
db.sqlite3
|
|
61
|
+
db.sqlite3-journal
|
|
62
|
+
|
|
63
|
+
# Flask stuff:
|
|
64
|
+
instance/
|
|
65
|
+
.webassets-cache
|
|
66
|
+
|
|
67
|
+
# Scrapy stuff:
|
|
68
|
+
.scrapy
|
|
69
|
+
|
|
70
|
+
# Sphinx documentation
|
|
71
|
+
docs/_build/
|
|
72
|
+
|
|
73
|
+
# PyBuilder
|
|
74
|
+
target/
|
|
75
|
+
|
|
76
|
+
# Jupyter Notebook
|
|
77
|
+
.ipynb_checkpoints
|
|
78
|
+
|
|
79
|
+
# IPython
|
|
80
|
+
profile_default/
|
|
81
|
+
ipython_config.py
|
|
82
|
+
|
|
83
|
+
# pyenv
|
|
84
|
+
.python-version
|
|
85
|
+
|
|
86
|
+
# pipenv
|
|
87
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
88
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
89
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
90
|
+
# install all needed dependencies.
|
|
91
|
+
#Pipfile.lock
|
|
92
|
+
|
|
93
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
|
94
|
+
__pypackages__/
|
|
95
|
+
|
|
96
|
+
# Celery stuff
|
|
97
|
+
celerybeat-schedule
|
|
98
|
+
celerybeat.pid
|
|
99
|
+
|
|
100
|
+
# SageMath parsed files
|
|
101
|
+
*.sage.py
|
|
102
|
+
|
|
103
|
+
# Environments
|
|
104
|
+
.env
|
|
105
|
+
.venv
|
|
106
|
+
env/
|
|
107
|
+
venv/
|
|
108
|
+
ENV/
|
|
109
|
+
env.bak/
|
|
110
|
+
venv.bak/
|
|
111
|
+
|
|
112
|
+
# Spyder project settings
|
|
113
|
+
.spyderproject
|
|
114
|
+
.spyproject
|
|
115
|
+
|
|
116
|
+
# Rope project settings
|
|
117
|
+
.ropeproject
|
|
118
|
+
|
|
119
|
+
# mkdocs documentation
|
|
120
|
+
/site
|
|
121
|
+
|
|
122
|
+
# mypy
|
|
123
|
+
.mypy_cache/
|
|
124
|
+
.dmypy.json
|
|
125
|
+
dmypy.json
|
|
126
|
+
|
|
127
|
+
# Pyre type checker
|
|
128
|
+
.pyre/
|
|
129
|
+
|
|
130
|
+
# test generated files
|
|
131
|
+
tests/test_json/MD_test.json
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: python-backpack
|
|
3
|
+
Version: 2.0.2
|
|
4
|
+
Summary: A collection of personal scripts for json, File/Folder Operations, String Validation, Custom Errors, Cache and stuff.
|
|
5
|
+
Author: Maximiliano Rocamora
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
|
|
10
|
+
[](https://pypi.python.org/pypi/python-backpack/)
|
|
11
|
+
[](https://badge.fury.io/py/python-backpack)
|
|
12
|
+
[](https://badge.fury.io/gh/MaxRocamora%2Fpython-backpack)
|
|
13
|
+
[](https://codecov.io/gh/MaxRocamora/python-backpack)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# Python-Backpack
|
|
17
|
+
|
|
18
|
+
Python-Backpack is a lightweight utility collection for common scripting tasks:
|
|
19
|
+
|
|
20
|
+
- JSON load/save helpers with validation
|
|
21
|
+
- user settings persistence under the OS user directory
|
|
22
|
+
- metadata export/import to JSON files
|
|
23
|
+
- file and folder operations
|
|
24
|
+
- string normalization and case conversion
|
|
25
|
+
- cache decorator with expiration support
|
|
26
|
+
- custom exceptions, logging helper, singleton pattern, and testing helpers
|
|
27
|
+
|
|
28
|
+
## Compatibility
|
|
29
|
+
|
|
30
|
+
- Python 3.11+
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pip install python-backpack
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Package API Reference
|
|
39
|
+
|
|
40
|
+
### Cache (`backpack.cache`)
|
|
41
|
+
|
|
42
|
+
- `timed_lru_cache(seconds: int, maxsize: int = 128)`
|
|
43
|
+
- `functools.lru_cache` decorator with expiration time.
|
|
44
|
+
- Wrapped calls support `force_clear=True` and `show_log=True`.
|
|
45
|
+
|
|
46
|
+
### Custom Errors (`backpack.custom_errors`)
|
|
47
|
+
|
|
48
|
+
- `EnvironmentVariableNotFoundError(var_name: str)`
|
|
49
|
+
- Raised when a required environment variable is missing.
|
|
50
|
+
- `ApplicationNotFoundError(app_name: str)`
|
|
51
|
+
- Raised when a required application is not found.
|
|
52
|
+
|
|
53
|
+
### File Utils (`backpack.file_utils`)
|
|
54
|
+
|
|
55
|
+
- `replace_strings_in_file(ascii_file: str, strings: list, new_string: str) -> None`
|
|
56
|
+
- Replaces multiple string occurrences in a text file.
|
|
57
|
+
- `remove_line_from_file(ascii_file: str, strings: list, verbose: bool = False) -> None`
|
|
58
|
+
- Removes exact matching lines from a text file.
|
|
59
|
+
- `file_is_writeable(filepath: str) -> bool`
|
|
60
|
+
- Checks whether a file can be opened for read/write.
|
|
61
|
+
- `get_version_from_filename(filename: str) -> str`
|
|
62
|
+
- Extracts a numeric version token from a filename.
|
|
63
|
+
|
|
64
|
+
### Folder Utils (`backpack.folder_utils`)
|
|
65
|
+
|
|
66
|
+
- `browse_folder(folder: str) -> bool`
|
|
67
|
+
- Opens a folder in Windows Explorer.
|
|
68
|
+
- `create_folders(folders: list, force_empty: bool = False, verbose: bool = False)`
|
|
69
|
+
- Creates multiple folders.
|
|
70
|
+
- `create_folder(path: str, force_empty: bool = False, verbose: bool = True)`
|
|
71
|
+
- Creates a folder and optionally clears it if it already exists.
|
|
72
|
+
- `remove_files_in_dir(path: str)`
|
|
73
|
+
- Removes all files and subdirectories inside a directory.
|
|
74
|
+
- `recursive_dir_copy(source_path: str, target_path: str)`
|
|
75
|
+
- Recursively copies files and subfolders from source to target.
|
|
76
|
+
|
|
77
|
+
### JSON Utils (`backpack.json_utils`)
|
|
78
|
+
|
|
79
|
+
- `json_load(json_file: str) -> dict`
|
|
80
|
+
- Loads JSON data from file with validation and error handling.
|
|
81
|
+
- `json_save(data: dict, json_file: str) -> bool`
|
|
82
|
+
- Saves a dictionary to JSON file.
|
|
83
|
+
|
|
84
|
+
### JSON Metadata (`backpack.json_metadata`)
|
|
85
|
+
|
|
86
|
+
- `JsonMetaFile(name: str, path: str)`
|
|
87
|
+
- Manages a metadata JSON file with package/system/time information.
|
|
88
|
+
- Main public methods:
|
|
89
|
+
- `has_file() -> bool`
|
|
90
|
+
- `load() -> None`
|
|
91
|
+
- `insert(key: str, value: Any) -> None`
|
|
92
|
+
- `remove(key: str) -> None`
|
|
93
|
+
- `save() -> None`
|
|
94
|
+
- `load_as_class() -> type`
|
|
95
|
+
- `insert_class(_class: type) -> None`
|
|
96
|
+
|
|
97
|
+
### JSON User Settings (`backpack.json_user_settings`)
|
|
98
|
+
|
|
99
|
+
- `JsonUserSettings(folder: str, name: str)`
|
|
100
|
+
- Saves and loads JSON settings in the current user's home directory.
|
|
101
|
+
- Main public methods:
|
|
102
|
+
- `save_settings(data: dict | None = None) -> bool | None`
|
|
103
|
+
- `load_settings() -> dict | bool`
|
|
104
|
+
|
|
105
|
+
### Logger (`backpack.logger`)
|
|
106
|
+
|
|
107
|
+
- `get_logger(name: str) -> logging.Logger`
|
|
108
|
+
- Returns a configured logger with stream handler.
|
|
109
|
+
|
|
110
|
+
### Patterns (`backpack.patterns`)
|
|
111
|
+
|
|
112
|
+
- `Singleton`
|
|
113
|
+
- Base class implementing singleton behavior via `__new__`.
|
|
114
|
+
|
|
115
|
+
### Strings (`backpack.strings`)
|
|
116
|
+
|
|
117
|
+
- `normalize_input_string(input_string: str, under_spaces: bool = True, under_hyphen: bool = True, replacer: str = '_') -> str`
|
|
118
|
+
- Keeps alphanumeric/space/hyphen characters and normalizes separators.
|
|
119
|
+
- `begin_or_end_with_numbers(input_string: str) -> bool`
|
|
120
|
+
- Checks whether first or last character is numeric.
|
|
121
|
+
- `begin_with_number(input_string: str) -> bool`
|
|
122
|
+
- Checks whether the first character is numeric.
|
|
123
|
+
- `has_numbers(input_string: str) -> bool`
|
|
124
|
+
- Checks whether any character is numeric.
|
|
125
|
+
- `camelcase_to_snakecase(input_string: str) -> str`
|
|
126
|
+
- Converts CamelCase to snake_case.
|
|
127
|
+
- Handles acronym prefixes, for example `HTTPServer -> http_server`.
|
|
128
|
+
|
|
129
|
+
### Test Utils (`backpack.test_utils`)
|
|
130
|
+
|
|
131
|
+
- `random_string(length: int = 10) -> str`
|
|
132
|
+
- Generates a random lowercase string.
|
|
133
|
+
- `time_function_decorator(method: type)`
|
|
134
|
+
- Decorator that logs execution time.
|
|
135
|
+
|
|
136
|
+
## Quick Example
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
from backpack.cache import timed_lru_cache
|
|
140
|
+
from backpack.strings import camelcase_to_snakecase, normalize_input_string
|
|
141
|
+
from backpack.json_utils import json_save, json_load
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
@timed_lru_cache(seconds=60)
|
|
145
|
+
def expensive_call():
|
|
146
|
+
return {'ok': True}
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
value = camelcase_to_snakecase('HTTPServer')
|
|
150
|
+
clean = normalize_input_string('Blade Runner-2049')
|
|
151
|
+
|
|
152
|
+
json_save({'value': value, 'clean': clean}, 'data.json')
|
|
153
|
+
data = json_load('data.json')
|
|
154
|
+
```
|
|
155
|
+
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
[](https://pypi.python.org/pypi/python-backpack/)
|
|
2
|
+
[](https://badge.fury.io/py/python-backpack)
|
|
3
|
+
[](https://badge.fury.io/gh/MaxRocamora%2Fpython-backpack)
|
|
4
|
+
[](https://codecov.io/gh/MaxRocamora/python-backpack)
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# Python-Backpack
|
|
8
|
+
|
|
9
|
+
Python-Backpack is a lightweight utility collection for common scripting tasks:
|
|
10
|
+
|
|
11
|
+
- JSON load/save helpers with validation
|
|
12
|
+
- user settings persistence under the OS user directory
|
|
13
|
+
- metadata export/import to JSON files
|
|
14
|
+
- file and folder operations
|
|
15
|
+
- string normalization and case conversion
|
|
16
|
+
- cache decorator with expiration support
|
|
17
|
+
- custom exceptions, logging helper, singleton pattern, and testing helpers
|
|
18
|
+
|
|
19
|
+
## Compatibility
|
|
20
|
+
|
|
21
|
+
- Python 3.11+
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install python-backpack
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Package API Reference
|
|
30
|
+
|
|
31
|
+
### Cache (`backpack.cache`)
|
|
32
|
+
|
|
33
|
+
- `timed_lru_cache(seconds: int, maxsize: int = 128)`
|
|
34
|
+
- `functools.lru_cache` decorator with expiration time.
|
|
35
|
+
- Wrapped calls support `force_clear=True` and `show_log=True`.
|
|
36
|
+
|
|
37
|
+
### Custom Errors (`backpack.custom_errors`)
|
|
38
|
+
|
|
39
|
+
- `EnvironmentVariableNotFoundError(var_name: str)`
|
|
40
|
+
- Raised when a required environment variable is missing.
|
|
41
|
+
- `ApplicationNotFoundError(app_name: str)`
|
|
42
|
+
- Raised when a required application is not found.
|
|
43
|
+
|
|
44
|
+
### File Utils (`backpack.file_utils`)
|
|
45
|
+
|
|
46
|
+
- `replace_strings_in_file(ascii_file: str, strings: list, new_string: str) -> None`
|
|
47
|
+
- Replaces multiple string occurrences in a text file.
|
|
48
|
+
- `remove_line_from_file(ascii_file: str, strings: list, verbose: bool = False) -> None`
|
|
49
|
+
- Removes exact matching lines from a text file.
|
|
50
|
+
- `file_is_writeable(filepath: str) -> bool`
|
|
51
|
+
- Checks whether a file can be opened for read/write.
|
|
52
|
+
- `get_version_from_filename(filename: str) -> str`
|
|
53
|
+
- Extracts a numeric version token from a filename.
|
|
54
|
+
|
|
55
|
+
### Folder Utils (`backpack.folder_utils`)
|
|
56
|
+
|
|
57
|
+
- `browse_folder(folder: str) -> bool`
|
|
58
|
+
- Opens a folder in Windows Explorer.
|
|
59
|
+
- `create_folders(folders: list, force_empty: bool = False, verbose: bool = False)`
|
|
60
|
+
- Creates multiple folders.
|
|
61
|
+
- `create_folder(path: str, force_empty: bool = False, verbose: bool = True)`
|
|
62
|
+
- Creates a folder and optionally clears it if it already exists.
|
|
63
|
+
- `remove_files_in_dir(path: str)`
|
|
64
|
+
- Removes all files and subdirectories inside a directory.
|
|
65
|
+
- `recursive_dir_copy(source_path: str, target_path: str)`
|
|
66
|
+
- Recursively copies files and subfolders from source to target.
|
|
67
|
+
|
|
68
|
+
### JSON Utils (`backpack.json_utils`)
|
|
69
|
+
|
|
70
|
+
- `json_load(json_file: str) -> dict`
|
|
71
|
+
- Loads JSON data from file with validation and error handling.
|
|
72
|
+
- `json_save(data: dict, json_file: str) -> bool`
|
|
73
|
+
- Saves a dictionary to JSON file.
|
|
74
|
+
|
|
75
|
+
### JSON Metadata (`backpack.json_metadata`)
|
|
76
|
+
|
|
77
|
+
- `JsonMetaFile(name: str, path: str)`
|
|
78
|
+
- Manages a metadata JSON file with package/system/time information.
|
|
79
|
+
- Main public methods:
|
|
80
|
+
- `has_file() -> bool`
|
|
81
|
+
- `load() -> None`
|
|
82
|
+
- `insert(key: str, value: Any) -> None`
|
|
83
|
+
- `remove(key: str) -> None`
|
|
84
|
+
- `save() -> None`
|
|
85
|
+
- `load_as_class() -> type`
|
|
86
|
+
- `insert_class(_class: type) -> None`
|
|
87
|
+
|
|
88
|
+
### JSON User Settings (`backpack.json_user_settings`)
|
|
89
|
+
|
|
90
|
+
- `JsonUserSettings(folder: str, name: str)`
|
|
91
|
+
- Saves and loads JSON settings in the current user's home directory.
|
|
92
|
+
- Main public methods:
|
|
93
|
+
- `save_settings(data: dict | None = None) -> bool | None`
|
|
94
|
+
- `load_settings() -> dict | bool`
|
|
95
|
+
|
|
96
|
+
### Logger (`backpack.logger`)
|
|
97
|
+
|
|
98
|
+
- `get_logger(name: str) -> logging.Logger`
|
|
99
|
+
- Returns a configured logger with stream handler.
|
|
100
|
+
|
|
101
|
+
### Patterns (`backpack.patterns`)
|
|
102
|
+
|
|
103
|
+
- `Singleton`
|
|
104
|
+
- Base class implementing singleton behavior via `__new__`.
|
|
105
|
+
|
|
106
|
+
### Strings (`backpack.strings`)
|
|
107
|
+
|
|
108
|
+
- `normalize_input_string(input_string: str, under_spaces: bool = True, under_hyphen: bool = True, replacer: str = '_') -> str`
|
|
109
|
+
- Keeps alphanumeric/space/hyphen characters and normalizes separators.
|
|
110
|
+
- `begin_or_end_with_numbers(input_string: str) -> bool`
|
|
111
|
+
- Checks whether first or last character is numeric.
|
|
112
|
+
- `begin_with_number(input_string: str) -> bool`
|
|
113
|
+
- Checks whether the first character is numeric.
|
|
114
|
+
- `has_numbers(input_string: str) -> bool`
|
|
115
|
+
- Checks whether any character is numeric.
|
|
116
|
+
- `camelcase_to_snakecase(input_string: str) -> str`
|
|
117
|
+
- Converts CamelCase to snake_case.
|
|
118
|
+
- Handles acronym prefixes, for example `HTTPServer -> http_server`.
|
|
119
|
+
|
|
120
|
+
### Test Utils (`backpack.test_utils`)
|
|
121
|
+
|
|
122
|
+
- `random_string(length: int = 10) -> str`
|
|
123
|
+
- Generates a random lowercase string.
|
|
124
|
+
- `time_function_decorator(method: type)`
|
|
125
|
+
- Decorator that logs execution time.
|
|
126
|
+
|
|
127
|
+
## Quick Example
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
from backpack.cache import timed_lru_cache
|
|
131
|
+
from backpack.strings import camelcase_to_snakecase, normalize_input_string
|
|
132
|
+
from backpack.json_utils import json_save, json_load
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
@timed_lru_cache(seconds=60)
|
|
136
|
+
def expensive_call():
|
|
137
|
+
return {'ok': True}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
value = camelcase_to_snakecase('HTTPServer')
|
|
141
|
+
clean = normalize_input_string('Blade Runner-2049')
|
|
142
|
+
|
|
143
|
+
json_save({'value': value, 'clean': clean}, 'data.json')
|
|
144
|
+
data = json_load('data.json')
|
|
145
|
+
```
|
|
146
|
+
|
|
File without changes
|
|
@@ -84,3 +84,44 @@ def file_is_writeable(filepath: str) -> bool:
|
|
|
84
84
|
log.info(x.strerror)
|
|
85
85
|
|
|
86
86
|
return False
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def get_version_from_filename(filename: str) -> str:
|
|
90
|
+
"""Extracts version from filename.
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
filename: (str) file name to extract version from
|
|
94
|
+
Returns:
|
|
95
|
+
str : version extracted from filename
|
|
96
|
+
|
|
97
|
+
Examples:
|
|
98
|
+
get_version_from_filename('myfile_23.txt') -> '23'
|
|
99
|
+
get_version_from_filename('myfile-9.txt') -> '9'
|
|
100
|
+
get_version_from_filename('myfile.130.txt') -> '130'
|
|
101
|
+
get_version_from_filename('myfile_v1002.txt') -> '1002'
|
|
102
|
+
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
filename_no_ext = filename.rsplit('.', 1)[0]
|
|
106
|
+
|
|
107
|
+
# guess is the separator is an underscore, dash, dot or v, and the version is the last part before the extension
|
|
108
|
+
separators = ['_', '-', '.', 'v']
|
|
109
|
+
if not any(sep in filename_no_ext for sep in separators):
|
|
110
|
+
log.info(f'No separator found in filename: {filename_no_ext}. Using fallback extraction.')
|
|
111
|
+
else:
|
|
112
|
+
for sep in separators:
|
|
113
|
+
if sep in filename_no_ext:
|
|
114
|
+
parts = filename_no_ext.split(sep)
|
|
115
|
+
version_part = parts[-1].split('.')[0] # Get the last part before the extension
|
|
116
|
+
if version_part.replace('.', '').isdigit(): # Check if it's a valid version number
|
|
117
|
+
log.info(
|
|
118
|
+
f'Extracted version: {version_part} from filename: {filename} using separator: {sep}'
|
|
119
|
+
)
|
|
120
|
+
return version_part
|
|
121
|
+
|
|
122
|
+
# Fallback: extract from the entire filename
|
|
123
|
+
version = filename_no_ext.lstrip('v')
|
|
124
|
+
version = version.replace('_', '.').replace('-', '.')
|
|
125
|
+
version = version if version.replace('.', '').isdigit() else '0'
|
|
126
|
+
log.info(f'Extracted version: {version} from filename: {filename}')
|
|
127
|
+
return version
|
|
@@ -40,7 +40,7 @@ def json_save(data: dict, json_file: str) -> bool:
|
|
|
40
40
|
"""
|
|
41
41
|
|
|
42
42
|
if not os.path.exists(os.path.dirname(json_file)):
|
|
43
|
-
os.makedirs(os.path.dirname(json_file))
|
|
43
|
+
os.makedirs(os.path.dirname(json_file), exist_ok=True)
|
|
44
44
|
|
|
45
45
|
try:
|
|
46
46
|
with open(json_file, 'w') as f:
|
|
@@ -9,12 +9,13 @@
|
|
|
9
9
|
# 1.1.2 07/2025 - Update actions versions, remove unused badge from README
|
|
10
10
|
# 1.1.3 07/2025 - Improve test class names and docstrings for clarity, ruff formatting
|
|
11
11
|
# 1.1.4 07/2025 - Moved from pipenv to poetry for dependency management.toml
|
|
12
|
-
# 2.0.
|
|
12
|
+
# 2.0.1 05/2026 - Remove tox workflow and setup.py, standardize uv + coverage tooling
|
|
13
|
+
# 2.0.2 05/2026 - Add PyPI long description metadata and release polish
|
|
13
14
|
# ----------------------------------------------------------------------------------------
|
|
14
15
|
|
|
15
16
|
VERSION_MAJOR = 2
|
|
16
17
|
VERSION_MINOR = 0
|
|
17
|
-
VERSION_PATCH =
|
|
18
|
+
VERSION_PATCH = 2
|
|
18
19
|
|
|
19
20
|
version = f'{VERSION_MAJOR}.{VERSION_MINOR}.{VERSION_PATCH}'
|
|
20
21
|
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"folders": [
|
|
3
|
+
{
|
|
4
|
+
"path": "."
|
|
5
|
+
}
|
|
6
|
+
],
|
|
7
|
+
"settings": {
|
|
8
|
+
"python.analysis.completeFunctionParens": false,
|
|
9
|
+
"python.analysis.autoImportCompletions": false,
|
|
10
|
+
"editor.formatOnSave": true,
|
|
11
|
+
"editor.formatOnPaste": false,
|
|
12
|
+
"editor.codeActionsOnSave": {
|
|
13
|
+
"source.organizeImports": "never"
|
|
14
|
+
},
|
|
15
|
+
"editor.rulers": [
|
|
16
|
+
90
|
|
17
|
+
],
|
|
18
|
+
"editor.defaultFormatter": "charliermarsh.ruff",
|
|
19
|
+
"editor.wordWrap": "wordWrapColumn",
|
|
20
|
+
"editor.wordWrapColumn": 120,
|
|
21
|
+
"workbench.colorTheme": "Monokai Pro (Filter Spectrum)",
|
|
22
|
+
"workbench.colorCustomizations": {
|
|
23
|
+
"titleBar.activeBackground": "#f36edf",
|
|
24
|
+
"titleBar.activeForeground": "#0c0b0b"
|
|
25
|
+
},
|
|
26
|
+
"python.analysis.extraPaths": [],
|
|
27
|
+
"python.testing.unittestArgs": [
|
|
28
|
+
"-v",
|
|
29
|
+
"-s",
|
|
30
|
+
".",
|
|
31
|
+
"-p",
|
|
32
|
+
"*test*.py"
|
|
33
|
+
],
|
|
34
|
+
"python.testing.pytestEnabled": false,
|
|
35
|
+
"python.testing.unittestEnabled": true,
|
|
36
|
+
"terminal.integrated.fontSize": 12,
|
|
37
|
+
"cSpell.words": [
|
|
38
|
+
"autopep",
|
|
39
|
+
"camelcase",
|
|
40
|
+
"charliermarsh",
|
|
41
|
+
"codecov",
|
|
42
|
+
"docstrings",
|
|
43
|
+
"ftime",
|
|
44
|
+
"jsonfile",
|
|
45
|
+
"levelname",
|
|
46
|
+
"Maximiliano",
|
|
47
|
+
"Monokai",
|
|
48
|
+
"numpy",
|
|
49
|
+
"Parens",
|
|
50
|
+
"pipenv",
|
|
51
|
+
"pycodestyle",
|
|
52
|
+
"pydocstyle",
|
|
53
|
+
"pylint",
|
|
54
|
+
"pypi",
|
|
55
|
+
"pytest",
|
|
56
|
+
"PYTHONPATH",
|
|
57
|
+
"Rocamora",
|
|
58
|
+
"runn",
|
|
59
|
+
"snakecase",
|
|
60
|
+
"strerror"
|
|
61
|
+
],
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
@echo off
|
|
2
|
+
REM Run tests with coverage and generate reports.
|
|
3
|
+
uv run coverage run --source=backpack -m pytest
|
|
4
|
+
if errorlevel 1 exit /b %errorlevel%
|
|
5
|
+
uv run coverage report -m --fail-under=70
|
|
6
|
+
if errorlevel 1 exit /b %errorlevel%
|
|
7
|
+
uv run coverage html
|
|
8
|
+
if errorlevel 1 exit /b %errorlevel%
|
|
9
|
+
uv run coverage xml
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "python-backpack"
|
|
3
|
+
version = "2.0.2"
|
|
4
|
+
description = "A collection of personal scripts for json, File/Folder Operations, String Validation, Custom Errors, Cache and stuff."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Maximiliano Rocamora" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.11"
|
|
10
|
+
dependencies = []
|
|
11
|
+
|
|
12
|
+
[dependency-groups]
|
|
13
|
+
dev = [
|
|
14
|
+
"coverage>=7.14.0",
|
|
15
|
+
"ruff>=0.15.12",
|
|
16
|
+
"mock>=5.2.0",
|
|
17
|
+
"pytest>=9.0.3",
|
|
18
|
+
"pytest-cov>=7.1.0",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[build-system]
|
|
22
|
+
requires = ["hatchling>=1.27.0"]
|
|
23
|
+
build-backend = "hatchling.build"
|
|
24
|
+
|
|
25
|
+
[tool.hatch.build.targets.wheel]
|
|
26
|
+
packages = ["backpack"]
|
|
27
|
+
|
|
28
|
+
[tool.pytest.ini_options]
|
|
29
|
+
addopts = "-p no:cacheprovider"
|
|
30
|
+
testpaths = ["tests"]
|