Useful Python decorators that I use in development.
Project description
nix-python-utils
Description: A collection of versatile classes, methods and decorators that I frequently use across my work projects.
Content
##Install
pip install nix-python-utils
Classes
For use classes you need to import them from the package:
import nix.classes as classes
| Method | Parameters | Returns | Description |
|---|---|---|---|
IO.read_file |
- file_path (str): The path to the file to read. |
str |
Reads a file from the specified path and returns its content as a string. |
IO.write_file |
- file_path (str): The path to the file to write to. |
None |
Writes the given content to the specified file. |
- content (str): The content to write to the file. |
|||
IO.create_folder_if_not_exists |
- folder_path (str): The path to the folder to create. |
None |
Creates a folder at the specified path if it does not already exist. |
Decorators
For use decorators you need to import them from the package:
import nix.utils.decorators as decorators
| Decorator | Arguments | Description |
|---|---|---|
| @retry | - max_retries (int, default=3) - delay (int, default=2) - exceptions (Exception or tuple of Exceptions, default=Exception) - backoff_factor (int, default=1) - logger (Logger, optional) |
A decorator that retries a function upon failure. You can customize the maximum number of retries, the delay between retries, the type of exceptions to catch, and the backoff factor to increase the delay with each retry. If a logger is provided, exceptions are logged. |
| @thread_pool | - num_threads (int, default=4) - exceptions (Exception or tuple of Exceptions, default=Exception) - raise_first_exception (bool, default=False) - logger (Logger, optional) |
A decorator that runs a function in a thread pool with a specified number of threads. It collects results from all tasks and returns them as a list. It also handles exceptions, with an option to re-raise the first exception or just log it. |
| @handle_exception | - handler (Callable[[Exception], Any], optional) - exceptions (Exception or tuple of Exceptions, default=Exception) - default_value (Any, optional) - raise_exception (bool, default=False) - logger (Logger, optional) |
A decorator for handling exceptions in a function. You can specify a custom exception handler, the type of exceptions to catch, a default return value if exceptions occur, and whether to re-raise exceptions. Logging is optional and can be controlled with a logger. It also supports additional arguments and keyword arguments for the exception handler. |
Licence
This project is licensed under the MIT License. See the LICENSE file for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nix_python_utils-0.1.0.tar.gz.
File metadata
- Download URL: nix_python_utils-0.1.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1e16723f3f701ad689c554e33d05f1e12b81b5ac4710460b7fe6fb89783ccca
|
|
| MD5 |
0205dcc16a5c332bfb2c98496d96dddc
|
|
| BLAKE2b-256 |
5bfef08dfaadc665f6c33573489b929a13be08522f3138729577d3174f1f58f0
|
File details
Details for the file nix_python_utils-0.1.0-py3-none-any.whl.
File metadata
- Download URL: nix_python_utils-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d0d60758a84543e478f396128831d92686415015f8a67042641f4e536f67a002
|
|
| MD5 |
d2ae0dcdebed22c707ed0039f914bf6c
|
|
| BLAKE2b-256 |
a2305f59cfb429e85a460fc272124bc0fe86c9af5ba0eda365935eb3c1f7941b
|