Package SloppyCell :: Package ExprManip :: Module Substitution
[hide private]

Module Substitution

source code

Functions [hide private]
 
sub_for_comps(expr, mapping)
For each pair out_name:in_expr in mapping, the returned string has all occurences of the variable out_compe substituted by in_expr.
source code
 
_sub_subtrees_for_comps(ast, ast_mappings) source code
 
sub_for_var(expr, out_name, in_expr)
Returns a string with all occurances of the variable out_name substituted by in_expr.
source code
 
sub_for_vars(expr, mapping)
For each pair out_name:in_expr in mapping, the returned string has all occurences of the variable out_name substituted by in_expr.
source code
 
_sub_subtrees_for_vars(ast, ast_mappings)
For each out_name, in_ast pair in mappings, substitute in_ast for all ...
source code
 
sub_for_func(expr, func_name, func_vars, func_expr)
Return a string with the function func_name substituted for its exploded form.
source code
 
_sub_for_func_ast(ast, func_name, func_vars, func_expr_ast)
Return an ast with the function func_name substituted out.
source code
 
make_c_compatible(expr)
Convert a python math string into one compatible with C.
source code
 
_make_c_compatible_ast(ast) source code

Imports: Slice, Const, Raise, For, AssTuple, Mul, Invert, RightShift, AssList, Add, Dict, flatten, UnaryAdd, Import, Print, Ellipsis, Sliceobj, Decorators, Subscript, Name, Node, Assert, Return, Power, Exec, CO_VARKEYWORDS, GenExprFor, Stmt, Or, Break, CO_VARARGS, Bitand, FloorDiv, Tuple, Bitxor, TryExcept, Not, nodes, EmptyNode, With, Class, Mod, Printnl, Function, TryFinally, GenExprIf, While, AssAttr, Keyword, GenExpr, Module, AugAssign, List, Yield, IfExp, AssName, From, Continue, Backquote, Discard, Div, Expression, Assign, Lambda, And, LeftShift, Compare, GenExprInner, CallFunc, Global, Getattr, ListCompIf, Sub, ListCompFor, flatten_nodes, Pass, UnarySub, Bitor, ListComp, If, copy, AST, strip_parse, ast2str, Simplify


Function Details [hide private]

sub_for_var(expr, out_name, in_expr)

source code 

Returns a string with all occurances of the variable out_name substituted by
in_expr.

Perhaps regular expressions could do this more simply...

_sub_subtrees_for_vars(ast, ast_mappings)

source code 

For each out_name, in_ast pair in mappings, substitute in_ast for all 
occurances of the variable named out_name in ast

sub_for_func(expr, func_name, func_vars, func_expr)

source code 

Return a string with the function func_name substituted for its exploded 
form.

func_name: The name of the function.
func_vars: A sequence variables used by the function expression
func_expr: The expression for the function.
For example:
    If f(x, y, z) = sqrt(z)*x*y-z
    func_name = 'f'
    func_vars = ['x', 'y', 'z']
    func_expr = 'sqrt(z)*x*y-z'

As a special case, functions that take a variable number of arguments can
use '*' for func_vars.
For example:
    sub_for_func('or_func(or_func(A,D),B,C)', 'or_func', '*', 'x or y')
    yields '(A or D) or B or C'

make_c_compatible(expr)

source code 

Convert a python math string into one compatible with C.

Substitute all python-style x**n exponents with pow(x, n).
Replace all integer constants with float values to avoid integer
 casting problems (e.g. '1' -> '1.0').
Replace 'and', 'or', and 'not' with C's '&&', '||', and '!'. This may be
 fragile if the parsing library changes in newer python versions.