Re: [Tutor] Why begin a function name with an underscore
2012/8/28 Richard D. Moores > On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett > wrote: > > > something like: > > > > def _validate_int(obj): > > """Raise an exception if obj is not an integer.""" > > m = int(obj + 0) # May raise TypeError. > > if obj != m: > > raise ValueError('expected an integer but got %r' % obj) > > > > > > is a really awkward way to test if something's an integer, and checking > > types in general is usually a sign of larger flaws in laying out useful > > code. > > What the best way to test if something's an integer? > >>>a = 4 >>>isinstance(a, int) True > > Dick Moores > ___ > 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
Re: [Tutor] Why begin a function name with an underscore
On Tue, Aug 28, 2012 at 12:13 AM, Jerry Zhang wrote: > > > 2012/8/28 Richard D. Moores > >> On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett >> wrote: >> >> > something like: >> > >> > def _validate_int(obj): >> > """Raise an exception if obj is not an integer.""" >> > m = int(obj + 0) # May raise TypeError. >> > if obj != m: >> > raise ValueError('expected an integer but got %r' % obj) >> > >> > >> > is a really awkward way to test if something's an integer, and checking >> > types in general is usually a sign of larger flaws in laying out useful >> > code. >> >> What the best way to test if something's an integer? > > a = 4 isinstance(a, int) > True >>> isinstance(3., int) False What if I wanted 3., 1234., etc. to be considered ints, as they are by _validate_int() ? Dick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why begin a function name with an underscore
Op 28-08-12 10:06, Richard D. Moores schreef: On Tue, Aug 28, 2012 at 12:13 AM, Jerry Zhang wrote: 2012/8/28 Richard D. Moores On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett wrote: something like: def _validate_int(obj): """Raise an exception if obj is not an integer.""" m = int(obj + 0) # May raise TypeError. if obj != m: raise ValueError('expected an integer but got %r' % obj) is a really awkward way to test if something's an integer, and checking types in general is usually a sign of larger flaws in laying out useful code. What the best way to test if something's an integer? a = 4 isinstance(a, int) True isinstance(3., int) False What if I wanted 3., 1234., etc. to be considered ints, as they are by _validate_int() ? >>> isinstance(3., (int, float)) True Because 3. is a float, not int. Timo Dick ___ 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
Re: [Tutor] Why begin a function name with an underscore
Timo wrote: > Op 28-08-12 10:06, Richard D. Moores schreef: >> On Tue, Aug 28, 2012 at 12:13 AM, Jerry Zhang >> wrote: >>> >>> 2012/8/28 Richard D. Moores >>> On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett wrote: > something like: > > def _validate_int(obj): > """Raise an exception if obj is not an integer.""" > m = int(obj + 0) # May raise TypeError. > if obj != m: > raise ValueError('expected an integer but got %r' % obj) > > > is a really awkward way to test if something's an integer, and > checking types in general is usually a sign of larger flaws in laying > out useful code. What the best way to test if something's an integer? >>> >> a = 4 >> isinstance(a, int) >>> True > isinstance(3., int) >> False >> >> What if I wanted 3., 1234., etc. to be considered ints, as they are by >> _validate_int() ? > > >>> isinstance(3., (int, float)) > True > > Because 3. is a float, not int. Note that the original check takes the value into account, too: >>> import pyprimes >>> pyprimes._validate_int(1.0) >>> pyprimes._validate_int(1.5) Traceback (most recent call last): File "", line 1, in File "pyprimes.py", line 286, in _validate_int raise ValueError('expected an integer but got %r' % obj) ValueError: expected an integer but got 1.5 >>> import fractions >>> pyprimes._validate_int(fractions.Fraction(10,2)) Personally, I'm a big fan of ducktyping, so I would probably remove the check completely and live with the consequences: >>> pyprimes._validate_int = lambda x: None >>> pyprimes.isprime_naive(8.5) True garbage-in, garbage-out -- so what. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why begin a function name with an underscore
On 28/08/2012 05:13, Richard D. Moores wrote: On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett wrote: something like: def _validate_int(obj): """Raise an exception if obj is not an integer.""" m = int(obj + 0) # May raise TypeError. if obj != m: raise ValueError('expected an integer but got %r' % obj) is a really awkward way to test if something's an integer, and checking types in general is usually a sign of larger flaws in laying out useful code. What the best way to test if something's an integer? Dick Moores ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor I rarely if ever test for any object type. I certainly wouldn't bother with the function above, I'd just like the program bomb and fix the fault at source. YMMV. -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why begin a function name with an underscore
On Tue, Aug 28, 2012 at 1:21 AM, Timo wrote: > Op 28-08-12 10:06, Richard D. Moores schreef: >> What if I wanted 3., 1234., etc. to be considered ints, as they are by >> _validate_int() ? > > isinstance(3., (int, float)) > True > > Because 3. is a float, not int. And >>> isinstance(3.7, (int, float)) True No, I'm asking for something equivalent to _validate_int(). Dick > > Timo > >> >> Dick >> >> ___ >> 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 maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why begin a function name with an underscore
On Tue, Aug 28, 2012 at 11:13 AM, Richard D. Moores wrote: > On Tue, Aug 28, 2012 at 1:21 AM, Timo wrote: > > Op 28-08-12 10:06, Richard D. Moores schreef: > > >> What if I wanted 3., 1234., etc. to be considered ints, as they are by > >> _validate_int() ? > > > > > isinstance(3., (int, float)) > > True > > > > Because 3. is a float, not int. > > And > >>> isinstance(3.7, (int, float)) > True > > No, I'm asking for something equivalent to _validate_int(). > > Dick > > kind of a hack, but how about: def validate_int(x): return int(x) == x >>> validate_int(3.4) False >>> validate_int(3.) True I know enough about floating point numbers to not trust this in obscure corner cases, but it might suffice for you. HTH, Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why begin a function name with an underscore
Richard D. Moores wrote: > On Tue, Aug 28, 2012 at 1:21 AM, Timo wrote: >> Op 28-08-12 10:06, Richard D. Moores schreef: > >>> What if I wanted 3., 1234., etc. to be considered ints, as they are by >>> _validate_int() ? >> >> > isinstance(3., (int, float)) >> True >> >> Because 3. is a float, not int. > > And isinstance(3.7, (int, float)) > True > > No, I'm asking for something equivalent to _validate_int(). That's not how it works. You have to give a spec first and then you can compare how well it is met by a particular implementation. If you start with an implementation and then declare its behaviour to be the spec the implementation will always "win". That's why ISO 29500 works so well -- for the company that wrote it. Anyway here's an alternative implementation: >>> def vi(x): ... if not isinstance(x, numbers.Number): ... raise TypeError ... if not int(x) == x: ... raise ValueError ... >>> vi("") Traceback (most recent call last): File "", line 1, in File "", line 3, in vi TypeError >>> vi(1.5) Traceback (most recent call last): File "", line 1, in File "", line 5, in vi ValueError >>> vi(1.0) >>> The differences to _validate_int() are subtle: >>> class S(str): ... def __eq__(self, other): return True ... def __ne__(self, other): return False ... def __add__(self, other): return self ... >>> vi(S("42")) Traceback (most recent call last): File "", line 1, in File "", line 3, in vi TypeError >>> _validate_int(S("42")) >>> ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Recursion always returns None
Hello, I am trying to do the following : 1) Ask user for the length of the word that he'd like to guess (for hangman game). 2) Pick a random word from /usr/share/dict/words (which I understand is not the best choice for hangman). 3) Call a function that would pick a random word to proceed further. Below is the code for the part I described above : [code] #!/bin/env python import random def pick_random(l, ln): # picks a random word from the list l of length ln global mystery word = random.choice(l) if word.__len__() != ln: pick_random(l, ln)# recursion else: print "Should return %s" % word # prints the chosen random word correctly return word # always return None, why? :( if __name__ == "__main__": ln = raw_input("How long word can you guess (number of alphabets) : ") ln = int(ln) l = [] with open("/usr/share/dict/words", "r") as f: for i in f.readlines(): i = i.split("\n")[0] if i.isalpha(): l.append(i) word = pick_random(l, ln) print word [/code] Sample output : $ python hangman.py How long word can you guess (number of alphabets) : 6 Should return inarch None $ The problem is that the last line "print word" always prints None. I know I am doing something wrong in the recursion part of the function "pick_random". Can someone please point what I am missing. Thank you! Cheers, Dharmit -- Dharmit Shah www.about.me/dharmit ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why begin a function name with an underscore
On Mon, 27 Aug 2012, Richard D. Moores wrote: On Mon, Aug 27, 2012 at 6:33 PM, Japhy Bartlett wrote: something like: def _validate_int(obj): """Raise an exception if obj is not an integer.""" m = int(obj + 0) # May raise TypeError. if obj != m: raise ValueError('expected an integer but got %r' % obj) is a really awkward way to test if something's an integer, and checking types in general is usually a sign of larger flaws in laying out useful code. What the best way to test if something's an integer? try: whatever_you_want(supposed_integer) except ValueError: print("Oops, that wasn't an integer! Please try again") That's usually the best way... HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recursion always returns None
On Tue, Aug 28, 2012 at 1:23 PM, Dharmit Shah wrote: > Hello, > > I am trying to do the following : > > 1) Ask user for the length of the word that he'd like to guess (for > hangman game). > 2) Pick a random word from /usr/share/dict/words (which I understand > is not the best choice for hangman). > 3) Call a function that would pick a random word to proceed further. > > Below is the code for the part I described above : > > [code] > > #!/bin/env python > import random > > def pick_random(l, ln): # picks a random word from the list > l of length ln > global mystery > word = random.choice(l) > if word.__len__() != ln: > pick_random(l, ln)# recursion > else: > print "Should return %s" % word # prints the chosen random > word correctly > return word # always > return None, why? :( > > Okay, a good technique here is to just go over the program step by step, keeping track of the call stack. An interactive debugger is great for this, but it's good to be able to do it in your mind too. Let's say we call pick_random with ln=6, and the nice wordlist you have. Our call stack looks like so: main (not in a function) --> pick_random(l, ln) So, we pick a random word, then compare its length (by the way, you should use len(word), not word.__len__(); it's much nicer to read). Now we have two possible options, either the word is the right length or it isn't. The first case is pretty trivial (return the word, done). So let's consider the second case. This will happen a lot, the chances of picking a 6 character word right away are pretty low. Okay so what happens now? We call pick_word again, pretty simple. Call stack: main --> pick_random(l, ln) --> pick_random(l, ln) pick_random's on the call stack twice now, which is what recursion means. Now we could go on and say we don't find the right length word again, and again, and again, but it won't give us any more information here. We'd just keep stacking up the same function. So let's say the second call to pick_random found a right word, and returned it. Call stack: main --> pick_random(l, ln) We're back in the first pick_random function, right where we left off. And now we can see our error, right here: if word.__len__() != ln: pick_random(l, ln)# recursion What do we do with the return value of the recursive call? Well, nothing. It just disappears. And then we continue on, past the if statement, fall out of the function, and pick_random will return None. What we should have done, is this: if word.__len__() != ln: return pick_random(l, ln)# recursion In general, when you call a function you should always be mindful of whether it has a return value, and where its return value is going. And when you're doing recursion, it is very important to understand how the call stack works, and how you can stack multiple instances of the same function on top of each other. HTH, Hugo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recursion always returns None
On 28-Aug-12 04:23, Dharmit Shah wrote: Hello, I am trying to do the following : 1) Ask user for the length of the word that he'd like to guess (for hangman game). 2) Pick a random word from /usr/share/dict/words (which I understand is not the best choice for hangman). 3) Call a function that would pick a random word to proceed further. Below is the code for the part I described above : I'm struggling to understand why you're using recursion here. It serves no evident purpose for what you're doing, and given a large dictionary of words, could seriously tank the performance of the application, quite possibly run it out of resources and crash it altogether. But since you're using recursion, note that when you make your recursive call, you're ignoring the return value from that call, so no returned value ever gets propagated out to the outer layers of recursion. If the outermost layer took the recursion (length of the chosen word is wrong), it calls itself and then never takes the branch including the return statement at all. ("return word" is never executed in that case). This causes the function to just 'fall off' the end which implicitly returns None. HTH [code] #!/bin/env python import random def pick_random(l, ln): # picks a random word from the list l of length ln global mystery word = random.choice(l) if word.__len__() != ln: pick_random(l, ln)# recursion else: print "Should return %s" % word # prints the chosen random word correctly return word # always return None, why? :( if __name__ == "__main__": ln = raw_input("How long word can you guess (number of alphabets) : ") ln = int(ln) l = [] with open("/usr/share/dict/words", "r") as f: for i in f.readlines(): i = i.split("\n")[0] if i.isalpha(): l.append(i) word = pick_random(l, ln) print word [/code] Sample output : $ python hangman.py How long word can you guess (number of alphabets) : 6 Should return inarch None $ The problem is that the last line "print word" always prints None. I know I am doing something wrong in the recursion part of the function "pick_random". Can someone please point what I am missing. Thank you! Cheers, Dharmit -- Steve Willoughby / st...@alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why begin a function name with an underscore
On Tue, Aug 28, 2012 at 6:00 AM, Peter Otten <__pete...@web.de> wrote: > > Anyway here's an alternative implementation: > def vi(x): > ... if not isinstance(x, numbers.Number): > ... raise TypeError > ... if not int(x) == x: > ... raise ValueError You could test against numbers.Integral. But it's not fool-proof. Someone can register an incompatible class with the abstract base class (ABC). >>> import numbers >>> isinstance("42", numbers.Integral) False >>> numbers.Integral.register(str) >>> isinstance("42", numbers.Integral) True http://docs.python.org/library/abc ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why begin a function name with an underscore
eryksun wrote: > On Tue, Aug 28, 2012 at 6:00 AM, Peter Otten <__pete...@web.de> wrote: >> >> Anyway here's an alternative implementation: >> > def vi(x): >> ... if not isinstance(x, numbers.Number): >> ... raise TypeError >> ... if not int(x) == x: >> ... raise ValueError > > You could test against numbers.Integral. That would reject "floats with an integral value" and therefore doesn't comply with the -- non-existing -- spec. > But it's not fool-proof. Nothing is. The code attempts to make expectations more explicit than Steven did in his original implementation. > Someone can register an incompatible class with the abstract base > class (ABC). > > >>> import numbers > >>> isinstance("42", numbers.Integral) > False > >>> numbers.Integral.register(str) > >>> isinstance("42", numbers.Integral) > True That's quite an elaborate scheme to shoot yourself in the foot ;) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why begin a function name with an underscore
On Tue, Aug 28, 2012 at 9:08 AM, Peter Otten <__pete...@web.de> wrote: > > That would reject "floats with an integral value" and therefore doesn't > comply with the -- non-existing -- spec. Gotcha. >> >>> import numbers >> >>> isinstance("42", numbers.Integral) >> False >> >>> numbers.Integral.register(str) >> >>> isinstance("42", numbers.Integral) >> True > > That's quite an elaborate scheme to shoot yourself in the foot ;) It was just a quick example. In practice what could happen is someone would register an integer-like class (instead of subclassing numbers.Integral or numbers.Number) that is incomplete and ends up raising an exception. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recursion always returns None
On 08/28/2012 07:23 AM, Dharmit Shah wrote: > Hello, > > I am trying to do the following : > > 1) Ask user for the length of the word that he'd like to guess (for > hangman game). > 2) Pick a random word from /usr/share/dict/words (which I understand > is not the best choice for hangman). > 3) Call a function that would pick a random word to proceed further. > > Below is the code for the part I described above : > > [code] > > #!/bin/env python > import random > > def pick_random(l, ln): # picks a random word from the list > l of length ln > global mystery > word = random.choice(l) > if word.__len__() != ln: > pick_random(l, ln)# recursion > else: > print "Should return %s" % word # prints the chosen random > word correctly > return word # always > return None, why? :( There's no return statement here, to cover the case where the if-clause succeeded. > if __name__ == "__main__": > ln = raw_input("How long word can you guess (number of alphabets) : ") > ln = int(ln) > l = [] > with open("/usr/share/dict/words", "r") as f: > for i in f.readlines(): > i = i.split("\n")[0] > if i.isalpha(): > l.append(i) > > word = pick_random(l, ln) > print word > > [/code] > > Sample output : > > $ python hangman.py > How long word can you guess (number of alphabets) : 6 > Should return inarch > None > $ > > The problem is that the last line "print word" always prints None. I > know I am doing something wrong in the recursion part of the function > "pick_random". Can someone please point what I am missing. Thank you! > > Cheers, > Dharmit > There are two things wrong, one of which has already been pointed out. But the most fundamental thing that's wrong is that once you have called the recursion, you don't return a value at all, simply falling off the end of the function. Python returns a value of None when you omit the return statement. So you should add a statement 'return word', which will eliminate the None. But of course it'll be the wrong word. To fix that, you need to assign the results of the call to pick_random() to the same local variable, word. As others have pointed out, this is a poor choice for recursion. Recursion can be more readable for some problems, when the problem statement is naturally recursive. But even then, it can frequently lead to stack overruns, and performance problems. But in this case a simple loop would make much more sense. So unless the instructor is requiring you to use recursion, please redo it as a loop. While we're at it, please use the len() function, rather than __len__() method. And instead doing a split() method for eliminating the linefeeds, what you really want to do is rstrip(). -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recursion always returns None
Hello, @Hugo Arts : Thank you! That was awesome to read. Thanks for the len() suggestion. @ Steve : Thank you. As suggested by Dave Angel, I am going to try the loop. And even before implementing it, I can feel that it's going to be more efficient than recursion. @Dave Angel : Thank you for the loop idea. It didn't strike me at all. @All : Thanks a bunch for helping me out. :) Cheers, Dharmit On Tue, Aug 28, 2012 at 7:18 PM, Dave Angel wrote: > On 08/28/2012 07:23 AM, Dharmit Shah wrote: >> Hello, >> >> I am trying to do the following : >> >> 1) Ask user for the length of the word that he'd like to guess (for >> hangman game). >> 2) Pick a random word from /usr/share/dict/words (which I understand >> is not the best choice for hangman). >> 3) Call a function that would pick a random word to proceed further. >> >> Below is the code for the part I described above : >> >> [code] >> >> #!/bin/env python >> import random >> >> def pick_random(l, ln): # picks a random word from the list >> l of length ln >> global mystery >> word = random.choice(l) >> if word.__len__() != ln: >> pick_random(l, ln)# recursion >> else: >> print "Should return %s" % word # prints the chosen random >> word correctly >> return word # always >> return None, why? :( > > There's no return statement here, to cover the case where the if-clause > succeeded. > >> if __name__ == "__main__": >> ln = raw_input("How long word can you guess (number of alphabets) : ") >> ln = int(ln) >> l = [] >> with open("/usr/share/dict/words", "r") as f: >> for i in f.readlines(): >> i = i.split("\n")[0] >> if i.isalpha(): >> l.append(i) >> >> word = pick_random(l, ln) >> print word >> >> [/code] >> >> Sample output : >> >> $ python hangman.py >> How long word can you guess (number of alphabets) : 6 >> Should return inarch >> None >> $ >> >> The problem is that the last line "print word" always prints None. I >> know I am doing something wrong in the recursion part of the function >> "pick_random". Can someone please point what I am missing. Thank you! >> >> Cheers, >> Dharmit >> > > There are two things wrong, one of which has already been pointed out. > But the most fundamental thing that's wrong is that once you have called > the recursion, you don't return a value at all, simply falling off the > end of the function. Python returns a value of None when you omit the > return statement. > > So you should add a statement 'return word', which will eliminate the > None. But of course it'll be the wrong word. To fix that, you need to > assign the results of the call to pick_random() to the same local > variable, word. > > As others have pointed out, this is a poor choice for recursion. > Recursion can be more readable for some problems, when the problem > statement is naturally recursive. But even then, it can frequently lead > to stack overruns, and performance problems. But in this case a simple > loop would make much more sense. So unless the instructor is requiring > you to use recursion, please redo it as a loop. > > While we're at it, please use the len() function, rather than __len__() > method. And instead doing a split() method for eliminating the > linefeeds, what you really want to do is rstrip(). > > > > -- > > DaveA > -- Dharmit Shah www.about.me/dharmit ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recursion always returns None
On 28/08/2012 16:51, Dharmit Shah wrote: @ Steve : Thank you. As suggested by Dave Angel, I am going to try the loop. And even before implementing it, I can feel that it's going to be more efficient than recursion. May I ask why you appear to be concerned with efficiency for a hangman game? -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recursion always returns None
On 28/08/12 16:51, Dharmit Shah wrote: @Dave Angel : Thank you for the loop idea. It didn't strike me at all. For some reason some beginners seem to find recursion a natural pattern. Many others, including quite experienced programmers find it a mind bending concept. But as a general rule, where you want to repeat an naction a number of times thing loops. If you know in advance how many times to repeat (eg all the items in a list) think 'for' loops, if you don't know how often (eg until some input condition is met) think 'while' loops. HTH, -- 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] Recursion always returns None
On 28-Aug-12 09:03, Mark Lawrence wrote: On 28/08/2012 16:51, Dharmit Shah wrote: @ Steve : Thank you. As suggested by Dave Angel, I am going to try the loop. And even before implementing it, I can feel that it's going to be more efficient than recursion. May I ask why you appear to be concerned with efficiency for a hangman game? Not the game per se, but if you're searching a file of thousands of words one at a time, randomly picking words and recursing if they're not what you intended, the hit--worst case--could be catastrophic. -- Steve Willoughby / st...@alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recursion always returns None
On 28-Aug-12 09:13, Alan Gauld wrote: On 28/08/12 16:51, Dharmit Shah wrote: @Dave Angel : Thank you for the loop idea. It didn't strike me at all. For some reason some beginners seem to find recursion a natural pattern. There is a certain "hey, you can do that? That's cool!" factor when you first discover recursion. When it naturally applies to a problem, it's still a powerful thing, but yes, it's often misapplied by beginners. -- Steve Willoughby / st...@alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Installing modules with easy_install
I'm working on another Python replacement for a Bash script, and I ran into a need for enhanced time zone functions. Following directions I found on a web site, I did the following: # easy_install --upgrade pytz Searching for pytz Reading http://pypi.python.org/simple/pytz/ Reading http://pytz.sourceforge.net Reading http://sourceforge.net/project/showfiles.php?group_id=79122 Reading http://www.stuartbishop.net/Software/pytz Reading http://sourceforge.net/projects/pytz/ Best match: pytz 2012d Downloading http://pypi.python.org/packages/2.7/p/pytz/pytz-2012d-py2.7.egg#md5=e6f9219ae6eff242f13c6700413df69e Processing pytz-2012d-py2.7.egg creating /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg Extracting pytz-2012d-py2.7.egg to /usr/local/lib/python2.7/dist-packages Adding pytz 2012d to easy-install.pth file Installed /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg Processing dependencies for pytz Finished processing dependencies for pytz Everything I'm reading suggests that now I should have the pytz module available to me. But from iPython: In [1]: import pytz --- ImportError Traceback (most recent call last) /home/ray/ in () ImportError: No module named pytz In [2]: import pytz-2012d File "", line 1 import pytz-2012d ^ SyntaxError: invalid syntax So what do I need to do to get Python to recognize this module? Ray ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
On Tue, Aug 28, 2012 at 1:41 PM, Ray Jones wrote: > I'm working on another Python replacement for a Bash script, and I ran > into a need for enhanced time zone functions. Following directions I > found on a web site, I did the following: > > # easy_install --upgrade pytz > Searching for pytz > Reading http://pypi.python.org/simple/pytz/ > Reading http://pytz.sourceforge.net > Reading http://sourceforge.net/project/showfiles.php?group_id=79122 > Reading http://www.stuartbishop.net/Software/pytz > Reading http://sourceforge.net/projects/pytz/ > Best match: pytz 2012d > Downloading > http://pypi.python.org/packages/2.7/p/pytz/pytz-2012d-py2.7.egg#md5=e6f9219ae6eff242f13c6700413df69e > Processing pytz-2012d-py2.7.egg > creating /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg > Extracting pytz-2012d-py2.7.egg to /usr/local/lib/python2.7/dist-packages > Adding pytz 2012d to easy-install.pth file > > Installed /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg > Processing dependencies for pytz > Finished processing dependencies for pytz > > > Everything I'm reading suggests that now I should have the pytz module > available to me. But from iPython: > > > In [1]: import pytz > --- > ImportError Traceback (most recent call last) > > /home/ray/ in () > > ImportError: No module named pytz > > > In [2]: import pytz-2012d > >File "", line 1 > import pytz-2012d > ^ > SyntaxError: invalid syntax > > > > So what do I need to do to get Python to recognize this module? > > > Ray > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor so you are calling it the wrong thing when you import see here: http://pytz.sourceforge.net/#example-usage Example & Usage Localized times and date arithmetic >>> from datetime import datetime, timedelta >>> from pytz import timezone >>> import pytz >>> utc = pytz.utc >>> utc.zone 'UTC' >>> eastern = timezone('US/Eastern') >>> eastern.zone 'US/Eastern' >>> amsterdam = timezone('Europe/Amsterdam') >>> fmt = '%Y-%m-%d %H:%M:%S %Z%z' -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
Ray Jones wrote: > I'm working on another Python replacement for a Bash script, and I ran > into a need for enhanced time zone functions. Following directions I > found on a web site, I did the following: > > # easy_install --upgrade pytz > Searching for pytz > Reading http://pypi.python.org/simple/pytz/ > Reading http://pytz.sourceforge.net > Reading http://sourceforge.net/project/showfiles.php?group_id=79122 > Reading http://www.stuartbishop.net/Software/pytz > Reading http://sourceforge.net/projects/pytz/ > Best match: pytz 2012d > Downloading > http://pypi.python.org/packages/2.7/p/pytz/pytz-2012d- py2.7.egg#md5=e6f9219ae6eff242f13c6700413df69e > Processing pytz-2012d-py2.7.egg > creating /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg > Extracting pytz-2012d-py2.7.egg to /usr/local/lib/python2.7/dist-packages > Adding pytz 2012d to easy-install.pth file > > Installed /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg > Processing dependencies for pytz > Finished processing dependencies for pytz > > > Everything I'm reading suggests that now I should have the pytz module > available to me. But from iPython: > > > In [1]: import pytz > --- > ImportError Traceback (most recent call > last) > > /home/ray/ in () > > ImportError: No module named pytz Do you have multiple python installations on your machine? Do you run easy_install in one and ipython in another? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
On 08/28/2012 11:06 AM, Peter Otten wrote: > Ray Jones wrote: > >> I'm working on another Python replacement for a Bash script, and I ran >> into a need for enhanced time zone functions. Following directions I >> found on a web site, I did the following: >> >> # easy_install --upgrade pytz >> Searching for pytz >> Reading http://pypi.python.org/simple/pytz/ >> Reading http://pytz.sourceforge.net >> Reading http://sourceforge.net/project/showfiles.php?group_id=79122 >> Reading http://www.stuartbishop.net/Software/pytz >> Reading http://sourceforge.net/projects/pytz/ >> Best match: pytz 2012d >> Downloading >> http://pypi.python.org/packages/2.7/p/pytz/pytz-2012d- > py2.7.egg#md5=e6f9219ae6eff242f13c6700413df69e >> Processing pytz-2012d-py2.7.egg >> creating /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg >> Extracting pytz-2012d-py2.7.egg to /usr/local/lib/python2.7/dist-packages >> Adding pytz 2012d to easy-install.pth file >> >> Installed /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg >> Processing dependencies for pytz >> Finished processing dependencies for pytz >> >> >> Everything I'm reading suggests that now I should have the pytz module >> available to me. But from iPython: >> >> >> In [1]: import pytz >> > --- >> ImportError Traceback (most recent call >> last) >> >> /home/ray/ in () >> >> ImportError: No module named pytz > Do you have multiple python installations on your machine? Do you run > easy_install in one and ipython in another? Perhaps. But the module is not accessible from the 'python' shell, from 'idle', or from iPython. As I peruse Synaptic I find I have active installations of Ubuntu's basic python, python2.7, and python2.7-minimal. But are these separate installations? Virtually every system package thinks it's dependent on each one of these python packages. Suggestions? Ray ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
On 29/08/12 03:41, Ray Jones wrote: I'm working on another Python replacement for a Bash script, and I ran into a need for enhanced time zone functions. Following directions I found on a web site, I did the following: # easy_install --upgrade pytz [...] Everything I'm reading suggests that now I should have the pytz module available to me. But from iPython: ImportError: No module named pytz Any time you get mysterious errors in iPython, or IDLE, or any other add-on to Python, it is important to determine whether the problem is with Python itself, or the add-on. In this case, start up the vanilla Python interactive environment by entering "python" at the $ prompt, then "import pytz" at the >>> prompt. If you get an error: - copy and paste the full traceback - show us the contents of sys.path You can also determine how many Python installations you have. At the bash $ prompt, type: python TAB TAB (that is, press the TAB key twice is succession, do not type the letters "t" "a" "b") and your shell will list the possible executable Python's on your system. Copy and paste that output. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
Ray Jones wrote: > On 08/28/2012 11:06 AM, Peter Otten wrote: >> Ray Jones wrote: >> >>> I'm working on another Python replacement for a Bash script, and I ran >>> into a need for enhanced time zone functions. Following directions I >>> found on a web site, I did the following: >>> >>> # easy_install --upgrade pytz >>> Searching for pytz >>> Reading http://pypi.python.org/simple/pytz/ >>> Reading http://pytz.sourceforge.net >>> Reading http://sourceforge.net/project/showfiles.php?group_id=79122 >>> Reading http://www.stuartbishop.net/Software/pytz >>> Reading http://sourceforge.net/projects/pytz/ >>> Best match: pytz 2012d >>> Downloading >>> http://pypi.python.org/packages/2.7/p/pytz/pytz-2012d- >> py2.7.egg#md5=e6f9219ae6eff242f13c6700413df69e >>> Processing pytz-2012d-py2.7.egg >>> creating /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg >>> Extracting pytz-2012d-py2.7.egg to >>> /usr/local/lib/python2.7/dist-packages Adding pytz 2012d to >>> easy-install.pth file >>> >>> Installed /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg >>> Processing dependencies for pytz >>> Finished processing dependencies for pytz >>> >>> >>> Everything I'm reading suggests that now I should have the pytz module >>> available to me. But from iPython: >>> >>> >>> In [1]: import pytz >>> >> --- >>> ImportError Traceback (most recent call >>> last) >>> >>> /home/ray/ in () >>> >>> ImportError: No module named pytz >> Do you have multiple python installations on your machine? Do you run >> easy_install in one and ipython in another? > Perhaps. But the module is not accessible from the 'python' shell, from > 'idle', or from iPython. > > As I peruse Synaptic I find I have active installations of Ubuntu's > basic python, python2.7, and python2.7-minimal. But are these separate > installations? No, I suspected that you had a system python and a self-compiled one, i. e. that you have both /usr/local/bin/python and /usr/bin/python If that were the case then you should be able to import pytz into one of these. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
On 08/28/2012 12:35 PM, Steven D'Aprano wrote: > On 29/08/12 03:41, Ray Jones wrote: >> I'm working on another Python replacement for a Bash script, and I ran >> into a need for enhanced time zone functions. Following directions I >> found on a web site, I did the following: >> >> # easy_install --upgrade pytz > [...] >> Everything I'm reading suggests that now I should have the pytz module >> available to me. But from iPython: > >> ImportError: No module named pytz > > > Any time you get mysterious errors in iPython, or IDLE, or any other > add-on to Python, it is important to determine whether the problem is > with Python itself, or the add-on. > > In this case, start up the vanilla Python interactive environment by > entering "python" at the $ prompt, then "import pytz" at the >>> prompt. > > If you get an error: > > - copy and paste the full traceback > > - show us the contents of sys.path > > > You can also determine how many Python installations you have. At the > bash $ prompt, type: > > python TAB TAB > > (that is, press the TAB key twice is succession, do not type the > letters "t" "a" "b") > > and your shell will list the possible executable Python's on your > system. Copy and paste that output. > I tried importing the module with each of the three shells that I have on my system (I think I need to get rid of idle - I never use it anymore): python, ipython, and idle. None of them recognize the module. Discovering which python binaries are available shows that I have /usr/bin/python and /usr/bin/python2.7. /usr/bin/python is a link to /usr/bin/python2.7. My sys.path shows the following: ['', '', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/pymodules/python2.7/gtk-2.0', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode', '/usr/lib/python2.7/dist-packages/IPython/Extensions', u'/home/ray/.ipython'] Aha! The sys path is in /usr/lib but the module was installed in /usr/local/lib. What's the easiest fix for this? Creating a link to the module in the OS, or adding (or changing) the sys.path? Ray ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
On 08/28/2012 12:44 PM, Peter Otten wrote: > Ray Jones wrote: > >> On 08/28/2012 11:06 AM, Peter Otten wrote: >>> Ray Jones wrote: >>> I'm working on another Python replacement for a Bash script, and I ran into a need for enhanced time zone functions. Following directions I found on a web site, I did the following: # easy_install --upgrade pytz Searching for pytz Reading http://pypi.python.org/simple/pytz/ Reading http://pytz.sourceforge.net Reading http://sourceforge.net/project/showfiles.php?group_id=79122 Reading http://www.stuartbishop.net/Software/pytz Reading http://sourceforge.net/projects/pytz/ Best match: pytz 2012d Downloading http://pypi.python.org/packages/2.7/p/pytz/pytz-2012d- >>> py2.7.egg#md5=e6f9219ae6eff242f13c6700413df69e Processing pytz-2012d-py2.7.egg creating /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg Extracting pytz-2012d-py2.7.egg to /usr/local/lib/python2.7/dist-packages Adding pytz 2012d to easy-install.pth file Installed /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg Processing dependencies for pytz Finished processing dependencies for pytz Everything I'm reading suggests that now I should have the pytz module available to me. But from iPython: In [1]: import pytz > --- ImportError Traceback (most recent call last) /home/ray/ in () ImportError: No module named pytz >>> Do you have multiple python installations on your machine? Do you run >>> easy_install in one and ipython in another? >> Perhaps. But the module is not accessible from the 'python' shell, from >> 'idle', or from iPython. >> >> As I peruse Synaptic I find I have active installations of Ubuntu's >> basic python, python2.7, and python2.7-minimal. But are these separate >> installations? > No, I suspected that you had a system python and a self-compiled one, i. e. > that you have both > > /usr/local/bin/python > > and > > /usr/bin/python > > If that were the case then you should be able to import pytz into one of > these. Bingo! Ray ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
On Tue, Aug 28, 2012 at 2:39 PM, Ray Jones wrote: > >> Do you have multiple python installations on your machine? Do you run >> easy_install in one and ipython in another? > > Perhaps. But the module is not accessible from the 'python' shell, from > 'idle', or from iPython. > > As I peruse Synaptic I find I have active installations of Ubuntu's > basic python, python2.7, and python2.7-minimal. But are these separate > installations? Virtually every system package thinks it's dependent on > each one of these python packages. > > Suggestions? Those are not separate installations. The python package depends on python2.7 and python-minimal. The latter depends on python2.7-minimal. You should be able to install pytz from the Ubuntu repository. Search for the package python-tz. If you install from the repository, be sure to manually delete the old installation in the local dist-packages directory. http://packages.ubuntu.com/search?searchon=names&keywords=python-tz That said, in Debian Wheezy, pytz installs and runs fine from /usr/local/lib/python2.7/dist-packages. You could hack a temporary fix with a .pth file or using the PYTHONPATH environment variable, but it's better to figure out the problem. To help debug your problem, first check which Python installation you're running. Run "ls -Hl `which python`". Is it /usr/bin/python? Next check whether the latter path is actually in sys.path. In Debian, on which Ubuntu is based, it gets added in /usr/lib/python2.7/site.py: elif os.sep == '/': sitepackages.append(os.path.join(prefix, "local/lib", "python" + sys.version[:3], "dist-packages")) sitepackages.append(os.path.join(prefix, "lib", "python" + sys.version[:3], "dist-packages")) Next check /usr/local/lib/python2.7/dist-packages/easy_install.pth. Does it have a line with "./pytz-2012d-py2.7.egg" to include the latter directory on the path (ignore the wonky code to modify the path's insertion point) ? Check inside that directory for a pytz directory that has an __init__.py. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
On 08/28/2012 12:35 PM, Steven D'Aprano wrote: > On 29/08/12 03:41, Ray Jones wrote: >> I'm working on another Python replacement for a Bash script, and I ran >> into a need for enhanced time zone functions. Following directions I >> found on a web site, I did the following: >> >> # easy_install --upgrade pytz > [...] >> Everything I'm reading suggests that now I should have the pytz module >> available to me. But from iPython: > >> ImportError: No module named pytz > > > Any time you get mysterious errors in iPython, or IDLE, or any other > add-on to Python, it is important to determine whether the problem is > with Python itself, or the add-on. > > In this case, start up the vanilla Python interactive environment by > entering "python" at the $ prompt, then "import pytz" at the >>> prompt. > > If you get an error: > > - copy and paste the full traceback > > - show us the contents of sys.path > > > You can also determine how many Python installations you have. At the > bash $ prompt, type: > > python TAB TAB > > (that is, press the TAB key twice is succession, do not type the > letters "t" "a" "b") > > and your shell will list the possible executable Python's on your > system. Copy and paste that output. Oops. No, I see that /usr/local/lib/python2.7/dist-packages is included in the sys.path. Now what? Ray ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
On 08/28/2012 12:52 PM, eryksun wrote: > On Tue, Aug 28, 2012 at 2:39 PM, Ray Jones wrote: >>> Do you have multiple python installations on your machine? Do you run >>> easy_install in one and ipython in another? >> Perhaps. But the module is not accessible from the 'python' shell, from >> 'idle', or from iPython. >> >> As I peruse Synaptic I find I have active installations of Ubuntu's >> basic python, python2.7, and python2.7-minimal. But are these separate >> installations? Virtually every system package thinks it's dependent on >> each one of these python packages. >> >> Suggestions? > Those are not separate installations. The python package depends on > python2.7 and python-minimal. The latter depends on python2.7-minimal. > You should be able to install pytz from the Ubuntu repository. Search > for the package python-tz. If you install from the repository, be sure > to manually delete the old installation in the local dist-packages > directory. I took out the pytz egg package and installed from Synaptic (I had looked there originally, but I searched pytz rather than python-tz). I now have an importably pytz. So that particular issue is cleared up, but what about modules that aren't available in Synaptic? It seems I still have an issue with easy_install, no? Ray ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
On Tue, Aug 28, 2012 at 4:00 PM, Ray Jones wrote: > > Oops. No, I see that /usr/local/lib/python2.7/dist-packages is included > in the sys.path. Now what? Good, but does sys.path contain /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
On 08/28/2012 01:11 PM, eryksun wrote: > On Tue, Aug 28, 2012 at 4:00 PM, Ray Jones wrote: >> Oops. No, I see that /usr/local/lib/python2.7/dist-packages is included >> in the sys.path. Now what? > Good, but does sys.path contain > /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg? No. Ray ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
On 08/28/2012 01:11 PM, eryksun wrote: > On Tue, Aug 28, 2012 at 4:00 PM, Ray Jones wrote: >> Oops. No, I see that /usr/local/lib/python2.7/dist-packages is included >> in the sys.path. Now what? > Good, but does sys.path contain > /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg? More info: /usr/lib/python2.7/dist-packages does not contain a easy-install.pth file. Ray ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
On 8/28/2012 1:17 PM Ray Jones said... On 08/28/2012 01:11 PM, eryksun wrote: On Tue, Aug 28, 2012 at 4:00 PM, Ray Jones wrote: Oops. No, I see that /usr/local/lib/python2.7/dist-packages is included in the sys.path. Now what? Good, but does sys.path contain /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg? More info: /usr/lib/python2.7/dist-packages does not contain a easy-install.pth file. You installed this with easy_install, so the version of python therein referenced is the same one that now should have access to it. Try : which easy_install then cat the result. The first line points to the executable python that installed pytz. On my local system this looks like: head -1 `which easy_install` #! /usr/bin/python copy and paste in the result, then import pytz from there and paste in the results if it doesn't work. Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
On 08/28/2012 01:35 PM, Emile van Sebille wrote: > On 8/28/2012 1:17 PM Ray Jones said... >> On 08/28/2012 01:11 PM, eryksun wrote: >>> On Tue, Aug 28, 2012 at 4:00 PM, Ray Jones wrote: Oops. No, I see that /usr/local/lib/python2.7/dist-packages is included in the sys.path. Now what? >>> Good, but does sys.path contain >>> /usr/local/lib/python2.7/dist-packages/pytz-2012d-py2.7.egg? >> More info: >> >> /usr/lib/python2.7/dist-packages does not contain a easy-install.pth >> file. >> > > You installed this with easy_install, so the version of python therein > referenced is the same one that now should have access to it. > > Try : which easy_install > > then cat the result. The first line points to the executable python > that installed pytz. On my local system this looks like: > > > head -1 `which easy_install` > #! /usr/bin/python > > copy and paste in the result, then import pytz from there and paste in > the results if it doesn't work. #! /usr/bin/python I think you come late to the party, but jump in - there's lots of room! We have solved the pytz problem by quarantining the pytz*.egg and installing python-tz from the Debian system. But while pytz now works, it does not address the problem of easy-install-ed modules not being recognized by python (btw, in perusing the /usr/local/bin/.../dist-packages I also found a shodan module (whatever that is) that python does not recognize - and I don't find shodan in the Debian packages). Ray ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recursion always returns None
On 28/08/12 17:34, Steve Willoughby wrote: For some reason some beginners seem to find recursion a natural pattern. There is a certain "hey, you can do that? That's cool!" factor when you first discover recursion. My point was that it seems to be a natural idea for many beginners, they discover it without being told. They just assume it will work. It comes up time and time again on this list from people who have never heard of it but are using it. Whereas others who need to be explicitly taught about it find it totally bizarre and mind bending. I've come to the conclusion that its a right-brain, left-brain type of thing. For some it just seems logical, for others perverse! Presumably John McCarthy was one of those who found it natural! :-) -- 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] Recursion always returns None
On Tue, Aug 28, 2012 at 5:14 PM, Alan Gauld wrote: > On 28/08/12 17:34, Steve Willoughby wrote: >>> For some reason some beginners seem to find recursion a natural pattern. >> >> >> There is a certain "hey, you can do that? That's cool!" factor when you >> first discover recursion. > > > My point was that it seems to be a natural idea for many beginners, they > discover it without being told. They just assume it will work. > It comes up time and time again on this list from people who have > never heard of it but are using it. > > Whereas others who need to be explicitly taught about it find it totally > bizarre and mind bending. I've come to the conclusion that its a > right-brain, left-brain type of thing. For some it just seems logical, for > others perverse! > > Presumably John McCarthy was one of those who found it natural! :-) > > > -- > 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 Interesting idea. I thought it was pretty cool when I studied it, and re-study recursion, but I seldom think of writing code with recursion. It scares me in an unnatural way. I think that best problems for recursion are ones with really deep data structures, and i fear they will run out of stack space. No evidence, just my take on using recursion as opposed to liking to read how small some recursive solutions are. -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Installing modules with easy_install
On 8/28/2012 1:48 PM Ray Jones said... On 08/28/2012 01:35 PM, Emile van Sebille wrote: You installed this with easy_install, so the version of python therein referenced is the same one that now should have access to it. Try : which easy_install then cat the result. The first line points to the executable python that installed pytz. On my local system this looks like: head -1 `which easy_install` #! /usr/bin/python copy and paste in the result, then import pytz from there and paste in the results if it doesn't work. #! /usr/bin/python I think you come late to the party, but jump in - there's lots of room! We have solved the pytz problem by quarantining the pytz*.egg and installing python-tz from the Debian system. But while pytz now works, it does not address the problem of easy-install-ed modules not being recognized by python (btw, in perusing the /usr/local/bin/.../dist-packages I also found a shodan module (whatever that is) that python does not recognize - and I don't find shodan in the Debian packages). My point was that easy_install isn't broken. Emile root@paj39:/home/emile/web# /usr/bin/easy_install pytz Searching for pytz Best match: pytz 2009l Adding pytz 2009l to easy-install.pth file Using /usr/lib/python2.6/dist-packages Processing dependencies for pytz Finished processing dependencies for pytz root@paj39:/home/emile/web# /usr/bin/python Python 2.6.4rc2 (r264rc2:75497, Oct 20 2009, 02:55:11) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import pytz >>> ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] running more than one python program at the same time
Hello, I wrote a program that I want to have running 24/7. But the problem is that I also want to write and run other programs. I'm using Idle and it won't let me run more than one script at a time. Do you know if there's a way to do this? Or do I need to buy a second computer? Thanks, Ben ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] running more than one python program at the same time
On Tue, Aug 28, 2012 at 3:30 PM, Benjamin Fishbein wrote: > Hello, > I wrote a program that I want to have running 24/7. But the problem is > that I also want to write and run other programs. I'm using Idle and it > won't let me run more than one script at a time. Do you know if there's a > way to do this? Or do I need to buy a second computer? > Thanks, > Ben > > IDLE is just an IDE (Integrated Development Environment), meant to improve the convenience and efficiency of writing Python. It is NOT intended to be the primary way you run your Python scripts once you've written them, and it specifically cannot handle multiple scripts executing simultaneously. So the general answer to your question is: only use IDLE for writing and testing your script, not for running it once it's production-ready. Under Windows (which, for my sins, is my primary environment), you can: - open multiple command prompts (Start/Run/CMD) and type "python MyScript.py" in each of them - double-click on the icon for each .py or file, which will launch a copy of Python and execute the script - create a Task or a Service which will run "python MyScript.py" either at boot-up or at a scheduled time - or any of a few other methods. You have similar options in Mac and Linux environments, but I'll leave it to others to enumerate them. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] running more than one python program at the same time
On 08/28/2012 03:30 PM, Benjamin Fishbein wrote: > Hello, > I wrote a program that I want to have running 24/7. But the problem is that I > also want to write and run other programs. I'm using Idle and it won't let me > run more than one script at a time. Do you know if there's a way to do this? > Or do I need to buy a second computer? > Thanks, > Ben Can you make each script executable and run them without idle? Ray ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] running more than one python program at the same time
On 28 Aug 2012 18:33, "Benjamin Fishbein" wrote: > > Hello, > I wrote a program that I want to have running 24/7. But the problem is that I also want to write and run other programs. I'm using Idle and it won't let me run more than one script at a time. Do you know if there's a way to do this? Or do I need to buy a second computer? > Thanks, > Ben Hi Ben, Idle may be useful for developing with (provided you aren't making an app with tkinter) but isn't always the best choice for running one. Do you know how to run python from a command prompt? (If not, post back to the list being sure to tell us your OS and I or someone else will help you.) If you run you 24/7 program that way, idle will be free for you to work. There are other ways to get your program to run in the background, but again these are OS-dependant. Best, Brian vdB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] running more than one python program at the same time
I'm on a Mac. Using Lion. I just tried opening the terminal and typing "python." And I'm able to open several terminal windows this way. I think this should be able to run many programs simultaneously. Thanks for your help. -Ben On Tue, Aug 28, 2012 at 6:04 PM, Brian van den Broek < brian.van.den.br...@gmail.com> wrote: > > On 28 Aug 2012 18:33, "Benjamin Fishbein" wrote: > > > > Hello, > > I wrote a program that I want to have running 24/7. But the problem is > that I also want to write and run other programs. I'm using Idle and it > won't let me run more than one script at a time. Do you know if there's a > way to do this? Or do I need to buy a second computer? > > Thanks, > > Ben > > Hi Ben, > > Idle may be useful for developing with (provided you aren't making an app > with tkinter) but isn't always the best choice for running one. > > Do you know how to run python from a command prompt? (If not, post back to > the list being sure to tell us your OS and I or someone else will help > you.) If you run you 24/7 program that way, idle will be free for you to > work. There are other ways to get your program to run in the background, > but again these are OS-dependant. > > Best, > > Brian vdB > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] running more than one python program at the same time
Stupid question: how do I run a program from the terminal? I've always just gone to the drop down menu and clicked run to do it in idle. On Tue, Aug 28, 2012 at 6:17 PM, Ben Fishbein wrote: > I'm on a Mac. Using Lion. I just tried opening the terminal and typing > "python." And I'm able to open several terminal windows this way. I think > this should be able to run many programs simultaneously. Thanks for your > help. > -Ben > > > On Tue, Aug 28, 2012 at 6:04 PM, Brian van den Broek < > brian.van.den.br...@gmail.com> wrote: > >> >> On 28 Aug 2012 18:33, "Benjamin Fishbein" wrote: >> > >> > Hello, >> > I wrote a program that I want to have running 24/7. But the problem is >> that I also want to write and run other programs. I'm using Idle and it >> won't let me run more than one script at a time. Do you know if there's a >> way to do this? Or do I need to buy a second computer? >> > Thanks, >> > Ben >> >> Hi Ben, >> >> Idle may be useful for developing with (provided you aren't making an app >> with tkinter) but isn't always the best choice for running one. >> >> Do you know how to run python from a command prompt? (If not, post back >> to the list being sure to tell us your OS and I or someone else will help >> you.) If you run you 24/7 program that way, idle will be free for you to >> work. There are other ways to get your program to run in the background, >> but again these are OS-dependant. >> >> Best, >> >> Brian vdB >> > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] running more than one python program at the same time
On 08/28/2012 07:19 PM, Ben Fishbein wrote: > Stupid question: how do I run a program from the terminal? I've always just > gone to the drop down menu and clicked run to do it in idle. > > Haven't you noticed that the correct method of posting on this forum is to put your remarks AFTER the part you're responding to? And deleting the parts you're not responding to? This message was top-posted. Anyway, to run a program from the terminal, you type its name, followed by any arguments it may have. So if you want to run python, type python from the prompt. i think you knew that. Perhaps you're asking how to pass the name of the script to python. Fortunately, it simply expects the path name to the script. So if your script is at ./'mydir/myscript.py, you'd type: Ben@mymachine:~$ python mydir/myscript.py If the script takes arguments, you'd put them after the script name. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] running more than one python program at the same time
On 29/08/12 08:30, Benjamin Fishbein wrote: Hello, I wrote a program that I want to have running 24/7. But the problem is that I also want to write and run other programs. I'm using Idle and it won't let me run more than one script at a time. Then don't use IDLE. IDLE is for running code interactively, not for long-running programs Do you know if there's a way to do this? Or do I need to buy a second computer? Your computer is already running dozens, possible hundreds of programs simultaneously. On my home box, I can see 198 programs currently active. They don't call them "multitasking operating systems" for nothing :) (Actually, "multitasking" is one of those things that people no longer talk about, because it's just assumed that *every* computer does it. This was not always the case -- twenty years ago, the difference between single and multitasking computers was a big deal. Now, probably the only computer you have that doesn't multitask is your microwave oven. Even your iPhone multitasks -- it just won't let apps multitask, but the phone itself does.) You need to run your python script the same way you run any other script for your system: In Windows, you will need something like a batch file or equivalent, which directly runs your script using the Python interpreter. Put this batch file wherever you put other batch files that you want to be run automatically when the system starts up. In Linux, you can set your script as an init.d script to have it automatically run by the operating system. If you're using a GUI desktop like KDE, Trinity, Gnome or similar, it will have something equivalent to a "startup folder" where you put files you want to run when the desktop starts. If you're running a Mac, there will be something similar. If you can't be bothered, or don't want, your system to automatically start up your script, you can run it manually from your system's shell. In Windows, that is the DOS prompt -- either cmd.com or command.exe, I never remember which one is which. In Linux, you start up an xterm or other terminal window. Using KDE, I can do either of these: Start menu > System > Terminal or right-click on the desktop and select "Konsole". Because I use a terminal so often, I have a shortcut permanently in my task bar so I can open a terminal with one click. Other desktops will have something similar. However you do it, you will get a terminal/xterm/console/DOS prompt window, showing a dollar sign prompt: $ That's the shell, waiting for you to give it commands. You can now run your python script. At the prompt, type: python /path/to/my/script.py then press ENTER. If your script needs arguments passed from the command line, put them after the path: python /path/to/my/script.py -z --foo --bar spam ham eggs 23 42 but I'm guessing that if you don't know how to run a script from the command line, it probably doesn't need command line arguments :) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] running more than one python program at the same time
> On 08/28/2012 03:30 PM, Benjamin Fishbein wrote: >> Hello, >> I wrote a program that I want to have running 24/7. But the problem is >> that I also want to write and run other programs. I'm using Idle and it >> won't let me run more than one script at a time. Do you know if there's >> a way to do this? Or do I need to buy a second computer? >> Thanks, >> Ben > Can you make each script executable and run them without idle? > > > Ray > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > The following works with Linux and might with OSX as well. add "#!/usr/bin/env python" as the first line of your script. Then from the terminal change its permissions: $ chmod 755 /paht/to/my/script/script.py After that you can start your program with: $ /path/to/my/script/script.py If you add "&" to the end of the line it'll go into the background and you'll get your terminal back. I am less confident that the following will work on your Mac but there is probably something equivalent. If you wanted it to run when ever the computer is on, see if there is a file called "/etc/rc.local" If there is, edit it (you'll need root privileges to do so) and add as a last line: "/path/to/my/script/script.py" The words between the slashes will of course have to be modified to suit your situation. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor