aeat.application.aggregation._grouping module¶
Shared group-by + name-cache helper for per-modelo aggregators.
Used by: _retenciones, _counterpart to bucket observations and cache canonical names.
Both implement the same shape of aggregation: bucket observations by a composite key, then roll up each bucket. They additionally need to resolve a stable human-readable name per (source_kind, identity_nif) pair across multiple observations.
This module extracts that shared mechanism. The per-domain aggregators retain their domain-specific rollup composition (e.g. counterpart adds country + readiness fields) — only the group-and-name-cache step is shared.
- group_and_collect_names(observations, *, group_key_fn, identity_key_fn, name_fn)[source]¶
Bucket observations by group key and cache a canonical name per identity.
- Behaviour invariants (shared by both per-modelo aggregators):
Iteration order of
observationsis preserved within each bucket.The first non-empty
name_fn(obs)peridentity_key_fn(obs)wins; later non-empty names for the same identity are discarded.An empty / falsy name is skipped (does not overwrite a prior win).
- Parameters:
observations (
Iterable[TypeVar(T)]) – Iterable of observation records.group_key_fn (
Callable[[TypeVar(T)],TypeVar(GroupKey, bound=tuple[object,...])]) – Composite key for bucketing (e.g. (source_kind, nif, scheme)).identity_key_fn (
Callable[[TypeVar(T)],TypeVar(IdentityKey, bound=tuple[object,...])]) – Sub-key for the name cache (e.g. (source_kind, nif)).name_fn (
Callable[[TypeVar(T)],str|None]) – Extractor for the human-readable name on each observation.
- Return type:
tuple[dict[TypeVar(GroupKey, bound=tuple[object,...]),list[TypeVar(T)]],dict[TypeVar(IdentityKey, bound=tuple[object,...]),str]]- Returns:
A two-tuple
(grouped, names)wheregroupedmaps eachgroup_key_fn(obs)to the list of observations sharing that key (insertion order), andnamesmaps eachidentity_key_fn(obs)to the first non-empty name observed.
- filter_observations_for_modelo(observations, *, modelo, catalogue, attribute_fn, aggregator_label)[source]¶
Keep observations whose classifying attribute is in-scope for
modelo.Shared by both per-modelo aggregators:
_counterpartfilters onoperation_kindagainst anOperationKind347/349catalogue;_retencionesfilters onschemeagainst aRetencionSchemecatalogue. The only per-domain inputs are the catalogue, the attribute getter, and the label used in the unsupported-modelo error.- Parameters:
observations (
tuple[TypeVar(T),...]) – Typed observation records to filter.modelo (
str) – The requested modelo code; must key intocatalogue.catalogue (
Mapping[str,Container[TypeVar(AttrValue)]]) – Maps each supported modelo code to the container of eligible attribute values.attribute_fn (
Callable[[TypeVar(T)],TypeVar(AttrValue)]) – Extracts the classifying attribute from each observation.aggregator_label (
str) – Human-readable aggregator name for theAggregationUnsupportedModeloErrormessage.
- Raises:
AggregationUnsupportedModeloError – When
modelois not a key incatalogue.- Return type:
- Returns:
The observations whose classifying attribute is eligible for
modelo, in input order.