[Tutor] 'source' in python or preloading variables

John washakie at gmail.com
Sat Oct 27 18:12:43 CEST 2007


Here's where I am:


def source(filename, vList):
 """ takes a file object and a list of variables as input """
 import re
 # Read the file
 fid=open(filename,'r')
 lines = fid.readlines()
 fid.close()
 #how many variables
 ns=len(lines)/len(vList)
 #predefine the varibles
 for v in vList:
  exec( 'global %s ; %s = [0]*(ns+1)' % (v, v))

 # Make it python friendly: put all values in 'single quotes'
 cmd = '\n'.join([re.sub( r'^([^=]+)=(.*)$', r"\1='\2'", v) for v in lines])
 exec(cmd)
 for v in vList:
  exec( '%s=%s[1:]' % (v,v))

source('infile.py',['files','nfiles','nreleases'])

print files
print nreleases
print nfiles



But oddly, my output is:

[0, 'ASP_200603', 'ASP_200604', 'ASP_200605']
[0, '248', '240', '248']
[0, '3', '3', '3']



So, I am not properly getting rid of the list[0], is it something with the
'global' nature of the vairables... it looks like it's only changing
locallly in my source function.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071027/7e06c657/attachment.htm 


More information about the Tutor mailing list