fix: Handle empty string in float conversions #44

Closed
opened 2026-02-05 00:01:24 +09:00 by agentson · 0 comments
Collaborator

Priority: LOW

Problem

could not convert string to float: ''
Failed to scan AAPL (US_NASDAQ)

API 응답에서 가격 데이터가 빈 문자열로 올 때 발생 (약 15% 빈도)
시장 마감 시간이나 데이터 없을 때 발생

Root Cause

current_price = float(price_data.get("output", {}).get("last", "0"))
# If "last" is "", float("") raises ValueError

Solution

Safe float conversion helper:

def safe_float(value: str | float, default: float = 0.0) -> float:
    """Convert to float, handling empty strings and None."""
    if not value or value == "":
        return default
    try:
        return float(value)
    except (ValueError, TypeError):
        return default

# Usage
current_price = safe_float(price_data.get("output", {}).get("last", "0"))
total_eval = safe_float(balance_info.get("frcr_evlu_tota", "0"))

Files

  • src/main.py: Add safe_float() helper, apply to all float conversions
  • src/analysis/scanner.py: Apply to price parsing

Tests

  • Test safe_float("") returns 0.0
  • Test safe_float(None) returns 0.0
  • Test safe_float("123.45") returns 123.45
## Priority: LOW ## Problem ``` could not convert string to float: '' Failed to scan AAPL (US_NASDAQ) ``` API 응답에서 가격 데이터가 빈 문자열로 올 때 발생 (약 15% 빈도) 시장 마감 시간이나 데이터 없을 때 발생 ## Root Cause ```python current_price = float(price_data.get("output", {}).get("last", "0")) # If "last" is "", float("") raises ValueError ``` ## Solution Safe float conversion helper: ```python def safe_float(value: str | float, default: float = 0.0) -> float: """Convert to float, handling empty strings and None.""" if not value or value == "": return default try: return float(value) except (ValueError, TypeError): return default # Usage current_price = safe_float(price_data.get("output", {}).get("last", "0")) total_eval = safe_float(balance_info.get("frcr_evlu_tota", "0")) ``` ## Files - `src/main.py`: Add safe_float() helper, apply to all float conversions - `src/analysis/scanner.py`: Apply to price parsing ## Tests - Test safe_float("") returns 0.0 - Test safe_float(None) returns 0.0 - Test safe_float("123.45") returns 123.45
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: jihoson/The-Ouroboros#44