cosapp.core.eval_str¶
Module handling the execution of code provided as string by the user.
This code is inspired from the OpenMDAO module openmdao.components.exec_comp.
Classes
|
Create an executable assignment of the kind 'lhs = rhs' from two evaluable expressions lhs and rhs. |
|
Sub-set of context attributes. |
|
Create a executable statement using an expression string. |
- class cosapp.core.eval_str.AssignString(lhs: str, rhs: Any, context: System)[source]¶
Bases:
object
Create an executable assignment of the kind ‘lhs = rhs’ from two evaluable expressions lhs and rhs.
- property constant: bool¶
True if assignment right-hand side is constant, False otherwise.
- Type:
bool
- property contextual_lhs: str¶
Contextual name of assignment left-hand side.
- Type:
str
- exec(context=None) Tuple[Any, bool] [source]¶
Evaluates rhs, and executes assignment lhs <- rhs.
- Returns:
(rhs, changed), where ‘changed’ is True if the value of rhs has changed, False otherwise.
- Return type:
Tuple[Any, bool]
- property lhs: str¶
Left-hand side of the assignment.
- Type:
str
- property lhs_variables: FrozenSet[str]¶
set of variable names in left-hand side.
- Type:
FrozenSet[str]
- property rhs: str¶
Right-hand side of the assignment.
- Type:
str
- property rhs_variables: FrozenSet[str]¶
set of variable names in right-hand side.
- Type:
FrozenSet[str]
- property shape: Tuple[int, int] | None¶
shape of assigned object (lhs) if it is an array, else None.
- Type:
Union[Tuple[int, int], None]
- class cosapp.core.eval_str.ContextLocals(context: System, *args, **kwargs)[source]¶
Bases:
dict
Sub-set of context attributes.
- Parameters:
context (System) – System whose attributes are looked up
- class cosapp.core.eval_str.EvalString(expression: Any, context: System)[source]¶
Bases:
object
Create a executable statement using an expression string.
The following functions are available for use in expression:
Function
Description
abs(x)
Absolute value of x
acos(x)
Inverse cosine of x
acosh(x)
Inverse hyperbolic cosine of x
arange(start, stop, step)
Array creation
arccos(x)
Inverse cosine of x
arccosh(x)
Inverse hyperbolic cosine of x
arcsin(x)
Inverse sine of x
arcsinh(x)
Inverse hyperbolic sine of x
arctan(x)
Inverse tangent of x
asin(x)
Inverse sine of x
asinh(x)
Inverse hyperbolic sine of x
atan(x)
Inverse tangent of x
cos(x)
Cosine of x
cosh(x)
Hyperbolic cosine of x
cross(x, y)
Cross product of arrays x and y
dot(x, y)
Dot-product of x and y
e
Euler’s number
erf(x)
Error function
erfc(x)
Complementary error function
exp(x)
Exponential function
expm1(x)
exp(x) - 1
factorial(x)
Factorial of all numbers in x
fmax(x, y)
Element-wise maximum of x and y
fmin(x, y)
Element-wise minimum of x and y
inner(x, y)
Inner product of arrays x and y
isinf(x)
Element-wise detection of numpy.inf
isnan(x)
Element-wise detection of numpy.nan
kron(x, y)
Kronecker product of arrays x and y
linspace(x, y, N)
Numpy linear spaced array creation
log(x)
Natural logarithm of x
log10(x)
Base-10 logarithm of x
log1p(x)
log(1+x)
matmul(x, y)
Matrix multiplication of x and y
maximum(x, y)
Element-wise maximum of x and y
minimum(x, y)
Element-wise minimum of x and y
ones(N)
Create an array of ones
outer(x, y)
Outer product of x and y
pi
Pi
power(x, y)
Element-wise x**y
prod(x)
The product of all elements in x
sin(x)
Sine of x
sinh(x)
Hyperbolic sine of x
sum(x)
The sum of all elements in x
round(x)
Round all elements in x
tan(x)
Tangent of x
tanh(x)
Hyperbolic tangent of x
tensordot(x, y)
Tensor dot product of x and y
zeros(N)
Create an array of zeros
Full list is returned by EvalString.available_symbols()
- Parameters:
expression (str) – Interpretable Python statement. In addition to standard Python operators, a subset of numpy and scipy functions is supported.
context (cosapp.core.module.Module or cosapp.core.numerics.basics.Residue) – System or Residue defining the local context in which the statement will be executed
Notes
The context is used to include the System ports, children, inwards and outwards. If the context is a Residue, the reference value from the residue is added as residue_reference variable in the execution context.
- classmethod available_symbols() Dict[str, Any] [source]¶
List of available symbols (constants and functions) in current execution context.
- Returns:
Mapping of available symbols by their name.
- Return type:
Dict[str, Any]
- property constant: bool¶
True if evaluated expression is constant, that is independent of its context; False otherwise.
- Type:
bool
- eval() Any [source]¶
Evaluate the expression in the system context.
- Returns:
The result of the expression evaluation.
- Return type:
Any
- property globals: Dict[str, Any]¶
Global functions and variables required to evaluate the string expression.
- Type:
Dict[str, Any]
- property locals: Dict[str, Any]¶
Context attributes required to evaluate the string expression.
- Type:
Dict[str, Any]
- static string(expression: Any) str [source]¶
Converts an expression into a suitably formatted string.
Notes
Necessary step for numpy arrays (and possibly other types), whose plain string conversion is not readily evaluable (hence the use of ‘repr’ instead of ‘str’).
Examples
>>> str(numpy.array([0.1, 0.2])) [0.1 0.2]
>>> repr(numpy.array([0.1, 0.2])) array([0.1, 0.2])
- variables(include_const=False) FrozenSet[str] [source]¶
Extracts all variables required for the evaluation of the expression, with or without system constant properties.
- Parameters:
[bool (include_const) – Determines whether or not read-only properties should be included (default: False).
optional] – Determines whether or not read-only properties should be included (default: False).
- Returns:
Variable names as a set of strings
- Return type:
FrozenSet[str]