azrvus-dev-toolkit 0.1.0__py3-none-any.whl
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.
- azrvus_dev_toolkit-0.1.0.dist-info/METADATA +113 -0
- azrvus_dev_toolkit-0.1.0.dist-info/RECORD +45 -0
- azrvus_dev_toolkit-0.1.0.dist-info/WHEEL +4 -0
- azrvus_dev_toolkit-0.1.0.dist-info/entry_points.txt +2 -0
- azrvus_dev_toolkit-0.1.0.dist-info/licenses/LICENSE +21 -0
- dev_toolkit/__init__.py +163 -0
- dev_toolkit/_version.py +3 -0
- dev_toolkit/async_io.py +19 -0
- dev_toolkit/banner.py +37 -0
- dev_toolkit/cache.py +88 -0
- dev_toolkit/cli.py +60 -0
- dev_toolkit/cli_formatters.py +79 -0
- dev_toolkit/cli_spinner.py +87 -0
- dev_toolkit/color.py +34 -0
- dev_toolkit/concurrency.py +58 -0
- dev_toolkit/config.py +75 -0
- dev_toolkit/crypto_utils.py +21 -0
- dev_toolkit/decorators.py +47 -0
- dev_toolkit/dict_utils.py +77 -0
- dev_toolkit/encoding.py +19 -0
- dev_toolkit/env.py +23 -0
- dev_toolkit/env_validator.py +24 -0
- dev_toolkit/events.py +44 -0
- dev_toolkit/formatters.py +45 -0
- dev_toolkit/hash.py +37 -0
- dev_toolkit/http.py +22 -0
- dev_toolkit/io.py +40 -0
- dev_toolkit/jsonl.py +62 -0
- dev_toolkit/logger.py +36 -0
- dev_toolkit/masking.py +51 -0
- dev_toolkit/pagination.py +29 -0
- dev_toolkit/path_utils.py +84 -0
- dev_toolkit/process.py +65 -0
- dev_toolkit/random_utils.py +19 -0
- dev_toolkit/rate_limiter.py +77 -0
- dev_toolkit/retry.py +37 -0
- dev_toolkit/retry_utils.py +93 -0
- dev_toolkit/system.py +24 -0
- dev_toolkit/templating.py +28 -0
- dev_toolkit/text.py +45 -0
- dev_toolkit/timer.py +41 -0
- dev_toolkit/transforms.py +21 -0
- dev_toolkit/url_utils.py +35 -0
- dev_toolkit/validators.py +74 -0
- dev_toolkit/watcher.py +66 -0
dev_toolkit/timer.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""High-precision execution benchmarking utilities."""
|
|
2
|
+
|
|
3
|
+
import functools
|
|
4
|
+
import time
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from typing import Any, Self
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Timer:
|
|
10
|
+
"""Context manager for measuring execution time in seconds."""
|
|
11
|
+
|
|
12
|
+
def __init__(self) -> None:
|
|
13
|
+
self.elapsed: float = 0.0
|
|
14
|
+
self._start_time: float | None = None
|
|
15
|
+
|
|
16
|
+
def __enter__(self) -> Self:
|
|
17
|
+
self._start_time = time.perf_counter()
|
|
18
|
+
return self
|
|
19
|
+
|
|
20
|
+
def __exit__(self, *args: object) -> None:
|
|
21
|
+
if self._start_time is not None:
|
|
22
|
+
self.elapsed = time.perf_counter() - self._start_time
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def timed(callback: Callable[[str, float], None] | None = None) -> Callable[..., Any]:
|
|
26
|
+
"""Decorator to measure and optionally report function execution duration."""
|
|
27
|
+
|
|
28
|
+
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
|
|
29
|
+
@functools.wraps(func)
|
|
30
|
+
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
31
|
+
start = time.perf_counter()
|
|
32
|
+
try:
|
|
33
|
+
return func(*args, **kwargs)
|
|
34
|
+
finally:
|
|
35
|
+
elapsed = time.perf_counter() - start
|
|
36
|
+
if callback:
|
|
37
|
+
callback(func.__name__, elapsed)
|
|
38
|
+
|
|
39
|
+
return wrapper
|
|
40
|
+
|
|
41
|
+
return decorator
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
|
|
5
|
+
__all__ = ["capitalize_words", "normalize_spaces", "to_snake_case"]
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def normalize_spaces(text: str) -> str:
|
|
9
|
+
"""Remove extra whitespace from text and normalize inner spacing."""
|
|
10
|
+
return " ".join(text.split())
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def capitalize_words(text: str) -> str:
|
|
14
|
+
"""Capitalize the first letter of each word in a string."""
|
|
15
|
+
return " ".join(word.capitalize() for word in text.split())
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def to_snake_case(text: str) -> str:
|
|
19
|
+
"""Convert a camelCase or PascalCase string into snake_case."""
|
|
20
|
+
s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", text)
|
|
21
|
+
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()
|
dev_toolkit/url_utils.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse
|
|
4
|
+
|
|
5
|
+
__all__ = ["add_query_params", "parse_query_params", "remove_query_params"]
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def parse_query_params(url: str) -> dict[str, str]:
|
|
9
|
+
"""Extract query parameters from a URL string into a dictionary."""
|
|
10
|
+
parsed = urlparse(url)
|
|
11
|
+
return dict(parse_qsl(parsed.query))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def add_query_params(url: str, params: dict[str, str]) -> str:
|
|
15
|
+
"""Add or update query parameters on a given URL."""
|
|
16
|
+
parsed = urlparse(url)
|
|
17
|
+
query_params = dict(parse_qsl(parsed.query))
|
|
18
|
+
query_params.update(params)
|
|
19
|
+
|
|
20
|
+
new_query = urlencode(query_params)
|
|
21
|
+
new_parts = parsed._replace(query=new_query)
|
|
22
|
+
return urlunparse(new_parts)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def remove_query_params(url: str, keys_to_remove: list[str]) -> str:
|
|
26
|
+
"""Remove specific query parameters from a URL string."""
|
|
27
|
+
parsed = urlparse(url)
|
|
28
|
+
query_params = dict(parse_qsl(parsed.query))
|
|
29
|
+
|
|
30
|
+
for key in keys_to_remove:
|
|
31
|
+
query_params.pop(key, None)
|
|
32
|
+
|
|
33
|
+
new_query = urlencode(query_params)
|
|
34
|
+
new_parts = parsed._replace(query=new_query)
|
|
35
|
+
return urlunparse(new_parts)
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""Data validation utilities for standard formats and bounds."""
|
|
2
|
+
|
|
3
|
+
import ipaddress
|
|
4
|
+
import re
|
|
5
|
+
from urllib.parse import urlparse
|
|
6
|
+
|
|
7
|
+
_EMAIL_REGEX = re.compile(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def is_email(value: str) -> bool:
|
|
11
|
+
"""Validate whether a string matches a standard email format."""
|
|
12
|
+
if not isinstance(value, str):
|
|
13
|
+
return False
|
|
14
|
+
return bool(_EMAIL_REGEX.match(value.strip()))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def is_url(value: str, require_protocol: bool = True) -> bool:
|
|
18
|
+
"""Validate whether a string is a valid HTTP/HTTPS URL."""
|
|
19
|
+
if not isinstance(value, str):
|
|
20
|
+
return False
|
|
21
|
+
|
|
22
|
+
target = value.strip()
|
|
23
|
+
if not require_protocol and not target.startswith(("http://", "https://")):
|
|
24
|
+
target = f"http://{target}"
|
|
25
|
+
|
|
26
|
+
try:
|
|
27
|
+
parsed = urlparse(target)
|
|
28
|
+
return parsed.scheme in ("http", "https") and bool(parsed.netloc)
|
|
29
|
+
except (ValueError, AttributeError):
|
|
30
|
+
return False
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def is_ipv4(value: str) -> bool:
|
|
34
|
+
"""Check if a string is a valid IPv4 address."""
|
|
35
|
+
if not isinstance(value, str):
|
|
36
|
+
return False
|
|
37
|
+
try:
|
|
38
|
+
return ipaddress.ip_address(value.strip()).version == 4
|
|
39
|
+
except ValueError:
|
|
40
|
+
return False
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def is_ipv6(value: str) -> bool:
|
|
44
|
+
"""Check if a string is a valid IPv6 address."""
|
|
45
|
+
if not isinstance(value, str):
|
|
46
|
+
return False
|
|
47
|
+
try:
|
|
48
|
+
return ipaddress.ip_address(value.strip()).version == 6
|
|
49
|
+
except ValueError:
|
|
50
|
+
return False
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def in_range(
|
|
54
|
+
value: float,
|
|
55
|
+
min_val: float | None = None,
|
|
56
|
+
max_val: float | None = None,
|
|
57
|
+
inclusive: bool = True,
|
|
58
|
+
) -> bool:
|
|
59
|
+
"""Verify if a numeric value falls within lower and upper bounds."""
|
|
60
|
+
if not isinstance(value, (int, float)):
|
|
61
|
+
return False
|
|
62
|
+
|
|
63
|
+
if inclusive:
|
|
64
|
+
if min_val is not None and value < min_val:
|
|
65
|
+
return False
|
|
66
|
+
if max_val is not None and value > max_val:
|
|
67
|
+
return False
|
|
68
|
+
else:
|
|
69
|
+
if min_val is not None and value <= min_val:
|
|
70
|
+
return False
|
|
71
|
+
if max_val is not None and value >= max_val:
|
|
72
|
+
return False
|
|
73
|
+
|
|
74
|
+
return True
|
dev_toolkit/watcher.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
__all__ = ["watch_path"]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def watch_path(
|
|
11
|
+
path: str | Path,
|
|
12
|
+
callback: Callable[[str, Path], None],
|
|
13
|
+
interval: float = 1.0,
|
|
14
|
+
extensions: list[str] | None = None,
|
|
15
|
+
max_iterations: int | None = None,
|
|
16
|
+
) -> None:
|
|
17
|
+
"""Watch a file or directory for modifications, creations, or deletions.
|
|
18
|
+
|
|
19
|
+
Polls the path at the specified interval and invokes the callback function
|
|
20
|
+
with an event type ('created', 'modified', 'deleted') and the path.
|
|
21
|
+
"""
|
|
22
|
+
target = Path(path)
|
|
23
|
+
if not target.exists():
|
|
24
|
+
raise FileNotFoundError(f"Target path does not exist: {target}")
|
|
25
|
+
|
|
26
|
+
known_files: dict[Path, float] = {}
|
|
27
|
+
|
|
28
|
+
def _snapshot(p: Path) -> dict[Path, float]:
|
|
29
|
+
snapshot: dict[Path, float] = {}
|
|
30
|
+
if p.is_file():
|
|
31
|
+
if extensions is None or p.suffix in extensions:
|
|
32
|
+
snapshot[p] = p.stat().st_mtime
|
|
33
|
+
elif p.is_dir():
|
|
34
|
+
for child in p.rglob("*"):
|
|
35
|
+
if child.is_file() and (
|
|
36
|
+
extensions is None or child.suffix in extensions
|
|
37
|
+
):
|
|
38
|
+
snapshot[child] = child.stat().st_mtime
|
|
39
|
+
return snapshot
|
|
40
|
+
|
|
41
|
+
known_files = _snapshot(target)
|
|
42
|
+
|
|
43
|
+
iterations = 0
|
|
44
|
+
try:
|
|
45
|
+
while max_iterations is None or iterations < max_iterations:
|
|
46
|
+
current_files = _snapshot(target) if target.exists() else {}
|
|
47
|
+
|
|
48
|
+
for fpath, mtime in current_files.items():
|
|
49
|
+
if fpath not in known_files:
|
|
50
|
+
callback("created", fpath)
|
|
51
|
+
elif mtime != known_files[fpath]:
|
|
52
|
+
callback("modified", fpath)
|
|
53
|
+
|
|
54
|
+
for fpath in known_files:
|
|
55
|
+
if fpath not in current_files:
|
|
56
|
+
callback("deleted", fpath)
|
|
57
|
+
|
|
58
|
+
known_files = current_files
|
|
59
|
+
iterations += 1
|
|
60
|
+
|
|
61
|
+
if max_iterations is not None and iterations >= max_iterations:
|
|
62
|
+
break
|
|
63
|
+
|
|
64
|
+
time.sleep(interval)
|
|
65
|
+
except KeyboardInterrupt:
|
|
66
|
+
pass
|