file_wrap.py

A collection of utilities for file wrapping.

Note: This is a work in progress.

class openmdao.utils.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.

Parameters
end_of_line_comment_charstr, optional

End-of-line comment character to be ignored (e.g., Python supports in-line comments with “#”).

full_line_comment_charstr, optional

Comment character that signifies a line should be skipped.

Attributes
_filenamestr

the name of the file.

_datalist of string

the contents of the file, by line

_delimiterstr

the name of the file.

_end_of_line_comment_charstr

end-of-line comment character to be ignored.

_full_line_comment_charstr

comment character that signifies a line should be skipped.

_current_rowint

the current row of the file.

_anchoredbool

indicator that position is relative to a landmark location.

__init__(end_of_line_comment_char=None, full_line_comment_char=None)[source]

Initialize attributes.

mark_anchor(anchor, occurrence=1)[source]

Mark 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() before mark_anchor.

Parameters
anchorstr

The text you want to search for.

occurrenceint

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.

reset_anchor()[source]

Reset anchor to the beginning of the file.

set_delimiters(delimiter)[source]

Set the delimiters that are used to identify field boundaries.

Parameters
delimiterstr

A string containing characters to be used as delimiters. The default value is ‘ t’, 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.

Parameters
filenamestr

Name of the input file to be generated.

transfer_2Darray(rowstart, fieldstart, rowend, fieldend=None)[source]

Get 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.

Parameters
rowstartint

Row number to start, relative to the current anchor.

fieldstartint

Field number to start.

rowendint

Row number to end relative to current anchor.

fieldendint (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]

Get 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.

Parameters
rowstartint

Row number to start, relative to the current anchor.

fieldstartint

Field number to start.

rowendint, optional

Row number to end. If not set, then only one row is grabbed.

fieldendint

Field number to end.

Returns
string

Data from the requested location in the file.

transfer_keyvar(key, field, occurrence=1, rowoffset=0)[source]

Search for a key relative to the current anchor and get a field from that line.

You can do the same thing with a call to mark_anchor and transfer_var. This function just combines them for convenience.

Parameters
keystr

The key to search for.

fieldint

Which field to transfer. Field 0 is the key.

occurrenceint

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.

rowoffsetint (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]

Return an entire line, relative to current anchor.

Parameters
rowint

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]

Get a single variable relative to the current anchor.

Parameters
rowint

Number of lines offset from anchor line (0 is anchor line). This can be negative.

fieldint

If the delimiter is a set of chars: which word in line to retrieve. If the delimiter is ‘columns’: character position to start.

fieldendint (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.

class openmdao.utils.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.

Attributes
_template_filenamestr or None

the name of the template file.

_output_filenamestr or None

the name of the output file.

_delimiterint

delimiter.

_regint

regular expression.

_datalist of string

the contents of the file, by line

_current_rowint

the current row of the file

_anchoredbool

indicator that position is relative to a landmark location.

__init__()[source]

Initialize attributes.

clearline(row)[source]

Replace the contents of a row with the newline character.

Parameters
rowint

Row number to clear, relative to current anchor.

generate(return_data=False)[source]

Use the template file to generate the input file.

Parameters
return_databool

If True, generated file data will be returned as a string.

Returns
string

The generated file data if return_data is True or output filename has not been provided, else None.

mark_anchor(anchor, occurrence=1)[source]

Mark the location of a landmark.

This 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() before mark_anchor.

Parameters
anchorstr

The text you want to search for.

occurrenceint, 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.

reset_anchor()[source]

Reset anchor to the beginning of the file.

set_delimiters(delimiter)[source]

Set the delimiters that are used to identify field boundaries.

Parameters
delimiterstr

A string containing characters to be used as delimiters.

set_generated_file(filename)[source]

Set the name of the file that will be generated.

Parameters
filenamestr

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.

Parameters
filenamestr

Name of the template file to be used.

transfer_2Darray(value, row_start, row_end, field_start, field_end)[source]

Change 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.

Parameters
valuendarray

Array of values to insert.

row_startint

Starting row for inserting the array. This is relative to the anchor, and can be negative.

row_endint

Final row for the array, relative to the anchor.

field_startint

Starting field in the given row_start as denoted by delimiter(s).

field_endint

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]

Change 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.

Parameters
valuefloat, int, bool, str

Array of values to insert.

row_startint

Starting row for inserting the array. This is relative to the anchor, and can be negative.

field_startint

Starting field in the given row_start as denoted by delimiter(s).

field_endint

The final field the array uses in row_end. We need this to figure out if the template is too small or large.

row_endint, optional

Use if the array wraps to cover additional lines.

sepint, optional

Separator to use if we go beyond the template.

transfer_var(value, row, field)[source]

Change a single variable in the template relative to the current anchor.

Parameters
valuefloat, int, bool, str

New value to set at the location.

rowint

Number of lines offset from anchor line (0 is anchor line). This can be negative.

fieldint

Which word in line to replace, as denoted by delimiter(s).