Spaces:
Running
Running
Download modules/scan/validation.py from Arrechenash/stocks: direct link, hf CLI and curl.
- Browser
- Download file 2.11 kB
-
https://huggingface.co/spaces/Arrechenash/stocks/resolve/main/modules/scan/validation.py
- Command line
-
hf download hf://spaces/Arrechenash/stocks/modules/scan/validation.py
-
curl -L -o validation.py https://huggingface.co/spaces/Arrechenash/stocks/resolve/main/modules/scan/validation.py
2.11 kB
| """Query-parameter parsing and validation for the scan API. | |
| The router receives dates and a news filter as plain strings; these helpers | |
| turn them into the values the pipeline expects or raise the 400 the caller | |
| should see. HTTP responses are raised here because this is the boundary for | |
| those rules. | |
| """ | |
| from datetime import datetime | |
| from fastapi import HTTPException | |
| from core.shared.ny_time import NY | |
| from modules.scan.extensions import parse_news_filter | |
| def resolve_calendar_month(year: int | None, month: int | None) -> tuple[int, int]: | |
| """Default missing year/month to the CURRENT NY-market month. | |
| The hardcoded 2026-01 default was a wrong default that only stayed | |
| harmless because the frontend always passes both explicitly. | |
| """ | |
| now = datetime.now(NY) | |
| return (year if year is not None else now.year, month if month is not None else now.month) | |
| def date_bound(value: str | None, name: str) -> str | None: | |
| """An optional ISO date bound as the SQL window wants it, or a 400. | |
| ``start``/``end`` are typed as plain strings and interpolated into the | |
| window, so an impossible date reached DuckDB, logged a conversion error per | |
| request and answered 200 with an empty result set -- a typo in the range | |
| reads as "the scan matched nothing". ``strptime`` rather than | |
| ``fromisoformat`` for the same reason the bankruptcy route uses it: the | |
| zero-padded form is what the SQL literal should carry, so 2026-9-1 is | |
| normalised rather than passed through. An absent bound stays absent, which | |
| is what "no bound" means to every consumer (`start or None`, the service's | |
| own window). | |
| """ | |
| if not value: | |
| return None | |
| try: | |
| return datetime.strptime(value.strip(), "%Y-%m-%d").date().isoformat() | |
| except ValueError: | |
| raise HTTPException(status_code=400, detail=f"Invalid {name} format, expected YYYY-MM-DD") from None | |
| def news_filter_or_400(value: str) -> str | None: | |
| try: | |
| return parse_news_filter(value) | |
| except ValueError as exc: | |
| raise HTTPException(status_code=400, detail=str(exc)) from exc | |