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, use parse_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", a FailedMaterial.

Return type:

list

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(); the path argument is a file path (str or os.PathLike). See parse_tape() for the return value and iter_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() or to_string(). To parse a file on disk, use iter_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 a FailedMaterial and parsing continues.

Yields:

dict or FailedMaterial – The parsed material dictionary (same shape as the result of a single-material parsefile), or a FailedMaterial.

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(); the path argument is a file path (str or os.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. See iter_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 FailedMaterial is 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 list of raw ENDF-6 lines, or a FailedMaterial – 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:

str

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(); path is a file path (str or os.PathLike). An existing file is only overwritten when overwrite=True. See write_tape() for the remaining parameters.

Each material is rendered and written before the next is pulled from materials, so when materials is 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() and iter_parse_tape() (and their _file variants) in place of a parsed material dictionary when on_error="mark" and the parsing of that material failed. Passing a FailedMaterial back to write_tape() writes its original lines verbatim.

exception

The exception raised while parsing the material.

Type:

Exception

raw_lines

The lines of the single-material tape that failed to parse.

Type:

list[str]

property mat

MAT number of the failed material, or None if unknown.