Re: [Tutor] mod_python mysqldb problem
On Thu, 23 Feb 2006, Patty wrote: def addpercent(mp, lp): conn = MySQLdb.connect(host = localhost, user = root, passwd = ,db =my_db) cursor = conn.cursor() cursor.execute ( UPDATE targets SET mario_percent = %d, lizard_percent = %d WHERE target_name = 'anyname' , (mp, lp)) db.commit() cursor.close() db.close() Hi Patty, Does this code work outside of the context of mod_python? There's something slightly suspicious here in the use of the '%d' format string: I'm not sure MySQLdb will accept it. Let me check... According to the DB API on parameter styles: paramstyle String constant stating the type of parameter marker formatting expected by the interface. Possible values are [2]: 'qmark' Question mark style, e.g. '...WHERE name=?' 'numeric' Numeric, positional style, e.g. '...WHERE name=:1' 'named' Named style, e.g. '...WHERE name=:name' 'format'ANSI C printf format codes, e.g. '...WHERE name=%s' 'pyformat' Python extended format codes, e.g. '...WHERE name=%(name)s' (http://www.python.org/peps/pep-0249.html) So '%s' acts as a placeholder --- a parameter marker --- where MySQLdb will later substitute parameters into. So I'm not certain that '%d' will work properly. In any case, you should never get a segfault in Python code, so something strange is happening. Try seeing if your code works outside of mod_python first; that'll give us at least a little more assurance that it isn't MySQLdb that's doing funny things. You might also want to talk with the mod_python folks, since they'll probably be able to give you ideas on how to get better debugging output here. Good luck to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] First Try 1.2
Hi John, Its developing nicely so I'll add some more style issue comments :-) import random # store the /home/mutt/lotto.txt in f f = file('/home/mutt/lotto.txt','w') # create or replace lotto.txt in my home directory f.write('Here are your numbers:\n\n') I'd move the middle comment up with the one above. Think of a block of comments commenting on a paragraph of code, rather than dealing in single lines: # store the /home/mutt/lotto.txt in f # create or replace lotto.txt in my home directory f = file('/home/mutt/lotto.txt','w') f.write('Here are your numbers:\n\n') Now the code is easier sepsarated from the comment. BUT, is the comment really needed, after all the explicit pathname shows whgere it is going and the variable tells us where we are storing it. How much of: f = file('/home/mutt/lotto.txt','w') f.write('Here are your numbers:\n\n') will be hard to understand if you come back in a few months? Comments are best used to explain the *why* of code and let good variable names and structure describe the how. A concept often called Self documenting code # user input for number of games to generate how_many_games = int(raw_input('How many games you would like generated? ')) # user input for number of balls (to allow different lotto games) number_of_balls = int(raw_input('\nHow many balls are in the lotto you wish to play? ')) # user input for how many numbers per game (to allow systems entries) how_many_numbers = int(raw_input('\nHow many numbers you would like per game? ')) # check if there is a power ball chk_pwrball = raw_input('Is there a power ball? (y/n) ') how_many_games = int(raw_input('How many games you would like generated? ')) number_of_balls = int(raw_input('\nHow many balls are in the lotto you wish to play? ')) how_many_numbers = int(raw_input('\nHow many numbers you would like per game? ')) chk_pwrball = raw_input('Is there a power ball? (y/n) ') Again comparing these two do the comments add much more information than the variable names do? And by removing them the code itself becomesa much more obvious and readable. print '\n\nHere are your numbers : \n' # loop for the number of games selected by user for game in range(1, how_many_games + 1): # generate 6 random numbers between 1 and 45 inclusive then sort them lotto_numbers = random.sample(range(1,number_of_balls + 1), how_many_numbers) lotto_numbers.sort() if chk_pwrball == 'n': # Right justified in 3 character width then a tab (\t) then a blank line (\n) print '%3s\t%s\n' % (game, lotto_numbers) # write the numbers to lotto.txt save_numbers = 'Game: %3s\t%3s\n' % (game, lotto_numbers) f.write(save_numbers) The commenting in this section is better spaced but again mostly just says what the code does. The comment explaining the string format is probably the most valid comment in the program, it describes what the format is trying to accomplish which is valid since format strings are not self evident. if chk_pwrball == 'y': pwrball = random.sample(range(1,number_of_balls +1), 1) print '%3s\t%s \tPower Ball: %s\n' % (game, lotto_numbers, pwrball) save_numbers = 'Game: %3s\t%s \tPower Ball: %s\n' % (game, lotto_numbers, pwrball) f.write(save_numbers) print '\nHope you win!' f.write('\nHope you win!') f.close() And interesting that you (coprrectly) chose not to comment this sectoon at all Don't take these comments too personally but it is just a classic case of how you have improved the code clarity to the point where the comments, which were originally necessary, are now superfluous! This is a common mistake for beginners so I thought I'd take the opportunity to highlight the issue: Comments are useful but clearly written code is much better Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Does casting exist in Python?
Subject: [Tutor] Does casting exist in Python? Does it? That depends on what you mean by casting. A cast in C literally telly the compiler to treat one piece of data as if it were another type of data. For example char c = 'a'; float f = (float)c; Tells C to take the character stored in c and treat it as a floating point number. It does not change the type of c it merely treats the data as if it were another type. However in practice many people use casting as a type conversion operation and think of it in that light - which often leads to strange and subtle bugs! Python does do type conversion by applying conversion functions such as str(), int(), float() list() etc [Actually I believe these are now callable types which is subtly different but I'll ignore that for now!] If you do want to do a cast (ie. re-interpret data) the only way that I know of is to use the struct module. That allows you to take a piece of data and write it as a byte string, you can then reinterpret the byte string using a different format set and effectively the same byte sequence that started as a char will be read as a float! (there might be issues if the buyte string lengths don;t match) HTH, Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter, Frame and Canvas question.
Hi Hugo, some experiments showed, that the following code - using borderwidth=-2 - works as you desired, at least on my windows machine: import Tkinter from Tkconstants import * class App(Tkinter.Frame): def __init__(self, master=None): Tkinter.Frame.__init__(self, master) self.pack() canvas1 = Tkinter.Canvas(self, background='#00', borderwidth=-2) canvas2 = Tkinter.Canvas(self, background='#00', borderwidth=-2) canvas1.pack(pady=0) canvas2.pack(pady=0) if __name__ == '__main__': myapp = App() myapp.mainloop() Alas, there is some strange anomaly: it only does so (i. e. doesn't display that gray bar between the canvases) when run from IDLE or by double-clicking the renamed file test.pyw or run from the command window with the command: python test.py. Double-clicking test.py performs differently(!) and still shows the border between the two canvases. Who understands? (Or do you think my python installation is in a corrupted state?) Regards, Gregor Hugo González Monteverde schrieb: Hi all, I'm running into trouble displaying some Tkinter Canvases, they keep a border between themselves and there's no way I can have them display without a grey gap between them. I've narrowed the problem to the following example. I've tried all kind of padding and border parameter to the Frame's pack() method and the Canvas' pack() method. Is there something I'm missing or plainly not understanding? I'd like to display canvases one next to the other without some kind of background showing throug. Thanks for taking a look, here's the example: === import Tkinter from Tkconstants import * class App(Tkinter.Frame): def __init__(self, master=None): Tkinter.Frame.__init__(self, master) self.pack(pady=0, ipady=0) canvas1 = Tkinter.Canvas(self, background='#00', borderwidth=0) canvas2 = Tkinter.Canvas(self, background='#00', borderwidth=0) canvas1.pack(pady=0) canvas2.pack(pady=0) if __name__ == '__main__': myapp = App() myapp.mainloop() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Gregor Lingl Reisnerstrasse 3/19 A-1030 Wien Telefon: +43 1 713 33 98 Mobil: +43 664 140 35 27 Website: python4kids.net ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] mod_python mysqldb problem
Hi Danny, I downloaded the latest version of mysqldb and my code worked. btw, I changed it to %s instead of %d. Thanks! Patty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter, Frame and Canvas question.
Alas, there is some strange anomaly: it only does so (i. e. doesn't display that gray bar between the canvases) when run from IDLE or by double-clicking the renamed file test.pyw or run from the command window with the command: python test.py. Double-clicking test.py performs differently(!) and still shows the border between the two canvases. Who understands? (Or do you think my python installation is in a corrupted state?) No, it does the same on my 2.3 installation in windows. I will test on Linux. Actually, it displays the grey border, but if you scroll any window in front of it, everything except those borders are redrawn... (!) I'll check what it does on Linux and then report back... Thanks, Hugo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] mod_python mysqldb problem
I downloaded the latest version of mysqldb and my code worked. btw, I changed it to %s instead of %d. Hi Patty, That's great news! Does the whole thing work now, including the integration with mod_python? I'm following up and making sure that the problem was simply the stale copy of MySQLdb. Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Locking a file in linux with Python
Danny and Hugo,Thanks that really explains it better on what was going on. I may try renaming the file to see if I can get around it that way. -- Pat Martin ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor