Python tricks
Hi, My code has a lot of while loops of the following format: while True: ... if condition: break The danger with such a code is that it might go to an infinite loop - if the condition never occurs. Is there a way - a python trick - to have a check such that if the loop goes for more than x number of steps, it will cause an exception? I do understand that we can use the code like - i = 0 while True: i++ if i 200: raise infinite_Loop_Exception ... if condition: break But I am not very happy with this code for 3 reasons 1. Verbosity (i=0 and i++) which doesnt add to the logic 2. The loop now has dual focus. - incrementing i, etc. 3. most important A person looks into the code and thinks 'i' has special significance. His/her mind will be focused on not the actual reason for the loop. The solution that I had in mind is: while True: ... if condition: break if inifinte_loop(): raise infiinte_loop_exception Wherein infinite_loop is a generator, which returns true if i 200 def infinite_loop(): i = 0 while i 200: i++ yield False yield True Could somebody let me know whether this is a good option? One of my main worries is - what will happen if I call this same procedure from another loop? Will it start again from 0 or will it again start from my previous stored i? i.e. def big_proc: while True: ... if infinite_loop: raise while True: ... if infinite_loop: raise In such a case, will we run it 200 times both the times or not? Could someone chip in with other suggestions? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python tricks
On Jan 12, 6:51 pm, Tim Chase python.l...@tim.thechases.com wrote: My code has a lot of while loops of the following format: while True: ... if condition: break The danger with such a code is that it might go to an infinite loop - if the condition never occurs. Is there a way - a python trick - to have a check such that if the loop goes for more than x number of steps, it will cause an exception? I do understand that we can use the code like - i = 0 while True: i++ if i 200: raise infinite_Loop_Exception ... if condition: break But I am not very happy with this code for 3 reasons 1. Verbosity (i=0 and i++) which doesnt add to the logic 2. The loop now has dual focus. - incrementing i, etc. 3. most important A person looks into the code and thinks 'i' has special significance. His/her mind will be focused on not the actual reason for the loop. My first thought would be to simply not use while True: INFINITE_LOOP_COUNT = 200 for _ in xrange(INFINITE_LOOP_COUNT): do_something() if condition: break else: raise InfiniteLoopException The solution that I had in mind is: while True: ... if condition: break if inifinte_loop(): raise infiinte_loop_exception Wherein infinite_loop is a generator, which returns true if i 200 def infinite_loop(): i = 0 while i 200: i++ yield False yield True Could somebody let me know whether this is a good option? To do this, you'd need to do the same sort of thing as you do with your i/i++ variable: i = infinite_loop() while True: ... if condition: break if i.next(): raise InfiniteLoopException which doesn't gain much, and makes it a whole lot more confusing. Could someone chip in with other suggestions? As an aside: the phrase is chime in[1] (to volunteer suggestions) Chip in[2] usually involves contributing money to a common fund (care to chip in $10 for Sally's wedding gift from the office? where the pool of money would then be used to buy one large/expensive gift for Sally) -tkc [1]http://www.thefreedictionary.com/chime+in [2]http://www.english-test.net/forum/ftopic1768.html Thank you very much Tim. I agree on all counts - esp the fact that my suggestion is very confusing + (chime in part too :) ). But, I still feel it would be much more aesthetically pleasing if I can call a single procedure like if infinite_loop() - to do the same. Is it somehow possible? - say by using static variables, iterators -- anything? -- http://mail.python.org/mailman/listinfo/python-list
Are python objects thread-safe?
Say, I have two threads, updating the same dictionary object - but for different parameters: Please find an example below: a = {file1Data : '', file2Data : ''} Now, I send it to two different threads, both of which are looping infinitely: In thread1: a['file1Data'] = open(filename1).read and in thread2: a['file2Data'] = open(filename2).read My question is - is this object threadsafe? - since we are working on two different parameters in the object. Or should I have to block the whole object? -- http://mail.python.org/mailman/listinfo/python-list
Re: Twisted for non-networking applications
On Dec 22, 3:26 am, James Mills prolo...@shortcircuit.net.au wrote: On Mon, Dec 22, 2008 at 4:27 AM, Kottiyath n.kottiy...@gmail.com wrote: Hi all, Is it a good idea to use Twisted inside my application, even though it has no networking part in it? Basically, my application needs lots of parallel processing - but I am rather averse to using threads - due to myraid issues it can cause. So, I was hoping to use a reactor pattern to avoid the threads. I am using twisted in another part of the application for networking, so I was hoping to use the same for the non-networking part for reusing the reactor pattern. If somebody can help me on this, it would be very helpful. Alternatively you could give circuits (1) a go. It _can_ be a nice alternative to Twisted and isn't necessarily focused on Networking applications. cheers James 1.http://trac.softcircuit.com.au/circuits/ I was unable to see documentation explaining this - so asking again. Suppose the event handlers in the component is doing blocking work, how is it handled? I went through ciruits.core, but was unable to understand exactly how blocking mechanisms are handled. My scenario is as follows: I have 4 loops, 1 small and high priority, 3 quite large and blocking (takes upto 3 seconds) and comparatively low priority. The small loops goes through everytime and does some work - and optionally uses the data sent by the other 3 loops. I do not want the smaller loop to get blocked by the other loops. So, if the event handler does blocking work, can that cause the whole loop to block? -- http://mail.python.org/mailman/listinfo/python-list
11001, 'getaddrinfo failed' : Error in httplib but not in urllib2
Hi all, I am trying to connect to localhost via httplib, but it fails. To check whether it is a firewall problem etc, I tried to connect via urllib2, but it went through fine. Could some one help me out on this? I cannot use urllib2 in the program because I have to send files via post to a url, and urllib2 doesn't support it (Python2.4) Please see the code below: --HTTPLIB-- h = httplib.HTTPConnection(http://127.0.0.1:8000;) h.request('GET', /accounts/) res = h.getresponse() --URLLIB2-- req = urllib2.Request(http://127.0.0.1:8000/accounts/;) fd = urllib2.urlopen(req) Regards Raj -- http://mail.python.org/mailman/listinfo/python-list
Re: 11001, 'getaddrinfo failed' : Error in httplib but not in urllib2
On Dec 1, 7:43 pm, Tim Golden [EMAIL PROTECTED] wrote: RajNewbie wrote: Hi all, I am trying to connect to localhost via httplib, but it fails. To check whether it is a firewall problem etc, I tried to connect via urllib2, but it went through fine. Could some one help me out on this? I cannot use urllib2 in the program because I have to send files via post to a url, and urllib2 doesn't support it (Python2.4) Please see the code below: --HTTPLIB-- h = httplib.HTTPConnection(http://127.0.0.1:8000;) h.request('GET', /accounts/) res = h.getresponse() Which aspect of the documentation: http://docs.python.org/library/httplib.html leads you to think that the first parameter to httplib.HTTPConnection should be a URL? dump import httplib httplib.HTTPConnection (127.0.0.1) httplib.HTTPConnection instance at 0x00AAB508 httplib.HTTPConnection (localhost) httplib.HTTPConnection instance at 0x00B40558 /dump TJG oops. My mistake. I am sorry to post such a braindead question here. I mistook the host for the url here. Sorry. -- http://mail.python.org/mailman/listinfo/python-list