pycsamt.seg.components#
Classes
Lightweight helpers to access and manage structured components stored in |
- class pycsamt.seg.components.ComponentsMixin[source]#
Bases:
objectLightweight helpers to access and manage structured components stored in
self.sections. The host class is expected to provide a minimal mapping-like API:add_section(key, obj)get_section(key)has_section(key)
Keys are normalized to lowercase to keep lookups stable across callers and file variants.
Notes
This mixin centralizes common section plumbing used by EDI readers and writers. It exposes both generic CRUD helpers and a small set of typed accessors for frequent sections (e.g.
head,info, spectra, and time series).The following utilities are provided:
cget(key, default=None): Safe fetch fromself.sectionswith key normalization and a default.cset(key, obj): Insert or replace a component under the normalized key.chas(key): Presence check under the normalized key.cdrop(key): Remove a component if present. Missing keys are ignored.snapshot(keys=None): Return a dict containing a shallow snapshot of the requested components. WhenkeysisNoneall components are included.compose_headers_from(keys_order=None): Serialize a subset of sections by calling each component’swrite()method, if available. A component may return eitherlist[str]orstr. Missing sections, components without awrite()method, or components whosewrite()raises are safely ignored. The result is a single concatenatedstr. Ifkeys_orderis given, it controls the emission order; otherwise the mixin uses a sensible default order for EDI files.
Typed accessors mirror common sections and include small quality-of-life fallbacks:
get_head(),set_head(obj)get_info(),set_info(obj)get_definemeasurement(),set_definemeasurement(obj)with a fallback that also checks the key'definemeas'when the canonical key is absent.get_spectra_sect(),set_spectra_sect(obj)andget_spectra_io(),set_spectra_io(obj), along withget_spectra()/set_spectra(obj). When a specific spectra header key is missing, the generic'spectra'key is consulted.get_timeseries_sect(),set_timeseries_sect(obj)andget_timeseries_io(),set_timeseries_io(obj), along withget_timeseries()/set_timeseries(obj). As with spectra, a generic'timeseries'key is used as a fallback for header lookups.
These helpers keep parsing and writing code concise while remaining tolerant to small input variations.
Examples
Create a tiny host and manage sections:
class Host(ComponentsMixin): def __init__(self): self.sections = {} def add_section(self, k, v): self.sections[k] = v def get_section(self, k): return self.sections.get(k) def has_section(self, k): return k in self.sections h = Host() h.cset("HEAD", {"dataid": "S01"}) assert h.chas("head") assert h.cget("Head")["dataid"] == "S01" snap = h.snapshot(keys=["head", "info"]) # {'head': {...}, 'info': None}
Compose headers from components that implement a
write()method:class _Head: def write(self): return [">HEAD\n", " DATAID=S01\n"] class _Info: def write(self): return ">INFO\n PROJECT=P\n" h.cset("head", _Head()) h.cset("info", _Info()) text = h.compose_headers_from(keys_order=["info", "head"]) # INFO appears before HEAD in the output.
See also
pycsamt.seg.cbase.ParseMixinSource discovery and normalization utilities.
pycsamt.seg.collection.EDICollectionHigher-level container that aggregates parsed EDI files and uses this mixin for section handling.
References