32 lines
704 B
Python
32 lines
704 B
Python
import os
|
|
import time
|
|
from functools import wraps
|
|
from loguru import logger
|
|
|
|
|
|
def timeit(func):
|
|
"""
|
|
装饰器:计算函数执行时间
|
|
"""
|
|
|
|
@wraps(func)
|
|
def wrapper(*args, **kwargs):
|
|
start_time = time.time()
|
|
result = func(*args, **kwargs)
|
|
end_time = time.time()
|
|
logger.info(
|
|
f"Function {func.__name__} executed in {end_time - start_time:.4f} seconds"
|
|
)
|
|
return result
|
|
|
|
return wrapper
|
|
|
|
|
|
def ensure_directory_exists(target_path: str, is_file: bool = False):
|
|
directory = target_path
|
|
if is_file:
|
|
directory = os.path.dirname(target_path)
|
|
|
|
if not os.path.exists(directory):
|
|
os.makedirs(directory)
|