Added query@v2
This commit is contained in:
@@ -14,3 +14,4 @@ UuidId = str | UUID
|
||||
SerialId = int
|
||||
|
||||
Id = UuidId | SerialId
|
||||
TNumber = int | float | complex
|
||||
|
||||
57
src/cpl-core/cpl/core/utils/benchmark.py
Normal file
57
src/cpl-core/cpl/core/utils/benchmark.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import time
|
||||
import tracemalloc
|
||||
from typing import List, Callable
|
||||
|
||||
from cpl.core.console import Console
|
||||
|
||||
|
||||
class Benchmark:
|
||||
|
||||
@staticmethod
|
||||
def all(label: str, func: Callable, iterations: int = 5):
|
||||
times: List[float] = []
|
||||
mems: List[float] = []
|
||||
|
||||
for _ in range(iterations):
|
||||
start = time.perf_counter()
|
||||
func()
|
||||
end = time.perf_counter()
|
||||
times.append(end - start)
|
||||
|
||||
for _ in range(iterations):
|
||||
tracemalloc.start()
|
||||
func()
|
||||
current, peak = tracemalloc.get_traced_memory()
|
||||
tracemalloc.stop()
|
||||
mems.append(peak)
|
||||
|
||||
avg_time = sum(times) / len(times)
|
||||
avg_mem = sum(mems) / len(mems) / (1024 * 1024)
|
||||
Console.write_line(f"{label:20s} -> min {min(times):.6f}s avg {avg_time:.6f}s mem {avg_mem:.8f} MB")
|
||||
|
||||
@staticmethod
|
||||
def time(label: str, func: Callable, iterations: int = 5):
|
||||
times: List[float] = []
|
||||
|
||||
for _ in range(iterations):
|
||||
start = time.perf_counter()
|
||||
func()
|
||||
end = time.perf_counter()
|
||||
times.append(end - start)
|
||||
|
||||
avg_time = sum(times) / len(times)
|
||||
Console.write_line(f"{label:20s} -> min {min(times):.6f}s avg {avg_time:.6f}s")
|
||||
|
||||
@staticmethod
|
||||
def memory(label: str, func: Callable, iterations: int = 5):
|
||||
mems: List[float] = []
|
||||
|
||||
for _ in range(iterations):
|
||||
tracemalloc.start()
|
||||
func()
|
||||
current, peak = tracemalloc.get_traced_memory()
|
||||
tracemalloc.stop()
|
||||
mems.append(peak)
|
||||
|
||||
avg_mem = sum(mems) / len(mems) / (1024 * 1024)
|
||||
Console.write_line(f"{label:20s} -> mem {avg_mem:.2f} MB")
|
||||
48
src/cpl-core/cpl/core/utils/number.py
Normal file
48
src/cpl-core/cpl/core/utils/number.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from typing import Any
|
||||
|
||||
|
||||
class Number:
|
||||
|
||||
@staticmethod
|
||||
def is_number(value: Any) -> bool:
|
||||
"""Check if the value is a number (int or float)."""
|
||||
return isinstance(value, (int, float, complex))
|
||||
|
||||
@staticmethod
|
||||
def to_number(value: Any) -> int | float | complex:
|
||||
"""
|
||||
Convert a given value into int, float, or complex.
|
||||
Raises ValueError if conversion is not possible.
|
||||
"""
|
||||
|
||||
if isinstance(value, (int, float, complex)):
|
||||
return value
|
||||
|
||||
if isinstance(value, str):
|
||||
value = value.strip()
|
||||
for caster in (int, float, complex):
|
||||
try:
|
||||
return caster(value)
|
||||
except ValueError:
|
||||
continue
|
||||
raise ValueError(f"Cannot convert string '{value}' to number.")
|
||||
|
||||
if isinstance(value, bool):
|
||||
return int(value)
|
||||
|
||||
try:
|
||||
return int(value)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
return float(value)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
return complex(value)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
raise ValueError(f"Cannot convert type {type(value)} to number.")
|
||||
Reference in New Issue
Block a user