67 production .py modules that the running PINK service imports but which were never committed: prod/bingx/ (HTTP client, market/user streams, journal, config), prod/clean_arch/ adapters/persistence/runtime/dita/dita_v2 production modules and their co-located tests. Rule going forward: every module imported by launch_dolphin_pink.py / pink_direct.py must appear in git ls-files. Excludes _backup dirs, __pycache__, and non-code files. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
"""Utility helpers for the DITAv2 kernel."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import asdict, is_dataclass
|
|
from datetime import datetime
|
|
from enum import Enum
|
|
from typing import Any
|
|
import json
|
|
import math
|
|
|
|
|
|
def safe_float(value: Any, default: float = 0.0) -> float:
|
|
"""Return a finite float or ``default``."""
|
|
try:
|
|
out = float(value)
|
|
except Exception:
|
|
return default
|
|
if not math.isfinite(out):
|
|
return default
|
|
return out
|
|
|
|
|
|
def json_safe(value: Any) -> Any:
|
|
"""Convert enums, dataclasses and datetimes to JSON-safe objects."""
|
|
if isinstance(value, Enum):
|
|
return value.value
|
|
if isinstance(value, datetime):
|
|
return value.isoformat()
|
|
if is_dataclass(value):
|
|
return json_safe(asdict(value))
|
|
if isinstance(value, dict):
|
|
return {str(key): json_safe(val) for key, val in value.items()}
|
|
if isinstance(value, list):
|
|
return [json_safe(item) for item in value]
|
|
if isinstance(value, tuple):
|
|
return [json_safe(item) for item in value]
|
|
return value
|
|
|
|
|
|
def json_text(value: Any) -> str:
|
|
"""Serialize a value using stable JSON settings."""
|
|
return json.dumps(json_safe(value), separators=(",", ":"), ensure_ascii=False, default=str)
|