2.9.1.2. pycsamt.seg.cbase#
Classes
|
Lightweight base for EDI collections. |
|
Robust multi-source EDI parser with error tracking and duplicate policies. |
|
Helpers for discovering and normalizing EDI sources. |
- class pycsamt.seg.cbase.ParseMixin[source]
Bases:
objectHelpers for discovering and normalizing EDI sources.
The mixin accepts files, folders, and glob patterns, and yields only paths that point to
.edifiles. All path inputs are normalized with user-home and relative segments resolved.- Parameters:
None – This is a mixin. It does not define its own public constructor parameters.
- Variables:
EDI_SUFFIXES (set of str) – Allowed filename suffixes. Defaults to
{'.edi'}.recursive (bool) – Expected to be provided by the host class. If
True, directory searches userglob._errors (list of tuple(Path, BaseException)) – Optional sink for discovery issues. When present, helpers record unmatched patterns or missing files.
Notes
The mixin exposes small helpers that higher-level parsers can reuse:
_as_pathconverts any pathish value into an absolutepathlib.Path._is_edi_pathreturnsTruefor files whose suffix belongs toEDI_SUFFIXES._iter_pathsnormalizes a single source or a sequence of sources into absolute paths._iter_edi_fileswalks files, directories, and glob patterns and yields only existing EDI files._fast_stationperforms a cheap scan of>HEADto locateDATAID, when available._push_errorrecords discovery problems into the host_errorslist if present.
Examples
Basic file enumeration:
class Finder(ParseMixin): recursive = True f = Finder() paths = list(f._iter_edi_files(["data", "logs/*.edi"], root=Path(".")))
See also
CoreParserHigh-level parser that builds
EDIFileobjects and aggregates errors.EDIFileReader and writer for single EDI files.
References
[ParseMixin-1]SEG EDI MT/EMAP standard (1987), MTNet. https://www.mtnet.info/docs/seg_mt_emap_1987.pdf
- EDI_SUFFIXES = {'.edi'}
- class pycsamt.seg.cbase.CoreParser(*, recursive=True, strict=False, on_dup='replace', verbose=0)[source]
Bases:
ParseMixinRobust multi-source EDI parser with error tracking and duplicate policies.
The parser consumes any combination of files, folders, or glob patterns, builds
EDIFileobjects, and records failures. Duplicates can be handled by station id or kept as they appear.- Parameters:
recursive (bool, default
True) – Recurse into subdirectories when scanning folders.strict (bool, default
False) – IfTrue, any read error is raised. IfFalse, errors are collected and the parse continues.on_dup ({‘replace’, ‘keep’}, default
'replace') – Duplicate policy by station id. With'replace', the last item wins. With'keep', the first seen item is preserved.verbose (int, default
0) – Verbosity passed toEDIFile.
- Variables:
results (list of _ParseResult) – Structured outcomes for each discovered input. Each entry stores the path, the optional
EDIFile, and the read error if one occurred._errors (list of tuple(Path, BaseException)) – Discovery issues, such as unmatched glob patterns or missing files. Filled by
ParseMixin._push_error().
- Returns:
The parsed and de-duplicated EDI objects.
- Return type:
Notes
Station identity is taken from
EDIFile.stationwhen available. If missing, a fast scan of>HEADis used as a fallback. When no station can be resolved, the file path string is used as the key.Examples
Parse a folder and a glob, keep the first copy:
cp = CoreParser(on_dup="keep", recursive=True) edis = cp.parse(["data/edi", "more/*.edi"]) errs = cp.errors()
See also
ParseMixinSource discovery utilities used by the parser.
EDIFileSingle-file reader used to load each path.
References
[CoreParser-1]SEG EDI MT/EMAP standard (1987), MTNet. https://www.mtnet.info/docs/seg_mt_emap_1987.pdf
- results: list[_ParseResult]
- parse(sources)[source]
- errors()[source]
- Return type:
- class pycsamt.seg.cbase.CBBase(items=None, *, verbose=0)[source]
Bases:
objectLightweight base for EDI collections.
The class provides a minimal container over multiple
EDIFileobjects. It focuses on indexing by station id, fast lookup by path, and simple iteration. Subclasses can add project-specific logic, caching, or derived computations.- Parameters:
edis (sequence of EDIFile, optional) – Initial items to populate the collection. When omitted the container starts empty.
index_by ({‘station’, ‘path’}, default
'station') – Key to index items. With'station'theEDIFile.stationis used, with fallback to file path if missing. With'path'the absolute path string is used as the key.on_dup ({‘replace’, ‘keep’}, default
'replace') – Duplicate policy when inserting items that share the same key.items (Iterable[EDIFile] | None)
verbose (int)
- Variables:
- add(ed)[source]
Insert one
EDIFilerespecting the duplicate policy.- Parameters:
ed (EDIFile)
- Return type:
None
- get(key)
Return the stored
EDIFileby its key.
- __iter__()[source]
Iterate over stored
EDIFileobjects in insertion order.
- keys()
Yield collection keys in insertion order.
- values()
Yield
EDIFileobjects in insertion order.
- items()[source]
Yield
(key, EDIFile)pairs in insertion order.
Notes
The base does not perform I/O or parsing. Use
CoreParserto build items, then feed them into the collection. The class aims to be small and easy to extend rather than fully featured.Examples
Build a collection indexed by station:
edis = CoreParser().parse(["data/edi"]) coll = CBBase(edis, index_by="station") for key, ed in coll.items(): print(key, ed.Z.n_freq)
See also
CoreParserParser that produces
EDIFileobjects to be stored in collections.EDIFileSingle-file container stored in the collection.
References
[CBBase-1]SEG EDI MT/EMAP standard (1987), MTNet. https://www.mtnet.info/docs/seg_mt_emap_1987.pdf
- classmethod load(sources, *, parser=None, recursive=True, strict=False, on_dup='replace', verbose=0)[source]
- interpolate(new_freq, *, kind='slinear', bounds_error=True, period_buffer=None)[source]
- write(savepath, *, pattern='{station}.edi', **kwargs)[source]
- property items[source]
unified iterator over stored items.
- Type:
Internal