code_utils.py

code_utils.py#

Tools for working with code.

class openmdao.utils.code_utils.LambdaPickleWrapper(lambda_func)[source]

Bases: object

A wrapper for a lambda function that allows it to be pickled.

Parameters:
lambda_funcfunction

The lambda function to be wrapped.

Attributes:
_funcfunction

The lambda function.

_srcstr

The isolated source of the lambda function.

__init__(lambda_func)[source]

Initialize the wrapper.

Parameters:
lambda_funcfunction

The lambda function to be wrapped.

openmdao.utils.code_utils.block_filter(tokiter, blocks_to_remove, block_start_tok)[source]

Remove blocks of code from a stream of tokens.

Blocks are removed based on indentation level. If a block’s name matches one in blocks_to_remove, all non-blank lines where the first token is indented to a greater level than the block start token are removed.

Parameters:
tokiteriterator

Iterator of tokens.

blocks_to_removeset

Set of block names to remove.

block_start_tokstr

The name of the block start token, e.g., ‘def’ or ‘class’.

Yields:
tuple

The next token in the stream, unless it is part of a block that should be removed.

openmdao.utils.code_utils.find_block_start(srccode, block_name, block_start_tok)[source]

Find the start of a block of code.

Parameters:
srccodestr

Source code to search for block.

block_namestr

The name of the block to find.

block_start_tokstr

The name of the block start token, e.g., ‘def’ or ‘class’.

Returns:
tuple

A tuple of the form (line number, column number, block start line) or (None, None, None) if the block was not found.

openmdao.utils.code_utils.get_class_attributes(fname, class_dict=None)[source]

Find all referenced attributes in all classes defined in the given file.

Parameters:
fnamestr

File name.

class_dictdict or None

Dict mapping class names to attribute names.

Returns:
dict

The dict maps class name to a set of attribute names.

openmdao.utils.code_utils.get_func_graph(func, outnames=None, display=False)[source]

Generate a graph between a function’s inputs and outputs.

Uses the AST to analyze the function and build a graph between inputs and outputs, so the function source must be available.

Parameters:
funcCallable

The function to be analyzed.

outnameslist or None

The list of expected output variable names.

displaybool

If True, display the graph using pydot.

Returns:
networkx.DiGraph

A graph containing edges from inputs to outputs.

openmdao.utils.code_utils.get_nested_calls(class_, method_name, stream=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]

Display the call tree for the specified class method and all ‘self’ class methods it calls.

Parameters:
class_class

The starting class.

method_namestr

The name of the class method.

streamfile-like

The output stream where output will be displayed.

Returns:
networkx.DiGraph

A graph containing edges from methods to their sub-methods.

openmdao.utils.code_utils.get_partials_deps(func, outputs=None)[source]

Generate tuples of the form (output, input) for the given function.

Only tuples where the output depends on the input are yielded. This can be used to determine which partials need to be declared.

Note that currently the function grapher doesn’t recurse into functions and assumes that all outputs of a sub-function are dependent on all inputs to that function. This may lead to a conservative estimate of partials that need to be declared.

Parameters:
funcCallable

The function to be analyzed.

outputslist or None

The list of output variable names.

Yields:
tuple

A tuple of the form (output, input).

openmdao.utils.code_utils.get_return_names(func)[source]

Return the names of the variables returned by the given function.

Returns None for any return values that aren’t a simple name.

Parameters:
funcfunction

The function to be examined.

Returns:
list

The names of the variables returned by the given function.

openmdao.utils.code_utils.is_lambda(f)[source]

Return True if the given function is a lambda function.

Parameters:
ffunction

The function to check.

Returns:
bool

True if the given function is a lambda function.

openmdao.utils.code_utils.remove_src_blocks(srccode, names, block_start_tok)[source]

Remove blocks from a piece of source code.

Parameters:
srccodestr

The source code.

nameslist of str

List of blocks to be removed.

block_start_tokstr

The name of the block start token, e.g., ‘def’ or ‘class’.

Returns:
str

The modified source code.

openmdao.utils.code_utils.replace_src_block(srccode, block_name, new_block, block_start_tok)[source]

Replace a block in a piece of source code.

Parameters:
srccodestr

The source code.

block_namestr

The name of the block to be replaced.

new_blockstr

The replacement block.

block_start_tokstr

The name of the block start token, e.g., ‘def’ or ‘class’.

Returns:
str

The modified source code.