does this exception mean something else?
Hi all, I have a file, pasted below for what good it will do, which makes a couple conditional calls to a function called writeDefaults. However, when I manually trigger a condition that causes the function to be called, Python gives me a name error and says that my writeDefaults does not exist. I am sure that it does, as I copied the text from the call and pasted it into the text search of my editor, and it found the line that defines the function, so the text in the call is obviously correct. Sometimes, especially in a language like Javascript, one error can mean something else or point to a problem other than the one it claims to be about, so I wondered if the same thing could be happening here? The error is on line 55, just after the if statement that uses os.path.exists to ensure my ini file is really there. TIA! BTW, speech is just a file that will output a given message through an active screen reader; changing all calls to print will work just as well. import os, sys import helpers from configobj import ConfigObj #for reading ini files from string import Template #for interpreting the template strings in the ini file from speech import speak #set up a default dict of options to be used general={ useSpeech:True, printMessages:False, roundTo:1, defaultStartMode:1 } enableModes={ resourceMonitor:True, weather:True, network:True } weatherOptions={ location:04938 } armOptions={ primaryDrive:c:, secondaryDrive:j:, currentIsPrimary:True, CIsSecondary:True, ramInfo:used, driveInfo:Used } templates={ sayCorePercentage:$percent% for core $core, sayProcessorAverage:$percent% average load, sayRamFree:$percent% ram free ($free of $total remaining), sayRamUsed:$percent% ram used ($used of $total used), sayDriveFree:$percent% free on $drive ($free of $total remaining), sayDriveUsed:$percent% used on $drive ($used of $total used), currentWeather:$condition, $temp degrees. $wind. $humidity., forecast:$day: $condition, $low to $high. } #first, load the ini file setting #so where is the ini? script_path/config.ini, of course! iniLoc=helpers.progdir+'\\config.ini' iniLoc=r'c:\arm\config.ni' #set up the ini object to read and write settings if(os.path.exists(iniLoc)): ini=ConfigObj(iniLoc) else: speak(The i n i file was not found. Now generating a default file.) writeDefaults() #end except #now get the settings general=ini[general] printMessages=general[printMessages] speech=general[useSpeech] rnd=int(general[roundTo]) mode=int(general[defaultStartMode]) enable=ini['enableModes'] rmOn=enable[resourceMonitor] weatherOn=enable[weather] networkOn=enable[network] aop=ini[armOptions] #the options section drive1=aop[primaryDrive] drive2=aop[secondaryDrive] ramInfo=aop[ramInfo] driveInfo=aop[driveInfo] useCurrentDrive=aop[currentIsPrimary] CIsSecondary=aop[CIsSecondary] wop=ini[weatherOptions] zip=str(wop[location]) subs=ini[templates] #short for substitute corePercent=Template(subs[sayCorePercentage]) avg=Template(subs[sayProcessorAverage]) sayRamFree=Template(subs[sayRamFree]) sayRamUsed=Template(subs[sayRamUsed]) sayDriveFree=Template(subs[sayDriveFree]) sayDriveUsed=Template(subs[sayDriveUsed]) currentWeather=Template(subs[currentWeather]) forecast=Template(subs[forecast]) def setOptions(): global ini try: ini.reload() except: speak(The i n i file was not found. Now generating a default file.) writeDefaults() #end except #now just a few settings that depend on the options #if useCurrentDrive is true, get that drive and set drive1 to it if(useCurrentDrive==True): #get current drive and assign it to primary currentDrive=os.path.splitdrive(sys.argv[0])[0] drive1=currentDrive #furthermore, if CIsSecondary is true, set that here, #since useCurrentDrive must be true for this one to take effect if(CIsSecondary==True): drive2=c: #endif #endif #end def def writeDefaults(): global ini global iniLoc ini.filename=iniLoc ini[general]=general ini[enableModes]=enableModes ini[weatherOptions]=weatherOptions ini[armOptions]=armOptions ini[templates]=templates #create the new file ini.write() #end def def speakSetOptions(): #just calls setOptions, but I use this separate def so I can speak a reloaded msg setOptions() speak(Settings reloaded.) #end def -- Have a great day, Alex (msg sent from GMail website) mehg...@gmail.com; http://www.facebook.com/mehgcap -- http://mail.python.org/mailman/listinfo/python-list
Re: does this exception mean something else?
On Thu, May 6, 2010 at 5:33 PM, Alex Hall mehg...@gmail.com wrote: Hi all, I have a file, pasted below for what good it will do, which makes a couple conditional calls to a function called writeDefaults. However, when I manually trigger a condition that causes the function to be called, Python gives me a name error and says that my writeDefaults does not exist. I am sure that it does, as I copied the text from the call and pasted it into the text search of my editor, and it found the line that defines the function, so the text in the call is obviously correct. You're calling the function before you've defined it; that doesn't work in Python. `def`s are executable statements, not declarations like in statically-typed languages. Move the def writeDefaults(): and its body so it comes before the #first, load the ini file setting code. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: does this exception mean something else?
Alex Hall wrote: Hi all, I have a file, pasted below for what good it will do, which makes a couple conditional calls to a function called writeDefaults. However, when I manually trigger a condition that causes the function to be called, Python gives me a name error and says that my writeDefaults does not exist. I am sure that it does, as I copied the text from the call and pasted it into the text search of my editor, and it found the line that defines the function, so the text in the call is obviously correct. Sometimes, especially in a language like Javascript, one error can mean something else or point to a problem other than the one it claims to be about, so I wondered if the same thing could be happening here? The error is on line 55, just after the if statement that uses os.path.exists to ensure my ini file is really there. TIA! BTW, speech is just a file that will output a given message through an active screen reader; changing all calls to print will work just as well. You need to remember that 'def' and 'class' are _statements_, not declarations; they have the side-effect of adding a name to the current namespace. A Python script is run from the first line. If/when Python reaches line 55 the 'def' statement that defines the function 'writeDefaults' hasn't been run yet. BTW, you should avoid 'bare excepts' (an except clause that doesn't say which exception to catch) because they catch _every_ exception, even KeyboardInterrupt and NameError. You might also want to look at PEP 8: http://www.python.org/dev/peps/pep-0008/ which is the Style Guide for Python code. -- http://mail.python.org/mailman/listinfo/python-list
Re: does this exception mean something else?
On 5/6/2010 8:33 PM, Alex Hall wrote: Hi all, I have a file, pasted below for what good it will do, which makes a couple conditional calls to a function called writeDefaults. However, when I manually trigger a condition that causes the function to be called, Python gives me a name error and says that my writeDefaults does not exist. It does not, when you try to call it, because the call occurs before you define it. Python code is executed top to bottom and function defines are executable statements. I suspect that you have used some other language that operates differently. ... #first, load the ini file setting #so where is the ini? script_path/config.ini, of course! iniLoc=helpers.progdir+'\\config.ini' iniLoc=r'c:\arm\config.ni' #set up the ini object to read and write settings if(os.path.exists(iniLoc)): ini=ConfigObj(iniLoc) else: speak(The i n i file was not found. Now generating a default file.) writeDefaults() #end except writeDefaults is not yet defined ... def writeDefaults(): global ini global iniLoc ini.filename=iniLoc ini[general]=general ini[enableModes]=enableModes ini[weatherOptions]=weatherOptions ini[armOptions]=armOptions ini[templates]=templates #create the new file ini.write() #end def Now it is. Move you def statements up closer to the top of the file. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: does this exception mean something else?
Changing the order so the function is first did it. Thanks. That is what I get for working with Java all semester... On 5/6/10, MRAB pyt...@mrabarnett.plus.com wrote: Alex Hall wrote: Hi all, I have a file, pasted below for what good it will do, which makes a couple conditional calls to a function called writeDefaults. However, when I manually trigger a condition that causes the function to be called, Python gives me a name error and says that my writeDefaults does not exist. I am sure that it does, as I copied the text from the call and pasted it into the text search of my editor, and it found the line that defines the function, so the text in the call is obviously correct. Sometimes, especially in a language like Javascript, one error can mean something else or point to a problem other than the one it claims to be about, so I wondered if the same thing could be happening here? The error is on line 55, just after the if statement that uses os.path.exists to ensure my ini file is really there. TIA! BTW, speech is just a file that will output a given message through an active screen reader; changing all calls to print will work just as well. You need to remember that 'def' and 'class' are _statements_, not declarations; they have the side-effect of adding a name to the current namespace. A Python script is run from the first line. If/when Python reaches line 55 the 'def' statement that defines the function 'writeDefaults' hasn't been run yet. BTW, you should avoid 'bare excepts' (an except clause that doesn't say which exception to catch) because they catch _every_ exception, even KeyboardInterrupt and NameError. You might also want to look at PEP 8: http://www.python.org/dev/peps/pep-0008/ which is the Style Guide for Python code. -- http://mail.python.org/mailman/listinfo/python-list -- Have a great day, Alex (msg sent from GMail website) mehg...@gmail.com; http://www.facebook.com/mehgcap -- http://mail.python.org/mailman/listinfo/python-list