parsing
parsing
¶
Tolerant parsing of raw data into validated model instances.
Provides a generic factory-based converter that iterates over a sequence of raw data, calls a user-supplied factory for each element, and collects only the successfully parsed results. Invalid entries are logged at WARNING level and skipped.
The module depends only on bigbrotr.models and the standard library,
keeping it safe to import from any layer above models.
Examples:
from bigbrotr.utils.parsing import safe_parse, parse_relay_url
relays = safe_parse(["wss://relay.example.com", "wss://nos.lol"], parse_relay_url)
relays = safe_parse(rows, lambda r: Relay(r["url"], discovered_at=r["discovered_at"]))
Classes¶
Functions¶
safe_parse
¶
Parse a sequence into model instances, skipping invalid entries.
Calls factory(item) for each element. Items that raise
ValueError, TypeError, or KeyError are logged and discarded.
Source code in src/bigbrotr/utils/parsing.py
parse_relay_url
¶
parse_relay_url(url: str) -> Relay
Sanitize a raw relay URL and construct a Relay.
Applies sanitize_relay_url to canonicalize the input, then constructs a Relay. Intended as a factory for safe_parse when parsing relay URLs from untrusted sources (config files, Nostr events, API responses).
Parameters:
-
url(str) –Raw relay URL string.
Returns:
Raises:
-
ValueError–If the URL is structurally unrecoverable.