Re: [Tutor] Finding prime numbers script runs much faster when run via bash shell than idle
Jaime Gago wrote: > I wrote a simple piece of code as an exercise to an online -free- class > that finds prime numbers. When I run it via IDLE it's taking way more time > than if I run it via a (bash) shell (on Os X 10.6) while doing $>python -d > mypp.py > > I'm really curious from a performance perspective as to what could cause > such a noticeable difference. Displaying text in Idle's Python Shell window has significant overhead If you remove the line > print prime_counter, 'found so far' from your script it should take roughly the same time to run via bash and in Idle. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding prime numbers script runs much faster when run viabash shell than idle
"Jaime Gago" wrote When I run it via IDLE it's taking way more time than if I run it via a (bash) shell (on Os X 10.6) while doing $>python -d mypp.py I'm really curious from a performance perspective as to what could cause such a noticeable difference. IDLE is a development environment. As such it adds lots of extra hooks and debugging things to the execution environment. (For example it traps things like Ctrl C) In addition, being a GUI written in Tkinter, it has a slower output mechanism so print statements will be slower. IDLE is not intended to be used to run code except during development, it has ot been built for speed but for debugging. HTH, -- Alan Gauld 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
[Tutor] HELP
Hi... My name is Isaac, I need some help to programm in python, I know some things that are really basic like lists and recursivity and with that we shoul create an GPS with a global variable, but what I'm trying to do is to make a function that could call the global variable can I do something like that... or maybe a function like this: def function(Word):Word = Word Word= [Word]print (Word) and the problem is that if I ejecute that in the shell later I'm not able to call >>> Word because it returns me an error!!! THANKS 4 THE HELP ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] FW: HELP
Hi... My name is Isaac, I need some help to programm in python, I know some things that are really basic like lists and recursivity and with that we shoul create an GPS with a global variable, but what I'm trying to do is to make a function that could call the global variable can I do something like that... or maybe a function like this: def function(Word):Word = Word Word= [Word]print (Word) and the problem is that if I ejecute that in the shell later I'm not able to call >>> Word because it returns me an error!!! THANKS 4 THE HELP ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] HELP
2011/4/2 ISAAC Ramírez Solano > > Hi... My name is Isaac, I need some help to programm in python, I know some > things that are really basic like lists and recursivity and with that we > shoul create an GPS with a global variable, but what I'm trying to do is to > make a function that could call the global variable > > Read this page: http://www.saltycrane.com/blog/2008/01/python-variable-scope-notes/ Then come back if you have further questions. Regards Walter. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] HELP
"ISAAC Ramírez Solano" wrote Hi... My name is Isaac, I need some help to program in python, I know some things that are really basic like lists and recursivity I'm not sure what you mean by recursivity, but I'm betting its not what I mean by that term! :-) with that we should create an GPS with a global variable, I'm also not sure what you mean by GPS - to me that means Global Positioning Satellite but I suspect you mean something different? what I'm trying to do is to make a function that could call the global variable can I do something like that... Yes and any Python tutorial will explain how. You can try the Modules and Functions topic in my tutorial if you are not already reading another one. [ If your first language is Spanish or Portuguese you may prefer the translations in those languages better. (But they are only available for Python v2) ] def function(Word): Word = Word Word= [Word] print (Word) Thats not legal python, it may just be a formatting error. Lets assume so, it would then look like: def function(Word): Word = Word Word= [Word] print (Word) That creates a list containing the parameter passed in and then prints it. It then throws it away. if I execute that in the shell later I'm not able to call Word because it returns me an error!!! That would depend on whether you created Word before you called the function. I suspect you are confused about Python variables, Python scope, and Python functions. Read about those topics and then come back with any further questions. HTH, -- Alan Gauld 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] Finding prime numbers script runs much faster when run viabash shell than idle
Ok I understand now, thank you very much! On Apr 2, 2011, at 1:05 AM, Alan Gauld wrote: > "Jaime Gago" wrote >> When I run it via IDLE it's taking way more time than if I run it via a >> (bash) shell (on Os X 10.6) while doing $>python -d mypp.py >> I'm really curious from a performance perspective as to what could cause >> such a noticeable difference. > > IDLE is a development environment. As such it adds lots of extra hooks and > debugging things to the execution environment. (For example it traps things > like Ctrl C) In addition, being a GUI written in Tkinter, it has a slower > output mechanism > so print statements will be slower. > > IDLE is not intended to be used to run code except during development, it has > ot been built for speed but for debugging. > > HTH, > > -- > Alan Gauld > 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] 'for' iteration stop problem
Hi, My problem is simple for sure, but unfortunately I'm a bit beginner and I've stucked in it. I hope it is not a problem since as I understand this mailing list is for beginners. I have some problem with 'for' loop in algorithm. Code and description for this problem could be find here: http://stackoverflow.com/questions/5520145/how-to-stop-iteration-when-if-statement-is-true I will be very thankful! Cheers, Mateusz ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 'for' iteration stop problem
"Mateusz Korycinski" wrote My problem is simple for sure, but unfortunately I'm a bit beginner and I've stucked in it. I hope it is not a problem since as I understand this mailing list is for beginners. No problem, we try to answer questions such as this here. I have some problem with 'for' loop in algorithm. Code and description for this problem could be find here: http://stackoverflow.com/questions/5520145/how-to-stop-iteration-when-if-statement-is-true There are several ways to get out of a loop, the most common are: a) To exit a single level of loop use break for n in range(500): if n == 200: break else; pass b) To break out of a set of nested loops such as you have then use break combined with a sentinal and check the sentinal at each level: exitLoop = False for x in range(20): if exitLoop : break for y in range(50): if exitLoop: break for z in range(500): if z == 200: exitLoop = True break c) Use an exception: class ExitLoopException(Exception): pass try: for x in range(20): for y in range(50): for z in range(500): if z == 200: raise ExitLoopException except ExitLoopException: pass There are other ways but those should cover most eventualities. HTH, -- Alan Gauld 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] 'for' iteration stop problem
On Sat, Apr 2, 2011 at 5:45 PM, Alan Gauld wrote: > > There are other ways but those should cover most eventualities. I find that my preferred way when the loops are nested is to move the loops into a function and then return: def do_something(collection, collection2): for x in collection: for y in collection 2: if someCase(x,y): return It seems (at least to me) a little nicer than sentinel values or throwing an exception. Just my $.02 -Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 'for' iteration stop problem
Mateusz Koryciński wrote: I have some problem with 'for' loop in algorithm. Code and description for this problem could be find here: http://stackoverflow.com/questions/5520145/how-to-stop-iteration-when-if-statement-is-true Other people have answered your question about exiting nested loops, but that's not your real problem. Your real problem is that the piece of code shown is unreadable and unmaintainable! (1) Python has functions: please use them to break your code into small, easily understandable pieces. (2) You have a *mess* of unreadable variable names. You have: w.steps w.x w.y w.world data.c data.n data.e data.s data.w data.cc (yes, you have data.c AND data.cc!!!) plus loop variables z i j and r, and z is never used! How is anyone supposed to understand what this does? Even if you solve this problem *today*, in a week you will have forgotten what the code does and you won't understand it. (3) w.steps is never used except to needlessly(?) repeat the same calculations over and over again. (4) w.x and w.y at least seem to be fairly obvious, although the names are terrible. (5) What are w.world and all the data.* variables? Are they "north", "south", "east", "west"? (6) You give an example of some output, but you don't show the *input* that is used to generate that data, so it is meaningless. (7) You seem to be re-writing the data in place. That's *usually* a mistake (not always, but, say, 95% of the time). It will help if you can describe what problem you are trying to solve. Not the algorithm you have already chosen, but the actual problem -- what to you hope to do? There is probably a much better way. But for now, let me see if I can help refactor the code. You have: for z in range(w.steps): for i in range(1,w.x-1): for j in range(1,w.y-1): print (i, j) for r in data.c: if w.world[i][j] in r: print r ind = data.c.index(r) print ind if w.world[i-1][j] in data.n[ind]: if w.world[i][j+1] in data.e[ind]: if w.world[i+1][j] in data.s[ind]: if w.world[i][j-1] in data.w[ind]: w.world[i][j] = data.cc[ind] * make it a function that takes meaningfully named arguments, not just global variables; * throw away the top level loop, because z never gets used -- you're just needlessly repeating the same calculation w.steps times; * use helper functions to make your code more readable and to allow for future changes to the data structure; * comment your code! def get_neighbours(data, i, j): """Return the four nearest neighbours of data[i,j]. Returns the north, south, east, west neighbours in that order: [ [ . ] [ ... . n . ... ] [ ... w X e ... ] [ ... . s . ... ] [ . ] ] """ return data[i-1][j], data[i+1][j], data[i][j+1], data[i][j-1] def match_item(item, values): """Given a list of lists "values", match item against the items in the sublists, returning the index of the first matching value found. ("values" is a terrible name, but since I don't know what the problem you are solving is, I can't give it a more meaningful name.) >>> match_item(42, [[1, 2, 3], [11, 23, 37, 42, 55], [100, 200]]) (1, 3) """ for i, sublist in enumerate(values): if item in sublist: return (i, sublist.index(item)) # If we never match at all, it is an error. raise ValueError('item not in values') def iterate_data(data, values, north, south, east, west): """Iterate over data, doing something to it, I don't know what. data is a 2D array of numbers. values is a list of lists. I don't know why. north, south, east, west are four lists of numbers. They must all have the same number of items. """ assert len(north) == len(south) == len(east) == len(west) directions = (north, south, east, west) # Sorry, that is a poor, undescriptive name. # Skip the first and last row and column. for i in range(1, num_rows-1): for j in range(1, num_cols-1): cell = data[i][j] # Find a match with values (whatever that is!) x,y = match_item(cell, values) # Now compare that cell's neighbours. neighbours = get_neighbours(data, i, j) if any(a in b[y] for (a,b) in zip(neighbours, directions)): print "Matched neighbour (now what?)" I hope this helps. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor