This wiki is in the process of being archived due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies. Edits are discouraged.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list.

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


2026年02月14日 16:07

AltStyle によって変換されたページ (->オリジナル) /