Re: [Tutor] 'source' in python or preloading variables

2007-11-04 Thread Kent Johnson
John wrote: > Does anyone know why the script below works fine (in regards to the > 'source function') but when I try to uncomment the line from mymod > import source and use it that way without defining the function in the > script, I get an error that N_id does not exist. It must have somethin

Re: [Tutor] 'source' in python or preloading variables

2007-11-04 Thread John
Does anyone know why the script below works fine (in regards to the 'source function') but when I try to uncomment the line from mymod import source and use it that way without defining the function in the script, I get an error that N_id does not exist. It must have something to do with namespace

Re: [Tutor] 'source' in python or preloading variables

2007-10-28 Thread Alan Gauld
"John" <[EMAIL PROTECTED]> wrote > But won't if fail since the variabls in the file are not quoted? No the quoting is only necessary if you use eval/exec. The quotes prevent the interpreter from treating them as variable names. If you read the values from the file they are already strings,

Re: [Tutor] 'source' in python or preloading variables

2007-10-28 Thread John
But won't if fail since the variabls in the file are not quoted? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor

Re: [Tutor] 'source' in python or preloading variables

2007-10-27 Thread Alan Gauld
"John" <[EMAIL PROTECTED]> wrote >I have a file sitelocations: > > STN_id[1]=AAA > STNlat[1]=58.80 > STNlon[1]=17.40 > STNelv[1]=20 > STN_id[2]=BBB > STNlat[2]=42.45 > STNlon[2]=25.58 > STNelv[2]=2925 > > which in shell scripts I can simple 'source'. > In Python I have to: >

Re: [Tutor] 'source' in python or preloading variables

2007-10-27 Thread John
Thanks, it's strange, it works within a script defined at the top, but if I try to import it from a module it fails: NameError: name 'files' is not defined On 10/27/07, Aditya Lal <[EMAIL PROTECTED]> wrote: > > > > On 10/27/07, John <[EMAIL PROTECTED]> wrote: > > > > Note, i need the ns+1 beca

Re: [Tutor] 'source' in python or preloading variables

2007-10-27 Thread Aditya Lal
On 10/27/07, John <[EMAIL PROTECTED]> wrote: > > Note, i need the ns+1 because the 'source files are not zero indexed. > > On 10/27/07, John <[EMAIL PROTECTED] > wrote: > > > > Here's where I am: > > > > > > def source(filename, vList): > > """ takes a file object and a list of variables as input

Re: [Tutor] 'source' in python or preloading variables

2007-10-27 Thread John
Note, i need the ns+1 because the 'source files are not zero indexed. On 10/27/07, John <[EMAIL PROTECTED]> wrote: > > 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') >

Re: [Tutor] 'source' in python or preloading variables

2007-10-27 Thread John
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(

Re: [Tutor] 'source' in python or preloading variables

2007-10-27 Thread Aditya Lal
On 10/27/07, John <[EMAIL PROTECTED]> 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 *arg

Re: [Tutor] 'source' in python or preloading variables

2007-10-27 Thread John
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 bee

Re: [Tutor] 'source' in python or preloading variables

2007-10-27 Thread Aditya Lal
On 10/27/07, John <[EMAIL PROTECTED]> wrote: > > Ok, so trying to convert it to a function: > > def source(ifile, *args): > """ takes a file object and a list of variables as input. > assumes, source file has variables in format: > VAR[i]=variable """ > > imp

Re: [Tutor] 'source' in python or preloading variables

2007-10-27 Thread John
Ok, so trying to convert it to a function: def source(ifile, *args): """ takes a file object and a list of variables as input. assumes, source file has variables in format: VAR[i]=variable """ import re # Read the file lines = fd.readlines()

Re: [Tutor] 'source' in python or preloading variables

2007-10-27 Thread John
Thanks! This is helpful.. I like the RE approach as it's conceivable to write a function... On 10/27/07, Aditya Lal <[EMAIL PROTECTED]> wrote: > > You can source the file in python provided that you make it python > friendly:- > > STN_id[1]='AAA' instead of STN_id[1]=AAA > ... > > > --- >

Re: [Tutor] 'source' in python or preloading variables

2007-10-27 Thread Aditya Lal
You can source the file in python provided that you make it python friendly:- STN_id[1]='AAA' instead of STN_id[1]=AAA ... --- import re # Read the file fd = open('sitelocations') lines = fd.readlines() fd.close() # Make it python friendly: put all values in 'single quotes' cmd = '\n'.joi

Re: [Tutor] 'source' in python or preloading variables

2007-10-27 Thread Ricardo Aráoz
John wrote: > I have a file sitelocations: > > STN_id[1]=AAA > STNlat[1]=58.80 > STNlon[1]=17.40 > STNelv[1]=20 > STN_id[2]=BBB > STNlat[2]=42.45 > STNlon[2]=25.58 > STNelv[2]=2925 > > which in shell scripts I can simple 'source'. In Python I have to: > sitesFile=file('sitelocat

[Tutor] 'source' in python or preloading variables

2007-10-27 Thread John
I have a file sitelocations: STN_id[1]=AAA STNlat[1]=58.80 STNlon[1]=17.40 STNelv[1]=20 STN_id[2]=BBB STNlat[2]=42.45 STNlon[2]=25.58 STNelv[2]=2925 which in shell scripts I can simple 'source'. In Python I have to: sitesFile=file('sitelocations','r') sites=sitesFile.readlines() i