[Tutor] getattr help
What is the purpose of getattr? Why not just use help or am I completely misunderstanding this? >From what I read, getattr allows you to get a reference to a function without >knowing its name until runtime. However, the example provided is: li = ['larry', 'curly] getattr(li, 'pop') It seems to me that I need to know the name of the function to use getattr? Please help. Sent from my iPhone ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Floating point error in tkinter
On 09/03/12 13:09, Válas Péter wrote: flood my screen and it won't work. Dou you think this is a Python bug? It's probably not a Python bug but a Tcl/Tk bug. It would be worth asking about it on a Tcl/Tk forum - or maybe even the Tkinter list. File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview res = self.tk.call(self._w, 'yview', *args) TclError: expected floating-point number but got "0,0033" Note that this error comes from Tcl not Python. Tcl/Tk often evaluates strings as a float but may well be expecting a decimal period rather than a comma in the string. (It really just uses the float as an encoding for two integers!) Can you hard code the period in your code? Or trap the string and do a replace() to convert it before it calls the Tkinter code? On the other hand it could just be that it expects a literal float and not a string representation of the same... With no sight of the original Python code it's hard to say. But Tk uses string floats often enough that I suspect that it is a Tcl/Tk issue. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Floating point error in tkinter
On Fri, Mar 09, 2012 at 02:09:23PM +0100, Válas Péter wrote: > Hi, > > I use a tkinter-based editor that ran properly earlier on English Windows > XP. Now I use Hungarian Windows 7 with Python 2.7.2. You must know that in > Hungary decimal fractions are marked with a decimal comma, not a dot (e.g. > pi=3,1415...). > > I suspect it somehow gets a Hungarian decimal from Windows and can't > understand it. So when I page up and down with PgUp/PgDn buttons, it works > well. When I use the wheel on mouse, it's OK, too. But when I try to use > the vertical scrollbar of the window with mouse, error messages flood my > screen and it won't work. Dou you think this is a Python bug? What makes you think it is a Python bug? Is there something that makes you expect that Tkinter should accept strings with commas instead of floats? The error message is pretty clear: tkinter is expecting a float, but receiving a string instead. TclError: expected floating-point number but got "0,0016" Unless there is documentation that says that it will accept strings with commas, this is not a Python bug. It may be a bug in the editor, but not in the Python language. By the way, it seems that a lot of other code has this same problem. You might find that running this: import locale locale.setlocale(locale.LC_ALL, 'english_us') just before the editor starts up might help: http://forums.arcgis.com/threads/27427-arcpy-and-easygui-eventloop-conflict -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Floating point error in tkinter
Hi, I use a tkinter-based editor that ran properly earlier on English Windows XP. Now I use Hungarian Windows 7 with Python 2.7.2. You must know that in Hungary decimal fractions are marked with a decimal comma, not a dot (e.g. pi=3,1415...). I suspect it somehow gets a Hungarian decimal from Windows and can't understand it. So when I page up and down with PgUp/PgDn buttons, it works well. When I use the wheel on mouse, it's OK, too. But when I try to use the vertical scrollbar of the window with mouse, error messages flood my screen and it won't work. Dou you think this is a Python bug? Shall I make a bugreport or just some config setting is missing? Exception in Tkinter callback Traceback (most recent call last): File "c:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__ return self.func(*args) File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview res = self.tk.call(self._w, 'yview', *args) TclError: expected floating-point number but got "0,0016" Exception in Tkinter callback Traceback (most recent call last): File "c:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__ return self.func(*args) File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview res = self.tk.call(self._w, 'yview', *args) TclError: expected floating-point number but got "0,0033" Exception in Tkinter callback Traceback (most recent call last): File "c:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__ return self.func(*args) File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview res = self.tk.call(self._w, 'yview', *args) TclError: expected floating-point number but got "0,0049" Exception in Tkinter callback Traceback (most recent call last): File "c:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__ return self.func(*args) File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview res = self.tk.call(self._w, 'yview', *args) TclError: expected floating-point number but got "0,0066" Exception in Tkinter callback Traceback (most recent call last): File "c:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__ return self.func(*args) File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview res = self.tk.call(self._w, 'yview', *args) TclError: expected floating-point number but got "0,0082" Exception in Tkinter callback Traceback (most recent call last): File "c:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__ return self.func(*args) File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview res = self.tk.call(self._w, 'yview', *args) TclError: expected floating-point number but got "0,0098" Exception in Tkinter callback Traceback (most recent call last): File "c:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__ return self.func(*args) File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview res = self.tk.call(self._w, 'yview', *args) TclError: expected floating-point number but got "0,0098" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about writing to Excel with slavic characters
Hi Walter It is as you say. Thanks for long explanation. I am using the newer version. Now I also understand difference between single underscore and double underscore. I would still have problems if I would want to programme them for instance. Well I always try to be independent and I want to answer the questions by myself. Learning the tutorial by heart is also not my way. Maybe I need just to understand it better and understand better the whole philosophy of private and public methods. And what is __init__ method and __enter__ method and so on. I was just asking of that getNumberofVariables() method, because I wanted to run the methods my self and see if I get the labels I wanted by myself. I wouldn't want to ask the author directly does this programme does this or that because that might be silly questions because the answer would be, 'well of course it does, run this and this method' and you will see. I don't want to consume too much time of the people. So I wanted to check first. As an amateur as I am. Nobody really trained me how to programme except for in the first years at the faculty some 10 years ago, but at that time I was not that interested in programming. Now we are resolving the issue directly with Albert. Have a nice day, Marko On Fri, Mar 9, 2012 at 11:49 AM, Walter Prins wrote: > Hi Marko, > > On 9 March 2012 08:34, Marko Limbek wrote: >> File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py", >> line 715, in >> mySavReaderObject.getNumberofVariables(savFileName, >> mySavReaderObject.fh, mySavReaderObject.spssio) >> AttributeError: 'SavReader' object has no attribute 'spssio' >> >> So the methods must really be somewhat internal. > > Python doesn't enforce access levels like some other languages do > which means effectively, any member of any object can in principle be > accessed. By "gentlemans agreement", members with a name starting > with a single underscore are supposed to be considered private > (although you can still ignore this agreement and access them anyway) > while members with double underscores get some behind the scenes > assistance to ensure privateness and name uniqueness via "name > mangling". (There's a couple of other minor behavioural differences, > but the point stands -- there's no preventing you as programmer from > accessing "private" members of a class if you insist to do so. But > then it's doubly your problem if that gets you into trouble ;) ) > > Anyway, the message then really means what it says -- The SavReader > object instance you're using really does not have an spssio member > (there should be no problem accessing it if it was there so I must > interpret that message to mean what it says.) I'm not sure why this > would be the case -- perhaps we're not looking/using the same version > of the reader class and the member name has changed? (I previously > guessed/assumed that you were using the following version, or > something close enough to it, given here: > http://code.activestate.com/recipes/577650-python-reader-for-spss-sav-files/ > Looking back I see you're actually using a slightly newer version > from here: > http://code.activestate.com/recipes/577811-python-reader-writer-for-spss-sav-files-linux-mac-/ > But, everything I've said appears to still hold true about the updated > version, so can you clarify which version you're currently using?) > > Regardless, the error simple enough to troubleshoot -- have a look at > *your* version of the SavReader class, and find out what the member > name should in fact be. There are calls to e.g. > getNumberofVariables() in the class itself, from which you can > determine what the object data/field/variable member is that holds the > value to pass to the spssio parameter of getNumberofVariables(). > > But, I have to repeat: The value that you get from > getNumberofVariables() is the exact same value that you get inside of > the numVars variable after calling: > > numVars, nCases, varNames, varTypes, printTypesFile, > printTypeLabels, varWids = mySavReaderObj.getSavFileInfo() > > You can see this will be the case if you read the code: > 1) The __init__ method assigns self.numVars_ from the result of > calling self._readBasicSavFileInfo(). > 2) This in turn calls on self.getNumberofVariables() as follows: > numVars = self.getNumberofVariables(self.fh, self.spssio)[1] > ... and that local variable numVars is what is returned and ends up in > the object member self.numVars_. > 3) Then, looking at getSavFileInfo() you can see that it in turn > simply returns self.numVars_, > 4) In other words it returns the same value that it previously > retrieved using self.getNumberofVariables(). > > So, the 2 ways are functionally identical w.r.t. the retrieval of the > "NumberofVariables". The only differences are that a) with the latter > call you get a bunch of other stuff besides the number of variables, > and b) With the latter call you don't have to worry about spssio or fh > parameters
Re: [Tutor] Question about writing to Excel with slavic characters
Hi Marko, On 9 March 2012 08:34, Marko Limbek wrote: > File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py", > line 715, in > mySavReaderObject.getNumberofVariables(savFileName, > mySavReaderObject.fh, mySavReaderObject.spssio) > AttributeError: 'SavReader' object has no attribute 'spssio' > > So the methods must really be somewhat internal. Python doesn't enforce access levels like some other languages do which means effectively, any member of any object can in principle be accessed. By "gentlemans agreement", members with a name starting with a single underscore are supposed to be considered private (although you can still ignore this agreement and access them anyway) while members with double underscores get some behind the scenes assistance to ensure privateness and name uniqueness via "name mangling". (There's a couple of other minor behavioural differences, but the point stands -- there's no preventing you as programmer from accessing "private" members of a class if you insist to do so. But then it's doubly your problem if that gets you into trouble ;) ) Anyway, the message then really means what it says -- The SavReader object instance you're using really does not have an spssio member (there should be no problem accessing it if it was there so I must interpret that message to mean what it says.) I'm not sure why this would be the case -- perhaps we're not looking/using the same version of the reader class and the member name has changed? (I previously guessed/assumed that you were using the following version, or something close enough to it, given here: http://code.activestate.com/recipes/577650-python-reader-for-spss-sav-files/ Looking back I see you're actually using a slightly newer version from here: http://code.activestate.com/recipes/577811-python-reader-writer-for-spss-sav-files-linux-mac-/ But, everything I've said appears to still hold true about the updated version, so can you clarify which version you're currently using?) Regardless, the error simple enough to troubleshoot -- have a look at *your* version of the SavReader class, and find out what the member name should in fact be. There are calls to e.g. getNumberofVariables() in the class itself, from which you can determine what the object data/field/variable member is that holds the value to pass to the spssio parameter of getNumberofVariables(). But, I have to repeat: The value that you get from getNumberofVariables() is the exact same value that you get inside of the numVars variable after calling: numVars, nCases, varNames, varTypes, printTypesFile, printTypeLabels, varWids = mySavReaderObj.getSavFileInfo() You can see this will be the case if you read the code: 1) The __init__ method assigns self.numVars_ from the result of calling self._readBasicSavFileInfo(). 2) This in turn calls on self.getNumberofVariables() as follows: numVars = self.getNumberofVariables(self.fh, self.spssio)[1] ... and that local variable numVars is what is returned and ends up in the object member self.numVars_. 3) Then, looking at getSavFileInfo() you can see that it in turn simply returns self.numVars_, 4) In other words it returns the same value that it previously retrieved using self.getNumberofVariables(). So, the 2 ways are functionally identical w.r.t. the retrieval of the "NumberofVariables". The only differences are that a) with the latter call you get a bunch of other stuff besides the number of variables, and b) With the latter call you don't have to worry about spssio or fh parameters (because they're absent/not required when calling getSavFileInfo() and c) with the latter call the actual retrieval of the number of variables happened slightly earlier on when the SavReader object was created, while with the direct call to getNumberofVariables() it is presumably read again directly from the file. So, I think you need to stop fixating on the getNumberofVariables() method as it's not, I suspect, the solution to your real problem like you seem to think, and it is also introducing a distraction (the parameters issue) since it's not really the intended way for you to use this class. (Not that you absolutely cannot use it if you're determined to do so, as I've already tried to explain, but it's just probably just easier to go with the intended means of use for now given that there's functionally no difference in the result up to this point, at least that I can see.) Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python using RXVT vs Konsole?
On 09/03/12 02:07, brandon w wrote: I am able to up-arrow to get the last typed command using rxvt but when I use konsole and I press the up-arrow I get the symbols: ^[[A Why is that? Your terminal settings look like they are messed up. Does up arrow work in any other applications? emacs or vim for example? If not you will need to configure your termcap/terminfo or stty settings I suspect. If it does work in other apps then I don't know what's happening! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about writing to Excel with slavic characters
Hi Walter I understand, thank you. Maybe I am trying to do what is not meant to be done. I tried as you suggested mySavReaderObject = SavReader(savFileName) mySavReaderObject.getNumberofVariables(savFileName, mySavReaderObject.fh, mySavReaderObject.spssio) but it won't work File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py", line 715, in mySavReaderObject.getNumberofVariables(savFileName, mySavReaderObject.fh, mySavReaderObject.spssio) AttributeError: 'SavReader' object has no attribute 'spssio' So the methods must really be somewhat internal. The information that I get in numVars, nCases, varNames, varTypes, printTypesFile, printTypeLabels, varWids is not sufficient to me. What I need from this programme are the labels that are in the column 'Values' in SPSS 'Variable view'. What is the most important is that unicode characters like 'č', 'š', 'ž', in the labels can be read. This is the only reason why I am touching this programme. I will forward the question to Albert. Thank you, Marko On Thu, Mar 8, 2012 at 5:18 PM, Walter Prins wrote: > Hi Marko, > > I'm going out on a limb here as I know next to nothing about either > SPSS or Albert-Jan's wrapper module, and so with that caveat, some > comments/observations: > > On 8 March 2012 14:59, Marko Limbek wrote: >> I overcame commenting. I managed to read my own file and print it. Now >> I am trying to use method getNumberofVariables() but unsuccesfully. >> First way >> >> SavReader(savFileName).getNumberofVariables() >> >> gives me this error >> >> Traceback (most recent call last): >> File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py", >> line 660, in >> SavReader(savFileName).getNumberofVariables() >> TypeError: getNumberofVariables() takes exactly 3 arguments (1 given) >> >> >> >> Second way >> >> getNumberofVariables(savFileName, fh, spssio) >> >> gives me this error >> >> Traceback (most recent call last): >> File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py", >> line 660, in >> getNumberofVariables(savFileName, fh, spssio) >> NameError: name 'getNumberofVariables' is not defined > > The short answer: > == > Don't use getNumberofVariables, use the script as demonstrated at the > bottom of the script in the "if __name__ == "__main__" section. I > suppose something like this should do (I've modified it slightly to > make it a bit more obvious what's happening): > > ## - Get some basic file info > savFileName = r"C:\Program > Files\IBM\SPSS\Statistics\19\Samples\English\Employee data.sav" > mySavReaderObj = SavReader(savFileName) > numVars, nCases, varNames, varTypes, printTypesFile, printTypeLabels, > varWids = \ > mySavReaderObj.getSavFileInfo() > #Now the number of variables is presumably in numVars... > > > The longer answer: > == > Firstly, getNumberofVariables() is defined as a class instance method. > This means you can only ever call it on an object instance of that > class. It is not a standalone function that can be called freestanding > as you tried to do in your latter attempt. That is why you got the > "not defined" error -- There is no freestanding function by that name > (even if there happens to be a /method/ by that name inside the > SavReader class.) so Python complained as such. > > Your prior attempt, which as you noticed requires to have parameters > fh and spssio supplied appears to me to be effectively somewhat of an > internal/private/helper method that is written in the style of a > function, so consequently the method has no external dependencies on > any state in the object/class and hence has to have the spssio and fh > supplied when it's called. You can see elsewhere in the class when > this method is called, the object itself keeps track of the fh and the > sspsio and so passes these into the method as required. > > >From this you can infer that you'd be able to do the same and thus > successfully call getNumberofVariables() by retrieving the fh and > sspsio from your SavReader object (e.g. what I called mySavReaderObj > above), by passing in mySavReaderObj.fh and mySavREaderObj.spssio when > calling mySavReaderObj.getNumberofVariables(). But, as per the short > answer, you probably don't want to do that. > > As an aside it also then follows it would be possible to > rewrite/refactor the getNumberofVariables() method (and indeed several > others) to remove the fh and sspsio parameters, by having them picked > up directly from the object instance when required. Debateably, it > might be an improvement that makes the code a bit more object oriented > and less surprising to use. > > HTH, > > Walter > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http: