montepy.input_parser.input_file module

class montepy.input_parser.input_file.MCNP_InputFile(path, parent_file=None, overwrite=False)

Bases: object

A class to represent a distinct input file.

Note

this is a bare bones implementation to be fleshed out in the future.

Changed in version 0.3.0: Added the overwrite attribute.

Parameters:
  • path (str) – the path to the input file

  • parent_file (str) – the parent file for this file if any. This occurs when a “read” input is used.

  • overwrite (bool) – Whether to overwrite the file ‘path’ if it exists

classmethod from_open_stream(fh)

Create an MCNP Input File from an open, writable stream

Parameters:

fh (io.TextIOBase) – An open and writable object, such as a file handle.

property lineno

The current line number being read in the file.

This is 1-indexed.

Return type:

int

property name
open(mode, encoding='ascii', replace=True)

Opens the underlying file, and returns self.

This should only ever be completed from within a with statement. For this reason, a close functional is intentionally not provided.

Note

For different encoding schemes see the available list here.

CP1252 is commonly referred to as “extended-ASCII”. You may have success with this encoding for working with special characters.

Changed in version 0.2.11: Added guardrails to raise FileExistsError and IsADirectoryError.

Parameters:
  • mode (str) – the mode to open the file in

  • encoding (str) – The encoding scheme to use. If replace is true, this is ignored, and changed to ASCII

  • replace (bool) – replace all non-ASCII characters with a space (0x20)

Returns:

self

Raises:
  • FileExistsError – if a file already exists with the same path while writing.

  • IsADirectoryError – if the path given is actually a directory while writing.

property parent_file

The parent file for this file.

This is only used when this file is pointed to by a “read” input.

Return type:

str

property path

The path for the file.

Return type:

str

read(size=-1)
readline(size=-1)
write(to_write)