Module casacore.util
¶
General utility functions for casacore modules¶
Utilities for casacore modules.
getlocals()
- Get local python variables
substitute()
- Substitute global python variables in a command string
Description¶
-
casacore.util.
getlocals
(back=2)¶ Get the local variables some levels back (-1 is top).
-
casacore.util.
substitute
(s, objlist=(), globals={}, locals={})¶ Substitute global python variables in a command string.
This function parses a string and tries to substitute parts like $name by their value. It is uses by
image
andtable
to handle image and table objects in a command, but also other variables (integers, strings, etc.) can be substituted. The following rules apply:- A name must start with an underscore or alphabetic, followed by zero or more alphanumerics and underscores.
- String parts enclosed in single or double quotes are literals and are left untouched. Furthermore a $ can be escaped by a backslash, which is useful if an environment variable is used. Note that an extra backslash is required in Python to escape the backslash. The output contains the quotes and backslashes.
- A variable is looked up in the global namespace; that is, in the outermost namespace.
- If the variable name has a vector value, its substitution is enclosed in square brackets and separated by commas.
- A string value is enclosed in double quotes. If the value contains a double quote, that quote is enclosed in single quotes.
- If the name’s value has a type mentioned in the argument objlist, it is substituted by $n (where n is a sequence number) and its value is added to the objects of that type in objlist.
- If the name is unknown or has an unknown type, it is left untouched.
The objlist argument is a list of tuples or lists where each tuple or list has three fields:
- The first field is the object type (e.g. table)
- The second field is a prefix for the sequence number (usually empty). E.g. regions could have prefix ‘r’ resulting in a substitution like $r1.
- The third field is a list of objects to be substituted. New objects get appended to it. Usually the list is initially empty.
Apart from substituting variables, it also substitutes $(expression) by the expression result. It correctly handles parentheses and quotes in the expression. For example:
a=2 b=3 substitute('$(a+b)+$a') # results in '5+2' (not '7') substitute('$((a+b)*(a+b))') # results in '25' substitute('$(len("ab cd( de"))') # results in '9'
Substitution is NOT recursive. E.g. if a=1 and b=”$a”, the result of substitute(“$b”) is “$a” and not 1.