Re: [Tutor] loop until a keypress
Surya K wrote: I want to run code until a enter is pressed. Well, it shouldn't wait for the user to enter enter This is my code: This is what it looks like over here: import msvcrtchr = 0while chr != 'q': print my code, if msvcrt.kbhit(): chr = msvcrt.getch() This is what I suppose you wrote: import msvcrt chr = 0 while chr != 'q': print my code, if msvcrt.kbhit(): chr = msvcrt.getch() This isn't working the way I wanted. When ever I press enter, the loop is starting in a new line and continuing. I even added break statement in if block but it isn't workingCan you tell me how to do that? You could make a little experiment. Run import msvcrt while True: if msvcrt.kbhit(): print msvcrt.getch() and then press the 'q' key. What does this little script print? Can you apply the newly gained information to your original code? I am on windows. So, as msvcrt is for windows, I wonder if there is any module that works for both, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] loop until a keypress
On 30/01/12 05:20, Surya K wrote: I want to run code until a enter is pressed. Well, it shouldn't wait for the user to enter enter This is my code: import msvcrt chr = 0 while chr != 'q': print my code, if msvcrt.kbhit(): chr = msvcrt.getch() You shouldn't need the kbhit test. Try just using: while ky != 'q': ky = msvcrt.getch() print ky I am on windows. So, as msvcrt is for windows, I wonder if there is any module that works for both, Both what? -- 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] loop until a keypress
On 2012/01/30 07:20 AM, Surya K wrote: I want to run code until a enter is pressed. Well, it shouldn't wait for the user to enter enter This is my code: import msvcrt chr = 0 while chr != 'q': print my code, if msvcrt.kbhit(): chr = msvcrt.getch() This isn't working the way I wanted. When ever I press enter, the loop is starting in a new line and continuing. I even added break statement in if block but it isn't working Can you tell me how to do that? I am on windows. So, as msvcrt is for windows, I wonder if there is any module that works for both, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor There isn't a platform independent module for capturing keystrokes unfortunately. You can take a look at this StackOverflow answer though which could help you out http://stackoverflow.com/questions/5044073/python-cross-platform-listening-for-keypresses -- Christian Witts Python Developer // ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] loop until a keypress
Surya K wrote: I am on windows. So, as msvcrt is for windows, I wonder if there is any module that works for both, http://code.activestate.com/recipes/577977 -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Sort
Hey all, again: Thanks for the delete thing. Helped with that problem a lot. Especially the getattr thing came in handy. Question 1: How do you guys indent and put in the triple greater than signs and all that when sending mail? Manually? Coz the last mail I sent was using the gmail indent thing and it doesnt seem to have worked. Anyway: Question 2: I have this script that downloaded a lot of files from one server into one local directory and the files are named in a particular order and I want them to be opened in this very same order so that I can copy the contents of each to one main file and then convert to pdf. However, the naming schema is like this (Roman numerals in parts) and python doesn't seem to be able to sort it properly. 1.I.1; 1.I.2; ; 1.I.20; 1.II.1.1;1.II.1.2 1.II.2;...1.II.10; 2.I.3; Because of the roman numerals that are always in the second part fo the filename, it could sort based on how I wanted. I listed all the files in the said directory: files=os.listdir(location) I tried using a sort algorithm that goes through the list starting from index 0 and then compares each index with the next one and if they are not arranged as they should be, the two are switched and then it starts again. The thing is, it is so slow! It hardly goes past index three in the list and there are so many files. *about fifty*. Could anyone help me with the python impelentation of such an algorith and how it should sort. Thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 95, Issue 80
to add here is, I am trying to create the GUI based chat server.(Attached the programs.) Please do not send large chunks of code like this, unless asked. Instead, you should try to produce a minimal example that demonstrates the problem. It should be: * short (avoid code which has nothing to do with the problem) * self-contained (other people must be able to run it) * correct (it must actually fail in the way you say it fails) See here for more: http://sscce.org/ In cutting your code down to a minimal example, 9 times out of 10 you will solve your problem yourself, and learn something in the process. bash-3.1$ python Client1.py Enter the server address:...9009 Traceback (most recent call last): File Client1.py, line 53, in module c = ClientChat(serverport) File Client1.py, line 24, in __init__ gui.callGui() File a:\FedEx\Exp\ClientGui.py, line 37, in callGui sendbutton =Button(f2, width = 5, height = 2, text = Send, command = C.ClientChat.senddata()) TypeError: unbound method senddata() must be called with ClientChat instance as first argument (got nothing instead) This one is easy. You need to initialize a ClientChat instance first. This may be as simple as: command = C.ClientChat().senddata although I'm not sure if ClientChat requires any arguments. Note that you call the ClientChat class, to create an instance, but you DON'T call the senddata method, since you want to pass the method itself as a callback function. The button will call it for you, when needed. Thanks for the clarification and telling me about SSCCE :) But just a simple thing,,, Can I call a method of another module while creating a GUI. For example C = Tk() .(Some more lines) self.sendbutton =Button(self.f2, width = 5, height = 2, text = Send, command = ANOTHER MODULE.METHOD OF THAT MODULE) self.sendbutton.pack(side = LEFT, padx = 10, pady = 10) .(Some more lines) C.mainloop() Because I am getting stuck in a loop. The client is keep on connecting to server without creating a GUI. -- Message: 4 Date: Mon, 30 Jan 2012 00:28:21 +0200 From: amt 0101...@gmail.com To: tutor@python.org Subject: Re: [Tutor] Why do you have to close files? Message-ID: CAEQEn016afJDN+2F_+R-rm2d4MbWGXvN9_ed-AQoNmT=f2v...@mail.gmail.com Content-Type: text/plain; charset=ISO-8859-1 All the replies were very helpful! Thank you very much for helping me out! -- Message: 5 Date: Mon, 30 Jan 2012 10:50:51 +0530 From: Surya K sur...@live.com To: Python Tutor tutor@python.org Subject: [Tutor] loop until a keypress Message-ID: snt130-w397e068f9e47abd82eea34a4...@phx.gbl Content-Type: text/plain; charset=iso-8859-1 I want to run code until a enter is pressed. Well, it shouldn't wait for the user to enter enter This is my code: import msvcrtchr = 0while chr != 'q': print my code, if msvcrt.kbhit(): chr = msvcrt.getch() This isn't working the way I wanted. When ever I press enter, the loop is starting in a new line and continuing. I even added break statement in if block but it isn't workingCan you tell me how to do that? I am on windows. So, as msvcrt is for windows, I wonder if there is any module that works for both, -- next part -- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20120130/a5996b2a/attachment-0001.html -- Message: 6 Date: Mon, 30 Jan 2012 11:55:12 +0530 From: Ganesh Kumar bugcy...@gmail.com To: tutor@python.org Subject: [Tutor] Help Glade Tutorial. Message-ID: cajzooyc-oar6joz9wrjbtv0w6t3l2zl0dzv+guyd-yup9xe...@mail.gmail.com Content-Type: text/plain; charset=utf-8 Hi Guys, I am searching for a Glade tutorial, on how to create simple projects Glade with python 1) design a simple interface in glade 2) use the glade interface to write some really simple application with python. I search in goggled i didn't get good tutorials, guide me guys How to start with Glade. -Ganesh Did I learn something today? If not, I wasted it. -- next part -- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/tutor/attachments/20120130/fec2e0a2/attachment-0001.html -- Message: 7 Date: Mon, 30 Jan 2012 00:51:58 -0600 From: Chris Fuller cfuller...@thinkingplanet.net To: tutor@python.org Subject: Re: [Tutor] Help Glade Tutorial. Message-ID: 201201300051.58828.cfuller...@thinkingplanet.net Content-Type: Text/Plain; charset=utf-8 Which ones did you look at, and why did you not like them? Keep in mind that Glade is an interface builder, and hasn't got anything much to do with the target language, other than requiring there be a libglade library to read the XML files. I actually got
Re: [Tutor] Sort
George Nyoro wrote: Hey all, again: Thanks for the delete thing. Helped with that problem a lot. Especially the getattr thing came in handy. Question 1: How do you guys indent and put in the triple greater than signs and all that when sending mail? Manually? Coz the last mail I sent was using the gmail indent thing and it doesnt seem to have worked. Anyway: Copy text including signs, and any other characters you want. Paste into your email client. The characters should appear exactly as you copied and pasted them. If your email client (gmail) takes it upon itself to mangle your text, then use a better email client that doesn't suck. (Any software which thinks it knows what you want better than you do should be taken out and slapped with a wet fish.) Question 2: I have this script that downloaded a lot of files from one server into one local directory and the files are named in a particular order and I want them to be opened in this very same order so that I can copy the contents of each to one main file and then convert to pdf. However, the naming schema is like this (Roman numerals in parts) and python doesn't seem to be able to sort it properly. 1.I.1; 1.I.2; ; 1.I.20; 1.II.1.1;1.II.1.2 1.II.2;...1.II.10; 2.I.3; I don't understand this. Is each line ONE file name, or are there many file names per line? Are the semi-colons part of the file names? I'm going to *guess* that each file name has THREE sections only, with no semi-colons, and look like these 3 examples: 1.I.1 1.III.20 5.IX.7 etc. Am I close? Because of the roman numerals that are always in the second part fo the filename, it could sort based on how I wanted. I listed all the files in the said directory: files=os.listdir(location) I tried using a sort algorithm that goes through the list starting from index 0 and then compares each index with the next one and if they are not arranged as they should be, the two are switched and then it starts again. The thing is, it is so slow! It hardly goes past index three in the list and there are so many files. *about fifty*. Congratulations! You appear to have re-invented one of the world's slowest sorting functions, bubblesort. wink Look it up on Wikipedia. Only probably with a bug in it, because even bubblesort shouldn't get stuck after only three files. Except as a learning exercise, or to prove you can, never try to program your own sort in Python. That's like buying a car, and then putting it on the back of your push-bike and trying to cycle home with it. Uphill. In the snow. With a flat tire. Instead, you should use Python's built-in sort function, which if you use it properly is faster than anything you can write (unless you are Tim Peters, who invented it in the first place). Normally, you would just do something like this: files = os.listdir(location) files.sort() # sort the list in-place, without making a copy or perhaps this: files = sorted(os.listdir(location)) # make a copy and sort but in this case you have special requirements, thanks to the naming convention for your files. In this case, you don't want the ordinary sort order for strings, you need a special sort order based on Roman numerals. The best way to sort these is with a technique called: Decorate, Sort, Undecorate also known as DSU, which sorted() supports using a key function. It sounds harder than it is. In this case, you need to decorate each file name (which is hard to sort!) with something that is easier to sort. In this case, the filenames look like: number DOT roman number DOT number all as one string, so the obvious way to sort them is to convert to three numbers. The first and last numbers are easy, you just use the int() function, the roman number is a bit trickier because Python doesn't have a built-in converter. So here is my converter, and a decorate function that uses it: def from_roman(s): # Convert Roman numerals into an integer. table = {'I': 1, 'II': 2, 'III': 3, 'IV': 4, 'V': 5, 'VI': 6, 'VII': 7, 'VIII': 8, 'IX': 9, 'X': 10} # Is this enough? return table[s.upper()] def decorate(filename): a, b, c = filename.split(.) # Split into three pieces, at the dots. a = int(a) # Convert the first and last into integers. c = int(c) b = from_roman(b) # and the middle using roman numerals return (a, b, c) And finally you can do the sorting like so: files = sorted(os.listdir(location), key=decorate) Note that Python will do the undecorate part automatically. Warning: I have not tested any of this. It should work, but it's not unpossible for me to have make a misteak. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] New user Knows nothing about python I Need HELP
hello I am trying to make a math functions program which includes ADDITION: 2+2=4 SUBTRACTION: 4-2=2 MULTIPLICATION: 4*2=8 DIVISION: 4/2=2 EXPONENT: 2**3=8 REMAINDER: 5%2=1 I have no Idea how to start this task I have never used ANY programming programs before And I dont Know the language either The online help files from python Did not help a bit please help thank you___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New user Knows nothing about python I Need HELP
On Mon, Jan 30, 2012 at 2:02 PM, William Stewart williamjstew...@rogers.com wrote: I have no Idea how to start this task I have never used ANY programming programs before And I dont Know the language either The online help files from python Did not help a bit Here's a few resources that might get you started. First, if you haven't seen it, I found the official python tutorial to be a great place to start: http://docs.python.org/tutorial/ That may assume more familiarity with programming in general than you are comfortable with, though. In that case, you might be interested in a whole bunch of resources designed to teach python to people who haven't done any programming at all: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers Once you're a bit more comfortable, there are a ton more documentation aimed at learning python for people who are already somewhat familiar with other programming languages: http://wiki.python.org/moin/BeginnersGuide/Programmers Hope that helps some, Jerry ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New user Knows nothing about python I Need HELP
On Mon, Jan 30, 2012 at 2:21 PM, Jerry Hill malaclyp...@gmail.com wrote: On Mon, Jan 30, 2012 at 2:02 PM, William Stewart williamjstew...@rogers.com wrote: I have no Idea how to start this task I have never used ANY programming programs before And I dont Know the language either The online help files from python Did not help a bit Here's a few resources that might get you started. First, if you haven't seen it, I found the official python tutorial to be a great place to start: http://docs.python.org/tutorial/ That may assume more familiarity with programming in general than you are comfortable with, though. In that case, you might be interested in a whole bunch of resources designed to teach python to people who haven't done any programming at all: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers Once you're a bit more comfortable, there are a ton more documentation aimed at learning python for people who are already somewhat familiar with other programming languages: http://wiki.python.org/moin/BeginnersGuide/Programmers Hope that helps some, Jerry ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From the nature of your question, I am guessing you are taking a class and this is homework? If so, can you tell us what you have learned so far or what topics have been covered in your class? If this just for general interest. the resources above are a good start -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New user Knows nothing about python I Need HELP
On Mon, Jan 30, 2012 at 5:41 PM, William Stewart williamjstew...@rogers.com wrote: thanks I havent learned anything about prgramming its a computer sicence class, I will try the links thanks again --- On *Mon, 1/30/12, Joel Goldstick joel.goldst...@gmail.com* wrote: From: Joel Goldstick joel.goldst...@gmail.com Subject: Re: [Tutor] New user Knows nothing about python I Need HELP To: Jerry Hill malaclyp...@gmail.com Cc: William Stewart williamjstew...@rogers.com, tutor@python.org Date: Monday, January 30, 2012, 3:00 PM On Mon, Jan 30, 2012 at 2:21 PM, Jerry Hill malaclyp...@gmail.comhttp://ca.mc886.mail.yahoo.com/mc/compose?to=malaclyp...@gmail.com wrote: On Mon, Jan 30, 2012 at 2:02 PM, William Stewart williamjstew...@rogers.comhttp://ca.mc886.mail.yahoo.com/mc/compose?to=williamjstew...@rogers.com wrote: I have no Idea how to start this task I have never used ANY programming programs before And I dont Know the language either The online help files from python Did not help a bit Here's a few resources that might get you started. First, if you haven't seen it, I found the official python tutorial to be a great place to start: http://docs.python.org/tutorial/ That may assume more familiarity with programming in general than you are comfortable with, though. In that case, you might be interested in a whole bunch of resources designed to teach python to people who haven't done any programming at all: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers Once you're a bit more comfortable, there are a ton more documentation aimed at learning python for people who are already somewhat familiar with other programming languages: http://wiki.python.org/moin/BeginnersGuide/Programmers Hope that helps some, Jerry ___ Tutor maillist - Tutor@python.orghttp://ca.mc886.mail.yahoo.com/mc/compose?to=Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From the nature of your question, I am guessing you are taking a class and this is homework? If so, can you tell us what you have learned so far or what topics have been covered in your class? If this just for general interest. the resources above are a good start -- Joel Goldstick William, First, reply to all so that the group sees your response. Second, aside from the reading, find a classmate or two who are feeling like you are. python has an interactive shell that you get to by just typing python from a terminal prompt. When you go through a tutorial or examples, try things out in the shell. You'll learn a lot that way -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] ASCII Conversion
I am trying to do a simple test but am not sure how to get around ASCII conversion of characters. I want to pass in y have the function test to see if y is an integer and print out a value if that integer satisfies the if statement. However, if I pass in a string, it's converted to ASCII and will still satisfy the if statement and print out value. How do I ensure that a string is caught as a ValueError instead of being converted? def TestY(y): try: y = int(y) except ValueError: pass if y -1 or y 1: value = 82 print value else: pass -- Michael J. Lewis mjole...@gmail.com 415.815.7257 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ASCII Conversion
On Tue, Jan 31, 2012 at 15:50, Michael Lewis mjole...@gmail.com wrote: ... However, if I pass in a string, it's converted to ASCII and will still satisfy the if statement and print out value. How do I ensure that a string is caught as a ValueError instead of being converted? It depends on what you want to do if you catch a string. If you just want to end the function, you could just try returning on the exception, rather than passing. This will stop the string from being processed any further. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ASCII Conversion
On 2012/01/31 06:50 AM, Michael Lewis wrote: I am trying to do a simple test but am not sure how to get around ASCII conversion of characters. I want to pass in y have the function test to see if y is an integer and print out a value if that integer satisfies the if statement. However, if I pass in a string, it's converted to ASCII and will still satisfy the if statement and print out value. How do I ensure that a string is caught as a ValueError instead of being converted? def TestY(y): try: y = int(y) except ValueError: pass if y -1 or y 1: value = 82 print value else: pass -- Michael J. Lewis mjole...@gmail.com mailto:mjole...@gmail.com 415.815.7257 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor If you just want to test if `y` is an integer you can do so with `type(y) == int`, and to get the ASCII value of a character you can use `ord` like `ord('a') == 97`. And how to avoid your ValueError with a bad conversion, do your type checking before hand. Hope that helps. -- Christian Witts Python Developer // ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ASCII Conversion
On 31/01/2012 05:33, Christian Witts wrote: On 2012/01/31 06:50 AM, Michael Lewis wrote: I am trying to do a simple test but am not sure how to get around ASCII conversion of characters. I want to pass in y have the function test to see if y is an integer and print out a value if that integer satisfies the if statement. However, if I pass in a string, it's converted to ASCII and will still satisfy the if statement and print out value. How do I ensure that a string is caught as a ValueError instead of being converted? def TestY(y): try: y = int(y) except ValueError: pass if y -1 or y 1: value = 82 print value else: pass -- Michael J. Lewis mjole...@gmail.com mailto:mjole...@gmail.com 415.815.7257 ___ Tutor maillist -Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor If you just want to test if `y` is an integer you can do so with `type(y) == int`, and to get the ASCII value of a character you can use `ord` like `ord('a') == 97`. And how to avoid your ValueError with a bad conversion, do your type checking before hand. Hope that helps. -- Christian Witts Python Developer // ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor The test of y would not normally be written as it is, comparisons can be chained see http://docs.python.org/reference/expressions.html#not-in. Also Python tends to use EAFP rather than LBYL see http://docs.python.org/glossary.html. -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ASCII Conversion
Michael Lewis wrote: I am trying to do a simple test but am not sure how to get around ASCII conversion of characters. I want to pass in y have the function test to see if y is an integer and print out a value if that integer satisfies the if statement. However, if I pass in a string, it's converted to ASCII and will still satisfy the if statement and print out value. How do I ensure that a string is caught as a ValueError instead of being converted? def TestY(y): try: y = int(y) except ValueError: pass if y -1 or y 1: value = 82 print value else: pass You have to remember somehow whether you have an integer or a string. A straightforward way is def test(y): try: y = int(y) isint = True except ValueError: isint = False if isint and y -1 or y 1: print 82 However, Python's try..except statement features an else suite that is only invoked when no exception is raised. So the idiomatic way is to drop the helper variable and change the control flow instead: def test(y): try: y = int(y) except ValueError: pass else: if y -1 or y 1: print 82 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ASCII Conversion
Peter Otten wrote: if isint and y -1 or y 1: Sorry, I forgot the parentheses. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor