inputmodifier.py

class InputModifier(input_file, comment_chars='#', end_chars='', var_start='_*', var_end='**')[source]

Bases: object

Class to modify variables in generic text-based input files. Once variables have been modified by the user by passing in a dictionary of new variables the input can be written to file.

__init__(input_file, comment_chars='#', end_chars='', var_start='_*', var_end='**')[source]

Initialise the class by reading in the input file. Find and read any variables that are at the top of the file. Default comment_char and end_char are set based on reading MOOSE *.i files.

Parameters:
  • input_file (Path) – Path to the input text file.

  • comment_chars (str, optional) – Character or characters for a comment in the input file, by default “#”.

  • end_chars (str, optional) – Character or characters for a line ending the file. If there is no line ending character this should be an empty string, by default “”.

  • var_start (str, optional) – Character sequence to look for in comments for starting the block of text to look for variables in, by default “_*”.

  • var_end (str, optional) – Character sequence to look for in comment for ending the block of text to look for variables in, by default “**”

read_vars()[source]

Reads the variables in the file

find_vars()[source]

Find what lines the variables are defined on.

update_vars(new_vars)[source]

Updates the variable dictionary that will be written to the input file.

Parameters:
  • new_vars (dict :) – new variables to be written to the input file. The keys must exist within the dictionary of variables extracted from the input file. Only the variables to be edited need to be present.

  • new_vars

write_file(input_write_file)[source]

Write the input file using the current variable dictionary.

Parameters:
  • input_write_file (Path :) – Path to where the file should be written.

  • input_write_file

get_vars()[source]

Gets the variables found in the file.

Returns:

dict – keys are variable strings and values are variable values.

get_var_keys()[source]

Gets a list of variable names found in the input file.

Returns:

list[str] – list of variables name as strings

get_input_file()[source]

Gets the path and input file name.

Returns:

Path – path and input file name.