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.

Attributes
_filenamestring

the name of the file.

_datalist of string

the contents of the file, by line

_delimiterstring

the name of the file.

_end_of_line_comment_charstring

end-of-line comment character to be ignored.

_full_line_comment_charstring

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.

Parameters
end_of_line_comment_charstring, optional

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

full_line_comment_charstring, optional

comment character that signifies a line should be skipped.

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.

occurrenceinteger

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
delimiterstring

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
filenamestring

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
rowstartinteger

Row number to start, relative to the current anchor.

fieldstartinteger

Field number to start.

rowendinteger

Row number to end relative to current anchor.

fieldendinteger (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
rowstartinteger

Row number to start, relative to the current anchor.

fieldstartinteger

Field number to start.

rowendinteger, optional

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

fieldendinteger

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
keystring

the key to search for.

fieldinteger

Which field to transfer. Field 0 is the key.

occurrenceinteger

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.

rowoffsetinteger (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
rowinteger

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
rowinteger

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

fieldinteger

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

fieldendinteger (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_filenamestring or None

the name of the template file.

_output_filenamestring 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
rowinteger

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
anchorstring

The text you want to search for.

occurrenceinteger, 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
filenamestring

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
filenamestring

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_startinteger

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

row_endinteger

Final row for the array, relative to the anchor.

field_startinteger

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

field_endinteger

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, integer, bool, str

Array of values to insert.

row_startinteger

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

field_startinteger

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

field_endinteger

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

row_endinteger, optional

Use if the array wraps to cover additional lines.

sepinteger, 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, integer, bool, string

New value to set at the location.

rowinteger

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

fieldinteger

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