wire.py¶
clew/wire.py
ONE serializer from an R2 dataclass to JSON.
_absent
bool clew.wire._absent(str name, Any value)
True when a value carries nothing a consumer could read.
Source · 12 lines
def _absent(name: str, value: Any) -> bool:
"""`False` and `0` are NOT absent — `crosses_thread: false` is a measurement.
@brief Report whether a value is absent rather than merely falsy.
@return True when the value carries no information.
@version 2
"""
if value is None or (isinstance(value, tuple | list | str) and not value):
return True
return value == "unknown" and name in _UNKNOWN_MEANS_ABSENT
Activity · 3 steps
Flows
_listed
dict[str, Any] clew.wire._listed(list[tuple[str, Any]] pairs)
asdict dict_factory that turns tuple-valued fields into lists.
Source · 8 lines
def _listed(pairs: list[tuple[str, Any]]) -> dict[str, Any]:
"""@brief Convert tuple fields to lists while serializing a dataclass.
@return JSON-safe dict for one dataclass.
@version 1
"""
return {name: list(value) if isinstance(value, tuple) else value for name, value in pairs}
Activity · 1 steps
Flows
_prune_rows
Any clew.wire._prune_rows(Any obj)
Prune absent fields from ROWS (dicts inside a list), leaving the envelope whole.
Source · 15 lines
def _prune_rows(obj: Any) -> Any:
"""Row elision throughout, plus internal-only keys dropped at EVERY level — a `rowid` is
equally misleading on an envelope and inside a row, so unlike elision it is not row-scoped.
@brief Apply row-level elision and strip internal-only keys.
@return Structure with rows pruned, internal ids gone, and the envelope intact.
@version 3
"""
if isinstance(obj, dict):
return {k: _prune_rows(v) for k, v in obj.items() if k not in _INTERNAL_ONLY}
if isinstance(obj, list):
return [_pruned_row(v) if isinstance(v, dict) else _prune_rows(v) for v in obj]
return obj
Activity · 5 steps
Flows
_pruned_row
dict[str, Any] clew.wire._pruned_row(dict[str, Any] row)
One row with its absent fields removed, recursing into nested rows.
Source · 13 lines
def _pruned_row(row: dict[str, Any]) -> dict[str, Any]:
"""Split out from `_prune_rows` to name the rule: this is what "a row" means on
the wire.
@brief Remove absent fields and internal-only keys from a single row.
@return Pruned row.
@version 4
"""
return {
k: _prune_rows(v) for k, v in row.items() if k not in _INTERNAL_ONLY and not _absent(k, v)
}
Activity · 1 steps
Flows
one
dict[str, Any] | None clew.wire.one(Any obj)
Serialize one R2 dataclass (an ENVELOPE) to its JSON wire form.
Called by _config_space, _files_corpus, _flatten_subject, _lock_inventory, _payload, _scope_payload, corpus_payload, files_context, graph_stats
Tested by 9 tests
test_depth_one_carries_no_chain_and_deeper_does, test_envelope_keys_all_survive, test_false_and_zero_are_measurements_not_absences, test_internal_row_ids_are_stripped_from_envelope_and_rows, test_none_serializes_to_none, test_rows_helper_treats_its_items_as_rows, test_rows_inside_an_envelope_are_pruned, test_tuples_become_lists, test_unknown_enums_are_elided_but_only_the_named_ones
Calls _prune_rows
Source · 11 lines
def one(obj: Any) -> dict[str, Any] | None:
"""The object itself is treated as an envelope — every one of its own keys
survives — while rows nested inside it are pruned.
@brief Wire form of a single dataclass result.
@return Serialized dict, or None.
@version 1
"""
return None if obj is None else _prune_rows(asdict(obj, dict_factory=_listed))
Activity · 1 steps
Requirements
Flows
prune_absent_keys
dict[str, Any] | None clew.wire.prune_absent_keys(dict[str, Any]|None payload, tuple[str,...] keys)
Drop named ENVELOPE keys that carry nothing, in place.
Source · 28 lines
def prune_absent_keys(
payload: dict[str, Any] | None, keys: tuple[str, ...]
) -> dict[str, Any] | None:
"""`one()` deliberately keeps every key of the envelope itself, because a consumer
reading a known shape should not have to ask whether a field it always saw is
missing this time. That guarantee is worth keeping for the fields that had it — so
elision is OPT-IN and NAMED, never blanket.
Applied to fields added later, whose absence is the common case and whose presence
is the news: a dossier for a function that opens no critical section should not
spend a key saying so on every call.
Uses the same `_absent` rule as row pruning, so `false` and `0` survive — a
`truncated: false` inside a body excerpt is a measurement, and an added field whose
value is `0` is an answer.
@brief Elide the named absent keys from one envelope.
@return The payload, mutated.
@version 1
"""
if payload is None:
return None
for key in keys:
if key in payload and _absent(key, payload[key]):
del payload[key]
return payload
Activity · 6 steps
Requirements
Flows
rows
list[dict[str, Any]] clew.wire.rows(Any items)
Serialize an iterable of R2 dataclasses as ROWS.
Source · 10 lines
def rows(items: Any) -> list[dict[str, Any]]:
"""These ARE rows — they are about to sit in a list — so each one is pruned.
Calling `one` per element instead would treat each as an envelope and prune
nothing, which is the mistake the parity test caught.
@brief Wire form of a list of dataclass results.
@return List of row dicts.
@version 1
"""
return [_pruned_row(asdict(i, dict_factory=_listed)) for i in items]1 more lines, not shown