A collection of small Python utilities for developers.
Project description
nano_dev_utils
A collection of small Python utilities for developers.
Modules
timers.py
This module provides a Timer class for measuring the execution time of code blocks and functions with additional features like timeout control and multi-iteration averaging.
Timer Class
-
__init__(self, precision: int = 4, verbose: bool = False): Initializes aTimerinstance.precision: The number of decimal places to record and display time durations. Defaults to 4.verbose: Optionally displays the function's positional arguments (args) and keyword arguments (kwargs). Defaults toFalse.
-
timeit( self, iterations: int = 1, timeout: float | None = None, per_iteration: bool = False ) -> Callable[[Callable[P, R]], Callable[P, R | None]]:
Decorator that times function execution with advanced features:iterations: Number of times to run the function (for averaging). Defaults to 1.timeout: Maximum allowed execution time in seconds. When exceeded:- Raises
TimeoutErrorimmediately - Warning: The function execution will be aborted mid-operation
- No return value will be available if timeout occurs
- Raises
per_iteration: If True, applies timeout check to each iteration; otherwise checks total time across all iterations.- Features:
- Records execution times
- Handles timeout conditions
- Calculates average execution time across iterations
- Prints the function name and execution time (with optional arguments)
- Returns the result of the original function (unless timeout occurs)
Example Usage:
import time
from nano_dev_utils.timers import Timer
timer = Timer(precision=6, verbose=True)
# Basic timing
@timer.timeit()
def my_function(a, b=10):
"""A sample function."""
time.sleep(0.1)
return a + b
# Advanced usage with timeout and iterations
@timer.timeit(iterations=5, timeout=0.5, per_iteration=True)
def critical_function(x):
"""Function with timeout check per iteration."""
time.sleep(0.08)
return x * 2
result1 = my_function(5, b=20) # Shows args/kwargs and timing
result2 = critical_function(10) # Runs 5 times with per-iteration timeout
dynamic_importer.py
This module provides an Importer class for lazy loading and caching module imports.
Importer Class
-
__init__(self): Initializes anImporterinstance with an empty dictionaryimported_modulesto cache imported modules. -
import_mod_from_lib(self, library: str, module_name: str) -> ModuleType | Any: Lazily imports a module from a specified library and caches it.library(str): The name of the library (e.g., "os", "requests").module_name(str): The name of the module to import within the library (e.g., "path", "get").- Returns the imported module. If the module has already been imported, it returns a cached instance.
- Raises
ImportErrorif the module cannot be found.
Example Usage:
from nano_dev_utils.dynamic_importer import Importer
importer = Importer()
os_path = importer.import_mod_from_lib("os", "path")
print(f"Imported os.path: {os_path}")
requests_get = importer.import_mod_from_lib("requests", "get")
print(f"Imported requests.get: {requests_get}")
# Subsequent calls will return the cached module
os_path_again = importer.import_mod_from_lib("os", "path")
print(f"Imported os.path again (cached): {os_path_again}")
release_ports.py
This module provides a PortsRelease class to identify and release processes
listening on specified TCP ports.
It supports Windows, Linux, and macOS.
PortsRelease Class
-
__init__(self, default_ports: list[int] | None = None): -
Initializes a
PortsReleaseinstance.default_ports: A list of default ports to manage. If not provided, it defaults to[6277, 6274].
-
get_pid_by_port(self, port: int) -> int | None: A static method that attempts to find
a process ID (PID) listening on a givenport. -
It uses platform-specific commands (
netstat,ss,lsof). -
Returns the PID if found, otherwise
None. -
kill_process(self, pid: int) -> bool: A static method that attempts to kill the process with the givenpid. -
It uses platform-specific commands (
taskkill,kill -9). -
Returns
Trueif the process was successfully killed,Falseotherwise. -
release_all(self, ports: list[int] | None = None) -> None: Releases all processes listening on the specifiedports.ports: A list of ports to release.- If
None, it uses thedefault_portsdefined during initialization. - For each port, it first tries to get the PID and then attempts to kill the process.
- It logs the actions and any errors encountered. Invalid port numbers in the provided list are skipped.
Example Usage:
import logging
from nano_dev_utils import PortsRelease
# For configuration of logging level and format (supported already):
logging.basicConfig(filename='port release.log',
level=logging.INFO, # DEBUG, WARNING, ERROR, CRITICAL
format='%(asctime)s - %(levelname)s: %(message)s',
datefmt='%d-%m-%Y %H:%M:%S')
# Create an instance with default ports
port_releaser = PortsRelease()
port_releaser.release_all()
# Create an instance with custom ports
custom_ports_releaser = PortsRelease(default_ports=[8080, 9000, 6274])
custom_ports_releaser.release_all(ports=[8080, 9000])
# Release only the default ports
port_releaser.release_all()
License
This project is licensed under the MIT License. See LICENSE for 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 nano_dev_utils-1.0.0.tar.gz.
File metadata
- Download URL: nano_dev_utils-1.0.0.tar.gz
- Upload date:
- Size: 21.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63851d5c83c9a84005f91cfea1987c717214144a396aeadc9170745862605139
|
|
| MD5 |
6ff12f9505e25b745f5549e12ae3d829
|
|
| BLAKE2b-256 |
ceafaa6214e0c737655f88049421a2b4e891f92aa79f90aa85e8e8fb4377b45d
|
File details
Details for the file nano_dev_utils-1.0.0-py3-none-any.whl.
File metadata
- Download URL: nano_dev_utils-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8db9e62cc0b993cc6269a1f0a32c79a2821327adf95ac8fcc83f49a10523870
|
|
| MD5 |
c706a686769299460cf6468d20f168aa
|
|
| BLAKE2b-256 |
20e58da62acfdc8d04d4d851ad8b82b67df13fab590312064267db8d4675d525
|