[Tutor] continuous running of a method
I have a method (I'll call it foo) that will either return None or an object depending on a random value generated. What I want to happen is that if I call foo(), i.e, f = foo() and it returns None, to re-call it until it returns something else. I would think this would be done with a while loop, but can't seem to get the details right. Any help would be greatly appreciated. Greg ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] continuous running of a method
On 08/23/2010 01:10 AM, James Mills wrote: On Mon, Aug 23, 2010 at 3:00 PM, Greg Bair wrote: I have a method (I'll call it foo) that will either return None or an object depending on a random value generated. What I want to happen is that if I call foo(), i.e, f = foo() and it returns None, to re-call it until it returns something else. I would think this would be done with a while loop, but can't seem to get the details right. Any help would be greatly appreciated. Quite simple really, and yes you are right. Use a while loop: from random import seed, random from time import time seed(time()) def foo(): ... if 0.5< random()< 0.6: ... return True ... x = foo() while x is None: ... x = foo() ... x True cheers James I don't know why I didn't think of that. Thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] continuous running of a method
On 08/25/2010 01:25 AM, Nitin Das wrote: The problem with this while loop is if your random value doesn't lie between the mentioned range then ur 100% cpu would be utilized. The one thing u can do is to sleep for some time lets say 0.5 sec after every while loop iteration , in this case ur cpu utilization will go down. It's not that the value doesn't lie between the mentioned range. What I'm doing is randomly fetching an item from a list of dicts (multi-dimensional ones from a JSON response) and accessing a value from it, but not every item has the key I'm looking for (can't change that). I suppose what I could do to not randomize every time is to iterate through the list and create a new list that only contains dicts that have that key, then get a random one from that. I suppose that would be more efficient. Any thoughts? Greg --nitin On Mon, Aug 23, 2010 at 8:21 PM, bob gailer <mailto:bgai...@gmail.com>> wrote: On 8/23/2010 1:00 AM, Greg Bair wrote: I have a method (I'll call it foo) that will either return None or an object depending on a random value generated. What I want to happen is that if I call foo(), i.e, f = foo() and it returns None, to re-call it until it returns something else. I would think this would be done with a while loop, but can't seem to get the details right. Even though a while will work, you will have tied up the CPU until the loop terminates. This is never a good idea. What is your higher level goal? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] why does this fail
On 08/25/2010 06:00 AM, Roelof Wobben wrote: > > Hello, > > > > I have this programm : > > > > def remove_letter(letter, strng): > """ > >>> remove_letter('a', 'apple') > 'pple' > >>> remove_letter('a', 'banana') > 'bnn' > >>> remove_letter('z', 'banana') > 'banana' > >>> remove_letter('i', 'Mississippi') > 'Mpp' > """ > antwoord="" > for letter in strng: > print letter, strng > if letter in strng: > print "false" > else: > print "true" > return antwoord > > > > x=remove_letter('a', 'apple') > print x > > > > But now everything is false even a in apple. > > > > What is here wrong ? > I'm assuming what you really want is : if letter in strng: print "true" else: print "false" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about import statement
On 08/26/2010 10:29 PM, Hugo Arts wrote: > On Thu, Aug 26, 2010 at 9:16 PM, Bill Allen wrote: >> >> I did try that and of course it gave an error because it was necessary. I >> just did not know why. However, I later found an explanation on the web. >> Here it is: >> >> from tkinter import * >> from tkinter import ttk >> >> These two lines tell Python that our program needs two modules. The first, >> "tkinter", is the standard binding to Tk, which when loaded also causes the >> existing Tk library on your system to be loaded. The second, "ttk", is >> Python's binding to the newer "themed widgets" that were added to Tk in 8.5. >> > > yeah, "from package import *" doesn't actually import every name from > a module. For example, by default, names starting with an underscore > are not imported. Alternatively, if you have a variable named __all__ > in your module, and it's a list of names, only those names in the list > actually get imported when you do a "from x import *" > > Hugo Why would the person who wrote this package choose to do it this way, though? If it was something that people would use and not just an internal name, why hide it this way? Greg ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Adding to a CSV file?
On 08/29/2010 02:12 PM, aenea...@priest.com wrote: > > Hi, > > I'm learning Python so I can take advantage of the really cool stuff in the > Natural Language Toolkit. But I'm having problems with some basic file > manipulation stuff. > > My basic question: How do I read data in from a csv, manipulate it, and then > add it back to the csv in new columns (keeping the manipulated data in the > "right row")? > > Here's an example of what my data looks like ("test-8-29-10.csv"): Python has a great module in the standard library - csv. It's really easy to use. http://docs.python.org/library/csv.html Greg ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor