Changeset 6556 for anuga_core/source/anuga/utilities/system_tools.py
- Timestamp:
- Mar 19, 2009, 4:08:50 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/utilities/system_tools.py
r5921 r6556 247 247 248 248 249 250 249 ## 250 # @brief Get list of variable names in an expression string. 251 # @param source A string containing a python expression. 252 # @return A list of variable name strings. 253 # @note Throws SyntaxError exception if not a valid expression. 254 def get_vars_in_expression(source): 255 '''Get list of variable names in a python expression.''' 256 257 import compiler 258 from compiler.ast import Node 259 260 ## 261 # @brief Internal recursive function. 262 # @param node An AST parse Node. 263 # @param var_list Input list of variables. 264 # @return An updated list of variables. 265 def get_vars_body(node, var_list=[]): 266 if isinstance(node, Node): 267 if node.__class__.__name__ == 'Name': 268 for child in node.getChildren(): 269 if child not in var_list: 270 var_list.append(child) 271 if any(isinstance(child, Node) for child in node.getChildren()): 272 for child in node.getChildren(): 273 var_list = get_vars_body(child, var_list) 274 275 return var_list 276 277 return get_vars_body(compiler.parse(source))
Note: See TracChangeset
for help on using the changeset viewer.