mapstp.utils package

Submodules

mapstp.utils.io module

Input/output utility methods.

class mapstp.utils._io.MCNPSections[source]

Bases: object

Text sections from an MCNP file.

__init__(cells, surfaces=None, cards=None, remainder=None)
Parameters:
  • cells (str)

  • surfaces (str | None)

  • cards (str | None)

  • remainder (str | None)

Return type:

None

cards: str | None = None
cells: str
remainder: str | None = None
surfaces: str | None = None
mapstp.utils._io.can_override(path, *, override)[source]

Check if it’s allowed to override a path.

Parameters:
  • path (Path) – path, where we are going to write

  • override (bool) – permission to override flag

Return type:

Path

Returns:

The input path to facilitate chaining in mapping in code.

Raises:

FileExistsError – if file exists, but override is not allowed.

mapstp.utils._io.find_first_cell_number(mcnp)[source]

Find the first cell number in MCNP model.

Parameters:

mcnp (str | Path) – an input MCNP model file name

Return type:

int

Returns:

the first cell number

Raises:

ValueError – if the cell is not found in the mcnp file.

mapstp.utils._io.find_first_void_cell_number(mcnp)[source]

Find the first void cell number in MCNP model.

Parameters:

mcnp (str | Path) – an input MCNP model file name

Return type:

int

Returns:

the first void cell number

Raises:

ValueError – if the cell is not found in the mcnp file.

mapstp.utils._io.read_mcnp_sections(mcnp_path)[source]

Read text sections from MCNP file.

Parameters:

mcnp_path (Path) – path to file.

Returns:

  • the text sections

Return type:

MCNPSections

mapstp.utils._io.select_output(output=None, *, override)[source]

Select stream for output.

If the output is specified, then checks if we can override it.

Parameters:
  • output (str | Path | PathLike[Any] | None) – optional file name for output stream

  • override (bool) – permission to override, if output file exists

Yields:
stdout, if output file name is not specified (None),

opened stream

Return type:

Iterator[TextIO]

mapstp.utils.re module

The regular expressions to be used in various modules.

mapstp.utils._re.CARD_PATTERN = re.compile('^\\s{0,4}(?:(?P<comment>[cC]\\s)|(?P<card>\\w+))')

Start of MCNP line with comment or any card.

mapstp.utils._re.CELLS_END_PATTERN = re.compile('^\\s*$')

Empty line.

Separates sections in MCNP file.

mapstp.utils._re.CELL_START_PATTERN = re.compile('^\\s{0,5}(?P<number>\\d+)\\s+(?P<material>\\d+)\\s+')

Line starts with two numbers.

mapstp.utils._re.MATERIAL_PATTERN = re.compile('^\\s{0,4}[mM](?P<material>\\d+)')

Start of an MCNP card with material specification.

mapstp.utils._re.VOID_CELL_START_PATTERN = re.compile('^\\s{0,5}(?P<number>\\d+)\\s+0\\s')

Line starts with a number and following zero.

Module contents

The mapstp package utility subpackages.

class mapstp.utils.MCNPSections[source]

Bases: object

Text sections from an MCNP file.

__init__(cells, surfaces=None, cards=None, remainder=None)
Parameters:
  • cells (str)

  • surfaces (str | None)

  • cards (str | None)

  • remainder (str | None)

Return type:

None

cards: str | None = None
cells: str
remainder: str | None = None
surfaces: str | None = None
mapstp.utils.can_override(path, *, override)[source]

Check if it’s allowed to override a path.

Parameters:
  • path (Path) – path, where we are going to write

  • override (bool) – permission to override flag

Return type:

Path

Returns:

The input path to facilitate chaining in mapping in code.

Raises:

FileExistsError – if file exists, but override is not allowed.

mapstp.utils.decode_russian(stp_text)[source]

Convert encoded Russian text to unicode string.

Parameters:

stp_text (str) – the text to decode

Return type:

str

Returns:

Decoded text.

mapstp.utils.find_first_cell_number(mcnp)[source]

Find the first cell number in MCNP model.

Parameters:

mcnp (str | Path) – an input MCNP model file name

Return type:

int

Returns:

the first cell number

Raises:

ValueError – if the cell is not found in the mcnp file.

mapstp.utils.read_mcnp_sections(mcnp_path)[source]

Read text sections from MCNP file.

Parameters:

mcnp_path (Path) – path to file.

Returns:

  • the text sections

Return type:

MCNPSections

mapstp.utils.select_output(output=None, *, override)[source]

Select stream for output.

If the output is specified, then checks if we can override it.

Parameters:
  • output (str | Path | PathLike[Any] | None) – optional file name for output stream

  • override (bool) – permission to override, if output file exists

Yields:
stdout, if output file name is not specified (None),

opened stream

Return type:

Iterator[TextIO]