Refactor code for improved readability and consistency
- Cleaned up whitespace and formatting in various files including http.py, language.py, logger.py, safe_exec.py, and SQL migration scripts. - Consolidated import statements and removed unnecessary blank lines. - Updated logging configuration for better clarity. - Enhanced the safe execution code with improved error handling and logging. - Removed commented-out code and unnecessary variables in backfill_zero_trades.py and other scripts. - Added a pyproject.toml for Ruff and Vulture configuration. - Introduced requirements-dev.txt for development dependencies. - Removed commented-out stock entries in init.sql for cleaner migration scripts.
This commit is contained in:
@@ -12,27 +12,19 @@ Notes:
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional
|
||||
|
||||
import re
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
import requests
|
||||
|
||||
from app.utils.logger import get_logger
|
||||
from app.data.market_symbols_seed import get_symbol_name as seed_get_symbol_name
|
||||
from app.data_sources.tencent import normalize_cn_code, normalize_hk_code
|
||||
from app.utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
def _normalize_symbol_for_market(market: str, symbol: str) -> str:
|
||||
m = (market or '').strip()
|
||||
s = (symbol or '').strip().upper()
|
||||
if m == 'CNStock':
|
||||
return normalize_cn_code(s)
|
||||
if m == 'HKStock':
|
||||
return normalize_hk_code(s)
|
||||
s = (symbol or "").strip().upper()
|
||||
return s
|
||||
|
||||
|
||||
@@ -40,21 +32,24 @@ def _resolve_name_from_yfinance(symbol: str) -> Optional[str]:
|
||||
"""
|
||||
Best-effort company name via yfinance.
|
||||
"""
|
||||
|
||||
def _try_one(sym: str) -> Optional[str]:
|
||||
import yfinance as yf
|
||||
|
||||
t = yf.Ticker(sym)
|
||||
info = getattr(t, "info", None)
|
||||
if not isinstance(info, dict) or not info:
|
||||
return None
|
||||
name = (info.get('longName') or info.get('shortName') or '').strip()
|
||||
name = (info.get("longName") or info.get("shortName") or "").strip()
|
||||
return name if name else None
|
||||
|
||||
try:
|
||||
# yfinance uses '-' for some tickers (e.g. BRK-B) while users may input 'BRK.B'
|
||||
out = _try_one(symbol)
|
||||
if out:
|
||||
return out
|
||||
if '.' in symbol:
|
||||
out = _try_one(symbol.replace('.', '-'))
|
||||
if "." in symbol:
|
||||
out = _try_one(symbol.replace(".", "-"))
|
||||
if out:
|
||||
return out
|
||||
return None
|
||||
@@ -69,7 +64,7 @@ def _resolve_name_from_finnhub(symbol: str) -> Optional[str]:
|
||||
https://finnhub.io/docs/api/company-profile2
|
||||
"""
|
||||
try:
|
||||
api_key = (os.getenv('FINNHUB_API_KEY') or '').strip()
|
||||
api_key = (os.getenv("FINNHUB_API_KEY") or "").strip()
|
||||
if not api_key:
|
||||
return None
|
||||
url = "https://finnhub.io/api/v1/stock/profile2"
|
||||
@@ -79,7 +74,7 @@ def _resolve_name_from_finnhub(symbol: str) -> Optional[str]:
|
||||
data = resp.json() if resp.text else {}
|
||||
if not isinstance(data, dict) or not data:
|
||||
return None
|
||||
name = (data.get("name") or data.get("ticker") or '').strip()
|
||||
name = (data.get("name") or data.get("ticker") or "").strip()
|
||||
return name if name else None
|
||||
except Exception as e:
|
||||
logger.debug(f"Finnhub name resolve failed: {symbol}: {e}")
|
||||
@@ -94,7 +89,7 @@ def resolve_symbol_name(market: str, symbol: str) -> Optional[str]:
|
||||
2) Market-specific public sources
|
||||
3) Reasonable fallback (None)
|
||||
"""
|
||||
m = (market or '').strip()
|
||||
m = (market or "").strip()
|
||||
s = _normalize_symbol_for_market(m, symbol)
|
||||
if not m or not s:
|
||||
return None
|
||||
@@ -105,35 +100,24 @@ def resolve_symbol_name(market: str, symbol: str) -> Optional[str]:
|
||||
return seed
|
||||
|
||||
# 2) Market-specific
|
||||
if m == 'USStock':
|
||||
if m == "USStock":
|
||||
# Prefer Finnhub if configured (more stable for company name),
|
||||
# otherwise fall back to yfinance.
|
||||
return _resolve_name_from_finnhub(s) or _resolve_name_from_yfinance(s)
|
||||
|
||||
# CN/HK stocks: try Tencent quote name first (no key), then yfinance best-effort.
|
||||
if m in ('CNStock', 'HKStock'):
|
||||
try:
|
||||
from app.data_sources.tencent import fetch_quote
|
||||
parts = fetch_quote(s)
|
||||
if parts and len(parts) > 1 and parts[1]:
|
||||
return str(parts[1]).strip()
|
||||
except Exception:
|
||||
pass
|
||||
return _resolve_name_from_yfinance(s)
|
||||
|
||||
# Crypto: at least return base ticker-like display (not a "company", but better than empty)
|
||||
if m == 'Crypto':
|
||||
if '/' in s:
|
||||
base = s.split('/')[0].strip()
|
||||
if m == "Crypto":
|
||||
if "/" in s:
|
||||
base = s.split("/")[0].strip()
|
||||
return base if base else None
|
||||
return s
|
||||
|
||||
# Forex: keep as-is (e.g. EURUSD) – you can later replace with a nicer mapping if needed.
|
||||
if m == 'Forex':
|
||||
if m == "Forex":
|
||||
return s
|
||||
|
||||
# Futures: keep as-is
|
||||
if m == 'Futures':
|
||||
if m == "Futures":
|
||||
return s
|
||||
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user