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.
Timer Class
-
__init__(self, precision=4, verbose=False): Initializes aTimerinstance.precision(int, optional): The number of decimal places to record and- display time durations. Defaults to 4.
verbose(bool, optional): IfTrue, the function's arguments and keyword- arguments will be included in the printed timing output. Defaults to
False.
-
timeit(self, func): A decorator that measures the execution time of the decorated function.- When the decorated function is called, this decorator records the start and end times,
- calculates the total execution time, prints the function name and execution time
- (optionally including arguments if
verboseisTrue), and returns the result of the original function.
Example Usage:
import time
from src.nano_dev_utils.timers import Timer
timer = Timer(precision=6, verbose=True)
@timer.timeit
def my_function(a, b=10):
"""A sample function."""
time.sleep(0.1)
return a + b
result = my_function(5, b=20)
print(f"Result: {result}")
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 the cached instance.
- Raises
ImportErrorif the module cannot be found.
Example Usage:
from src.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: Optional[list[int]] = None): -
Initializes a
PortsReleaseinstance.default_ports(list[int], optional): A list of default ports to manage.- If not provided, it defaults to
[6277, 6274].
-
get_pid_by_port(port: int) -> Optional[int]: A static method that attempts to -
find the process ID (PID) listening on the given
port. It uses platform-specific -
commands (
netstat,ss,lsof). Returns the PID if found, otherwiseNone. -
kill_process(pid: int) -> bool: A static method that attempts to kill the process -
with the given
pid. It uses platform-specific commands (taskkill,kill -9). -
Returns
Trueif the process was successfully killed,Falseotherwise. -
release_all(self, ports: Optional[list[int]] = None) -> None: Releases all processes -
listening on the specified
ports.ports(list[int], optional): A list of ports to release. IfNone, 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:
from src.nano_dev_utils.release_ports import PortsRelease
# 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-0.3.4.tar.gz.
File metadata
- Download URL: nano_dev_utils-0.3.4.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
292945d0248b2640f9273919cd887b461b9972606fd2c372fdb2322c7ef9654e
|
|
| MD5 |
074cd6d1ea1980e1ee951adc291a7082
|
|
| BLAKE2b-256 |
cb034c69bcde2e2c7de31242639f6a4bcf7df62452b867cd04390ea1e2243595
|
File details
Details for the file nano_dev_utils-0.3.4-py3-none-any.whl.
File metadata
- Download URL: nano_dev_utils-0.3.4-py3-none-any.whl
- Upload date:
- Size: 6.8 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 |
ba28a1f321052df3f8ed29a4f4b27b633da7229c4e23e745bc972749504a9f4a
|
|
| MD5 |
5b2b4cdf2a85387e1fd461cb649ad4aa
|
|
| BLAKE2b-256 |
059259d20b31deeba5f7553f4dbccc77d3491d2038dfc3c651fd30930e40f939
|