Create a variable "on the fly"

Paul D.Smith paul_d_smith at x-hotmail.com
Thu Jul 28 04:04:17 EDT 2005


Bruno,

FYI, notes in-line...

Cheers,
Paul DS

> > a look instantiate Python variables of the appropriate type.
>
> What do you mean "of the appropriate type" ? You want to typecast (eg.
> from string to numeric) ? Then you need to know what env var must be
> casted to what type ? Then you don't need to create variables 'on the fly'
?
>
> I must have missed something...

Good point - my Python "nomenclature" is still in its infancy.  The
background is that I've inherited some historical Python scripts that need
to be configured from a bash shell script (as these Python scripts are being
integrated into a bigger system driven by shell scripts and with a
pre-existing single "point-of-configuration") instead of the existing
Pything config script. The existing Python script configures these
parameters thus...

PARM1='Fred'
PARM2=12
...

So I could so the following...

PARM1=os.environ['PARM1']
PARM2=os.environ['PARM2']
...

The problem is that this config file is almost certainly not complete (as
the complete integration of all scripts has not been done) and I don't want
to spend my life tweaking not one config file (the shell script), but two
(the shell script and the Python script).

Now I'm helped because in fact the parameters have a common format e.g.

MY_PARM1...
MY_PARM2...

so I can define shell environment variables with the same names and then
look for any parameter defined in the shell and named "MY_..." and
instantiate the Python variable from that.

What I'm left with is the following...

1. A shell script which I maintain.
2. A simple Python config which searches for all shell environment variables
named "MY_..." and instantiates then as Python variables.
3. Historical scripts that run without me needing to spend time hunting down
all the config variables and replacing them with os.environ['MY_...'].

Agreed that long term this is not good practice, but short term it's a time
save.





More information about the Python-list mailing list