Re: Python tricks with applescript in OS-X
Thanks for the pointers to appscript, and for the comments on the page. I have changed the examples at http://juanreyero.com/article/python/os-x-python.html to reflect them. Cheers, Juan -- http://juanreyero.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python tricks with applescript in OS-X
On 12/11/09 3:13 AM, joa...@gmail.com wrote: Greetings, I've written a short document with some working examples of how to interface python with other applications in OS-X via applescript (had to spend some time figuring it out, and thought I might as well write it down). The examples include asking Google Earth for the latitude and longitude of the point at the center of its screen, or using the finder to pop-up a dialog window to get input from the user: http://juanreyero.com/article/python/os-x-python.html Cheers, Juan http://juanreyero.com Thanks for these examples. There is also a Python package that allows you to interface with an application's AppleScript dictionary via Python itself, without the intermediate step of using the osascript command-line tool: http://appscript.sourceforge.net/ You might find that of interest. --Kevin -- Kevin Walzer Code by Kevin http://www.codebykevin.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Python tricks
Scott David Daniels scott.dani...@acm.org wrote: RajNewbie wrote: On Jan 12, 6:51 pm, Tim Chase python.l...@tim.thechases.com wrote: [a perfectly fine reply which is how I'd solve it] RajNewbie wrote: ... 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? ... 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? Yes, it is possible. After: def Fuse(count, exception): for i in range(count): yield None raise exception You can do your loop as: check_infinite = Fuse(200, ValueError('Infinite Loop')).next while True: ... check_infinite() Or related to the above and the original proposal class InfiniteLoopError(Exception): An 'infinite' loop has been detected def infinite_loop(max=200): for i in xrange(max): yield i raise InfiniteLoopError() Use it like this for i in infinite_loop(): if i 10: break print iteration, i or for i in infinite_loop(10): print iteration, i but I agree with Tim that a for ... else loop for the limit is clearer. Probably yes -- Nick Craig-Wood n...@craig-wood.com -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: Python tricks
RajNewbie raj.indian...@gmail.com writes: Could someone chip in with other suggestions? Set up an iterable that will end under the right conditions. Then, iterate over that with ‘for foo in that_iterable’. This idiom is usually far more expressive than any tricks with ‘while’ loops and ‘break’ statements. For tools to work with that can give you such an iterable without needing to make one from scratch, try the following and use the one that is most suitable to the problem at hand: * list * dict * list comprehension * generator expression * generator function * functions from the ‘itertools’ module -- \ “Too many Indians spoil the golden egg.” —Sir Joh | `\ Bjelke-Petersen | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Python tricks
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 -- 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
Re: Python tricks
RajNewbie raj.indian...@gmail.com writes: 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 I prefer: from itertools import count for i in count(): if i 200: raise infinite_Loop_Exception ... You could also use: for i in xrange(200): ... else: raise infinite_Loop_Exception The else clause runs only if no break statement is executed. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python tricks
On Jan 13, 12:51 am, Tim Chase python.l...@tim.thechases.com took a walk on the OT side: 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) [1]http://www.thefreedictionary.com/chime+in [2]http://www.english-test.net/forum/ftopic1768.html All rather locale-dependent; see e.g. http://www.answers.com/topic/chip-in -- http://mail.python.org/mailman/listinfo/python-list
Re: Python tricks
RajNewbie wrote: On Jan 12, 6:51 pm, Tim Chase python.l...@tim.thechases.com wrote: [a perfectly fine reply which is how I'd solve it] RajNewbie wrote: ... 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? ... 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? 1) Please cut down quoted text to as little as needed to understand the reply. Yes, it is possible. After: def Fuse(count, exception): for i in range(count): yield None raise exception You can do your loop as: check_infinite = Fuse(200, ValueError('Infinite Loop')).next while True: ... check_infinite() but I agree with Tim that a for ... else loop for the limit is clearer. --Scott David Daniels scott.dani...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Python tricks
RajNewbie wrote: 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. Maybe you should call the counter variable something meaningful instead of -- of all things -- i, which is idiomatic for soething entirely else. And add a comment, and be done with it. The solution that I had in mind is: while True: ... if condition: break if inifinte_loop(): raise infiinte_loop_exception This is a lot less understandable because whoever is working with your code will now have to check an additional function rather than a pretty self-explanatory variable increment. robert -- http://mail.python.org/mailman/listinfo/python-list
Re: Python tricks
RajNewbie wrote: 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. You may find it aesthetically pleasing, and it may very well be, but it will obfuscate your code and make it less maintainable. robert -- http://mail.python.org/mailman/listinfo/python-list