[Tutor] 'source' in python or preloading variables

Aditya Lal aditya.n.lal at gmail.com
Sat Oct 27 17:33:47 CEST 2007


On 10/27/07, John <washakie at gmail.com> wrote:
>
> The problem is the infies are also being used in a shell scripted
> environment, they are frequently updated and cannot be changed.
>
> So ideadly I could just define a function which sourced the file, assuming
> the variable names passed in the *args list. So, yes, I know the names, they
> just haven't been set in the program. I would like the source program to
> then define them. My re is not so great, but I'm assuming the statement
> here:
>
> cmd = '\n'.join([re.sub( r'^([^=]+)=(.*)$', r"\1='\2'", v) for v in
> lines])
>
> assumes the pattern VAR[i]=variable , and then makes it Python friendly.
>
> So it would look like:
>
> my_source(fid,['STN_id','STNlat','STNlon','STNelv'])
>
> then in the program, before exec(cmd) the *args list has to be converted
> into empy lists, preparing it for the cmd. Does that make sense??
>
> Thanks!
>
>

the re expression just changes "lvalue=rvalue" to "lvalue='rvalue'". It
doesn't know whether lvalue is array or something else.

Anyway, you can initialize the variables as follows -

for v in ['STN_id', ... ] :
   exec( 'global %s ; %s = [0]*5' % (v, v))

Unfortunately these variable need to be made global for them to be visible
everywhere.


HTH
Aditya
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071027/f2728fa3/attachment.htm 


More information about the Tutor mailing list