amps / utils /common_utils.py
jibsn's picture
Upload 20 files
163107e verified
import time
import asyncio
from functools import wraps
def execution_time(func):
@wraps(func)
def sync_wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Execution time of {func.__name__}: {elapsed_time:.4f} seconds")
return result
@wraps(func)
async def async_wrapper(*args, **kwargs):
start_time = time.time()
result = await func(*args, **kwargs)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Execution time of {func.__name__}: {elapsed_time:.4f} seconds")
return result
if asyncio.iscoroutinefunction(func):
return async_wrapper
else:
return sync_wrapper
def escape_csv_field(field):
"""
Escapes fields to ensure proper CSV formatting.
- Wraps the field in double quotes if it contains a comma, double quote, or newline.
- Escapes double quotes inside the field by doubling them.
"""
field_str = str(field) # Convert the field to a string
if ',' in field_str or '"' in field_str or '\n' in field_str:
field_str = '"' + field_str.replace('"', '""') + '"'
return field_str