pyessential 0.1.8__tar.gz → 0.2.0__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.
- {pyessential-0.1.8 → pyessential-0.2.0}/CHANGELOG.md +23 -2
- pyessential-0.2.0/PKG-INFO +316 -0
- pyessential-0.2.0/README.md +291 -0
- pyessential-0.2.0/pyessential/_version.py +1 -0
- pyessential-0.2.0/pyessential/json_utils.py +810 -0
- pyessential-0.2.0/pyessential/validators.py +272 -0
- pyessential-0.2.0/pyessential.egg-info/PKG-INFO +316 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential.egg-info/SOURCES.txt +4 -0
- pyessential-0.2.0/tests/test_json_utils.py +522 -0
- pyessential-0.2.0/tests/test_validators.py +231 -0
- pyessential-0.1.8/PKG-INFO +0 -180
- pyessential-0.1.8/README.md +0 -155
- pyessential-0.1.8/pyessential/_version.py +0 -1
- pyessential-0.1.8/pyessential.egg-info/PKG-INFO +0 -180
- {pyessential-0.1.8 → pyessential-0.2.0}/LICENSE +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/MANIFEST.in +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential/__init__.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential/__main__.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential/cli.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential/decorators/__init__.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential/decorators/deprecated.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential/decorators/retry.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential/decorators/timer.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential/folders.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential/generators.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential/logger.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential/py.typed +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential.egg-info/dependency_links.txt +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential.egg-info/entry_points.txt +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyessential.egg-info/top_level.txt +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/pyproject.toml +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/setup.cfg +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/setup.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/tests/decorators/test_deprecated.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/tests/decorators/test_retry.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/tests/decorators/test_timer.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/tests/test_cli.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/tests/test_folders.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/tests/test_generators.py +0 -0
- {pyessential-0.1.8 → pyessential-0.2.0}/tests/test_logger.py +0 -0
|
@@ -4,7 +4,26 @@ All notable changes to this project are documented here.
|
|
|
4
4
|
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
5
5
|
and this project uses [Semantic Versioning](https://semver.org/).
|
|
6
6
|
|
|
7
|
-
## [Unreleased]
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.2.0] - 2026-11-24
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
- `pyessential.validators`: helpers for validating common data formats with zero external
|
|
13
|
+
dependencies (`is_email`, `is_url`, `is_uuid`, `is_ip`, `is_port`, `is_hex`,
|
|
14
|
+
`is_domain`, `is_mac_address`, `is_json`, and `is_slug`).
|
|
15
|
+
- Unit tests in `tests/test_validators.py` and documentation in `docs/validators.md`.
|
|
16
|
+
|
|
17
|
+
## [0.1.9] - 2026-11-14
|
|
18
|
+
|
|
19
|
+
### Added
|
|
20
|
+
- `pyessential.json_utils`: helpers for reading and writing JSON and JSON Lines files
|
|
21
|
+
(atomic writes), converting dates/UUIDs/sets, and working with nested data
|
|
22
|
+
(`get_by_path`, `set_by_path`, `flatten_json`, `deep_merge`, `diff_json`, `remove_keys`,
|
|
23
|
+
`keys_to_snake_case`, and more).
|
|
24
|
+
|
|
25
|
+
### Fixed
|
|
26
|
+
- pins CI to ubuntu-24.04 so Python 3.9 is available.
|
|
8
27
|
|
|
9
28
|
## [0.1.8] - 2026-11-03
|
|
10
29
|
### Added
|
|
@@ -54,7 +73,9 @@ and this project uses [Semantic Versioning](https://semver.org/).
|
|
|
54
73
|
- Minimum supported Python version is now 3.9.
|
|
55
74
|
- Releases are now published from `v*` git tags.
|
|
56
75
|
|
|
57
|
-
[Unreleased]: https://github.com/Abhi39054/PyEssential/compare/v0.
|
|
76
|
+
[Unreleased]: https://github.com/Abhi39054/PyEssential/compare/v0.2.0...HEAD
|
|
77
|
+
[0.2.0]: https://github.com/Abhi39054/PyEssential/releases/tag/v0.2.0
|
|
78
|
+
[0.1.9]: https://github.com/Abhi39054/PyEssential/releases/tag/v0.1.9
|
|
58
79
|
[0.1.8]: https://github.com/Abhi39054/PyEssential/releases/tag/v0.1.8
|
|
59
80
|
[0.1.7]: https://github.com/Abhi39054/PyEssential/releases/tag/v0.1.7
|
|
60
81
|
[0.1.6]: https://github.com/Abhi39054/PyEssential/releases/tag/v0.1.6
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyessential
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: A growing collection of Python utilities and decorators.
|
|
5
|
+
Home-page: https://github.com/Abhi39054/PyEssential
|
|
6
|
+
Author: Abhishek Kumar
|
|
7
|
+
Author-email: abhishekkumar39054@gmail.com
|
|
8
|
+
Maintainer: Abhishek Kumar
|
|
9
|
+
License: MIT
|
|
10
|
+
Project-URL: Changelog, https://github.com/Abhi39054/PyEssential/blob/main/CHANGELOG.md
|
|
11
|
+
Project-URL: Source, https://github.com/Abhi39054/PyEssential
|
|
12
|
+
Project-URL: Issues, https://github.com/Abhi39054/PyEssential/issues
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
19
|
+
Classifier: Operating System :: OS Independent
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# PyEssential
|
|
27
|
+
|
|
28
|
+
**A growing collection of Python utilities and decorators that simplify common development tasks.**
|
|
29
|
+
|
|
30
|
+
[](https://github.com/Abhi39054/PyEssential/actions/workflows/pypi-publish.yaml)
|
|
31
|
+
[](https://pypi.org/project/pyessential/)
|
|
32
|
+
[](https://pypi.org/project/pyessential/)
|
|
33
|
+
[](https://github.com/Abhi39054/PyEssential/blob/main/LICENSE)
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Table of Contents
|
|
38
|
+
|
|
39
|
+
- [Introduction](#introduction)
|
|
40
|
+
- [Features](#features)
|
|
41
|
+
- [Installation](#installation)
|
|
42
|
+
- [Quick Start](#quick-start)
|
|
43
|
+
- [Usage](#usage)
|
|
44
|
+
- [Decorators](#decorators)
|
|
45
|
+
- [Random generators](#random-generators)
|
|
46
|
+
- [Logger](#logger)
|
|
47
|
+
- [Command line](#command-line)
|
|
48
|
+
- [Documentation](#documentation)
|
|
49
|
+
- [Development](#development)
|
|
50
|
+
- [Prerequisites](#prerequisites)
|
|
51
|
+
- [Clone and set up](#clone-and-set-up)
|
|
52
|
+
- [Run the tests](#run-the-tests)
|
|
53
|
+
- [Releasing](#releasing)
|
|
54
|
+
- [Roadmap](#roadmap)
|
|
55
|
+
- [Changelog](#changelog)
|
|
56
|
+
- [Contributing](#contributing)
|
|
57
|
+
- [License](#license)
|
|
58
|
+
- [Author](#author)
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## Introduction
|
|
63
|
+
|
|
64
|
+
PyEssential is a small library of reusable tools that developers often end up writing from
|
|
65
|
+
scratch: timing and retrying functions, marking old code as deprecated, generating secure random
|
|
66
|
+
values, and setting up rotating log files.
|
|
67
|
+
|
|
68
|
+
The goals are simple:
|
|
69
|
+
|
|
70
|
+
- **Easy to adopt.** Import a function or class and start using it, with no setup code.
|
|
71
|
+
- **No dependencies.** It uses only the Python standard library.
|
|
72
|
+
- **Well typed.** It ships a `py.typed` marker, so editors and type checkers can use its type hints.
|
|
73
|
+
- **Built to grow.** New utility groups (for example networking, file I/O or validation) can be
|
|
74
|
+
added without changing what already exists.
|
|
75
|
+
|
|
76
|
+
## Features
|
|
77
|
+
|
|
78
|
+
| Area | What you get |
|
|
79
|
+
|---|---|
|
|
80
|
+
| **Decorators** | `@timeit` for benchmarking, `@retry` with exponential backoff, and `@deprecated` for marking old code. `@timeit` and `@retry` also work with `async` functions. |
|
|
81
|
+
| **Generators** | Cryptographically secure random integers and secret keys. |
|
|
82
|
+
| **Logging** | A `Logger` with rotating log files for the main log, errors and input events, plus optional console output. |
|
|
83
|
+
| **Command line** | Check your installed version with `pyessential --version`. |
|
|
84
|
+
| **Typing** | Ships a `py.typed` marker. |
|
|
85
|
+
| **JSON utilities** | Safe JSON and JSON Lines file I/O, nested-data helpers, merging and diffing. |
|
|
86
|
+
|
|
87
|
+
## Installation
|
|
88
|
+
|
|
89
|
+
**Requirements:** Python 3.9 or newer.
|
|
90
|
+
|
|
91
|
+
Install from [PyPI](https://pypi.org/project/pyessential/):
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
pip install pyessential
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Upgrade to the latest release:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
pip install --upgrade pyessential
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Check that it is installed:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
pyessential --version
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Quick Start
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from pyessential import Logger, generate_secret_key
|
|
113
|
+
from pyessential.decorators import retry, timeit
|
|
114
|
+
|
|
115
|
+
@timeit
|
|
116
|
+
@retry(tries=3, delay=0.5, exceptions=ConnectionError)
|
|
117
|
+
def fetch_data():
|
|
118
|
+
...
|
|
119
|
+
|
|
120
|
+
with Logger(log_name="my_app", enable_console=True) as log:
|
|
121
|
+
log.info("Starting up")
|
|
122
|
+
log.info("Generated a %d-character key", len(generate_secret_key()))
|
|
123
|
+
fetch_data()
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Usage
|
|
127
|
+
|
|
128
|
+
### Decorators
|
|
129
|
+
|
|
130
|
+
#### `@timeit`
|
|
131
|
+
|
|
132
|
+
Prints how long a function took, even if it raises. Works with `async def` functions too.
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
from pyessential.decorators import timeit
|
|
136
|
+
|
|
137
|
+
@timeit
|
|
138
|
+
def slow_function():
|
|
139
|
+
sum(range(1_000_000))
|
|
140
|
+
|
|
141
|
+
slow_function()
|
|
142
|
+
# [slow_function] executed in 0.0123 seconds.
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### `@retry`
|
|
146
|
+
|
|
147
|
+
Retries a function when it raises, waiting longer between each attempt. After the last attempt,
|
|
148
|
+
the original exception is re-raised.
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
from pyessential.decorators import retry
|
|
152
|
+
|
|
153
|
+
@retry(tries=4, delay=0.5, backoff=2, exceptions=(ConnectionError, TimeoutError))
|
|
154
|
+
def fetch_data():
|
|
155
|
+
...
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
This waits 0.5 s, 1 s, then 2 s between attempts. Only the listed exceptions trigger a retry;
|
|
159
|
+
anything else is raised immediately.
|
|
160
|
+
|
|
161
|
+
| Option | Meaning |
|
|
162
|
+
|--------|---------|
|
|
163
|
+
| `tries` | Total attempts, including the first (default `3`). |
|
|
164
|
+
| `delay` | Seconds to wait after the first failure (default `1.0`). |
|
|
165
|
+
| `backoff` | Multiplier applied to the wait after each failure (default `2.0`; use `1` for a constant delay). |
|
|
166
|
+
| `max_delay` | Upper limit for any single wait. |
|
|
167
|
+
| `jitter` | Up to this many extra random seconds added to each wait. |
|
|
168
|
+
| `exceptions` | Exception class or tuple of classes that trigger a retry (default `Exception`). |
|
|
169
|
+
| `on_retry` | Callback `on_retry(exc, attempt, wait)`, for example for logging. |
|
|
170
|
+
|
|
171
|
+
#### `@deprecated`
|
|
172
|
+
|
|
173
|
+
Emits a `DeprecationWarning` when the function or class is used.
|
|
174
|
+
|
|
175
|
+
```python
|
|
176
|
+
from pyessential.decorators import deprecated
|
|
177
|
+
|
|
178
|
+
@deprecated("use new_func instead", version="0.2.0")
|
|
179
|
+
def old_func():
|
|
180
|
+
...
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
> **Note:** Python hides `DeprecationWarning` by default outside of `__main__` and test runs.
|
|
184
|
+
> Pass `category=FutureWarning` if you want end users to always see it.
|
|
185
|
+
|
|
186
|
+
### Random generators
|
|
187
|
+
|
|
188
|
+
```python
|
|
189
|
+
from pyessential import generate_random_int, generate_secret_key
|
|
190
|
+
|
|
191
|
+
generate_random_int(1, 10) # e.g. 7 (both ends are included)
|
|
192
|
+
generate_secret_key(16) # 32 hex characters (the length is in bytes)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Both functions use Python's `secrets` module, so the values are suitable for tokens and keys.
|
|
196
|
+
|
|
197
|
+
### Logger
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
from pyessential import Logger
|
|
201
|
+
|
|
202
|
+
with Logger(log_name="my_app", log_dir="logs", enable_console=True) as log:
|
|
203
|
+
log.info("Application started")
|
|
204
|
+
log.error("Something went wrong")
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
The logger writes rotating files to `log_dir`: a main log, an error log, and an input log.
|
|
208
|
+
|
|
209
|
+
### Command line
|
|
210
|
+
|
|
211
|
+
```bash
|
|
212
|
+
pyessential --version
|
|
213
|
+
python -m pyessential --version # also works
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## Documentation
|
|
217
|
+
|
|
218
|
+
Detailed guides with parameters and examples:
|
|
219
|
+
|
|
220
|
+
- [Decorators](https://github.com/Abhi39054/PyEssential/blob/main/.docs/decorators.md)
|
|
221
|
+
- [Generators](https://github.com/Abhi39054/PyEssential/blob/main/.docs/generators.md)
|
|
222
|
+
- [Logger](https://github.com/Abhi39054/PyEssential/blob/main/.docs/logger.md)
|
|
223
|
+
- [JSON utilities](https://github.com/Abhi39054/PyEssential/blob/main/.docs/json_utils.md)
|
|
224
|
+
- [Validator Utility](https://github.com/Abhi39054/PyEssential/blob/main/.docs/validators.md)
|
|
225
|
+
|
|
226
|
+
## Development
|
|
227
|
+
|
|
228
|
+
### Prerequisites
|
|
229
|
+
|
|
230
|
+
- Python 3.9 or newer
|
|
231
|
+
- [Git](https://git-scm.com/downloads)
|
|
232
|
+
|
|
233
|
+
### Clone and set up
|
|
234
|
+
|
|
235
|
+
```bash
|
|
236
|
+
git clone https://github.com/Abhi39054/PyEssential.git
|
|
237
|
+
cd PyEssential
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
Create and activate a virtual environment:
|
|
241
|
+
|
|
242
|
+
```bash
|
|
243
|
+
# macOS / Linux
|
|
244
|
+
python -m venv .venv
|
|
245
|
+
source .venv/bin/activate
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
```powershell
|
|
249
|
+
# Windows (PowerShell)
|
|
250
|
+
python -m venv .venv
|
|
251
|
+
.venv\Scripts\Activate.ps1
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
Install the package in editable mode, so your changes are picked up without reinstalling, along
|
|
255
|
+
with the test tools:
|
|
256
|
+
|
|
257
|
+
```bash
|
|
258
|
+
pip install -e .
|
|
259
|
+
pip install pytest
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### Run the tests
|
|
263
|
+
|
|
264
|
+
```bash
|
|
265
|
+
pytest
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Releasing
|
|
269
|
+
|
|
270
|
+
1. Move the entries under `[Unreleased]` in `CHANGELOG.md` to a new version heading with
|
|
271
|
+
today's date.
|
|
272
|
+
2. Bump the version in `pyessential/_version.py`, commit, and push to `main`:
|
|
273
|
+
|
|
274
|
+
```bash
|
|
275
|
+
git commit -am "chore: bump version to X.Y.Z"
|
|
276
|
+
git push origin main
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
3. Tag the release and push the tag:
|
|
280
|
+
|
|
281
|
+
```bash
|
|
282
|
+
git tag -a vX.Y.Z -m "Release X.Y.Z"
|
|
283
|
+
git push origin vX.Y.Z
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
4. CI runs the tests, builds the package, publishes to TestPyPI, waits for approval, publishes
|
|
287
|
+
to PyPI, and creates the GitHub Release.
|
|
288
|
+
|
|
289
|
+
The tag must match the version in `_version.py` (`vX.Y.Z` corresponds to `X.Y.Z`), or the
|
|
290
|
+
build fails.
|
|
291
|
+
|
|
292
|
+
## Roadmap
|
|
293
|
+
|
|
294
|
+
PyEssential is structured so that new utility groups can be added easily. Ideas under
|
|
295
|
+
consideration include networking, file I/O and validation helpers. Suggestions are welcome:
|
|
296
|
+
open an [issue](https://github.com/Abhi39054/PyEssential/issues).
|
|
297
|
+
|
|
298
|
+
## Changelog
|
|
299
|
+
|
|
300
|
+
See [CHANGELOG.md](https://github.com/Abhi39054/PyEssential/blob/main/CHANGELOG.md) for what
|
|
301
|
+
changed in each release.
|
|
302
|
+
|
|
303
|
+
## Contributing
|
|
304
|
+
|
|
305
|
+
Contributions are welcome. Please read
|
|
306
|
+
[CONTRIBUTING.md](https://github.com/Abhi39054/PyEssential/blob/main/CONTRIBUTING.md) before
|
|
307
|
+
opening a pull request. To report a bug or request a feature, open an
|
|
308
|
+
[issue](https://github.com/Abhi39054/PyEssential/issues).
|
|
309
|
+
|
|
310
|
+
## License
|
|
311
|
+
|
|
312
|
+
Released under the [MIT License](https://github.com/Abhi39054/PyEssential/blob/main/LICENSE).
|
|
313
|
+
|
|
314
|
+
## Author
|
|
315
|
+
|
|
316
|
+
Created and maintained by **Abhishek Kumar** ([@Abhi39054](https://github.com/Abhi39054)).
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
# PyEssential
|
|
2
|
+
|
|
3
|
+
**A growing collection of Python utilities and decorators that simplify common development tasks.**
|
|
4
|
+
|
|
5
|
+
[](https://github.com/Abhi39054/PyEssential/actions/workflows/pypi-publish.yaml)
|
|
6
|
+
[](https://pypi.org/project/pyessential/)
|
|
7
|
+
[](https://pypi.org/project/pyessential/)
|
|
8
|
+
[](https://github.com/Abhi39054/PyEssential/blob/main/LICENSE)
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Table of Contents
|
|
13
|
+
|
|
14
|
+
- [Introduction](#introduction)
|
|
15
|
+
- [Features](#features)
|
|
16
|
+
- [Installation](#installation)
|
|
17
|
+
- [Quick Start](#quick-start)
|
|
18
|
+
- [Usage](#usage)
|
|
19
|
+
- [Decorators](#decorators)
|
|
20
|
+
- [Random generators](#random-generators)
|
|
21
|
+
- [Logger](#logger)
|
|
22
|
+
- [Command line](#command-line)
|
|
23
|
+
- [Documentation](#documentation)
|
|
24
|
+
- [Development](#development)
|
|
25
|
+
- [Prerequisites](#prerequisites)
|
|
26
|
+
- [Clone and set up](#clone-and-set-up)
|
|
27
|
+
- [Run the tests](#run-the-tests)
|
|
28
|
+
- [Releasing](#releasing)
|
|
29
|
+
- [Roadmap](#roadmap)
|
|
30
|
+
- [Changelog](#changelog)
|
|
31
|
+
- [Contributing](#contributing)
|
|
32
|
+
- [License](#license)
|
|
33
|
+
- [Author](#author)
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Introduction
|
|
38
|
+
|
|
39
|
+
PyEssential is a small library of reusable tools that developers often end up writing from
|
|
40
|
+
scratch: timing and retrying functions, marking old code as deprecated, generating secure random
|
|
41
|
+
values, and setting up rotating log files.
|
|
42
|
+
|
|
43
|
+
The goals are simple:
|
|
44
|
+
|
|
45
|
+
- **Easy to adopt.** Import a function or class and start using it, with no setup code.
|
|
46
|
+
- **No dependencies.** It uses only the Python standard library.
|
|
47
|
+
- **Well typed.** It ships a `py.typed` marker, so editors and type checkers can use its type hints.
|
|
48
|
+
- **Built to grow.** New utility groups (for example networking, file I/O or validation) can be
|
|
49
|
+
added without changing what already exists.
|
|
50
|
+
|
|
51
|
+
## Features
|
|
52
|
+
|
|
53
|
+
| Area | What you get |
|
|
54
|
+
|---|---|
|
|
55
|
+
| **Decorators** | `@timeit` for benchmarking, `@retry` with exponential backoff, and `@deprecated` for marking old code. `@timeit` and `@retry` also work with `async` functions. |
|
|
56
|
+
| **Generators** | Cryptographically secure random integers and secret keys. |
|
|
57
|
+
| **Logging** | A `Logger` with rotating log files for the main log, errors and input events, plus optional console output. |
|
|
58
|
+
| **Command line** | Check your installed version with `pyessential --version`. |
|
|
59
|
+
| **Typing** | Ships a `py.typed` marker. |
|
|
60
|
+
| **JSON utilities** | Safe JSON and JSON Lines file I/O, nested-data helpers, merging and diffing. |
|
|
61
|
+
|
|
62
|
+
## Installation
|
|
63
|
+
|
|
64
|
+
**Requirements:** Python 3.9 or newer.
|
|
65
|
+
|
|
66
|
+
Install from [PyPI](https://pypi.org/project/pyessential/):
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
pip install pyessential
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Upgrade to the latest release:
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
pip install --upgrade pyessential
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Check that it is installed:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
pyessential --version
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Quick Start
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from pyessential import Logger, generate_secret_key
|
|
88
|
+
from pyessential.decorators import retry, timeit
|
|
89
|
+
|
|
90
|
+
@timeit
|
|
91
|
+
@retry(tries=3, delay=0.5, exceptions=ConnectionError)
|
|
92
|
+
def fetch_data():
|
|
93
|
+
...
|
|
94
|
+
|
|
95
|
+
with Logger(log_name="my_app", enable_console=True) as log:
|
|
96
|
+
log.info("Starting up")
|
|
97
|
+
log.info("Generated a %d-character key", len(generate_secret_key()))
|
|
98
|
+
fetch_data()
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Usage
|
|
102
|
+
|
|
103
|
+
### Decorators
|
|
104
|
+
|
|
105
|
+
#### `@timeit`
|
|
106
|
+
|
|
107
|
+
Prints how long a function took, even if it raises. Works with `async def` functions too.
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from pyessential.decorators import timeit
|
|
111
|
+
|
|
112
|
+
@timeit
|
|
113
|
+
def slow_function():
|
|
114
|
+
sum(range(1_000_000))
|
|
115
|
+
|
|
116
|
+
slow_function()
|
|
117
|
+
# [slow_function] executed in 0.0123 seconds.
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
#### `@retry`
|
|
121
|
+
|
|
122
|
+
Retries a function when it raises, waiting longer between each attempt. After the last attempt,
|
|
123
|
+
the original exception is re-raised.
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
from pyessential.decorators import retry
|
|
127
|
+
|
|
128
|
+
@retry(tries=4, delay=0.5, backoff=2, exceptions=(ConnectionError, TimeoutError))
|
|
129
|
+
def fetch_data():
|
|
130
|
+
...
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
This waits 0.5 s, 1 s, then 2 s between attempts. Only the listed exceptions trigger a retry;
|
|
134
|
+
anything else is raised immediately.
|
|
135
|
+
|
|
136
|
+
| Option | Meaning |
|
|
137
|
+
|--------|---------|
|
|
138
|
+
| `tries` | Total attempts, including the first (default `3`). |
|
|
139
|
+
| `delay` | Seconds to wait after the first failure (default `1.0`). |
|
|
140
|
+
| `backoff` | Multiplier applied to the wait after each failure (default `2.0`; use `1` for a constant delay). |
|
|
141
|
+
| `max_delay` | Upper limit for any single wait. |
|
|
142
|
+
| `jitter` | Up to this many extra random seconds added to each wait. |
|
|
143
|
+
| `exceptions` | Exception class or tuple of classes that trigger a retry (default `Exception`). |
|
|
144
|
+
| `on_retry` | Callback `on_retry(exc, attempt, wait)`, for example for logging. |
|
|
145
|
+
|
|
146
|
+
#### `@deprecated`
|
|
147
|
+
|
|
148
|
+
Emits a `DeprecationWarning` when the function or class is used.
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
from pyessential.decorators import deprecated
|
|
152
|
+
|
|
153
|
+
@deprecated("use new_func instead", version="0.2.0")
|
|
154
|
+
def old_func():
|
|
155
|
+
...
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
> **Note:** Python hides `DeprecationWarning` by default outside of `__main__` and test runs.
|
|
159
|
+
> Pass `category=FutureWarning` if you want end users to always see it.
|
|
160
|
+
|
|
161
|
+
### Random generators
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
from pyessential import generate_random_int, generate_secret_key
|
|
165
|
+
|
|
166
|
+
generate_random_int(1, 10) # e.g. 7 (both ends are included)
|
|
167
|
+
generate_secret_key(16) # 32 hex characters (the length is in bytes)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
Both functions use Python's `secrets` module, so the values are suitable for tokens and keys.
|
|
171
|
+
|
|
172
|
+
### Logger
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
from pyessential import Logger
|
|
176
|
+
|
|
177
|
+
with Logger(log_name="my_app", log_dir="logs", enable_console=True) as log:
|
|
178
|
+
log.info("Application started")
|
|
179
|
+
log.error("Something went wrong")
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
The logger writes rotating files to `log_dir`: a main log, an error log, and an input log.
|
|
183
|
+
|
|
184
|
+
### Command line
|
|
185
|
+
|
|
186
|
+
```bash
|
|
187
|
+
pyessential --version
|
|
188
|
+
python -m pyessential --version # also works
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Documentation
|
|
192
|
+
|
|
193
|
+
Detailed guides with parameters and examples:
|
|
194
|
+
|
|
195
|
+
- [Decorators](https://github.com/Abhi39054/PyEssential/blob/main/.docs/decorators.md)
|
|
196
|
+
- [Generators](https://github.com/Abhi39054/PyEssential/blob/main/.docs/generators.md)
|
|
197
|
+
- [Logger](https://github.com/Abhi39054/PyEssential/blob/main/.docs/logger.md)
|
|
198
|
+
- [JSON utilities](https://github.com/Abhi39054/PyEssential/blob/main/.docs/json_utils.md)
|
|
199
|
+
- [Validator Utility](https://github.com/Abhi39054/PyEssential/blob/main/.docs/validators.md)
|
|
200
|
+
|
|
201
|
+
## Development
|
|
202
|
+
|
|
203
|
+
### Prerequisites
|
|
204
|
+
|
|
205
|
+
- Python 3.9 or newer
|
|
206
|
+
- [Git](https://git-scm.com/downloads)
|
|
207
|
+
|
|
208
|
+
### Clone and set up
|
|
209
|
+
|
|
210
|
+
```bash
|
|
211
|
+
git clone https://github.com/Abhi39054/PyEssential.git
|
|
212
|
+
cd PyEssential
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Create and activate a virtual environment:
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
# macOS / Linux
|
|
219
|
+
python -m venv .venv
|
|
220
|
+
source .venv/bin/activate
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
```powershell
|
|
224
|
+
# Windows (PowerShell)
|
|
225
|
+
python -m venv .venv
|
|
226
|
+
.venv\Scripts\Activate.ps1
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
Install the package in editable mode, so your changes are picked up without reinstalling, along
|
|
230
|
+
with the test tools:
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
pip install -e .
|
|
234
|
+
pip install pytest
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Run the tests
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
pytest
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Releasing
|
|
244
|
+
|
|
245
|
+
1. Move the entries under `[Unreleased]` in `CHANGELOG.md` to a new version heading with
|
|
246
|
+
today's date.
|
|
247
|
+
2. Bump the version in `pyessential/_version.py`, commit, and push to `main`:
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
git commit -am "chore: bump version to X.Y.Z"
|
|
251
|
+
git push origin main
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
3. Tag the release and push the tag:
|
|
255
|
+
|
|
256
|
+
```bash
|
|
257
|
+
git tag -a vX.Y.Z -m "Release X.Y.Z"
|
|
258
|
+
git push origin vX.Y.Z
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
4. CI runs the tests, builds the package, publishes to TestPyPI, waits for approval, publishes
|
|
262
|
+
to PyPI, and creates the GitHub Release.
|
|
263
|
+
|
|
264
|
+
The tag must match the version in `_version.py` (`vX.Y.Z` corresponds to `X.Y.Z`), or the
|
|
265
|
+
build fails.
|
|
266
|
+
|
|
267
|
+
## Roadmap
|
|
268
|
+
|
|
269
|
+
PyEssential is structured so that new utility groups can be added easily. Ideas under
|
|
270
|
+
consideration include networking, file I/O and validation helpers. Suggestions are welcome:
|
|
271
|
+
open an [issue](https://github.com/Abhi39054/PyEssential/issues).
|
|
272
|
+
|
|
273
|
+
## Changelog
|
|
274
|
+
|
|
275
|
+
See [CHANGELOG.md](https://github.com/Abhi39054/PyEssential/blob/main/CHANGELOG.md) for what
|
|
276
|
+
changed in each release.
|
|
277
|
+
|
|
278
|
+
## Contributing
|
|
279
|
+
|
|
280
|
+
Contributions are welcome. Please read
|
|
281
|
+
[CONTRIBUTING.md](https://github.com/Abhi39054/PyEssential/blob/main/CONTRIBUTING.md) before
|
|
282
|
+
opening a pull request. To report a bug or request a feature, open an
|
|
283
|
+
[issue](https://github.com/Abhi39054/PyEssential/issues).
|
|
284
|
+
|
|
285
|
+
## License
|
|
286
|
+
|
|
287
|
+
Released under the [MIT License](https://github.com/Abhi39054/PyEssential/blob/main/LICENSE).
|
|
288
|
+
|
|
289
|
+
## Author
|
|
290
|
+
|
|
291
|
+
Created and maintained by **Abhishek Kumar** ([@Abhi39054](https://github.com/Abhi39054)).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.2.0"
|