inpRW._inpR

Module Contents

This module contains functions for parsing the data in the input file.

class inpRW._inpR.Read

The Read class contains functions related to reading information from the input file.

createPathFromSequence(seq, base='self')

Generates a path string from a sequence of sequences.

This function takes a sequence of sequences representing the keyword position, suboptions (optional), and data (optional). It will generate a path string from the sequence. If calling this function from inside the inpRW class, do not specify base. If calling it outside the class, base can be set to the instance name of inpRW.

Example

>>> import inpRW
>>> inp = inpRW.inpRW('dummy.inp')
>>> inp.createPathFromSequence([0])
'self.keywords[0]'

If we need to specify a series of suboptions locations, we use a sublist as the first entry of seq. We can also specify a different base string:

>>> inp.createPathFromSequence([0, [0, 1]], base='inp')
'inp.keywords[0].suboptions[0].suboptions[1]'

Finally, we can specify the path to data items in the 2nd entry of seq. Make sure to include a blank list for the suboptions entry if you do not need to specify a sub-keyword block. Negative integers are allowed:

>>> inp.createPathFromSequence([-1, [], [0, 1]])
'self.keywords[-1].data[0][1]'
Parameters:
  • seq (list, tuple) – A list or tuple of the form [0, [0,1], [0,1]] or [0, [0,1], 0]. Item 0 represents the keyword index, item 1 is optional and represents the suboption indices, item 2 is optional and represents the data position(s). Defaults to [None, [], None].

  • base (str) – A string that will be the base of the output path. Defaults to ‘self’.

Returns:

A string containing the path to the object as specified by seq.

Return type:

str

parsePath(path)

Returns a list containing the object at path and a list of integers representing the path. The object should be somewhere in the keywords -> subkeywords -> data structure (i.e. it should represent a keyword block, or a data item in a keyword block.

Here’s an example. First, we need to parse the input file:

>>> import inpRW
>>> inp = inpRW.inpRW('example_inp.inp')
>>> inp.parse()

Now, we can call the function and print the keyword block and the position list (positionl):

>>> block, positionl =  inp.parsePath('self.keywords[16].suboptions[2]')
>>> print(block)

*Restart, write, frequency=0
>>> positionl
[16, [2], None]
We can also retrieve a particular data item:
>>> inp.parsePath('self.keywords[12].suboptions[0].data[0][0]')
(inpDecimal('210000.'), [12, [0], [0, 0]])

If there is no object at the specified path, this will return (None, indices):

>>> inp.parsePath('self.keywords[18]')
No keyword block at self.keywords[18].
(None, [18, [], None])
Parameters:

path (str) – Should be the path string from an inpKeyword object.

Returns:

A list containing the object at path and a list of lists containing integers representing the

keyword index and possibly suboptions and data indices.

Return type:

list

sortKWs(iterable, reverse=False)

This function will sort the keyword blocks in iterable by their top-down order in the input file.

It is needed to sort keywords properly when using organize = True when instancing inpRW.

Parameters:
  • iterable (list) – A sequence of inpKeyword blocks.

  • reverse (bool) – If True, sort the keyword blocks in reverse order. Defaults to False.

Returns:

The keyword blocks sorted in the top-down (or reversed) order as they appear in the input file.

Return type:

list

subBlockLoop(block, func)

Performs func on all items in block.suboptions.

This will perform func on all subblocks in block.suboptions. func should be a function reference.

Example:

self._potentialBlocks = []
pBa = self._potentialBlocks.append
for block in self.keywords:
    pBa(block)
    if block.suboptions:
        self.subBlockLoop(block, pBa)
Parameters:
updateObjectsPath(startIndex=0, _parentBlock='')

Updates the path string of each keyword block after startIndex.

This should be called after inserting, deleting, or replacing keyword blocks. It is called in insertKeyword(), deleteKeyword(), and replaceKeyword(), unless it is toggled off.

_parentBlock is intended to be used when this function calls itself recursively, so that nested suboptions will also have their paths updated.

Parameters:
  • startIndex (int) – The index of a keyword block in keywords

  • _parentBlock (inpKeyword) – When _parentBlock is specified, this function will loop through _parentBlock.suboptions and update those paths.

This function is called by updateInp().

_createSubKW(block)

Checks if the keyword name in block matches a key in _subBlockKWs. If so, a subkw block, and subsequent keyword blocks will be placed into suboptions of this keyword block if they are valid suboptions.

Parameters:

block (inpKeyword) – An inpKeyword object.

_endSubKW(block)

This function tries to close any and all open sub keyword blocks.

It compares the values of _subBlockKWs with the name of block to determine if block can close the open parent block.

Parameters:

block (inpKeyword) – An inpKeyword object.

_getLeadingCommentsPattern()

This function finds the location where the first keyword block begins in an input file and returns a regular expression pattern to identify that location.

The pattern will be used by _splitKeywords() to split the input file immediately before the start of the first keyword block.

Returns:

A string representing the re pattern which will be used to split the text of the input file on

the characters prior to the first keyword block.

Return type:

str

_groupKeywordBlocks(b=None, parentBlock='')

Creates kwg, with the unique keyword names in the input file as the keys, and a set of blocks as each value.

kwg can be used to quickly find all keyword blocks of a given keyword name.

Parameters:
  • b (list) – A list of inpKeyword blocks. If specified, only the blocks in b will be updated in kwg.

  • _parentBlock (inpKeyword) – Only meant to be specified when this function calls itself recursively, so subblocks are included in kwg.

This function is called by updateInp().

_readInclude(block)

Creates a sub-instance of inpRW to read the *INCLUDE input file.

The contents of the new input will be placed in the suboptions of block.

Parameters:

block (inpKeyword) – An inpKeyword object with name = INCLUDE

_readManifest(block)

Creates a sub-instance of inpRW to read the *MANIFEST input files.

The contents of the new input will be placed in the suboptions of block.

Parameters:

block (inpKeyword) – An inpKeyword object with name = MANIFEST

_splitKeywords()

Splits the text string of the input file using the * and letter characters.

This function splits the text string of the input file using the * and letter characters. Comment lines (denoted with “**”) will be included with the previous string. Comments prior to the first keyword block will be stored in _leadingcomments.

_subParam(obj)

Converts references to a parameter defined in *PARAMETER to the actual value.

If obj is not an instance of str, obj will be returned untouched. If obj is an instance of str this function will check if it is a reference to a *PARAMETER value (i.e. ‘<var1>’). If so, it will retrieve the appropriate value from pd. If not, obj will be returned.

This function will not modify the original data object.

Parameters:

obj (str or inpString) – obj should be a str or inpString for this function to operate on it.

Returns:

The type of the returned item is determined by the *PARAMETER function, or obj is returned.