Re: [Tutor] ElementTree: finding a tag with specific attribute
Kent Johnson wrote: > I looked at this again and there is a bug in BS that causes this > behaviour. It's kind of an interesting bug that is a side-effect of > the way BS uses introspection to access child tags. There is a new release of BS that fixes this problem and one Danny found recently (broken fetch). http://www.crummy.com/software/BeautifulSoup/index.html Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ElementTree: finding a tag with specific attribute
Thanks a lot everyone for this! Glad I could help debug BS! Bernard On 9/19/05, Kent Johnson <[EMAIL PROTECTED]> wrote: > Kent Johnson wrote: > > I looked at this again and there is a bug in BS that causes this > > behaviour. It's kind of an interesting bug that is a side-effect of > > the way BS uses introspection to access child tags. > > There is a new release of BS that fixes this problem and one Danny found > recently (broken fetch). > http://www.crummy.com/software/BeautifulSoup/index.html > > Kent > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ElementTree: finding a tag with specific attribute
Now I can replace my my Kent-and-Danny patched version :)On 9/19/05, Bernard Lebel <[EMAIL PROTECTED]> wrote: Thanks a lot everyone for this! Glad I could help debug BS!Bernard On 9/19/05, Kent Johnson <[EMAIL PROTECTED]> wrote:> Kent Johnson wrote:> > I looked at this again and there is a bug in BS that causes this> > behaviour. It's kind of an interesting bug that is a side-effect of > > the way BS uses introspection to access child tags.>> There is a new release of BS that fixes this problem and one Danny found recently (broken fetch).> http://www.crummy.com/software/BeautifulSoup/index.html>> Kent>> ___> Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor>___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IRC Client Trouble -- too many new lines
Hi, Liam Clarke-Hutchinson wrote: Hi Joseph, while (1): buffer = Data.IRC.recv(1024) msg = string.split(buffer) Just a warning, the string module will be removed/deprecated come Py3K. Better to use - buffer.split() nick_name = msg[0][:msg[0].find("!")] filetxt.write(nick_name.lstrip(':') + ' -> ' + message.lstrip(':') + '\n') Ok. Thanks for the heads up. As to your printing problem, I'm not sure if you're referring to - print msg[print_msg] Oh, sorry. I'm refering to the print msg[print_msg] And I'm not sure if you want it to print without an additional newline, or with a newline. Well every word is on it's own new line (I get a huge message from the IRC server) and I'd like a word wrap or something to fix that. #Incidentally, it's a bit simpler to maintain this kinda loop #instead of a while loop. for item in msg: item.rstrip() #Will strip whitespace (\t\r\n etc.) by default print item Ah. Thanks If you're wanting to print without the newline that print adds, why not do a join like this one? message = ' '.join(msg[3:]) print ' '.join(msg) ? Thanks again. This is proving very usefull. What output you're expecting I'm expecting a large amount of text without new lines for each word, and possible word wrapping. What output you're getting Example: this is the type of message I'm getting and as you can see , it's really annoying! PS Your code is interesting, I've never dealt with the IRC protocol before,so it's good to see a demonstration of it. I edited the code from a Python Cookbook recipe. I may toddle off and check out that RFC. Yes. Some people on a forum that I hang oput with suggested a chat room. A freind of mine suggested IRC and python. He's doing the server and I'm doing the client. I'm testing the client on freenode.. only thing I could think of at the time. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ElementTree: finding a tag with specific attribute
Well I have just retested BS with my XML file and now it's much, much faster (I would say instantaneous). Bernard On 9/19/05, Kent Johnson <[EMAIL PROTECTED]> wrote: > Kent Johnson wrote: > > I looked at this again and there is a bug in BS that causes this > > behaviour. It's kind of an interesting bug that is a side-effect of > > the way BS uses introspection to access child tags. > > There is a new release of BS that fixes this problem and one Danny found > recently (broken fetch). > http://www.crummy.com/software/BeautifulSoup/index.html > > Kent > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] tuples and mysqldb tables (fwd)
[Again forwarding to tutor. Ed, please keep tutor@python.org in CC; otherwise, your questions might lose themselves in my mailbox. I don't want to be a bottleneck in your learning.] -- Forwarded message -- Date: Mon, 19 Sep 2005 00:06:13 -0400 From: Ed Hotchkiss <[EMAIL PROTECTED]> To: Danny Yoo <[EMAIL PROTECTED]> Subject: Re: [Tutor] tuples and mysqldb tables Thanks for the debugging help :P - I've edited the error handling line, and defined the port_counter above, but I am still not getting any output. I have inserted print "scanned: ",port_counter,"\n" into the thread, so that should at least print to screen each time the thread is ran, regardless of the port being open or closed. Any ideas why the thread itself though is not working? A cleaner view of the code is here: http://deadbeefbabe.org/paste/1672?_nevow_carryover_=1127102735.7127.0.0.10.809800804887 Thanks again for the error handling help! On 9/18/05, Danny Yoo <[EMAIL PROTECTED]> wrote: > > > > > def run(self): > > try: > > ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM) > > ss.connect((ip, port_counter)) > > print "%s | %d OPEN" % (ip, port_counter) > > ss.close() > > except: pass > > > Hi Ed, > > Yikes. Don't do that. *grin* > > By "that", I mean setting the exception handler here to do nothing. > Exception handling shouldn't abused to silence errors like that, at least, > not wholesale like that. At the very least, while we're debugging this, > import the 'traceback' module and at least give a heads up, like this: > > except: > traceback.print_exc() > > Once this is in place, expect to see errors. I know what you meant to do: > you wanted to ignore network errors, so try to make the exception handler > a little more specific in the exceptions it's trying to silence. > > The problem here is that the handler has been inadvertantely silencing a > legitimate NameError. From casual inspection, it's not clear what > 'port_counter' is. I suspect you want to add that as part of the thread's > state, in which case consider adding it within your thread's __init__ > method. > > > > def scan(ip, begin, end): > > for port_counter in range(begin, end): > > while threading < MAX_THREADS: > > scanThread().start() > > # end function --- > > > Ok, I definitely suspect port_counter now as the culprit here. Make sure > you create each thread with port_counter as a part of each thread's state. > > > [Aside: are you trying to a port scanner in the style that Jacob Matthews > wrote about here? > > http://www.kuro5hin.org/story/2004/3/17/93442/8657 > > Just curious.] > > > > Good luck to you! > > -- edward hotchkiss ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] More IDE's (was: Boa-Constructor)
Well, for starters. 1. PyDEV does not have any type of prespective. I use the java prespective or resources. 2. Check out this guide, granted it is a bit old(2003'ish) but it is still pretty good. http://www-128.ibm.com/developerworks/opensource/library/os-ecant/ 3. Terry, what do you mean by "other functions?" Are you talking about the ant/python extension libraries? 4. Denise, regarding the code-completion as long as you have the libs within the PYTHONPATH you should be able to generate the code completion. Do you have some specific example of a library that is not working for you? Cheers -george -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of ->Terry<- Sent: Saturday, September 17, 2005 12:07 AM To: D. Hartley Cc: tutor@python.org Subject: Re: [Tutor] More IDE's (was: Boa-Constructor) -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Today (Sep 16, 2005) at 5:29pm, D. Hartley spoke these wise words: - ->I know how to use the code completion (hooray for me!) but so far, - ->most everything else that's going on in PyDev/Eclipse is a mystery to - ->me. But I managed to get an interactive interpreter as an external - ->tool, so that's a lot of progress for me. - -> - ->Technically I suppose this is OT, but did you guys that have used/are - ->using PyDev/Eclipse use any kind of tutorials for those other - ->functions? (Not sure if there even *are* any, besides the standard - ->documentation on PyDev's website) Most of the things I've found on - ->eclipse are for much more advanced eyes than mine, it would seem. - -> - ->I'd appreciate any pointers! :) - -> - ->~Denise I too am having problems getting my head wrapped around Pydev/Eclipse. The docs on Eclipse don't seem to be geared toward Pydev and the FAQ on the Pydev site is rather sparse. At this point, I'm not even sure the install went properly since I see error messages such as: An internal error occurred during:"Pydev code completion:rebuilding modules" among others I didn't take the time to copy down. For now I am back to Gedit and an xterm. If you run across more documentation could you please cc me as well? Thanks, - -- Terry ,-~~-.___. Terry Randall / | ' \ < )0Linux Counter Project User# 98233 \_/, ,-' // / \-'~;/~~~(0) / __/~| / | If only Snoopy had Slackware... =( __| (| "He is your friend, your partner, your defender, your dog. You are his life, his love, his leader. He will be yours, faithful and true, to the last beat of his heart. You owe it to him to be worthy of such devotion."-- Unknown (Best viewed with a mono-spaced font.) -BEGIN PGP SIGNATURE- Version: GnuPG v1.2.7 (GNU/Linux) iD8DBQFDK5aDQvSnsfFzkV0RAv4YAJ4jPjR8z7X/afxscr+wt3eyeKcvnACeLfj9 TMyOGvnEYPqrZNQfQc67mBs= =0pNZ -END PGP SIGNATURE- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] tuples and mysqldb tables (fwd)
> Thanks for the debugging help :P - I've edited the error handling line, and > defined the port_counter above, but I am still not getting any output. I > have inserted print "scanned: ",port_counter,"\n" > into the thread, so that should at least print to screen each time the > thread is ran, regardless of the port being open or closed. Any ideas why > the thread itself though is not working? > A cleaner view of the code is here: > http://deadbeefbabe.org/paste/1672?_nevow_carryover_=1127102735.7127.0.0.10.809800804887 > Thanks again for the error handling help! Hi Ed, There are logically two possible areas where there might be a bug: there might be a problem in the scanThread class, or there might be a problem in scan. Or there could be problems in both. *grin* You're still running into some fundamentally basic problems dealing with variables and how variable scope works. See: http://www.freenetpages.co.uk/hp/alan.gauld/tutclass.htm and go through the examples there: it should help clarify some of the confusion you have about maintaining state in a class. As it stands, there is nothing in the class definition that knows about what 'ip' or 'port_counter' are: those two aren't parameters of the class or its methods, nor are they instance variables. > I have inserted print "scanned: ",port_counter,"\n" into the thread, so > that should at least print to screen each time the thread is ran, > regardless of the port being open or closed. The direct implication here is that scanThread.run() itself is NOT being executed. So there's also a problem in scan() that prevents the threads from getting started in the first place. I don't understand what you're trying to do with: while threading < MAX_THREADS: ... since 'threading' is the 'threading' module. Unfortunately, Python won't treat this as a TypeError, even though it really should be treated as such (what does it mean for a module to be "smaller" than something else?!) And MAX_THREADS is undefined. Are you getting any kind of error messages when you run your program? I'm a bit confused, because there's so many opportunities here for Python to tell you that there's a NameError here: are you seeing those error messages too? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Port scanner (was: tuples and mysqldb tables)
Danny Yoo wrote: > [Again forwarding to tutor. Ed, please keep tutor@python.org in CC; > otherwise, your questions might lose themselves in my mailbox. I don't > want to be a bottleneck in your learning.] > > -- Forwarded message -- > Date: Mon, 19 Sep 2005 00:06:13 -0400 > From: Ed Hotchkiss <[EMAIL PROTECTED]> > To: Danny Yoo <[EMAIL PROTECTED]> > Subject: Re: [Tutor] tuples and mysqldb tables > > Thanks for the debugging help :P - I've edited the error handling line, and > defined the port_counter above, but I am still not getting any output. I > have inserted print "scanned: ",port_counter,"\n" > into the thread, so that should at least print to screen each time the > thread is ran, regardless of the port being open or closed. Any ideas why > the thread itself though is not working? Do you get an error message printed? If so, it's helpful if you copy and paste the exact traceback into your email. > A cleaner view of the code is here: > http://deadbeefbabe.org/paste/1672?_nevow_carryover_=1127102735.7127.0.0.10.809800804887 You still have not defined port_counter in the scope of class scanThread. As Danny suggests, you should pass this value to the scanThread constructor and save it as an instance variable, then use the saved value in scanThread.run(). You might want to try making a non-threaded version of this program first - maybe you could write a function that tests a single port. Once you have that working then you can learn how to call it in a new thread. Kent > Thanks again for the error handling help! > > On 9/18/05, Danny Yoo <[EMAIL PROTECTED]> wrote: > >> >> >>>def run(self): >>>try: >>>ss = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >>>ss.connect((ip, port_counter)) >>>print "%s | %d OPEN" % (ip, port_counter) >>>ss.close() >>>except: pass >> >> >> >>Hi Ed, >> >>Yikes. Don't do that. *grin* >> >>By "that", I mean setting the exception handler here to do nothing. >>Exception handling shouldn't abused to silence errors like that, at least, >>not wholesale like that. At the very least, while we're debugging this, >>import the 'traceback' module and at least give a heads up, like this: >> >>except: >>traceback.print_exc() >> >>Once this is in place, expect to see errors. I know what you meant to do: >>you wanted to ignore network errors, so try to make the exception handler >>a little more specific in the exceptions it's trying to silence. >> >>The problem here is that the handler has been inadvertantely silencing a >>legitimate NameError. From casual inspection, it's not clear what >>'port_counter' is. I suspect you want to add that as part of the thread's >>state, in which case consider adding it within your thread's __init__ >>method. >> >> >> >>>def scan(ip, begin, end): >>>for port_counter in range(begin, end): >>>while threading < MAX_THREADS: >>>scanThread().start() >>># end function --- >> >> >>Ok, I definitely suspect port_counter now as the culprit here. Make sure >>you create each thread with port_counter as a part of each thread's state. >> >> >>[Aside: are you trying to a port scanner in the style that Jacob Matthews >>wrote about here? >> >>http://www.kuro5hin.org/story/2004/3/17/93442/8657 >> >>Just curious.] >> >> >> >>Good luck to you! >> >> > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] tuples and mysqldb tables (fwd)
Thanks for the link! It is EXACTLY what I have been looking for. When I used to use Flash a bit, and did some actionscript they had a similiar setup for OOP (Do all languages use OOP so similiarly?) which I never learned. I'm doing these examples, and now I'm going to try and rework the code from the beginning. Thanks again! -edward ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] More IDE's (was: Boa-Constructor)
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Reply in-line. Today (Sep 19, 2005) at 2:21pm, George Flaherty spoke these wise words: - ->Well, for starters. - -> - ->1. PyDEV does not have any type of prespective. I use the java prespective or resources. - -> - ->2. Check out this guide, granted it is a bit old(2003'ish) but it is still pretty good. - ->http://www-128.ibm.com/developerworks/opensource/library/os-ecant/ Thanks George, I'll check it out. - ->3. Terry, what do you mean by "other functions?" Are you talking about the ant/python extension libraries? I believe you meant Denise. - ->4. Denise, regarding the code-completion as long as you have the libs within the PYTHONPATH you should be able to generate the code completion. Do you have some specific example of a library that is not working for you? - -> - ->Cheers - ->-george - -> - -> - ->-Original Message- - ->From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of ->Terry<- - ->Sent: Saturday, September 17, 2005 12:07 AM - ->To: D. Hartley - ->Cc: tutor@python.org - ->Subject: Re: [Tutor] More IDE's (was: Boa-Constructor) - -> - -> - ->Today (Sep 16, 2005) at 5:29pm, D. Hartley spoke these wise words: - -> - ->->I know how to use the code completion (hooray for me!) but so far, - ->->most everything else that's going on in PyDev/Eclipse is a mystery to - ->->me. But I managed to get an interactive interpreter as an external - ->->tool, so that's a lot of progress for me. - ->-> - ->->Technically I suppose this is OT, but did you guys that have used/are - ->->using PyDev/Eclipse use any kind of tutorials for those other - ->->functions? (Not sure if there even *are* any, besides the standard - ->->documentation on PyDev's website) Most of the things I've found on - ->->eclipse are for much more advanced eyes than mine, it would seem. - ->-> - ->->I'd appreciate any pointers! :) - ->-> - ->->~Denise - -> - ->I too am having problems getting my head wrapped around Pydev/Eclipse. - ->The docs on Eclipse don't seem to be geared toward Pydev and the FAQ on the Pydev site is rather sparse. At this point, I'm not even sure the install went properly since I see error messages such as: - -> - ->An internal error occurred during:"Pydev code completion:rebuilding modules" - -> - ->among others I didn't take the time to copy down. For now I am back to Gedit and an xterm. If you run across more documentation could you please cc me as well? - -> - ->Thanks, - ->-- - ->Terry Cheers, - -- Terry ,-~~-.___. Terry Randall / | ' \ < )0Linux Counter Project User# 98233 \_/, ,-' // / \-'~;/~~~(0) / __/~| / | If only Snoopy had Slackware... =( __| (| "He is your friend, your partner, your defender, your dog. You are his life, his love, his leader. He will be yours, faithful and true, to the last beat of his heart. You owe it to him to be worthy of such devotion."-- Unknown (Best viewed with a mono-spaced font.) -BEGIN PGP SIGNATURE- Version: GnuPG v1.2.7 (GNU/Linux) iD8DBQFDLxquQvSnsfFzkV0RAsAoAKCEuQMWaFQTzL9b74VCSW5y0X9WogCeOR6/ NSSsxdyE/d8k/fUdHjBXkVA= =rZMF -END PGP SIGNATURE- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] OOP fundamentals
On Mon, 19 Sep 2005, Ed Hotchkiss wrote: > Thanks for the link! It is EXACTLY what I have been looking for. When I > used to use Flash a bit, and did some actionscript they had a similiar > setup for OOP (Do all languages use OOP so similiarly?) which I never > learned. Hi Ed, I believe that most things are fairly close among the languages that claim to support OOP. Just for comparison, let's see what this looks like in some other languages. For example, let's say we have a Person class: ### Python ### class Person(object): def __init__(self, name): self.name = name def sayHello(self): print "hello, my name is", name p = Person("ed") p.sayHello() ## This creates a Person class, who extends the behavior of the base 'object' class. This class has a bit of state to remember what the respective 'name' is. We also a method called 'sayHello' to make sure the person can respond to a "sayHello" message. Here's what things look like in Java: /*** Java ***/ public class Person extends java.lang.Object { String name; public Person(String name) { this.name = name; } public void sayHello() { System.out.println("hello, my name is " + this.name); } static public void main(String[] args) { Person p = new Person("ed"); p.sayHello(); } } // Pretty much the same thing: we define what things an instance of the class needs to store in its personal state, and we also define a method that instances can respond to. Finally, just to make this idea concrete, let's switch to PLT Scheme: ;;; PLT Scheme ;;; (require (lib "class.ss")) (define person% (class object% (init-field name) (public say-hello!) (define (say-hello!) (printf "hello, my name is ~a~%" name)) (super-new))) (define p (new person% (name "ed"))) (send p say-hello!) ;; Same thing, just more parentheses. *grin* Hope this helps! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OOP fundamentals
Thanks Danny! Tommorrow I am off to get "Programming for Python, 2nd edition" and learn everything - all of it, before I even bother with Sockets. Afterall, I want python for EVERYTHING not just sockets and inet based scripts/applications. I realized that I need to take a step back, make port scanner a class that does nothing but really help me learn classes, then insert threading, then once that works, insert the actual sockets into their respective class def etc ... Thanks again ... Next time I post, I'll have something either more abstract/theory question, or something that isn't quite so simple! Thanks again everyone thats been helping me out especially danny! -edward ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Timer on CGI
Some of my pupils at school are working through simple mathematic problems (multiplication tables) through my website. I basically show some tables, they enter the results and then get feedback on how they did. I have two questions - and my guess is that one will be easier than the other. 1. I want to store the results in a mySQL database - I've done this kind of thing before using PHP - are there any good tutorial resources for using mysql with python? 2. Today the children asked if they could be timed when they complete the problem. Is there any way of knowing how long they spent completing the task? TIA Adam -- http://www.monkeez.org PGP key: 0x7111B833 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why won't it enter the quiz?
You're not returning the values of answer and guess so it jumps out of the while loop and does the else. Try this: def add(a,b): answer = a+b guess = float(raw_input(a," + ",b," = ")) return answer, guess answer, guess = add(num1,num2) if guess != answer:On 17/09/05, Nathan Pinno <[EMAIL PROTECTED]> wrote: Hi all, I've got this program I've written that should give an addition quiz, except it never enters the quiz. How do I make it enter the quiz? Here is the code: import random def add(a,b): answer = a+b guess = float(raw_input(a," + ",b," = ")) num1 = random.choice(range(1,10))num2 = random.choice(range(1,10)) while 1: q = random.choice(range(15,31)) cq = 1 correct = 0 while cq >= q: add(num1,num2) if guess != answer: print "Incorrect! The correct answer is: ",answer cq += 1 elif guess == answer: print "Correct!" correct += 1 cq += 1 else: print "Questions: ",q print "Correct: ",correct print "Percent Correct: ",(cq/q)*100 break print "Goodbye." It just prints out for an example: Questions: 17Correct: 0Percent Correct: 0Goodbye. Thanks, Nathan Pinno ___Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OOP fundamentals
On Mon, 19 Sep 2005, Ed Hotchkiss wrote: > Thanks Danny! Tommorrow I am off to get "Programming for Python, 2nd > edition" and learn everything - all of it, before I even bother with > Sockets. Hi Ed, I'd disrecommend Programming Python if you're a beginner. Programming Python is really more of an expedition over Python's features than a tutorial. Furthermore, it's a very thick and heavy book, and I have something of a grudge against books that make me strain my wrists. I personally think that "Learning Python" will be a better fit for you. I also have heard good things about Alan Gauld's "Learn to Program Using Python", which is an expansion of the online tutorials you've been reading into book form. And I've heard that the author is pretty responsive. *grin* For more information, you can take a look at: http://wiki.python.org/moin/IntroductoryBooks for book reviews of other introductory texts. But finally, you may also want to look at: http://wiki.python.org/moin/BeginnersGuide/Programmers http://wiki.python.org/moin/BeginnersGuide/NonProgrammers You might be able to get pretty far from the online tutorials there. Good luck! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Timer on CGI
Adam Cripps schrieb: > Some of my pupils at school are working through simple mathematic > problems (multiplication tables) through my website. I basically show > some tables, they enter the results and then get feedback on how they > did. > > I have two questions - and my guess is that one will be easier than the > other. > > 1. I want to store the results in a mySQL database - I've done this > kind of thing before using PHP - are there any good tutorial resources > for using mysql with python? Don't know about that... > 2. Today the children asked if they could be timed when they complete > the problem. Is there any way of knowing how long they spent > completing the task? You'd have to use Cookies for that and (possibly) JavaScript for that. The basic approach would be: 1) When they first request a task, send a session Cookie along with the page and save it on the server side too. 2) provide a button on the page that says: "Start task". That button starts a javascript timer. 3) Provide another Button that says "Finish / Send solution". This buttons reads the timer value ans sends it along with the other request variables. 4) on the server read the request variables and the the cookie and match it to the saved session ids, so you can determine which student this answer was for. I hope I was able to convey the general idea. Please ask if you need to know more! Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Timer on CGI
> 1. I want to store the results in a mySQL database - I've done > this > kind of thing before using PHP - are there any good tutorial > resources > for using mysql with python? There is a generic DB API howto document and I have a database topic in my tutor which uses SQLite but the basic principles are identical you just load a different driver... > 2. Today the children asked if they could be timed when they > complete > the problem. Is there any way of knowing how long they spent > completing the task? You should be able to store the time of page display and submission as hidden fields. Javascript might be the easiest way to do this using the onLoad event. Then get the difference in times when they hit submit and pass the elapsed time as a hidden field for the CGI to pick up and display. Alternatively just pass the onLoad time when submit is called and do the calculations at the CGI, but then you get network transit and server queuing times added... -- 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] IRC Client Trouble -- too many new lines
Hi Joseph, I'm unable to cc to the Python list from this address, so I'll try my forwarding address. >Example: >this >is >the >type >of >message >I'm >getting >and >as >you >can >see >, >it's >really >annoying! You know that 'print' adds a newline? You could either use - import sys sys.stdout.write(msg[print_msg]) or, to simplify things - I would recommend either - space = ' ' for item in msg: item.rstrip() joinedMsg = space.join(msg) or nullString = '' joinedMsg = nullString.join(msg) noNewlinesMsg = joinedMsg.replace('\n', nullString) Let me know how it goes. Regards, Liam Clarke-Hutchinson From: Joseph Quigley [mailto:[EMAIL PROTECTED] Sent: Tuesday, 20 September 2005 2:19 a.m. To: Liam Clarke-Hutchinson; tutor@python.org Subject: Re: [Tutor] IRC Client Trouble -- too many new lines Hi, Liam Clarke-Hutchinson wrote: Hi Joseph, while (1): buffer = Data.IRC.recv(1024) msg = string.split(buffer) Just a warning, the string module will be removed/deprecated come Py3K. Better to use - buffer.split() nick_name = msg[0][:msg[0].find("!")] filetxt.write(nick_name.lstrip(':') + ' -> ' + message.lstrip(':') + '\n') Ok. Thanks for the heads up. As to your printing problem, I'm not sure if you're referring to - print msg[print_msg] Oh, sorry. I'm refering to the print msg[print_msg] And I'm not sure if you want it to print without an additional newline, or with a newline. Well every word is on it's own new line (I get a huge message from the IRC server) and I'd like a word wrap or something to fix that. #Incidentally, it's a bit simpler to maintain this kinda loop #instead of a while loop. for item in msg: item.rstrip() #Will strip whitespace (\t\r\n etc.) by default print item Ah. Thanks If you're wanting to print without the newline that print adds, why not do a join like this one? message = ' '.join(msg[3:]) print ' '.join(msg) ? Thanks again. This is proving very usefull. What output you're expecting I'm expecting a large amount of text without new lines for each word, and possible word wrapping. What output you're getting Example: this is the type of message I'm getting and as you can see , it's really annoying! PS Your code is interesting, I've never dealt with the IRC protocol before,so it's good to see a demonstration of it. I edited the code from a Python Cookbook recipe. I may toddle off and check out that RFC. Yes. Some people on a forum that I hang oput with suggested a chat room. A freind of mine suggested IRC and python. He's doing the server and I'm doing the client. I'm testing the client on freenode.. only thing I could think of at the time. A new monthly electronic newsletter covering all aspects of MED's work is now available. Subscribers can choose to receive news from any or all of seven categories, free of charge: Growth and Innovation, Strategic Directions, Energy and Resources, Business News, ICT, Consumer Issues and Tourism. See http://news.business.govt.nz for more details. http://www.govt.nz - connecting you to New Zealand central & local government services Any opinions expressed in this message are not necessarily those of the Ministry of Economic Development. This message and any files transmitted with it are confidential and solely for the use of the intended recipient. If you are not the intended recipient or the person responsible for delivery to the intended recipient, be advised that you have received this message in error and that any use is strictly prohibited. Please contact the sender and delete the message and any attachment from your computer. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OOP fundamentals
Excellent, I'll be getting that book tomorrow! Thanks again, I'm doing a tutorial as we speak! On 9/19/05, Danny Yoo <[EMAIL PROTECTED]> wrote: On Mon, 19 Sep 2005, Ed Hotchkiss wrote:> Thanks Danny! Tommorrow I am off to get "Programming for Python, 2nd > edition" and learn everything - all of it, before I even bother with> Sockets.Hi Ed,I'd disrecommend Programming Python if you're a beginner. ProgrammingPython is really more of an expedition over Python's features than a tutorial. Furthermore, it's a very thick and heavy book, and I havesomething of a grudge against books that make me strain my wrists. Ipersonally think that "Learning Python" will be a better fit for you. I also have heard good things about Alan Gauld's "Learn to Program UsingPython", which is an expansion of the online tutorials you've been readinginto book form. And I've heard that the author is pretty responsive. *grin*For more information, you can take a look at: http://wiki.python.org/moin/IntroductoryBooksfor book reviews of other introductory texts. But finally, you may also want to look at: http://wiki.python.org/moin/BeginnersGuide/Programmers http://wiki.python.org/moin/BeginnersGuide/NonProgrammersYou might be able to get pretty far from the online tutorials there.Good luck!-- edward hotchkiss ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Any suggestions for optimizing this code?
Replying to myself, I got some speedups by replacing:def makeArray1(matrix): result = matrix result[0][w/2] = 1 for row in range(h-1): last = result[row] next = result[row+1] for i in range(w-1): next[i] = rule[4*last[i-1]+2*last[i]+last[i+1]] next[i+1] = rule[4*last[i]+2*last[i+1]+last[0]] return resultwith this using Numerical Python:def makeArray2(matrix): result = matrix result[0,w/2] = 1 for n in range(h-1): r = result[n] r2 = result[n+1] r2[1:-1] = choose(4*r[:-2]+2*r[1:-1]+r[2:],rule) r2[0] = rule[4*r[-1]+2*r[0]+r[1]] r2[-1] = rule[4*r[-2]+2*r[-1]+r[0]] return resultIt finally clicked that instead of a sliding window, I could just add 4*row + 2*row + row, each offset by one, and that would give the same results using Numpy as stepping through three elements at a time. It's not pretty looking, but it works. This is about 6x faster overall. The bottleneck is in choose, where each element gets looked up and replaced. I'm not sure how to do it any faster tho. Choose is much faster than a for loop, which is where the 6x speedup is really coming from. Numpy just adding and multiplying the row is more like 20x faster than stepping through an element at time with a three element window :) As a sidenote that may be helpful to someone, makeArray1() is 22x faster using psyco, and 4x faster than the Numpy solution. Psyco doesn't speed up the Numpy calculations at all, which isn't surprsing, since it's mostly written in C. If you only use x86, that might be ok. Numpy is a lot more elegant of a solution it seems. I'm positive I could bring those closer together, if I could somehow not use a lookup table to convert binary numbers to integers and back to binary numbers.Normally I wouldn't care a whit about optimisation, but number crunching through a million items and suddenly Numpy seemed pretty cool. I know this is the first time I understood the power of being able to perform a calculation on an entire array, and stacking slices. Multiplying and adding three rows is a lot faster than stepping through a single row three elements at a time. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Timer on CGI
On 9/19/05, Alan Gauld <[EMAIL PROTECTED]> wrote: > > 1. I want to store the results in a mySQL database - I've done > > this > > kind of thing before using PHP - are there any good tutorial > > resources > > for using mysql with python? > > There is a generic DB API howto document and I have a database > topic in my tutor which uses SQLite but the basic principles > are identical you just load a different driver... > > > 2. Today the children asked if they could be timed when they > > complete > > the problem. Is there any way of knowing how long they spent > > completing the task? > > You should be able to store the time of page display and > submission > as hidden fields. Javascript might be the easiest way to do this > using the onLoad event. Then get the difference in times when > they > hit submit and pass the elapsed time as a hidden field for the > CGI > to pick up and display. Alternatively just pass the onLoad time > when submit is called and do the calculations at the CGI, but > then you get network transit and server queuing times added... Thanks to all for suggestions and link - will look at this and probably come back for more help! Adam -- http://www.monkeez.org PGP key: 0x7111B833 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OOP fundamentals
Hi Ed, last month I have found this beautifull sample about threads and sockets: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/114642 It helped me to a lot to understand how these can be used together on an OOP way. It helped me much better, than any hypothetical OOP samples about cars and wheels, those really usefull just for programming teachers who never made any real programm, but has to tell something about why OOP is good to learn. It was so nice to read and understand a so clean code. Probably it can help your understanding eighter. The other place where I feel OOP very natural is using wxPython. There is another recipe about portscanning with OOP and threading: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286240 Yours sincerely, __ János Juhász > Message: 5 > Date: Mon, 19 Sep 2005 17:01:30 -0400 > From: Ed Hotchkiss <[EMAIL PROTECTED]> > Subject: Re: [Tutor] OOP fundamentals > To: Danny Yoo <[EMAIL PROTECTED]> > Cc: Tutor > Message-ID: <[EMAIL PROTECTED]> > Content-Type: text/plain; charset="iso-8859-1" > Thanks Danny! Tommorrow I am off to get "Programming for Python, 2nd > edition" and learn everything - all of it, before I even bother with > Sockets. Afterall, I want python for EVERYTHING not just sockets and inet > based scripts/applications. > I realized that I need to take a step back, make port scanner a class that > does nothing but really help me learn classes, then insert threading, then > once that works, insert the actual sockets into their respective class def > etc ... Thanks again ... > Next time I post, I'll have something either more abstract/theory question, > or something that isn't quite so simple! > Thanks again everyone thats been helping me out especially danny! > -edward > -- next part -- > An HTML attachment was scrubbed... > URL: http://mail.python. > org/pipermail/tutor/attachments/20050919/41d24153/attachment.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor