Files
exchange_monitor_sync/utils/helpers.py
lz_db f85f4ef152 1
2025-12-04 15:40:19 +08:00

32 lines
772 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from typing import List, Dict, Optional, Any
from loguru import logger
def safe_float(value, default=0.0):
"""安全转换为float处理None和空值"""
if value is None:
return default
try:
return float(value)
except (ValueError, TypeError):
return default
def safe_int(value, default=0):
"""安全转换为int"""
if value is None:
return default
try:
return int(float(value))
except (ValueError, TypeError):
return default
def safe_str(self, value: Any, default: str = '') -> str:
"""安全转换为str"""
if value is None:
return ""
try:
return str(value)
except Exception as e:
logger.error(f"safe_str error: {e}")
return ""