⇤ ← Revision 1 as of 2005-12-16 20:37:13
857
Comment: tempering strings with environment variables; passing assignments to environment
|
← Revision 2 as of 2008-11-15 14:01:27 ⇥
857
converted to 1.6 markup
|
Deletions are marked like this. | Additions are marked like this. |
Line 33: | Line 33: |
-- LionKimbro [[DateTime(2005-12-16T20:37:07Z)]] | -- LionKimbro <<DateTime(2005-12-16T20:37:07Z)>> |
Bash-like Global Substitution
1 globals().update(os.environ) # Import environment
2
3
4 def replace_w_global_value(mo):
5 key = mo.group(0)
6 without_dollar = key[1:]
7 return str(globals().get(without_dollar, key))
8
9
10 def subs(input):
11 """Perform Bash-like substitutions.
12
13 Any "$FOO" in a string will be replaced with the value of FOO.
14 If it's not defined, it stays as it was: "$FOO".
15 """
16 return re.sub('[$][A-Za-z_]+', replace_w_global_value, input)
Discussion
The only question I have is: Is there a way to make all assignments within the module, automatically call os.putenv?
So, intercept assignment with setattr or something, on the module.
I don't know how to get a handle to the current module, though.
-- LionKimbro 2005-12-16 20:37:07