file_wrap.py¶
A collection of utilities for file wrapping.
Note: This is a work in progress.
-
class
openmdao.util.file_wrap.
InputFileGenerator
[source]¶ Bases:
object
Utility to generate an input file from a template. Substitution of values is supported. Data is located with a simple API.
-
clearline
(row)[source]¶ Replace the contents of a row with the newline character.
Args: row : integer
Row number to clear, relative to current anchor.
-
mark_anchor
(anchor, occurrence=1)[source]¶ Marks the location of a landmark, which lets you describe data by relative position. Note that a forward search begins at the old anchor location. If you want to restart the search for the anchor at the file beginning, then call
reset_anchor()
beforemark_anchor
.Args: anchor : string
The text you want to search for.
occurrence : integer, optional
Find nth instance of text; default is 1 (first). Use -1 to find last occurrence. Reverse searches always start at the end of the file no matter the state of any previous anchor.
-
set_delimiters
(delimiter)[source]¶ Lets you change the delimiter that is used to identify field boundaries.
Args: delimiter : str
A string containing characters to be used as delimiters.
-
set_generated_file
(filename)[source]¶ Set the name of the file that will be generated.
Args: filename : string
Name of the input file to be generated.
-
set_template_file
(filename)[source]¶ Set the name of the template file to be used The template file is also read into memory when this method is called.
Args: filename : string
Name of the template file to be used.
-
transfer_2Darray
(value, row_start, row_end, field_start, field_end)[source]¶ Changes the values of a 2D array in the template relative to the current anchor. This method is specialized for 2D arrays, where each row of the array is on its own line.
Args: value : ndarray
Array of values to insert.
row_start : integer
Starting row for inserting the array. This is relative to the anchor, and can be negative.
row_end : integer
Final row for the array, relative to the anchor.
field_start : integer
starting field in the given row_start as denoted by delimiter(s).
field_end : integer
The final field the array uses in row_end. We need this to figure out if the template is too small or large.
-
transfer_array
(value, row_start, field_start, field_end, row_end=None, sep=', ')[source]¶ Changes the values of an array in the template relative to the current anchor. This should generally be used for one-dimensional or free form arrays.
Args: value : float, integer, bool, str
Array of values to insert.
row_start : integer
Starting row for inserting the array. This is relative to the anchor, and can be negative.
field_start : integer
Starting field in the given row_start as denoted by delimiter(s).
field_end : integer
The final field the array uses in row_end. We need this to figure out if the template is too small or large
row_end : integer, optional
Use if the array wraps to cover additional lines.
sep : integer, optional
Separator to use if we go beyond the template.
-
transfer_var
(value, row, field)[source]¶ Changes a single variable in the template relative to the current anchor.
Args: value : float, integer, bool, string
New value to set at the location.
row : integer
Number of lines offset from anchor line (0 is anchor line). This can be negative.
field : integer
Which word in line to replace, as denoted by delimiter(s)
-
-
class
openmdao.util.file_wrap.
FileParser
(end_of_line_comment_char=None, full_line_comment_char=None)[source]¶ Bases:
object
Utility to locate and read data from a file.
Args: end_of_line_comment_char : string, optional
Specify an end-of-line comment character to be ignored (e.g., Python supports in-line comments with “#”.)
full_line_comment_char : string, optional
Sepcify a comment character that signifies a line should be skipped.
-
mark_anchor
(anchor, occurrence=1)[source]¶ Marks the location of a landmark, which lets you describe data by relative position. Note that a forward search begins at the old anchor location. If you want to restart the search for the anchor at the file beginning, then call
reset_anchor()
beforemark_anchor
.Args: anchor : str
The text you want to search for.
occurrence : integer
Find nth instance of text; default is 1 (first). Use -1 to find last occurrence. Reverse searches always start at the end of the file no matter the state of any previous anchor.
-
set_delimiters
(delimiter)[source]¶ Lets you change the delimiter that is used to identify field boundaries.
Args: delimiter : string
A string containing characters to be used as delimiters. The default value is ‘ ‘, which means that spaces and tabs are not taken as data but instead mark the boundaries. Note that the parser is smart enough to recognize characters within quotes as non-delimiters.
-
set_file
(filename)[source]¶ Set the name of the file that will be generated.
Args: filename : string
Name of the input file to be generated.
-
transfer_2Darray
(rowstart, fieldstart, rowend, fieldend=None)[source]¶ Grabs a 2D array of variables relative to the current anchor. Each line of data is placed in a separate row.
If the delimiter is set to ‘columns’, then the values contained in fieldstart and fieldend should be the column number instead of the field number.
Args: rowstart : integer
Row number to start, relative to the current anchor.
fieldstart : integer
Field number to start.
rowend : integer
Row number to end relative to current anchor.
fieldend : integer (optional)
Field number to end. If not specified, grabs all fields up to the end of the line.
Returns: string : data from the requested location in the file
-
transfer_array
(rowstart, fieldstart, rowend=None, fieldend=None)[source]¶ Grabs an array of variables relative to the current anchor.
Setting the delimiter to ‘columns’ elicits some special behavior from this method. Normally, the extraction process wraps around at the end of a line and continues grabbing each field at the start of a newline. When the delimiter is set to columns, the parameters (rowstart, fieldstart, rowend, fieldend) demark a box, and all values in that box are retrieved. Note that standard whitespace is the secondary delimiter in this case.
Args: rowstart : integer
Row number to start, relative to the current anchor.
fieldstart : integer
Field number to start.
rowend : integer, optional
Row number to end. If not set, then only one row is grabbed.
fieldend : integer
Field number to end.
Returns: string : data from the requested location in the file
-
transfer_keyvar
(key, field, occurrence=1, rowoffset=0)[source]¶ Searches for a key relative to the current anchor and then grabs a field from that line.
You can do the same thing with a call to
mark_anchor
andtransfer_var
. This function just combines them for convenience.Args: field : integer
Which field to transfer. Field 0 is the key.
occurrence : integer
Find nth instance of text; default is 1 (first value field). Use -1 to find last occurance. Position 0 is the key field, so it should not be used as a value for occurrence.
rowoffset : integer (optional)
Optional row offset from the occurrence of key. This can also be negative.
Returns: string : data from the requested location in the file
-
transfer_line
(row)[source]¶ Returns a whole line, relative to current anchor.
Args: row : integer
Number of lines offset from anchor line (0 is anchor line). This can be negative.
Returns: string : line at the location requested
-
transfer_var
(row, field, fieldend=None)[source]¶ Grabs a single variable relative to the current anchor.
Args: row : integer
Number of lines offset from anchor line (0 is anchor line). This can be negative.
field : integer
If the delimiter is a set of chars: which word in line to retrieve. If the delimiter is ‘columns’: character position to start.
fieldend : integer (optional)
If the delimiter is a set of chars: IGNORED. If the delimiter is ‘columns’: position of last character to return, or if omitted, the end of the line is used.
Returns: string : data from the requested location in the file
-