pycsamt.seg.components#

Classes

ComponentsMixin()

Lightweight helpers to access and manage structured components stored in self.sections.

class pycsamt.seg.components.ComponentsMixin[source]#

Bases: object

Lightweight 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 from self.sections with 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. When keys is None all components are included.

  • compose_headers_from(keys_order=None) : Serialize a subset of sections by calling each component’s write() method, if available. A component may return either list[str] or str. Missing sections, components without a write() method, or components whose write() raises are safely ignored. The result is a single concatenated str. If keys_order is 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) and get_spectra_io(), set_spectra_io(obj), along with get_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) and get_timeseries_io(), set_timeseries_io(obj), along with get_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.ParseMixin

Source discovery and normalization utilities.

pycsamt.seg.collection.EDICollection

Higher-level container that aggregates parsed EDI files and uses this mixin for section handling.

References

cget(key, default=None)[source]#
Parameters:
Return type:

Any

cset(key, obj)[source]#
Parameters:
Return type:

None

chas(key)[source]#
Parameters:

key (str)

Return type:

bool

cdrop(key)[source]#
Parameters:

key (str)

Return type:

None

snapshot(keys=None)[source]#

Return a shallow snapshot mapping key -> component or None if absent. If keys is omitted, use all current keys.

Parameters:

keys (Iterable[str] | None)

Return type:

dict[str, Any]

get_head()[source]#
Return type:

Any

set_head(obj)[source]#
Parameters:

obj (Any)

Return type:

None

get_info()[source]#
Return type:

Any

set_info(obj)[source]#
Parameters:

obj (Any)

Return type:

None

get_definemeasurement()[source]#
Return type:

Any

set_definemeasurement(obj)[source]#
Parameters:

obj (Any)

Return type:

None

get_mtsect()[source]#
Return type:

Any

set_mtsect(obj)[source]#
Parameters:

obj (Any)

Return type:

None

get_other()[source]#
Return type:

Any

set_other(obj)[source]#
Parameters:

obj (Any)

Return type:

None

get_other_io()[source]#
Return type:

Any

set_other_io(obj)[source]#
Parameters:

obj (Any)

Return type:

None

get_spectra_sect()[source]#
Return type:

Any

set_spectra_sect(obj)[source]#
Parameters:

obj (Any)

Return type:

None

get_spectra_io()[source]#
Return type:

Any

set_spectra_io(obj)[source]#
Parameters:

obj (Any)

Return type:

None

get_spectra()[source]#
Return type:

Any

set_spectra(obj)[source]#
Parameters:

obj (Any)

Return type:

None

get_timeseries_sect()[source]#
Return type:

Any

set_timeseries_sect(obj)[source]#
Parameters:

obj (Any)

Return type:

None

get_timeseries_io()[source]#
Return type:

Any

set_timeseries_io(obj)[source]#
Parameters:

obj (Any)

Return type:

None

get_timeseries()[source]#
Return type:

Any

set_timeseries(obj)[source]#
Parameters:

obj (Any)

Return type:

None

compose_headers_from(keys_order=None)[source]#

Compose textual headers by calling write() on registered section objects. The order can be controlled with keys_order; unknown keys are ignored; remaining keys are appended by insertion order.

Parameters:

keys_order (list[str] | None)

Return type:

str