Tape functions
The following functions read and write ENDF-6 files that
contain several materials. They express a tape as a list of
ordinary material dictionaries, each of the same form as the
result of parsefile().
Each operation comes as a pair, mirroring the parse /
parsefile naming of the single-material parser: the bare
name works on an in-memory ENDF-6 string, the _file variant
on a file path.
- endf_parserpy.parse_tape(text, *, parser=None, exclude=None, include=None, on_error='mark')[source]
Parse a multi-material ENDF tape string into a list of materials.
This is the eager counterpart of
iter_parse_tape(); see there for a description of the parameters. To parse a file on disk, useparse_tape_file().- Returns:
One entry per material, in tape order. Each entry is either a parsed material dictionary or, for a material that failed to parse with
on_error="mark", aFailedMaterial.- Return type:
- endf_parserpy.parse_tape_file(path, *, parser=None, exclude=None, include=None, on_error='mark')[source]
Parse a multi-material ENDF tape file into a list of materials.
The file counterpart of
parse_tape(); thepathargument is a file path (stroros.PathLike). Seeparse_tape()for the return value anditer_parse_tape()for the parameters.
- endf_parserpy.iter_parse_tape(text, *, parser=None, exclude=None, include=None, on_error='mark')[source]
Parse a multi-material ENDF tape, yielding one material at a time.
- Parameters:
text (str) – The ENDF-6 formatted tape as a single string, as produced by
write_tape()orto_string(). To parse a file on disk, useiter_parse_tape_file().parser (EndfParserBase, optional) – The single-material parser used for each material. Defaults to
EndfParserFactory.create(select="fastest").exclude (optional) – MF / (MF, MT) sections to exclude from / restrict the parsing to. Forwarded unchanged to the single-material parser and applied to every material.
include (optional) – MF / (MF, MT) sections to exclude from / restrict the parsing to. Forwarded unchanged to the single-material parser and applied to every material.
on_error ({"raise", "mark"}) – With
"raise"the first material that fails to parse aborts the iteration. With"mark"(the default) a failing material is yielded as aFailedMaterialand parsing continues.
- Yields:
dict or FailedMaterial – The parsed material dictionary (same shape as the result of a single-material
parsefile), or aFailedMaterial.
Notes
Only one material is held in parsed form at a time, so the parsed data does not accumulate; for a tape too large to hold in memory at all, parse it from disk with
iter_parse_tape_file().The arguments are validated when this function is called; the returned iterator then yields the materials lazily.
- endf_parserpy.iter_parse_tape_file(path, *, parser=None, exclude=None, include=None, on_error='mark')[source]
Parse a multi-material ENDF tape from a file, one material at a time.
The file counterpart of
iter_parse_tape(); thepathargument is a file path (stroros.PathLike). The file is read incrementally, so peak memory use is bounded by the largest single material rather than by the size of the whole tape. Seeiter_parse_tape()for the remaining parameters.
- endf_parserpy.write_tape(materials, *, parser=None, exclude=None, include=None)[source]
Assemble materials into a single multi-material ENDF tape string.
A
FailedMaterialis written verbatim from its stored lines.- Parameters:
materials (Iterable) – The materials, in the desired tape order. Each entry is either a parsed material dictionary (rendered by the parser), a
listof raw ENDF-6 lines, or aFailedMaterial– the latter two are written verbatim, with no intermediate parse or render, so an already-formatted material is copied through unchanged.parser (EndfParserBase, optional) – Parser used to write each material dictionary. Defaults to
EndfParserFactory.create(select="fastest").exclude (optional) – Forwarded unchanged to the single-material parser’s
write.include (optional) – Forwarded unchanged to the single-material parser’s
write.
- Returns:
The assembled tape as a single ENDF-6 formatted string, ending with a newline. This necessarily holds the whole tape in memory; to write a large tape with bounded memory, use
write_tape_file().- Return type:
- endf_parserpy.write_tape_file(materials, path, *, parser=None, exclude=None, include=None, overwrite=False)[source]
Assemble materials and write the tape to a file.
The file counterpart of
write_tape();pathis a file path (stroros.PathLike). An existing file is only overwritten whenoverwrite=True. Seewrite_tape()for the remaining parameters.Each material is rendered and written before the next is pulled from
materials, so whenmaterialsis a generator the peak memory stays bounded by the size of a single material rather than by the size of the whole tape.
- class endf_parserpy.FailedMaterial(exception, raw_lines)[source]
Placeholder for a material that could not be parsed.
Returned by
parse_tape()anditer_parse_tape()(and their_filevariants) in place of a parsed material dictionary whenon_error="mark"and the parsing of that material failed. Passing aFailedMaterialback towrite_tape()writes its original lines verbatim.- property mat
MAT number of the failed material, or
Noneif unknown.