CandidateExplorer / utils /decorator.py
ishaq101's picture
clean init
478dec6
raw
history blame
1.87 kB
import asyncio
import inspect
import functools
import time
import warnings
from functools import wraps
from typing import Callable, Any
def trace_runtime(func: Callable) -> Callable:
@wraps(func)
async def async_wrapper(*args, **kwargs) -> Any:
# This wrapper runs if the original func was async
start_time = time.perf_counter()
# Await the coroutine returned by func(*args, **kwargs)
result = await func(*args, **kwargs)
end_time = time.perf_counter()
duration = end_time - start_time
print(f"⏱️ ASYNC Function '{func.__name__}' took {duration:.6f} seconds")
return result
@wraps(func)
def sync_wrapper(*args, **kwargs) -> Any:
# This wrapper runs if the original func was sync
start_time = time.perf_counter()
result = func(*args, **kwargs)
end_time = time.perf_counter()
duration = end_time - start_time
print(f"⏱️ SYNC Function '{func.__name__}' took {duration:.6f} seconds")
return result
# Check if the function being decorated is an async function
if inspect.iscoroutinefunction(func):
# If it's async, return the async wrapper
return async_wrapper
else:
# If it's sync, return the sync wrapper
return sync_wrapper
def deprecated(reason):
"""
This is a decorator which can be used to mark functions
and classes as deprecated.
"""
def decorator(func_or_class):
@functools.wraps(func_or_class)
def new_func(*args, **kwargs):
warnings.warn(f"Call to deprecated {func_or_class.__name__}."
f" Reason: {reason}",
category=FutureWarning,
stacklevel=2)
return func_or_class(*args, **kwargs)
return new_func
return decorator