Querying Fields
Filtering and Sorting via extract()
Pass a therismos expression as filter_expr and/or a
SortSpec as sort_spec to extract(). Both are applied at return time and never affect the
cache.
from therismos import F
from therismos.sorting import SortCriterion, SortOrder, SortSpec
from fields_metadata import MetadataExtractor
extractor = MetadataExtractor()
# Only categorical, non-computed fields
expr = (F("categorical") == True) & (F("computed") == False)
metadata = extractor.extract(Person, filter_expr=expr)
# Only str fields, sorted alphabetically
metadata = extractor.extract(
Person,
filter_expr=F("field_type") == "str",
sort_spec=SortSpec([SortCriterion("field_name", SortOrder.ASCENDING)]),
)
# Filter by nested classification
metadata = extractor.extract(Person, filter_expr=F("classification.tlp") == "GREEN")
# Unfiltered call still returns all fields (cache is intact)
full_metadata = extractor.extract(Person)
Each field's metadata is evaluated as a flat dictionary. Scalar properties (field_name,
multivalued, composite, optional, numeric, categorical, computed, derived,
final, order, doc, parent_field) are available directly. Type-object fields
(field_type, items_type, effective_type) are exposed as their type name strings.
Nested dict fields (classification, extra) support dot-notation (e.g. F("classification.tlp")).
Standalone Query Functions
filter_metadata, sort_metadata, and group_metadata operate on an already-extracted
dict[str, TMetadata].
from therismos import F
from therismos.grouping import Aggregation, AggregationFunction, GroupSpec
from therismos.sorting import SortCriterion, SortOrder, SortSpec
from fields_metadata import MetadataExtractor, filter_metadata, group_metadata, sort_metadata
extractor = MetadataExtractor()
metadata = extractor.extract(Person)
filter_metadata
Returns a new dict; the input is not modified.
sort_metadata
sorted_fields = sort_metadata(
metadata,
SortSpec([
SortCriterion("order", SortOrder.ASCENDING),
SortCriterion("field_name", SortOrder.ASCENDING),
]),
)
SortOrder.NONE criteria are skipped. Fields whose sort key is None appear last.
Returns a new ordered dict.
group_metadata
groups = group_metadata(
metadata,
GroupSpec(group_by=["classification.tlp"], aggregations=[]),
)
# groups[("GREEN",)] → {field_path: metadata, ...}
# groups[(None,)] → fields with no TLP classification
aggregations in the GroupSpec are ignored — only partitioning is applied.
Returns a new dict.