From swdunning at me.com Tue Apr 1 00:53:59 2014 From: swdunning at me.com (Scott Dunning) Date: Mon, 31 Mar 2014 15:53:59 -0700 Subject: [Tutor] while loop In-Reply-To: References: <272461FE-60A1-4614-A968-18EC0AF87831@cox.net> <397E85C0-1C2C-44AF-9379-A2C8F4E48B86@me.com> <5D78B877-4314-4FA8-A547-A402F962A6E2@me.com> Message-ID: On Mar 31, 2014, at 2:01 AM, Alan Gauld wrote: > > Incidentally, your assignment does not appear to require > a while loop, just iteration? If thats the case you could > use a for loop instead and it would actually be more > suitable. Have you covered for loops yet? > No, we haven?t got to for loops yet, that?s why it needs to be done with a while for now. From swdunning at me.com Tue Apr 1 01:04:51 2014 From: swdunning at me.com (Scott Dunning) Date: Mon, 31 Mar 2014 16:04:51 -0700 Subject: [Tutor] while loop In-Reply-To: References: <272461FE-60A1-4614-A968-18EC0AF87831@cox.net> <397E85C0-1C2C-44AF-9379-A2C8F4E48B86@me.com> <5D78B877-4314-4FA8-A547-A402F962A6E2@me.com> Message-ID: On Mar 31, 2014, at 1:39 AM, Mark Lawrence wrote: > > They say that the truth hurts, so if that's the best you can come up with, I suggest you give up programming :( You?re in the TUTOR section. People in here are new to programming. I?ve only been doing this for a couple months and I just learned about while loops two days ago. If my questions are annoying you I suggest you not read them. : ( From scott.w.d at cox.net Tue Apr 1 03:07:52 2014 From: scott.w.d at cox.net (Scott W Dunning) Date: Mon, 31 Mar 2014 18:07:52 -0700 Subject: [Tutor] exercise (while loop) Message-ID: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> I?m working on a few exercises and I?m a little stuck on this one. This is what the book has but it just gives me an endless loop. def square_root(a, eps=1e-6): while True: print x y = (x + a/x) / 2 if abs(y-x) < epsilon: break round(square_root(9)) I tweaked it to what I thought was correct but when I test it I get nothing back. def square_root(a, eps=1e-6): x = a/2.0 while True: y = (x + a/x)/2.0 if abs(x - y) < eps: return y x = y round(square_root(9)) The way I tweaked it seems to work, I?m getting the correct answer on the calculator but the interpreter is not returning anything when I check in python. The books way is just print whatever I use for x, so I don?t understand that at all. From dyoo at hashcollision.org Tue Apr 1 03:47:52 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 31 Mar 2014 18:47:52 -0700 Subject: [Tutor] exercise (while loop) In-Reply-To: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> Message-ID: On Mar 31, 2014 6:22 PM, "Scott W Dunning" wrote: > > I?m working on a few exercises and I?m a little stuck on this one. > > This is what the book has but it just gives me an endless loop. > > def square_root(a, eps=1e-6): > while True: > print x > y = (x + a/x) / 2 > if abs(y-x) < epsilon: > break > > round(square_root(9)) Hi Scott, Ah. I think I see what might be wrong, but let's make sure about this. Can you explain what 'x', 'y' are in this function? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Tue Apr 1 03:50:39 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 31 Mar 2014 18:50:39 -0700 Subject: [Tutor] exercise (while loop) In-Reply-To: References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> Message-ID: Also, which book? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Tue Apr 1 04:10:06 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 31 Mar 2014 19:10:06 -0700 Subject: [Tutor] exercise (while loop) In-Reply-To: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> Message-ID: > I tweaked it to what I thought was correct but when I test it I get nothing back. > > def square_root(a, eps=1e-6): > x = a/2.0 > while True: > y = (x + a/x)/2.0 > if abs(x - y) < eps: > return y > x = y > > round(square_root(9)) > > The way I tweaked it seems to work, I?m getting the correct answer on the calculator but the interpreter is not returning anything when I check in python. I didn't want to keep you waiting, so I'll cut to the chase. This line here in your program: round(square_root(9)) computes a value... But it doesn't do anything with that value. Try printing the value. You may also try to see that your program is doing something effective by "unit testing" it. This is often a lot better than just printing values and looking at them, because the test case will say what the _expected_ value is, so it's more informative. For this example, the following is a start at unit testing the above function. Add the following to the bottom of your program's source. ############################################### ## See: http://www.openp2p.com/pub/a/python/2004/12/02/tdd_pyunit.html import unittest class SquareRootTests(unittest.TestCase): def testSimpleCases(self): self.assertAlmostEqual(square_root(1), 1.0) self.assertAlmostEqual(square_root(4), 2.0) if __name__ == '__main__': unittest.main() ############################################### Here's what it looks like when I run this: ############################################## $ python sqrt.py 4.472135955 . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK ############################################## You can then start adding more and more to tests to gain confidence that the code is doing something reasonable. If we try to put in an intentionally broken test, like: self.assertAlmostEqual(square_root(3), 2.0) in the body of testSimpleCases(), then we'll see the following error when running the program: ############################################## $ python sqrt.py 4.472135955 F ====================================================================== FAIL: testSimpleCases (__main__.SquareRootTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "sq.py", line 20, in testSimpleCases self.assertAlmostEqual(square_root(3), 2.0) AssertionError: 1.7320508075688772 != 2.0 within 7 places ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (failures=1) ############################################## And that's what you want to see. If either the test or the code is bad, it'll say something about it. One other thing: you will want to check a particularly insidious case that will cause the program here to behave badly. Consider the zero case: square_root(0). Write the test case. Run it. You'll see something interesting. Good luck! From dyoo at hashcollision.org Tue Apr 1 04:29:16 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 31 Mar 2014 19:29:16 -0700 Subject: [Tutor] Fwd: Python bingo game. In-Reply-To: References: <2FBCC06A-A81B-41C9-8C67-F808F2AA28F0@yahoo.com> Message-ID: > > What difficulty are you having? I need to be straightforward so that > you understand, without ambiguity: we do not do your homework. We > will not violate the honor code of your institution. To do so is > anathema to why folks here volunteer to help beginners. > I do want to apologize if the tone of the reply was a bit curt. It's just this: if you provide any information about what you've tried, or what your experience is, you untie our hands and let us help. Otherwise, our hands really are tied and we're limited in what we can say. The more you can say about what you've tried and done, the more freedom you give us to help you. For the question you've provided, if it we're not phrased as a homework question, I'd point directly to the Numpy library, because matrix transpose is already provided in that library: http://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html But in your context, I don't think this is what you're asking. So that's another reason why you need to tell us enough sufficient detail that we can give you appropriate advice. Good luck! From dyoo at hashcollision.org Tue Apr 1 05:04:39 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 31 Mar 2014 20:04:39 -0700 Subject: [Tutor] FASTA parsing, biological sequence analysis In-Reply-To: <5331A26C.7050302@virginmedia.com> References: <5331A26C.7050302@virginmedia.com> Message-ID: On Tue, Mar 25, 2014 at 8:36 AM, Sydney Shall wrote: > I did not know about biopython, but then I am a debutant. > I tried to import biopython and I get the message that the name is unknown. No problem. It is an external library; I hope that you were able to find it! I just want to make sure no one else tries to write yet another FASTA parser badly. It's all too easy to code something quick-and-dirty that almost solves the issue. The devil's in the details. It might be instructive to look at source code. You can look at: https://github.com/biopython/biopython/blob/master/Bio/SeqIO/FastaIO.py and see all the implementation details the Biopython community has had to consider in the real world. These include things like skipping crazy garbage at the beginning of files, https://github.com/biopython/biopython/blob/master/Bio/SeqIO/FastaIO.py#L40-L45 and providing a stream-like interface by using generators (using the "yield" command): https://github.com/biopython/biopython/blob/master/Bio/SeqIO/FastaIO.py#L65 But also consider data validation facilities. At least, the Biopython folks have. They provide a way to declare the genomic alphabet to be used: https://github.com/biopython/biopython/blob/master/Bio/SeqIO/FastaIO.py#L73 https://github.com/biopython/biopython/blob/master/Bio/Alphabet/ where if the input data doesn't match the allowed alphabet, you'll get a good warning about it ahead of time. This is checked in places like: https://github.com/biopython/biopython/blob/master/Bio/Alphabet/__init__.py#L375 https://github.com/biopython/biopython/blob/master/Bio/Seq.py#L336 In short, in the presence of potentially messy data, the developers have thought about these sorts of issues and have programmed for those situations. As the commit history demonstrates: https://github.com/biopython/biopython/commits/master they started work in the last century or so (since at least 1999-12-07), and continue to work on it even now. So taking advantage of their generous and hard work is a good idea. From dyoo at hashcollision.org Tue Apr 1 06:19:21 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 31 Mar 2014 21:19:21 -0700 Subject: [Tutor] exercise (while loop) In-Reply-To: References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> Message-ID: On Mon, Mar 31, 2014 at 8:48 PM, Scott W Dunning wrote: > > On Mar 31, 2014, at 7:10 PM, Danny Yoo wrote: > Thanks for the info Danny! I?ll try that and I should be able to figure it out with your help! > > The book I was referring to is greentreepress. The reason I'm asking is I want to double check the example code. Checking... http://greenteapress.com/ ... but Green Tree Press publishes a few Python books. Hmmm. I will guess that you mean: Allen Downey's: "How to Think Like a Computer Scientist". Ah, found it. http://greenteapress.com/thinkpython/html/thinkpython008.html#toc81 But please, try to provide details. You tend to suppress helpful details. I would like to avoid guessing next time, so be aware that we don't see what you're thinking. Ok, I see now what you were looking at. But we need to wheel back around to one of your original questions. You said: > This is what the book has but it just gives me an endless loop. > > def square_root(a, eps=1e-6): > while True: > print x > y = (x + a/x) / 2 > if abs(y-x) < epsilon: > break > > round(square_root(9)) Go back and look at that text again: http://greenteapress.com/thinkpython/html/thinkpython008.html#toc81 and now see that the book does not present a function in that section. Instead, it's showing exploratory code. There's no function there, all the state is global, and it's not computing a return value. So you shouldn't be too surprised that the code the book is presenting, as a non-functional example, requires some adaptation before it works as a function. From scott.w.d at cox.net Tue Apr 1 05:48:23 2014 From: scott.w.d at cox.net (Scott W Dunning) Date: Mon, 31 Mar 2014 20:48:23 -0700 Subject: [Tutor] exercise (while loop) In-Reply-To: References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> Message-ID: On Mar 31, 2014, at 7:10 PM, Danny Yoo wrote: Thanks for the info Danny! I?ll try that and I should be able to figure it out with your help! The book I was referring to is greentreepress. From swdunning at me.com Tue Apr 1 01:22:13 2014 From: swdunning at me.com (Scott Dunning) Date: Mon, 31 Mar 2014 16:22:13 -0700 Subject: [Tutor] while loop In-Reply-To: References: <272461FE-60A1-4614-A968-18EC0AF87831@cox.net> <397E85C0-1C2C-44AF-9379-A2C8F4E48B86@me.com> <5D78B877-4314-4FA8-A547-A402F962A6E2@me.com> Message-ID: On Mar 31, 2014, at 5:15 AM, Dave Angel wrote: > > Do you know how to define and initialize a second local variable? > Create one called i, with a value zero. > > You test expression will not have a literal, but compare the two > locals. And the statement that increments will change i, not > n. So like this? def print_n(s,n): i = 0 while i < n: print s, i += 1 print_n('a',3) So this is basically making i bigger by +1 every time the while loop passes until it passes n, then it becomes false right? Also, with this exercise it?s using a doctest so I don?t actually call the function so I can?t figure out a way to make the string?s print on separate lines without changing the doctest code? Thanks for all of the help! From breamoreboy at yahoo.co.uk Tue Apr 1 11:09:34 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 01 Apr 2014 10:09:34 +0100 Subject: [Tutor] exercise (while loop) In-Reply-To: References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> Message-ID: On 01/04/2014 02:47, Danny Yoo wrote: > > On Mar 31, 2014 6:22 PM, "Scott W Dunning" > wrote: > > > > I?m working on a few exercises and I?m a little stuck on this one. > > > > This is what the book has but it just gives me an endless loop. > > > > def square_root(a, eps=1e-6): > > while True: > > print x > > y = (x + a/x) / 2 > > if abs(y-x) < epsilon: > > break > > > > round(square_root(9)) > > Hi Scott, > > Ah. I think I see what might be wrong, but let's make sure about this. > > Can you explain what 'x', 'y' are in this function? > And the difference between eps and epsilon while (ouch) we're at it. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From davea at davea.name Tue Apr 1 11:51:48 2014 From: davea at davea.name (Dave Angel) Date: Tue, 1 Apr 2014 05:51:48 -0400 (EDT) Subject: [Tutor] exercise (while loop) References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> Message-ID: Scott W Dunning Wrote in message: > I???m working on a few exercises and I???m a little stuck on this one. > > This is what the book has but it just gives me an endless loop. > > def square_root(a, eps=1e-6): > while True: > print x > y = (x + a/x) / 2 > if abs(y-x) < epsilon: > break > Without an initial value for x, this should give an immediate exception. Assuming you fix that as below, you now have the problem that they never change x, so if it isn't right on first loop, it never will be. Next you have the problem of inconsistent spelling of eps. And final thing I notice is that it doesn't return a value. Once you remove the debug print in the function, there's no way to see the result. > round(square_root(9)) > > I tweaked Good job, you fixed most of the bugs. > it to what I thought was correct but when I test it I get nothing back. > > def square_root(a, eps=1e-6): > x = a/2.0 > while True: > y = (x + a/x)/2.0 > if abs(x - y) < eps: > return y > x = y > > round(square_root(9)) > > The way I tweaked it seems to work, I???m getting the correct answer on the calculator but the interpreter is not returning anything when I check in python. Sure it is, you're just not printing it. You forgot to save the result of rounding, and forgot to print the saved value. -- DaveA From davea at davea.name Tue Apr 1 11:58:42 2014 From: davea at davea.name (Dave Angel) Date: Tue, 1 Apr 2014 05:58:42 -0400 (EDT) Subject: [Tutor] while loop References: <272461FE-60A1-4614-A968-18EC0AF87831@cox.net> <397E85C0-1C2C-44AF-9379-A2C8F4E48B86@me.com> <5D78B877-4314-4FA8-A547-A402F962A6E2@me.com> Message-ID: Scott Dunning Wrote in message: > > On Mar 31, 2014, at 5:15 AM, Dave Angel wrote: >> >> Do you know how to define and initialize a second local variable? >> Create one called i, with a value zero. >> >> You test expression will not have a literal, but compare the two >> locals. And the statement that increments will change i, not >> n. > > So like this? > def print_n(s,n): > i = 0 > while i < n: > print s, > i += 1 > > print_n('a',3) > > So this is basically making i bigger by +1 every time the while loop passes until it passes n, then it becomes false right? > > Also, with this exercise it???s using a doctest so I don???t actually call the function so I can???t figure out a way to make the string???s print on separate lines without changing the doctest code? > The trailing comma on the print statement is suppressing the newline that's printed by default. If you want them on separate lines, get rid of the comma. -- DaveA From alan.gauld at btinternet.com Tue Apr 1 12:15:18 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 01 Apr 2014 11:15:18 +0100 Subject: [Tutor] exercise (while loop) In-Reply-To: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> Message-ID: On 01/04/14 02:07, Scott W Dunning wrote: > I?m working on a few exercises and I?m a little stuck on this one. > > This is what the book has but it just gives me an endless loop. > > def square_root(a, eps=1e-6): > while True: > print x > y = (x + a/x) / 2 > if abs(y-x) < epsilon: > break Are you sure that's what the book has? If so I'd consider another book. That code is seriously broken. - It prints x before an x is defined. - It never modifies x and so the abs(y-x) will always be the same so the loop never breaks. - it tests for epsilon but has eps as parameter - It also never returns any value from the function. > round(square_root(9)) So this call will always try to round None(the default return value) And of course it produces no output since it prints nothing. Are you sure that's actually what is in the book? > I tweaked it to what I thought was correct but when I test it I get nothing back. > > def square_root(a, eps=1e-6): > x = a/2.0 > while True: > y = (x + a/x)/2.0 > if abs(x - y) < eps: > return y > x = y This is slightly better than the above, at least it creates an x and modifies it and returns a value. And it seems to work on my system. How did you test it? > round(square_root(9)) If you used the >>> prompt this would have produced a result but if you used a script file you would need to print it. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Tue Apr 1 12:19:13 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 01 Apr 2014 11:19:13 +0100 Subject: [Tutor] while loop In-Reply-To: References: <272461FE-60A1-4614-A968-18EC0AF87831@cox.net> <397E85C0-1C2C-44AF-9379-A2C8F4E48B86@me.com> <5D78B877-4314-4FA8-A547-A402F962A6E2@me.com> Message-ID: On 01/04/14 00:22, Scott Dunning wrote: > def print_n(s,n): > i = 0 > while i < n: > print s, > i += 1 > > print_n('a',3) > > Also, with this exercise it?s using a doctest so I don?t actually call the function I have no idea what you mean buy this? There is no doctest above and you do call the function... > so I can?t figure out a way to make the string?s print on > separate lines without changing the doctest code? You don't have any doctest code and the printing on one line is a result of the comma in the print statement. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From s.shall at virginmedia.com Tue Apr 1 13:30:46 2014 From: s.shall at virginmedia.com (Sydney Shall) Date: Tue, 01 Apr 2014 12:30:46 +0100 Subject: [Tutor] unittests In-Reply-To: References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> Message-ID: <533AA366.2000308@virginmedia.com> Another debutant! I am having trouble learning to use unittests. My question is; In the example below, did you write the class "SquareRootTests(unittest.TestCase):" ? Or do I find a set of them in the library? And what is the significance of the name chosen "self.assertAlmostEqual(square_root(3), 2.0) "? I take it the first argument is calling my function with its argument and the second argument is the correct answer? I am quite unclear how one proceeds to set up unittests. With many thanks in advance, Sydney On 01/04/2014 03:10, Danny Yoo wrote: >> I tweaked it to what I thought was correct but when I test it I get nothing back. >> >> def square_root(a, eps=1e-6): >> x = a/2.0 >> while True: >> y = (x + a/x)/2.0 >> if abs(x - y) < eps: >> return y >> x = y >> >> round(square_root(9)) >> >> The way I tweaked it seems to work, I?m getting the correct answer on the calculator but the interpreter is not returning anything when I check in python. > > I didn't want to keep you waiting, so I'll cut to the chase. This > line here in your program: > > round(square_root(9)) > > computes a value... But it doesn't do anything with that value. > > Try printing the value. > > > You may also try to see that your program is doing something effective > by "unit testing" it. This is often a lot better than just printing > values and looking at them, because the test case will say what the > _expected_ value is, so it's more informative. > > > > For this example, the following is a start at unit testing the above > function. Add the following to the bottom of your program's source. > > > ############################################### > ## See: http://www.openp2p.com/pub/a/python/2004/12/02/tdd_pyunit.html > import unittest > class SquareRootTests(unittest.TestCase): > def testSimpleCases(self): > self.assertAlmostEqual(square_root(1), 1.0) > self.assertAlmostEqual(square_root(4), 2.0) > > if __name__ == '__main__': > unittest.main() > ############################################### > > > Here's what it looks like when I run this: > > ############################################## > $ python sqrt.py > 4.472135955 > . > ---------------------------------------------------------------------- > Ran 1 test in 0.000s > > OK > ############################################## > > > You can then start adding more and more to tests to gain confidence > that the code is doing something reasonable. > > > > If we try to put in an intentionally broken test, like: > > self.assertAlmostEqual(square_root(3), 2.0) > > in the body of testSimpleCases(), then we'll see the following error > when running the program: > > > ############################################## > $ python sqrt.py > 4.472135955 > F > ====================================================================== > FAIL: testSimpleCases (__main__.SquareRootTests) > ---------------------------------------------------------------------- > Traceback (most recent call last): > File "sq.py", line 20, in testSimpleCases > self.assertAlmostEqual(square_root(3), 2.0) > AssertionError: 1.7320508075688772 != 2.0 within 7 places > > ---------------------------------------------------------------------- > Ran 1 test in 0.000s > > FAILED (failures=1) > ############################################## > > > And that's what you want to see. If either the test or the code is > bad, it'll say something about it. > > > One other thing: you will want to check a particularly insidious case > that will cause the program here to behave badly. Consider the zero > case: square_root(0). Write the test case. Run it. You'll see > something interesting. > > > > Good luck! > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Sydney Shall From steve at pearwood.info Tue Apr 1 14:45:31 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 1 Apr 2014 23:45:31 +1100 Subject: [Tutor] unittests In-Reply-To: <533AA366.2000308@virginmedia.com> References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> <533AA366.2000308@virginmedia.com> Message-ID: <20140401124530.GS16526@ando> On Tue, Apr 01, 2014 at 12:30:46PM +0100, Sydney Shall wrote: > Another debutant! > I am having trouble learning to use unittests. > My question is; > In the example below, did you write the class > "SquareRootTests(unittest.TestCase):" ? Yes, that unit test was written by Danny (I assume -- I suppose he might have copied it from somewhere else.) > Or do I find a set of them in the library? There are a HUGE number of unit tests that come supplied with Python's source code, but they are used for testing Python itself. If your Python installation includes a "test" subdirectory, you can see the tests in there. They aren't reusable in your own code, except to read them and learn from them. Instead, you write your own tests, to suit your own code, using the unittest module to do all the heavy-lifting. > And what is the significance of the name chosen > "self.assertAlmostEqual(square_root(3), 2.0) "? > I take it the first argument is calling my function with its argument > and the second argument is the correct answer? Correct. This "assertAlmostEqual" test takes two numbers: - first Python calls your square_root function with 3, and gets some number which hopefully will be around 1.732; - then Python takes the number 2.0; - finally it calls the "assertAlmostEqual" test, which tests whether or not 1.732 is "almost equal" to 2.0, according to whatever scheme it uses to compare two numbers. (They don't.) If they compared "almost equal", then that specific test would have counted as a passing test. But since they don't, then it counts as a test failure. The idea is that you collect all the test failures, and they represent bugs in your code that should be fixed. Although they might be a bug in the test! In this case, Danny intends that as a deliberately buggy test -- 1.732... is *not* "approximately equal" to 2, at least not according to the rules of unittest. > I am quite unclear how one proceeds to set up unittests. Fortunately, most of the hard work is done for you by the unittest module. Suppose you have a simple module with one function and you want to test it: # module pizza.py def make_pizzas(number): return "made %d pizzas" % number That's pretty simple, right? So simple that you can almost look at it and see that it works correctly. make_pizza(5) => returns "made 5 pizzas" But let's do some nice simple unit tests for it. Start with a new file, which I would call "test_pizza.py", and import two modules: unittest, and your own module. (You need to import your module, so Python knows what you are testing.) # test_pizza.py import pizza import unittest The unittest module has a *lot* of complex code in it, because it has a lot of work that it needs to do. It needs to find the tests, initialise them, run the tests, collect the results, display a visual indication of whether tests are passing or failing, and so on. But to make this work, all we need do is write the tests in a way that the unittest module understands. We do that by using inheritance: we inherit from the TestCase class: class MakePizzaTests(unittest.TestCase): """Collect all the tests for the make_pizza function.""" It's good practice to have a separate class for each group of related tests. In this case, because there's only one function, we can put all the tests for that function into one class. If there was a second function, we would use a second class: class MakeSaladTests(unittest.TestCase): ... but there isn't, so we won't. At the moment, if we run the tests in this, there will be zero tests run and zero failed. Because we haven't actually written any tests, just the beginnings of it! So let's add a simple test. What's the simplest thing we can think of about the make_pizza function? It returns a string! So let's test it: class MakePizzaTests(unittest.TestCase): """Collect all the tests for the make_pizza function.""" def test_make_pizza_returns_string(self): # Test that make_pizza returns a string. for num in (1, 2, 3, 100, 1000, 78901): result = pizza.make_pizza(num) self.assertIsInstance(result, str) Here, we don't actually case what result make_pizza gives us, we just want to check that it returns a string. Any old string will do. It's good practice to start with more general tests, then work your way down to writing specific tests. Notice that this test tries the function with half a dozen different values. If I tried it with only one value, say 23, and the test passed, maybe the code only passes with 23 but fails with everything else. In a perfect world, I could try testing with *every* possible value, but that might take a while, a long, long while, so I compromise by testing with just a handful of representative values. Now, let's test this out. At your shell prompt (not the Python interactive interpreter!), run this command from inside the same directory as your pizza.py and test_pizza.py files: python3.3 -m unittest test_pizza Notice that I *don't* include the .py after test_pizza. Unittest needs to import it as a module. If you do this, unittest will run the one test it finds, print a single dot, and you're done. Success! Our first test passed. Let's add a few more: edit the test_pizza file and add these two methods to the MakePizzaTests class: def test_make_pizzas(self): # Test that make_pizza returns the expected result. self.assertEqual(pizza.make_pizza(7), "made 7 pizzas") self.assertEqual(pizza.make_pizza(23), "made 23 pizzas") def test_make_1_pizza(self): # Test that make_pizza gives us a special result with 1 pizza. result = pizza.make_pizza(1) self.assertEqual(result, "made 1 pizza") This time, if you run the tests, you'll get two dots and an F for Fail. The test_make_1_pizza fails. Can you see why, and how to fix the bug in the make_pizza function? -- Steven From bgailer at gmail.com Tue Apr 1 15:15:05 2014 From: bgailer at gmail.com (bob gailer) Date: Tue, 01 Apr 2014 09:15:05 -0400 Subject: [Tutor] Vending machine problem. Message-ID: <533ABBD9.9060406@gmail.com> I'm posting this to include you in this conversation. Recently I got the following Request: can you write me a code in python please or if you have one already my response: print('hello world') what more can I do for you? (next two lines are best guesses as I can't find the relevant emails. Request: can you write me a code for a vending machine in python please or if you have one already Response: I need a lot more detail. Request: The vending machine must have 5 prices with items it should accept 10p, 20p 50p and ?1 coins it should allow the user to purchase a item and give him a choice of purchasing something else. it should display the remaining credit once the item is purchased. i will let you know of anything else Tutors: sounds familiar, eh? Response: This sounds like a homework assignment. We don't provide answers for homework. We will help you once you show some effort and tell us where you need a hand. The requirements are kinda vague. I would have a hard time guessing exactly what the instructor is looking for. Did you get any other information? Did you get some sample of the expected input and output? If no to the latter, good starting place is to write down a sample dialog, then apply the Python functions you should have learned by now to make that dialog happen. How would you store the collection of items and their prices? What Python data types have you learned that you could use here? How would you look up an item to get its price? Show us your answers to any of the above, tell us exactly where you are stuck, and we will see what we can do to help. From bgailer at gmail.com Tue Apr 1 15:15:14 2014 From: bgailer at gmail.com (bob gailer) Date: Tue, 01 Apr 2014 09:15:14 -0400 Subject: [Tutor] Vending machine problem. In-Reply-To: References: , <5338A8F1.4050108@gmail.com> , <533A17B6.60206@gmail.com> Message-ID: <533ABBE2.4000006@gmail.com> On 4/1/2014 3:26 AM, Sebastien Gomez wrote: > The vending machine must have 5 prices with items > it should accept 10p, 20p 50p and ?1 coins > it should allow the user to purchase a item and give him a choice of > purchasing something else. it should display the remaining credit once > the item is purchased. i will let you know of anything else Thank you. I am copying this to tutor at python.org where a bunch of us hang out and collectively help others. PLEASE in future always reply-all so a copy goes to the list. I am changing the subject line to something meaningful. This sounds like a homework assignment. We don't provide answers for homework. We will help you once you show some effort and tell us where you need a hand. The requirements are kinda vague. I would have a hard time guessing exactly what the instructor is looking for. Did you get any other information? Did you get some sample of the expected input and output? If no th the latter, good starting place is to write down a sample dialog, then apply the Python functions you should have learned by now to make that dialog happen. How do you plan to store the collection of items and their prices? What Python data types have you learned that you could use here? How do you plan to look up an item to get its price? Show us your answers to any of the above, tell us exactly where you are stuck, and we will see what we can do to help. From pscott_74 at yahoo.com Tue Apr 1 18:07:06 2014 From: pscott_74 at yahoo.com (Patti Scott) Date: Tue, 1 Apr 2014 09:07:06 -0700 (PDT) Subject: [Tutor] conditional execution Message-ID: <1396368426.26327.YahooMailNeo@web161804.mail.bf1.yahoo.com> I've been cheating:? comment out the conditional statement and adjust the indents. But, how do I make my program run with if __name__ == 'main': main() at the end?? I thought I understood the idea to run a module called directly but not a module imported.? My program isn't running, though. Below is the last textbook example (Python Programming, Zelle) I reworked.? Runs when I comment out the conditional statement.? I am self-studying.? Python 2.7.3,? Notepad++,? Windows PowerShell. # calc.pyw # a 4-function calculator that uses Python arithmetic # illustrates use of objects and lists to build a simple GUI from graphics import * from button import Button class Calculator: ??? # this class? implements simple calculator GUI ??? ??? def __init__(self): ??? ??? # create window for calculator ??? ??? win = GraphWin('calculator') ??? ??? win.setCoords(0,0,6,7) ??? ??? win.setBackground('slategray') ??? ??? self.win = win ??? ??? # now create the widgets ??? ??? self.__createButtons() ??? ??? self.__createDisplay() ??? ??? ??? def __createButtons(self): ??? ??? # create list of buttons ??? ??? # start with all the standard-sized buttons ??? ??? # bSpecs gives center coords and labels of buttons ??? ??? bSpecs = [(2,1,'0'), (3,1,'.'), ??? ??? ??? (1,2,'1'), (2,2,'2'), (3,2,'3'), (4,2,'+'), (5,2,'-'), ??? ??? ??? (1,3,'4'), (2,3,'5'), (3,3,'6'), (4,3,'*'), (5,3,'/'), ??? ??? ??? (1,4,'7'), (2,4,'8'), (3,4,'9'), (4,4,'<-'), (5,4,'C')] ??? ??? self.buttons = [] ??? ??? for (cx,cy, label) in bSpecs: ??? ??? ??? self.buttons.append(Button(self.win, Point(cx,cy), .75,.75, label)) ??? ??? # create the larger equals button ??? ??? self.buttons.append(Button(self.win, Point(4.5,1), 1.75, .75, '=')) ??? ??? # activate all buttons ??? ??? for b in self.buttons: ??? ??? ??? b.activate() ??? ??? ??? ??? def __createDisplay(self): ??? ??? bg = Rectangle(Point(.5,5.5), Point(5.5, 6.5)) ??? ??? bg.setFill('white') ??? ??? bg.draw(self.win) ??? ??? text = Text(Point(3,6), "") ??? ??? text.setFace('courier') ??? ??? text.setStyle('bold') ??? ??? text.setSize(16) ??? ??? text.draw(self.win) ??? ??? self.display = text ??? ??? ??? def getButton(self): ??? ??? # waits for button to be clicked and returns label of that button ??? ??? while True: ??? ??? ??? p = self.win.getMouse() ??? ??? ??? for b in self.buttons: ??? ??? ??? ??? if b.clicked(p): ??? ??? ??? ??? ??? return b.getLabel()? # method exit ??? ??? def processButton(self, key): ??? ??? # updates calculator display for press of this key ??? ??? text = self.display.getText() ??? ??? if key == 'C': ??? ??? ??? self.display.setText("") ??? ??? elif key == "<-": ??? ??? ??? # backspace, slice off the last character ??? ??? ??? self.display.setText(text[:-1]) ??? ??? elif key == '=': ??? ??? ??? # evaluate the expression and display result ??? ??? ??? # the try ... except mechanism catches errors in the ??? ??? ??? #??? formula being evaluated ??? ??? ??? try: ??? ??? ??? ??? result = eval(text) ??? ??? ??? except: ??? ??? ??? ??? result ='ERROR' ??? ??? ??? self.display.setText(str(result)) ??? ??? else: ??? ??? ??? # normal key press, append it to end of display ??? ??? ??? self.display.setText(text+key) ??? ??? ??? ??? def run(self): ??? ??? # infinite event loop to process button clicks ??? ??? while True: ??? ??? ??? key = self.getButton() ??? ??? ??? self.processButton(key) ??? ??? ??? ? #this runs the program if __name__ == 'main': ??? #first create a calulator object ??? theCalc = Calculator() # now call the calculator's run methond ??? theCalc.run() and I get PS C:\Users\Owner\Py Programs> python calc.py PS C:\Users\Owner\Py Programs> Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From kwpolska at gmail.com Tue Apr 1 18:22:07 2014 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Tue, 1 Apr 2014 18:22:07 +0200 Subject: [Tutor] conditional execution In-Reply-To: <1396368426.26327.YahooMailNeo@web161804.mail.bf1.yahoo.com> References: <1396368426.26327.YahooMailNeo@web161804.mail.bf1.yahoo.com> Message-ID: On Tue, Apr 1, 2014 at 6:07 PM, Patti Scott wrote: > I've been cheating: comment out the conditional statement and adjust the > indents. But, how do I make my program run with if __name__ == 'main': > main() at the end? I thought I understood the idea to run a module called > directly but not a module imported. My program isn't running, though. The statement is: if __name__ == '__main__': You?re missing the double underscores on both sides of __main__. If you added them in, this would work. > Below is the last textbook example (Python Programming, Zelle) I reworked. > Runs when I comment out the conditional statement. I am self-studying. > Python 2.7.3, Notepad++, Windows PowerShell. > > # calc.pyw > # a 4-function calculator that uses Python arithmetic > # illustrates use of objects and lists to build a simple GUI > > from graphics import * > from button import Button > > class Calculator: It?s better to inherit from object (i.e. use `class Calculator(object):`) in this case. Saves you a lot of trouble when you encounter new-style-only things. > # this class implements simple calculator GUI > > def __init__(self): > # create window for calculator > win = GraphWin('calculator') > win.setCoords(0,0,6,7) > win.setBackground('slategray') > self.win = win > # now create the widgets > self.__createButtons() > self.__createDisplay() > > def __createButtons(self): > # create list of buttons > # start with all the standard-sized buttons > # bSpecs gives center coords and labels of buttons > bSpecs = [(2,1,'0'), (3,1,'.'), > (1,2,'1'), (2,2,'2'), (3,2,'3'), (4,2,'+'), (5,2,'-'), > (1,3,'4'), (2,3,'5'), (3,3,'6'), (4,3,'*'), (5,3,'/'), > (1,4,'7'), (2,4,'8'), (3,4,'9'), (4,4,'<-'), (5,4,'C')] > self.buttons = [] > for (cx,cy, label) in bSpecs: > self.buttons.append(Button(self.win, Point(cx,cy), .75,.75, > label)) > # create the larger equals button > self.buttons.append(Button(self.win, Point(4.5,1), 1.75, .75, '=')) > # activate all buttons > for b in self.buttons: > b.activate() > > def __createDisplay(self): > bg = Rectangle(Point(.5,5.5), Point(5.5, 6.5)) > bg.setFill('white') > bg.draw(self.win) > text = Text(Point(3,6), "") > text.setFace('courier') > text.setStyle('bold') > text.setSize(16) > text.draw(self.win) > self.display = text > > def getButton(self): > # waits for button to be clicked and returns label of that button > while True: > p = self.win.getMouse() > for b in self.buttons: > if b.clicked(p): > return b.getLabel() # method exit > > def processButton(self, key): > # updates calculator display for press of this key > text = self.display.getText() > if key == 'C': > self.display.setText("") > elif key == "<-": > # backspace, slice off the last character > self.display.setText(text[:-1]) > elif key == '=': > # evaluate the expression and display result > # the try ... except mechanism catches errors in the > # formula being evaluated > try: > result = eval(text) > except: > result ='ERROR' > self.display.setText(str(result)) > else: > # normal key press, append it to end of display > self.display.setText(text+key) > > def run(self): > # infinite event loop to process button clicks > while True: > key = self.getButton() > self.processButton(key) > > #this runs the program > if __name__ == 'main': > #first create a calulator object > theCalc = Calculator() > # now call the calculator's run methond > theCalc.run() > > > > and I get > > PS C:\Users\Owner\Py Programs> python calc.py > PS C:\Users\Owner\Py Programs> > > > > Thanks > > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From zachary.ware+pytut at gmail.com Tue Apr 1 18:24:16 2014 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Tue, 1 Apr 2014 11:24:16 -0500 Subject: [Tutor] conditional execution In-Reply-To: <1396368426.26327.YahooMailNeo@web161804.mail.bf1.yahoo.com> References: <1396368426.26327.YahooMailNeo@web161804.mail.bf1.yahoo.com> Message-ID: Hi Patti, On Tue, Apr 1, 2014 at 11:07 AM, Patti Scott wrote: > I've been cheating: comment out the conditional statement and adjust the > indents. But, how do I make my program run with if __name__ == 'main': > main() at the end? I thought I understood the idea to run a module called > directly but not a module imported. My program isn't running, though. The simple fix to get you going is to change your ``if __name__ == 'main':`` statement to ``if __name__ == '__main__':`` (add two underscores on each side of "main"). To debug this for yourself, try putting ``print(__name__)`` right before your ``if __name__ ...`` line, and see what is printed when you run it in different ways. Hope this helps, and if you need any more help or a more in-depth explanation of what's going on, please don't hesitate to ask :) Regards, -- Zach From dyoo at hashcollision.org Tue Apr 1 19:29:02 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 1 Apr 2014 10:29:02 -0700 Subject: [Tutor] exercise (while loop) In-Reply-To: References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> Message-ID: > So this call will always try to round None(the default return value) > And of course it produces no output since it prints nothing. > > Are you sure that's actually what is in the book? No. That's very much why I wanted a reference to the original source of the problem. Scott attributed too much to the book when he presented the problem. In the original content, http://greenteapress.com/thinkpython/html/thinkpython008.html#toc81 it simply presents a running dialogue exploring the idea of computing square roots iteratively, culminating in a toplevel for-loop that simply prints out its improving guess. There is no function there. This is why we want to be a bit more careful when saying "The book said this..." following up with a paraphrase, because sometimes we can get the paraphrasing wrong. Similarly issues occur when one is presenting error message content and asking for debugging advice. Pointing to primary sources is usually a good idea, especially when debugging or trying to get at root causes. Let's head-off this sort of confusion quickly next time. From dyoo at hashcollision.org Tue Apr 1 19:54:45 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 1 Apr 2014 10:54:45 -0700 Subject: [Tutor] unittests In-Reply-To: <20140401124530.GS16526@ando> References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> <533AA366.2000308@virginmedia.com> <20140401124530.GS16526@ando> Message-ID: > Yes, that unit test was written by Danny (I assume -- I suppose he might > have copied it from somewhere else.) Oh, who knows where I got that code from. :P --- Sydney, you can also take a look at some of the official documentation of the unittest library: https://docs.python.org/2/library/unittest.html#basic-example You don't have to read all of it, but touching on it and knowing where the documentation and reference is can be helpful. The "assertAlmostEqual" is part of the library, https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertAlmostEqual In Scott's case, he was computing with floating point, so writing the tests to use inexact almost-equality comparison seemed reasonable to me. You might find this also useful: http://www.diveintopython.net/unit_testing/ > Although they might be a bug in the test! In this case, Danny intends > that as a deliberately buggy test -- 1.732... is *not* "approximately > equal" to 2, at least not according to the rules of unittest. As a side note, when I'm writing tests, I usually write them deliberately wrong the first time and run them to make sure that the framework is properly reporting errors. Only after I see failing tests do I put in the right values for the test. It helps me gain more confidence that the universe is all right. >> I am quite unclear how one proceeds to set up unittests. Functions that take inputs and return values are usually easy to test. For simple programs, express a piece of functionality and some property you expect that functionality to have. In pure computations like math functions, you can state the inputs and expected outputs as a test. By the way, if we can't even do that, to express the expected output of our functions, then that might be a sign that we don't understand what we're trying to code! So there's a good reason to consider test cases early: it forces us to put a stake in the ground and say: "This is what the function is supposed to do, and if it doesn't do this, the code is wrong." If I know that a properly functioning f() is supposed to behave like this: f(9) ==> 3 f(10) ==> 42 f(1) ==> 32 then I want to write those concrete cases as tests. An easy way to do so is to use the unittest library to write those tests. We can write the cases above as the following test: ############################### class MyTests(unittest.TestCase): def testCrazyFunction(self): self.assertEqual(f(9), 3) self.assertEqual(f(10), 42) self.assertEqual(f(1), 32) ############################### What this means is that I have some expectations on what the function is supposed to do, apart from how it is actually coded. That's important, to express those expectations, because usually you trust your expectations more than you trust the implementing code. So if the test breaks, usually it's the code that's broken, so it gives a quick canary-in-the-coalmine. If you want to explore this more, check for Kent Beck's: "Test-driven Development: By Example". From denis.spir at gmail.com Wed Apr 2 09:18:16 2014 From: denis.spir at gmail.com (spir) Date: Wed, 02 Apr 2014 09:18:16 +0200 Subject: [Tutor] conditional execution In-Reply-To: References: <1396368426.26327.YahooMailNeo@web161804.mail.bf1.yahoo.com> Message-ID: <533BB9B8.5040008@gmail.com> On 04/01/2014 06:24 PM, Zachary Ware wrote: > Hi Patti, > > On Tue, Apr 1, 2014 at 11:07 AM, Patti Scott wrote: >> I've been cheating: comment out the conditional statement and adjust the >> indents. But, how do I make my program run with if __name__ == 'main': >> main() at the end? I thought I understood the idea to run a module called >> directly but not a module imported. My program isn't running, though. > > The simple fix to get you going is to change your ``if __name__ == > 'main':`` statement to ``if __name__ == '__main__':`` (add two > underscores on each side of "main"). To debug this for yourself, try > putting ``print(__name__)`` right before your ``if __name__ ...`` > line, and see what is printed when you run it in different ways. > > Hope this helps, and if you need any more help or a more in-depth > explanation of what's going on, please don't hesitate to ask :) And you don't even need this idiom if your module is only to be executed (not imported). Just write "main()". d From fomcl at yahoo.com Wed Apr 2 09:37:21 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 2 Apr 2014 00:37:21 -0700 (PDT) Subject: [Tutor] unittest decorators Message-ID: <1396424241.48225.YahooMailNeo@web163803.mail.gq1.yahoo.com> Hi, ? The unittest module has some really handy decorators: @unittest.skip and @unittest.skipIf. I use the former for temporary TODO or FIXME things, but I use the latter for a more permanent thing: @unittest.skipif(sys.version_info()[0] > 2). Yet, in the test summary you just see error, skipped, failed. Is it possible to not count the skipIf tests? (other than using if-else inside the test --not really a bad solution either ;-)? ? Thanks! Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ From pscott_74 at yahoo.com Wed Apr 2 17:37:56 2014 From: pscott_74 at yahoo.com (Patti Scott) Date: Wed, 2 Apr 2014 08:37:56 -0700 (PDT) Subject: [Tutor] conditional execution In-Reply-To: <533BB9B8.5040008@gmail.com> References: <1396368426.26327.YahooMailNeo@web161804.mail.bf1.yahoo.com> <533BB9B8.5040008@gmail.com> Message-ID: <1396453076.40902.YahooMailNeo@web161805.mail.bf1.yahoo.com> Thank you Emile, Zach, Chris and d. ....? I am actually catching lots of my typos before I try to run anything ... ________________________________ From: spir To: tutor at python.org Sent: Wednesday, April 2, 2014 3:18 AM Subject: Re: [Tutor] conditional execution On 04/01/2014 06:24 PM, Zachary Ware wrote: > Hi Patti, > > On Tue, Apr 1, 2014 at 11:07 AM, Patti Scott wrote: >> I've been cheating:? comment out the conditional statement and adjust the >> indents. But, how do I make my program run with if __name__ == 'main': >> main() at the end?? I thought I understood the idea to run a module called >> directly but not a module imported.? My program isn't running, though. > > The simple fix to get you going is to change your ``if __name__ == > 'main':`` statement to ``if __name__ == '__main__':`` (add two > underscores on each side of "main").? To debug this for yourself, try > putting ``print(__name__)`` right before your ``if __name__ ...`` > line, and see what is printed when you run it in different ways. > > Hope this helps, and if you need any more help or a more in-depth > explanation of what's going on, please don't hesitate to ask :) And you don't even need this idiom if your module is only to be executed (not imported). Just write "main()". d _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Wed Apr 2 18:02:06 2014 From: bgailer at gmail.com (bob gailer) Date: Wed, 02 Apr 2014 12:02:06 -0400 Subject: [Tutor] Vending machine problem. In-Reply-To: References: , <5338A8F1.4050108@gmail.com> , <533A17B6.60206@gmail.com> , <533ABBE2.4000006@gmail.com> Message-ID: <533C347E.10909@gmail.com> On 4/1/2014 5:09 PM, Sebastien Gomez wrote: > I have started my code but there are some errors including indentation > and syntax, please fix it if you can: > Did you miss my request that you send a copy to the tutor list (tutor at python.org)? Were there any more specifications in the assignment? Please take care of these; then I will respond. Also keep in mind that our goal is to help you think for yourself, so we may not fix it but rather help you understand and fix the problems. What tool did you use to discover multiple errors? If you try to run the program directly the Python interpreter will report the first error it finds and then stop. In general it is a good idea to tell us (so please do so now) - your operating system - Python version - what you use to edit and run the program I am sorry to be so wordy and rigorous but we really need to know if we are to help. From bgailer at gmail.com Thu Apr 3 06:04:27 2014 From: bgailer at gmail.com (bob gailer) Date: Thu, 03 Apr 2014 00:04:27 -0400 Subject: [Tutor] Vending machine problem. In-Reply-To: References: , <5338A8F1.4050108@gmail.com> , <533A17B6.60206@gmail.com> , <533ABBE2.4000006@gmail.com> , <533C347E.10909@gmail.com> Message-ID: <533CDDCB.9080100@gmail.com> On 4/2/2014 5:06 PM, Sebastien Gomez wrote: > i am using python 3.2 > windows vista > This is my last email to you. Communicating with you is way too time consuming; you consistently do not provide all the information I request, nor do you include the tutor list in your responses. If you want any more help you will have to ask it from the tutor list. From longelp at gmail.com Thu Apr 3 06:21:03 2014 From: longelp at gmail.com (zhijun long) Date: Thu, 3 Apr 2014 12:21:03 +0800 Subject: [Tutor] Fwd: Python bingo game. In-Reply-To: References: <2FBCC06A-A81B-41C9-8C67-F808F2AA28F0@yahoo.com> Message-ID: >>> matrix = [ ... [1, 2, 5, 7, 9], ... [25, 67, 78, 23, 34], ... [33, 22, 66, 88, 98], ... [32, 31, 41, 56, 78], ... [21, 34, 58, 99, 76], ... ] >>> for item in [[row[i] for row in matrix] for i in range(5)]: ... print item ... [1, 25, 33, 32, 21] [2, 67, 22, 31, 34] [5, 78, 66, 41, 58] [7, 23, 88, 56, 99] [9, 34, 98, 78, 76] >>> 2014-03-31 22:36 GMT+08:00 Hardik Gandhi : > > > Hello, > > > > Can some one help me with displaying a matrix vertically. > > > > For example my output matrix is:- > > > > [1 2 5 7 9] > > [25 67 78 23 34] > > [33 22 66 88 98] > > [32 31 41 56 78] > > [21 34 58 99 76] > > > > And i want my matrix to look like this:- > > [1 25 33 32 21] > > [2 67 22 31 34] > > [5 78 66 41 58] > > [7 23 88 56 99] > > [9 34 98 78 76] > > > > Please, help me with the code in eclipse using py-dev as preference. > > > > Thank you > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Thu Apr 3 06:58:17 2014 From: davea at davea.name (DaveA) Date: Thu, 03 Apr 2014 00:58:17 -0400 Subject: [Tutor] Gmane not picking up messages lately Message-ID: <4gt6iq6huqfa0nwk5503lqgp.1396501097304@email.android.com> Gmane doesn't seem to be getting messages from either python tutor or python list, for the last day or two. I'm using NNTP NewsReader on Android, and there's no new messages.? I'm sure it's not just an extraordinary lull, because I am still getting tutor emails.? Anybody else want to comment? Is the gateway broken or the particular Android software?? --? DaveA Sent from Samsung tablet -------------- next part -------------- An HTML attachment was scrubbed... URL: From hgandhi7760 at yahoo.com Thu Apr 3 05:40:30 2014 From: hgandhi7760 at yahoo.com (Hardik Gandhi) Date: Wed, 2 Apr 2014 23:40:30 -0400 Subject: [Tutor] Fwd: Fwd: Python bingo game. References: <1396473920.20518.YahooMailNeo@web160305.mail.bf1.yahoo.com> Message-ID: Begin forwarded message: > From: Hardik Gandhi > Date: 2 April 2014 5:25:20 pm EDT > To: Danny Yoo > Subject: Re: [Tutor] Fwd: Python bingo game. > Reply-To: Hardik Gandhi > > Hello! > > I am trying to build a bingo game on command prompt. That's my task in this semester and i have studied some of the commands from internet, but ya definitely i am still not so comfortable with the logic and use of commands in python. > > I have attach -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Apr 3 13:07:47 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 03 Apr 2014 12:07:47 +0100 Subject: [Tutor] Gmane not picking up messages lately In-Reply-To: <4gt6iq6huqfa0nwk5503lqgp.1396501097304@email.android.com> References: <4gt6iq6huqfa0nwk5503lqgp.1396501097304@email.android.com> Message-ID: On 03/04/14 05:58, DaveA wrote: > Gmane doesn't seem to be getting messages from either python tutor or > python list, for the last day or two. ... > Anybody else want to comment? Is the gateway broken or the particular > Android software? I assume gmane since I've not been seeing anything on my PC in Thunderbird newsreader either. But this came thru ok so I assume its fixed now... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From leamhall at gmail.com Thu Apr 3 15:24:28 2014 From: leamhall at gmail.com (leam hall) Date: Thu, 3 Apr 2014 09:24:28 -0400 Subject: [Tutor] System output into variable? Message-ID: I've been trying to so a simple "run a command and put the output into a variable". Using Python 2.4 and 2.6 with no option to move. The go is to do something like this: my_var = "ls -l my_file" So far the best I've seen is: line = os.popen('ls -l my_file', stdout=subprocess.PIPE, shell=True) (out, err) = line.communicate() With no way to make 'my_file' a variable. I've got to be missing something, but I'm not sure what. Python has always impressed me a a language without a lot of hoops to go through. Leam -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Thu Apr 3 15:12:35 2014 From: davea at davea.name (Dave Angel) Date: Thu, 3 Apr 2014 09:12:35 -0400 (EDT) Subject: [Tutor] Gmane not picking up messages lately References: <4gt6iq6huqfa0nwk5503lqgp.1396501097304@email.android.com> Message-ID: DaveA Wrote in message: > Gmane doesn't seem to be getting messages from either python tutor or python > list, for the last day or two. It's working again. > -- DaveA From __peter__ at web.de Thu Apr 3 15:13:49 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 03 Apr 2014 15:13:49 +0200 Subject: [Tutor] unittest decorators References: <1396424241.48225.YahooMailNeo@web163803.mail.gq1.yahoo.com> Message-ID: Albert-Jan Roskam wrote: > The unittest module has some really handy decorators: @unittest.skip > and @unittest.skipIf. I use the former for temporary TODO or FIXME things, > but I use the latter for a more permanent thing: > @unittest.skipif(sys.version_info()[0] > 2). Yet, in the test summary you > just see error, skipped, failed. Is it possible to not count the skipIf > tests? You mean like this? $ cat skiptest.py import unittest import sys def hide_if(condition): def g(f): return None if condition else f return g class T(unittest.TestCase): @hide_if(sys.version_info[0] > 2) def test_two(self): pass @hide_if(sys.version_info[0] < 3) def test_three(self): pass if __name__ == "__main__": unittest.main() $ python skiptest.py -v test_two (__main__.T) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.000s OK $ python3 skiptest.py -v test_three (__main__.T) ... ok ---------------------------------------------------------------------- Ran 1 test in 0.000s OK $ > (other than using if-else inside the test --not really a bad > solution either ;-)? I don't understand that remark. From __peter__ at web.de Thu Apr 3 16:21:35 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 03 Apr 2014 16:21:35 +0200 Subject: [Tutor] System output into variable? References: Message-ID: leam hall wrote: > I've been trying to so a simple "run a command and put the output into a > variable". Using Python 2.4 and 2.6 with no option to move. The go is to > do something like this: > > my_var = "ls -l my_file" > > So far the best I've seen is: > > line = os.popen('ls -l my_file', stdout=subprocess.PIPE, shell=True) > (out, err) = line.communicate() > > With no way to make 'my_file' a variable. > > I've got to be missing something, but I'm not sure what. Python has always > impressed me a a language without a lot of hoops to go through. In Python 2.7 and above there is subprocess.check_output(): >>> import subprocess >>> filename = "my_file" >>> my_var = subprocess.check_output(["ls", "-l", filename]) >>> my_var '-rw-r--r-- 1 nn nn 0 Apr 3 16:14 my_file\n' Have a look at its source code, it should be possible to backport the function: >>> import inspect def check_output(*popenargs, **kwargs): r"""Run command with arguments and return its output as a byte string. If the exit code was non-zero it raises a CalledProcessError. The CalledProcessError object will have the return code in the returncode attribute and output in the output attribute. The arguments are the same as for the Popen constructor. Example: >>> check_output(["ls", "-l", "/dev/null"]) 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' The stdout argument is not allowed as it is used internally. To capture standard error in the result, use stderr=STDOUT. >>> check_output(["/bin/sh", "-c", ... "ls -l non_existent_file ; exit 0"], ... stderr=STDOUT) 'ls: non_existent_file: No such file or directory\n' """ if 'stdout' in kwargs: raise ValueError('stdout argument not allowed, it will be overridden.') process = Popen(stdout=PIPE, *popenargs, **kwargs) output, unused_err = process.communicate() retcode = process.poll() if retcode: cmd = kwargs.get("args") if cmd is None: cmd = popenargs[0] raise CalledProcessError(retcode, cmd, output=output) return output From wprins at gmail.com Thu Apr 3 19:14:09 2014 From: wprins at gmail.com (Walter Prins) Date: Thu, 3 Apr 2014 19:14:09 +0200 Subject: [Tutor] System output into variable? In-Reply-To: References: Message-ID: Hi Leam, On 3 April 2014 15:24, leam hall wrote: > > I've been trying to so a simple "run a command and put the output into a variable". Using Python 2.4 and 2.6 with no option to move. The go is to do something like this: > > my_var = "ls -l my_file" > > So far the best I've seen is: > > line = os.popen('ls -l my_file', stdout=subprocess.PIPE, shell=True) > (out, err) = line.communicate() > > With no way to make 'my_file' a variable. If you use IPython, this can be as simple as the following (this is on windows, so 'ls -l' has been replaced by 'dir'): Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] Type "copyright", "credits" or "license" for more information. IPython 1.0.0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: my_file='virtualenv.exe' In [2]: my_var=!dir {my_file} In [3]: print my_var [' Volume in drive C has no label.', ' Volume Serial Number is E8D7-900D', '', ' Directory of C:\\Python27\\Scripts', '', '2011/06/24 11:12 7\xff168 virtualenv.exe', ' 1 File(s) 7\xff168 bytes', ' 0 Dir(s) 47\xff655\xff469\xff056 bytes free'] In [4]: Walter From dyoo at hashcollision.org Thu Apr 3 19:28:07 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 3 Apr 2014 10:28:07 -0700 Subject: [Tutor] System output into variable? In-Reply-To: References: Message-ID: On Thu, Apr 3, 2014 at 10:14 AM, Walter Prins wrote: > Hi Leam, > > > On 3 April 2014 15:24, leam hall wrote: >> >> I've been trying to so a simple "run a command and put the output into a variable". Using Python 2.4 and 2.6 with no option to move. The go is to do something like this: >> >> my_var = "ls -l my_file" >> >> So far the best I've seen is: >> >> line = os.popen('ls -l my_file', stdout=subprocess.PIPE, shell=True) >> (out, err) = line.communicate() HI Walter, If you can, use the subprocess module instead. Also, avoid passing a single string representing the command list, but rather pass a list. This will also give you an easier opportunity to pass in variable data to the external call. e.g. cmd = subprocess.check_output(['ls', '-l', 'my_file']) where it should be easier to see that you can replace any of the string literals there, like 'my_file', with any other expression that evaluates to a string value. Do NOT use shell=True unless you really know what you're doing: you can open a potential security hole in your programs if you use that option indiscriminately. See the subprocess documentation for a few examples. https://docs.python.org/2/library/subprocess.html By the way, if you are trying to get information about the file, don't depend on an external system call here. Use the standard library's file stat functions. You'll have an easier time at it. See: https://docs.python.org/2/library/os.html#files-and-directories https://docs.python.org/2/library/os.html?highlight=stat#os.stat for more details. From dyoo at hashcollision.org Thu Apr 3 19:30:05 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 3 Apr 2014 10:30:05 -0700 Subject: [Tutor] System output into variable? In-Reply-To: References: Message-ID: Oh, sorry Walter. I thought you were the original questioner. Sorry about confusing you with Learn Hall. From alan.gauld at btinternet.com Thu Apr 3 19:42:19 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 03 Apr 2014 18:42:19 +0100 Subject: [Tutor] System output into variable? In-Reply-To: References: Message-ID: On 03/04/14 14:24, leam hall wrote: > I've been trying to so a simple "run a command and put the output into a > variable". Using Python 2.4 and 2.6 with no option to move. The go is to > do something like this: > > my_var = "ls -l my_file" I'm not sure what you mean here? What should my_var contain? The command string or the result of the command? In this case that would be a long file listing including all the access, size info etc? > So far the best I've seen is: > > line = os.popen('ls -l my_file', stdout=subprocess.PIPE, shell=True) > (out, err) = line.communicate() Don't use os.popen(), it should be viewed as legacy code. Use subprocess instead. There are several options including the call() function, or the full blown Popen class. > With no way to make 'my_file' a variable. In your original version you could use string formatting to insert the file name but using subprocess the issue goes away because you build the command as a list of substrings, one of which is the file(in your case) and it can be a variable or literal as you require. > I've got to be missing something, but I'm not sure what. Python has > always impressed me a a language without a lot of hoops to go through. subprocess can be a tad daunting but it is very powerful and once you get the hang of it quite straightforward. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From welcome.to.eye.o.rama at gmail.com Thu Apr 3 20:14:24 2014 From: welcome.to.eye.o.rama at gmail.com (John Aten) Date: Thu, 3 Apr 2014 13:14:24 -0500 Subject: [Tutor] Storing dictionary value, indexed by key, into a variable In-Reply-To: References: <8D6408E3-43DD-4235-8F78-B6E1F1FB3EA3@gmail.com> Message-ID: <5A5BF28B-2997-4E54-B418-AA235B0FE774@gmail.com> I apologize for the omissions, I thought that I had isolated the problem, but I was way off the mark. The problem was, as suggested by Danny and Peter, in the function where the dictionary is assigned. I ran the type function, as Alex advised, and lo and behold the function was returning a string. In researching this, I learned that a function can return multiple values, so I expanded things a bit. Now, the function that randomly selects which demonstrative to drill also selects which a string to present as the clue (the first part, anyway), and returns the dictionary and the first part of the clue in a tuple. I then unpack that tuple into variables and work with those. The thing is, this looks really messy Could anyone give me some pointers on how this could be more elegantly done? Here's the full code: import random import os # ille, that/those masculine that_those_Masculine_Singular = {'nom': 'ille', 'gen': 'ill?us', 'dat': 'ill?', 'acc': 'illum', 'abl': 'ill?'} that_those_Masculine_Plural = {'nom': 'ill?', 'gen': 'ill?rum', 'dat': 'ill?s', 'acc': 'ill?s', 'abl': 'ill?s'} # the rest of the dictionaries are empty. I wanted to get it working before putting everything in. # ille, that/those feminine that_those_Feminine_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} that_those_Feminine_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} # ille, that/those neuter that_those_Neuter_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} that_those_Neuter_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} # hic, this/these masculine this_these_Masculine_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} this_these_Masculine_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} # hic, this/these feminine this_these_Feminine_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} this_these_Feminine_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} # hic, this/these neuter this_these_Neuter_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} this_these_Neuter_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} # iste, that (near you/of yours) masculine that_near_you_Masculine_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} that_near_you_Masculine_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} # iste, that (near you/of yours) feminine that_near_you_Feminine_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} that_near_you_Feminine_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} # iste, that (near you/of yours) neuter that_near_you_Neuter_Singular = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} that_near_you_Neuter_Plural = {'nom': '', 'gen': '', 'dat': '', 'acc': '', 'abl': ''} guess = '' score = 0 tries = 0 while guess != 'exit': def chooseDemonstrative(): # N = random.randint(1,18) N = 1 # This line will be removed so that it will pick any one of the dictionaries to be filled in above. This is just to get it working. Demonstrative = {} Clue1 = '' if N == 1: Demonstrative = that_those_Masculine_Singular Clue1 = 'That/Those, Masculine Singular Demonstrative Pronoun/Adjective' elif N == 2: Demonstrative = 'that_those_Masculine_Plural' elif N == 3: Demonstrative = 'that_those_Feminine_Singular' elif N == 4: Demonstrative = 'that_those_Feminine_Plural' elif N == 5: Demonstrative = 'that_those_Neuter_Singular' elif N == 6: Demonstrative = 'that_those_Neuter_Plural' elif N == 7: Demonstrative = 'this_these_Masculine_Singular' elif N == 8: Demonstrative = 'this_these_Masculine_Plural' elif N == 9: Demonstrative = 'this_these_Feminine_Singular' elif N == 10: Demonstrative = 'this_these_Feminine_Plural' elif N == 11: Demonstrative = 'this_these_Neuter_Singular' elif N == 12: Demonstrative = 'this_these_Neuter_Plural' elif N == 13: Demonstrative = 'that_near_you_Masculine_Singular' elif N == 14: Demonstrative = 'that_near_you_Masculine_Plural' elif N == 15: Demonstrative = 'that_near_you_Feminine_Singular' elif N == 16: Demonstrative = 'that_near_you_Feminine_Plural' elif N == 17: Demonstrative = 'that_near_you_Neuter_Singular' else: Demonstrative = 'that_near_you_Neuter_Plural ' return Demonstrative, Clue1 def chooseCase(): c = '' Clue2 = '' number = random.randint(1,5) if number == 1: c = 'nom' Clue2 = 'Nominative' elif number == 2: c = 'gen' Clue2 = 'Genitive' elif number == 3: c = 'dat' Clue2 = 'Dative' elif number == 4: c = 'acc' Clue2 = 'Accusative' else: c = 'abl' Clue2 = 'Ablative' return c, Clue2 Dem_and_clue = chooseDemonstrative() Case_and_clue = chooseCase() Dem = Dem_and_clue[0] Clue = Dem_and_clue[1] + ' ' + Case_and_clue[1] c = Case_and_clue[0] answer = Dem[c] os.system('clear') print(Clue) guess = input() if guess == 'exit': N = 0 percentage = int((score/tries) * 100) print("You scored", score, "out of", tries, ", or", percentage, "%") tries +=1 if guess == answer: score += 1 print('faster!') else: print(answer) print('slower...') input() From __peter__ at web.de Thu Apr 3 21:08:59 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 03 Apr 2014 21:08:59 +0200 Subject: [Tutor] Storing dictionary value, indexed by key, into a variable References: <8D6408E3-43DD-4235-8F78-B6E1F1FB3EA3@gmail.com> <5A5BF28B-2997-4E54-B418-AA235B0FE774@gmail.com> Message-ID: John Aten wrote: > I apologize for the omissions, I thought that I had isolated the problem, > but I was way off the mark. The problem was, as suggested by Danny and > Peter, in the function where the dictionary is assigned. I ran the type > function, as Alex advised, and lo and behold the function was returning a > string. In researching this, I learned that a function can return multiple > values, so I expanded things a bit. Now, the function that randomly > selects which demonstrative to drill also selects which a string to > present as the clue (the first part, anyway), and returns the dictionary > and the first part of the clue in a tuple. I then unpack that tuple into > variables and work with those. > > The thing is, this looks really messy Could anyone give me some pointers > on how this could be more elegantly done? Instead of the many if...elif switches try to put the alternatives into a list, e. g. >>> cases = [ ... "Nominative", ... "Genitive", ... "Dative", ... "Accusative", ... "Ablative" ... ] (These could also be tuples cases = [("nom", "Nominative"), ...] but I wouldn't bother because you can derive "nom" from "Nominative" when the need arises. How?) You can then pick a random number in the range 0 <= random_number < len(cases) to choose an item from the list: >>> import random >>> cases[random.randrange(len(cases))] 'Accusative' >>> cases[random.randrange(len(cases))] 'Ablative' This operation is so common that there is a dedicated function: >>> random.choice(cases) 'Dative' Reimplement the chooseCase() function with this approach first and then see if you can build an appropriate list for an analogous implementation of chooseDemonstrative(). Come back here for more hints if you are stuck. From dyoo at hashcollision.org Thu Apr 3 22:52:04 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 3 Apr 2014 13:52:04 -0700 Subject: [Tutor] Storing dictionary value, indexed by key, into a variable In-Reply-To: References: <8D6408E3-43DD-4235-8F78-B6E1F1FB3EA3@gmail.com> <5A5BF28B-2997-4E54-B418-AA235B0FE774@gmail.com> Message-ID: >> The thing is, this looks really messy Could anyone give me some pointers >> on how this could be more elegantly done? > > Instead of the many if...elif switches try to put the alternatives into a > list, e. g. > >>>> cases = [ > ... "Nominative", > ... "Genitive", > ... "Dative", > ... "Accusative", > ... "Ablative" > ... ] To restate what Peter is presenting: transforming the control flow structure of the program (if statements) is the key. That is, we take that control flow structure and rephrase it as _data_ structures (lists, dicts). You'll also hear the term "Data Driven Programming" or "Table Driven Programming" to refer to this idea. For example: http://blogs.msdn.com/b/ericlippert/archive/2004/02/24/79292.aspx It's a core idea, and definitely worth practicing by fixing the program. You'll see all that control flow structure dissolve away. From breamoreboy at yahoo.co.uk Fri Apr 4 00:44:42 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 03 Apr 2014 23:44:42 +0100 Subject: [Tutor] Learn programming by visualizing code execution Message-ID: That's what this site http://pythontutor.com/ claims although I haven't tried it myself. Hopefully it's of some use to all you newbies out there :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From akleider at sonic.net Fri Apr 4 01:09:03 2014 From: akleider at sonic.net (Alex Kleider) Date: Thu, 03 Apr 2014 16:09:03 -0700 Subject: [Tutor] Storing dictionary value, indexed by key, into a variable In-Reply-To: References: <8D6408E3-43DD-4235-8F78-B6E1F1FB3EA3@gmail.com> <5A5BF28B-2997-4E54-B418-AA235B0FE774@gmail.com> Message-ID: <0b353a1f4ab5d23f733cd8520e0fcac3@sonic.net> On 2014-04-03 13:52, Danny Yoo wrote: > You'll also hear the term "Data Driven Programming" or "Table Driven > Programming" to refer to this idea. For example: > > http://blogs.msdn.com/b/ericlippert/archive/2004/02/24/79292.aspx > Does anyone know of a similar blog or tutorial (regarding Data Driven Programming) that uses Python for it's example(s)? From what I've read so far, 'data driven' is a concept quite independent of 'object oriented'. From alan.gauld at btinternet.com Fri Apr 4 01:33:33 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 04 Apr 2014 00:33:33 +0100 Subject: [Tutor] Learn programming by visualizing code execution In-Reply-To: References: Message-ID: On 03/04/14 23:44, Mark Lawrence wrote: > That's what this site http://pythontutor.com/ claims although I haven't > tried it myself. Hopefully it's of some use to all you newbies out > there :) Ooh, clever! I was expecting the code trace on the left but not the graphical data display on the right. I actually tried to build something similar into my tutor when I did the V3 update but it got far too complex (my JavaScript skills are not that good and JQuery was in its infancy then). I don't know how good it is as a tutor but the visuals are certainly slick. Pity I can't see the source code to see how they do it... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Fri Apr 4 01:36:53 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 04 Apr 2014 00:36:53 +0100 Subject: [Tutor] Storing dictionary value, indexed by key, into a variable In-Reply-To: <0b353a1f4ab5d23f733cd8520e0fcac3@sonic.net> References: <8D6408E3-43DD-4235-8F78-B6E1F1FB3EA3@gmail.com> <5A5BF28B-2997-4E54-B418-AA235B0FE774@gmail.com> <0b353a1f4ab5d23f733cd8520e0fcac3@sonic.net> Message-ID: On 04/04/14 00:09, Alex Kleider wrote: > On 2014-04-03 13:52, Danny Yoo wrote: > >> You'll also hear the term "Data Driven Programming" or "Table Driven >> Programming" to refer to this idea. For example: >> >> http://blogs.msdn.com/b/ericlippert/archive/2004/02/24/79292.aspx >> > > Does anyone know of a similar blog or tutorial (regarding Data Driven > Programming) that uses Python for it's example(s)? From what I've read > so far, 'data driven' is a concept quite independent of 'object oriented'. I don't know of a Python tutorial. Yes, data driven is completely different to OO. It is complementary in that you can data drive OO programs too. And to some extent Python is an example of a data driven design since function (and method) calls are associated with dictionaries (ie Python namespaces). hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From breamoreboy at yahoo.co.uk Fri Apr 4 02:14:50 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 04 Apr 2014 01:14:50 +0100 Subject: [Tutor] conditional execution In-Reply-To: <533BB9B8.5040008@gmail.com> References: <1396368426.26327.YahooMailNeo@web161804.mail.bf1.yahoo.com> <533BB9B8.5040008@gmail.com> Message-ID: On 02/04/2014 08:18, spir wrote: > On 04/01/2014 06:24 PM, Zachary Ware wrote: >> Hi Patti, >> >> On Tue, Apr 1, 2014 at 11:07 AM, Patti Scott wrote: >>> I've been cheating: comment out the conditional statement and adjust >>> the >>> indents. But, how do I make my program run with if __name__ == 'main': >>> main() at the end? I thought I understood the idea to run a module >>> called >>> directly but not a module imported. My program isn't running, though. >> >> The simple fix to get you going is to change your ``if __name__ == >> 'main':`` statement to ``if __name__ == '__main__':`` (add two >> underscores on each side of "main"). To debug this for yourself, try >> putting ``print(__name__)`` right before your ``if __name__ ...`` >> line, and see what is printed when you run it in different ways. >> >> Hope this helps, and if you need any more help or a more in-depth >> explanation of what's going on, please don't hesitate to ask :) > > And you don't even need this idiom if your module is only to be executed > (not imported). Just write "main()". > A counter to the above comment http://www.jeffknupp.com/blog/2014/04/03/dont-write-python-scripts-write-python-libraries/ -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From dyoo at hashcollision.org Fri Apr 4 02:28:21 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 3 Apr 2014 17:28:21 -0700 Subject: [Tutor] Learn programming by visualizing code execution In-Reply-To: References: Message-ID: > I don't know how good it is as a tutor but the visuals are certainly slick. > Pity I can't see the source code to see how they do it... https://github.com/pgbovine/OnlinePythonTutor/ From dyoo at hashcollision.org Fri Apr 4 02:29:16 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 3 Apr 2014 17:29:16 -0700 Subject: [Tutor] Learn programming by visualizing code execution In-Reply-To: References: Message-ID: By the way, the main developer on the pythontutor.org project is awesome. http://www.pgbovine.net/ From akleider at sonic.net Fri Apr 4 04:33:19 2014 From: akleider at sonic.net (Alex Kleider) Date: Thu, 03 Apr 2014 19:33:19 -0700 Subject: [Tutor] data driven programming In-Reply-To: References: <8D6408E3-43DD-4235-8F78-B6E1F1FB3EA3@gmail.com> <5A5BF28B-2997-4E54-B418-AA235B0FE774@gmail.com> <0b353a1f4ab5d23f733cd8520e0fcac3@sonic.net> Message-ID: On 2014-04-03 16:36, Alan Gauld wrote: > On 04/04/14 00:09, Alex Kleider wrote: >> On 2014-04-03 13:52, Danny Yoo wrote: >> >>> You'll also hear the term "Data Driven Programming" or "Table Driven >>> Programming" to refer to this idea. For example: >>> >>> http://blogs.msdn.com/b/ericlippert/archive/2004/02/24/79292.aspx >>> >> >> Does anyone know of a similar blog or tutorial (regarding Data Driven >> Programming) that uses Python for it's example(s)? From what I've >> read >> so far, 'data driven' is a concept quite independent of 'object >> oriented'. > > I don't know of a Python tutorial. > Yes, data driven is completely different to OO. > > It is complementary in that you can data drive OO programs too. > And to some extent Python is an example of a data driven > design since function (and method) calls are associated with > dictionaries (ie Python namespaces). > > hth Can you elaborate, please, on what you mean by pointing out that the fact that Python's function and method calls are associated with dictionaries makes it 'an example of data driven design?' I'm not clear on that relationship. Thanks, Alex From fomcl at yahoo.com Fri Apr 4 08:58:12 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 3 Apr 2014 23:58:12 -0700 (PDT) Subject: [Tutor] unittest decorators In-Reply-To: References: <1396424241.48225.YahooMailNeo@web163803.mail.gq1.yahoo.com> Message-ID: <1396594692.80429.YahooMailNeo@web163803.mail.gq1.yahoo.com> ________________________________ > From: Peter Otten <__peter__ at web.de> >To: tutor at python.org >Sent: Thursday, April 3, 2014 3:13 PM >Subject: Re: [Tutor] unittest decorators > > >Albert-Jan Roskam wrote: > >> The unittest module has some really handy decorators: @unittest.skip >> and @unittest.skipIf. I use the former for temporary TODO or FIXME things, >> but I use the latter for a more permanent thing: >> @unittest.skipif(sys.version_info()[0] > 2). Yet, in the test summary you >> just see error, skipped, failed. Is it possible to not count the skipIf >> tests? > >You mean like this? > >$ cat skiptest.py >import unittest >import sys > >def hide_if(condition): >? ? def g(f): >? ? ? ? return None if condition else f >? ? return g > >class T(unittest.TestCase): >? ? @hide_if(sys.version_info[0] > 2) >? ? def test_two(self): >? ? ? ? pass >? ? @hide_if(sys.version_info[0] < 3) >? ? def test_three(self): >? ? ? ? pass > >if __name__ == "__main__": >? ? unittest.main() >$ python skiptest.py -v >test_two (__main__.T) ... ok > >---------------------------------------------------------------------- >Ran 1 test in 0.000s > >OK >$ python3 skiptest.py -v >test_three (__main__.T) ... ok > >---------------------------------------------------------------------- >Ran 1 test in 0.000s > >OK >$ Wow, yes, this is exactly what I meant! Thank you! Now the "bad/code smell/TODO" skips are separated from the legitimately skipped tests. >> (other than using if-else inside the test --not really a bad >> solution either ;-)? > >I don't understand that remark. Ok, that was indeed a bit cryptic, sorry. I meant something like this: class T(unittest.TestCase): ??? def test_combined(self): ??????? if(sys.version_info[0] > 2: ??????????? pass? ??????? else: ??????????? pass From alan.gauld at btinternet.com Fri Apr 4 10:51:34 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 04 Apr 2014 09:51:34 +0100 Subject: [Tutor] data driven programming In-Reply-To: References: <8D6408E3-43DD-4235-8F78-B6E1F1FB3EA3@gmail.com> <5A5BF28B-2997-4E54-B418-AA235B0FE774@gmail.com> <0b353a1f4ab5d23f733cd8520e0fcac3@sonic.net> Message-ID: On 04/04/14 03:33, Alex Kleider wrote: >> And to some extent Python is an example of a data driven >> design since function (and method) calls are associated with >> dictionaries (ie Python namespaces). >> > Can you elaborate, please, on what you mean by pointing out that the > fact that Python's function and method calls are associated with > dictionaries makes it 'an example of data driven design?' I'm not clear > on that relationship. Thanks, Alex Most of Python is built on dictionaries under the surface. When you define a function you create a function object and associate it with a name. In other words you store the name as a key in a dictionary and have the function object as the value. When you call the function Python looks up the name in the dictionary and calls the associated object. This is very similar to what was recommended to you to avoid the long list of if/elif. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Fri Apr 4 13:16:40 2014 From: __peter__ at web.de (Peter Otten) Date: Fri, 04 Apr 2014 13:16:40 +0200 Subject: [Tutor] conditional execution References: <1396368426.26327.YahooMailNeo@web161804.mail.bf1.yahoo.com> <533BB9B8.5040008@gmail.com> Message-ID: spir wrote: > On 04/01/2014 06:24 PM, Zachary Ware wrote: >> Hi Patti, >> >> On Tue, Apr 1, 2014 at 11:07 AM, Patti Scott wrote: >>> I've been cheating: comment out the conditional statement and adjust >>> the indents. But, how do I make my program run with if __name__ == >>> 'main': >>> main() at the end? I thought I understood the idea to run a module >>> called >>> directly but not a module imported. My program isn't running, though. >> >> The simple fix to get you going is to change your ``if __name__ == >> 'main':`` statement to ``if __name__ == '__main__':`` (add two >> underscores on each side of "main"). To debug this for yourself, try >> putting ``print(__name__)`` right before your ``if __name__ ...`` >> line, and see what is printed when you run it in different ways. >> >> Hope this helps, and if you need any more help or a more in-depth >> explanation of what's going on, please don't hesitate to ask :) > > And you don't even need this idiom if your module is only to be executed > (not imported). Just write "main()". How do you write tests for the code in the module then? From s.shall at virginmedia.com Fri Apr 4 15:12:09 2014 From: s.shall at virginmedia.com (Sydney Shall) Date: Fri, 04 Apr 2014 14:12:09 +0100 Subject: [Tutor] unittests In-Reply-To: References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> <533AA366.2000308@virginmedia.com> <20140401124530.GS16526@ando> Message-ID: <533EAFA9.30000@virginmedia.com> I would like to thank both Steven and Danny for their explanations. They were much easier for me to understand than the documentation. I now think that I understand what I have to do and how I proceed. Thanks to you both for spending so much time on the answer. Sydney On 01/04/2014 18:54, Danny Yoo wrote: >> Yes, that unit test was written by Danny (I assume -- I suppose he might >> have copied it from somewhere else.) > Oh, who knows where I got that code from. :P > > --- > > Sydney, you can also take a look at some of the official documentation > of the unittest library: > > https://docs.python.org/2/library/unittest.html#basic-example > > You don't have to read all of it, but touching on it and knowing where > the documentation and reference is can be helpful. The > "assertAlmostEqual" is part of the library, > > https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertAlmostEqual > > In Scott's case, he was computing with floating point, so writing the > tests to use inexact almost-equality comparison seemed reasonable to > me. > > > You might find this also useful: > > http://www.diveintopython.net/unit_testing/ > > > >> Although they might be a bug in the test! In this case, Danny intends >> that as a deliberately buggy test -- 1.732... is *not* "approximately >> equal" to 2, at least not according to the rules of unittest. > As a side note, when I'm writing tests, I usually write them > deliberately wrong the first time and run them to make sure that the > framework is properly reporting errors. Only after I see failing > tests do I put in the right values for the test. It helps me gain > more confidence that the universe is all right. > > >>> I am quite unclear how one proceeds to set up unittests. > Functions that take inputs and return values are usually easy to test. > For simple programs, express a piece of functionality and some > property you expect that functionality to have. In pure computations > like math functions, you can state the inputs and expected outputs as > a test. > > By the way, if we can't even do that, to express the expected output > of our functions, then that might be a sign that we don't understand > what we're trying to code! So there's a good reason to consider test > cases early: it forces us to put a stake in the ground and say: "This > is what the function is supposed to do, and if it doesn't do this, the > code is wrong." > > If I know that a properly functioning f() is supposed to behave like this: > > f(9) ==> 3 > f(10) ==> 42 > f(1) ==> 32 > > then I want to write those concrete cases as tests. An easy way to do > so is to use the unittest library to write those tests. We can write > the cases above as the following test: > > ############################### > class MyTests(unittest.TestCase): > def testCrazyFunction(self): > self.assertEqual(f(9), 3) > self.assertEqual(f(10), 42) > self.assertEqual(f(1), 32) > ############################### > > What this means is that I have some expectations on what the function > is supposed to do, apart from how it is actually coded. That's > important, to express those expectations, because usually you trust > your expectations more than you trust the implementing code. So if > the test breaks, usually it's the code that's broken, so it gives a > quick canary-in-the-coalmine. > > > If you want to explore this more, check for Kent Beck's: "Test-driven > Development: By Example". > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Sydney Shall From fomcl at yahoo.com Fri Apr 4 15:55:43 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 4 Apr 2014 06:55:43 -0700 (PDT) Subject: [Tutor] unittests In-Reply-To: <533EAFA9.30000@virginmedia.com> References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> <533AA366.2000308@virginmedia.com> <20140401124530.GS16526@ando> <533EAFA9.30000@virginmedia.com> Message-ID: <1396619743.29998.YahooMailNeo@web163804.mail.gq1.yahoo.com> ----- Original Message ----- > From: Sydney Shall > To: tutor at python.org > Cc: > Sent: Friday, April 4, 2014 3:12 PM > Subject: Re: [Tutor] unittests > > I would like to thank both Steven and Danny for their explanations. > They were much easier for me to understand than the documentation. > I now think that I understand what I have to do and how I proceed. > Thanks to you both for spending so much time on the answer. > Sydney Sydney, ? I found this a very useful book: http://www.amazon.com/Python-Testing-Beginners-Daniel-Arbuckle/dp/1847198848 ? regards, Albert-Jan From s.shall at virginmedia.com Fri Apr 4 16:13:34 2014 From: s.shall at virginmedia.com (Sydney Shall) Date: Fri, 04 Apr 2014 15:13:34 +0100 Subject: [Tutor] unittests In-Reply-To: <1396619743.29998.YahooMailNeo@web163804.mail.gq1.yahoo.com> References: <9655F555-FE91-4EE5-B15A-01BFB48FCCD4@cox.net> <533AA366.2000308@virginmedia.com> <20140401124530.GS16526@ando> <533EAFA9.30000@virginmedia.com> <1396619743.29998.YahooMailNeo@web163804.mail.gq1.yahoo.com> Message-ID: <533EBE0E.4060906@virginmedia.com> On 04/04/2014 14:55, Albert-Jan Roskam wrote: > ----- Original Message ----- >> From: Sydney Shall >> To: tutor at python.org >> Cc: >> Sent: Friday, April 4, 2014 3:12 PM >> Subject: Re: [Tutor] unittests >> >> I would like to thank both Steven and Danny for their explanations. >> They were much easier for me to understand than the documentation. >> I now think that I understand what I have to do and how I proceed. >> Thanks to you both for spending so much time on the answer. >> Sydney > Sydney, > > I found this a very useful book: http://www.amazon.com/Python-Testing-Beginners-Daniel-Arbuckle/dp/1847198848 > > regards, > Albert-Jan > Dear Albert-Jan, Many thanks for the suggestion. I will have a look at the book. Best wishes, Sydney -- Sydney Shall -------------- next part -------------- An HTML attachment was scrubbed... URL: From akleider at sonic.net Sat Apr 5 00:45:32 2014 From: akleider at sonic.net (Alex Kleider) Date: Fri, 04 Apr 2014 15:45:32 -0700 Subject: [Tutor] data driven programming In-Reply-To: References: <8D6408E3-43DD-4235-8F78-B6E1F1FB3EA3@gmail.com> <5A5BF28B-2997-4E54-B418-AA235B0FE774@gmail.com> <0b353a1f4ab5d23f733cd8520e0fcac3@sonic.net> Message-ID: <8e8e8bec2e8cfa28b691bd0e6251527b@sonic.net> On 2014-04-04 01:51, Alan Gauld wrote: > On 04/04/14 03:33, Alex Kleider wrote: > >>> And to some extent Python is an example of a data driven >>> design since function (and method) calls are associated with >>> dictionaries (ie Python namespaces). >>> >> Can you elaborate, please, on what you mean by pointing out that the >> fact that Python's function and method calls are associated with >> dictionaries makes it 'an example of data driven design?' I'm not >> clear >> on that relationship. Thanks, Alex > > > Most of Python is built on dictionaries under the surface. > When you define a function you create a function object > and associate it with a name. In other words you store > the name as a key in a dictionary and have the function > object as the value. > > When you call the function Python looks up the name in > the dictionary and calls the associated object. > This is very similar to what was recommended to you > to avoid the long list of if/elif. Thank you. I thought there was something more. ak From kiethadu at icloud.com Sat Apr 5 01:49:16 2014 From: kiethadu at icloud.com (Keith Adu) Date: Fri, 04 Apr 2014 19:49:16 -0400 Subject: [Tutor] One on one tutor Message-ID: <1AD1283B-DF03-40F6-9BA3-6CAD9527F165@icloud.com> Hi my name is Keith, am a beginner with no experience in python or computer science. Am looking for someone to work with me one on one, I have many question that I need answered, my question are basic as of the moment because am starting, I don't want to send an email to everyone about my questions because I feel it will be a waste of their time. If you are interested please let me know. Thank you for reading this. From leamhall at gmail.com Sat Apr 5 11:40:29 2014 From: leamhall at gmail.com (Leam Hall) Date: Sat, 05 Apr 2014 05:40:29 -0400 Subject: [Tutor] One on one tutor In-Reply-To: <1AD1283B-DF03-40F6-9BA3-6CAD9527F165@icloud.com> References: <1AD1283B-DF03-40F6-9BA3-6CAD9527F165@icloud.com> Message-ID: <533FCF8D.3090604@gmail.com> On 04/04/2014 07:49 PM, Keith Adu wrote: > Hi my name is Keith, am a beginner with no experience in python or computer science. Am looking for someone to work with me one on one, I have many question that I need answered, my question are basic as of the moment because am starting, I don't want to send an email to everyone about my questions because I feel it will be a waste of their time. If you are interested please let me know. Thank you for reading this. Good morning Keith! There is no education like self-education! Alan's Python tutorial (http://www.alan-g.me.uk/) is a great place to start. If you just ask questions you will probably not learn as much as if you try and then ask specific questions. This is a beginner's list; people expect you to ask beginner questions. However, some might be a bit short in their answers if you have not at least tried to figure out the problem on your own. You will not really learn to program, either. Try. Fail. Explain the problem to yourself. Fail again. Ask the list for help when you can really explain the problem. Leam -- http://31challenge.net http://31challenge.net/insight From breamoreboy at yahoo.co.uk Sat Apr 5 11:42:05 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 05 Apr 2014 10:42:05 +0100 Subject: [Tutor] One on one tutor In-Reply-To: <1AD1283B-DF03-40F6-9BA3-6CAD9527F165@icloud.com> References: <1AD1283B-DF03-40F6-9BA3-6CAD9527F165@icloud.com> Message-ID: On 05/04/2014 00:49, Keith Adu wrote: > > Hi my name is Keith, am a beginner with no experience in python or computer science. Am looking for someone to work with me one on one, I have many question that I need answered, my question are basic as of the moment because am starting, I don't want to send an email to everyone about my questions because I feel it will be a waste of their time. If you are interested please let me know. Thank you for reading this. Welcome :) What do you think you've just done? You've sent an email to everybody on this list. That's the whole point *OF* this list. Please feel free to ask questions here, we don't bite. I'd simply ask that you try reading a tutorial or two and trying to write some code before you ask, that way you're far more likely to get answers. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From alan.gauld at btinternet.com Sat Apr 5 19:00:12 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 05 Apr 2014 18:00:12 +0100 Subject: [Tutor] One on one tutor In-Reply-To: <1AD1283B-DF03-40F6-9BA3-6CAD9527F165@icloud.com> References: <1AD1283B-DF03-40F6-9BA3-6CAD9527F165@icloud.com> Message-ID: On 05/04/14 00:49, Keith Adu wrote: > Hi my name is Keith, Hi Keith. > am a beginner with no experience in python or computer science. That's fine, about half of the people on the tutor list are beginners of one kind or another. I'd guess maybe 15% at any one time are complete beginners like yourself. So join the crowd. > Am looking for someone to work with me one on one, But we don't do that. This is a list where everyone learns from everyone else. Sometimes it's best to get an answer from an expert. Sometimes a fellow beginner can understand what you need better. Sometimes another beginner has the same issue as you but didn't ask... And sometimes it's better to get 5 answers to the same question, each presenting a different slant, than a single answer you only half understand. > I have many question that I need answered, my question are basic > as of the moment because am starting, I don't want to send an email > to everyone about my questions because I feel it will be a waste > of their time. Basic questions are what we are here for. In fact if you ask a very advanced question we will most likely tell you to go elsewhere! This list is for people like you. Jump in and join the fun. Just be sure to tell us: 1) Your Python version and OS version 2) Your tutorial/book if you are following one 3) And always post full error messages. They may not look like much to you (yet) but they mean a lot to us! :-) 4) Be as specific as possible about what you need to know. One of the key features of programming is attention to detail. It will also help if you can post in plain text rather than HTML. And don't top-post (insert your comments after the text you are replying to, as I did above). Some folks here get a bit tetchy about such things ;-) HTH -- Alan G Tutor List moderator Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From jf_byrnes at comcast.net Sat Apr 5 19:46:19 2014 From: jf_byrnes at comcast.net (Jim Byrnes) Date: Sat, 05 Apr 2014 12:46:19 -0500 Subject: [Tutor] Question about equality of sets Message-ID: Ubuntu 12.04 python 3.3 I was working through an exercise about sets. I needed to find the duplicates in a list and put them in a set. I figured the solution had to do with sets not supporting duplicates. I finally figured it out but along the way I was experimenting in idle and got some results I don't understand. >>> s = {1,2,3} >>> s {1, 2, 3} >>> s.add(1) == s # <1> False >>> s.add(1) == s.add(2) # <2> True >>> Neither <1> or <2> changes s, so why is <1> False and <2> True ? Thanks, Jim From steve at pearwood.info Sat Apr 5 20:15:40 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 6 Apr 2014 04:15:40 +1000 Subject: [Tutor] Question about equality of sets In-Reply-To: References: Message-ID: <20140405181540.GD16466@ando> On Sat, Apr 05, 2014 at 12:46:19PM -0500, Jim Byrnes wrote: > Ubuntu 12.04 python 3.3 > > I was working through an exercise about sets. I needed to find the > duplicates in a list and put them in a set. I figured the solution had > to do with sets not supporting duplicates. I finally figured it out but > along the way I was experimenting in idle and got some results I don't > understand. > > >>> s = {1,2,3} > >>> s > {1, 2, 3} > >>> s.add(1) == s # <1> > False > >>> s.add(1) == s.add(2) # <2> > True > >>> > > Neither <1> or <2> changes s, so why is <1> False and <2> True ? You're making an assumption about what s.add returns. You're assuming it returns a new set. It doesn't. Try this: print(s.add(100)) and see what it prints. set.add modifies the set in place. So calling s.add(1) tries to change s, it doesn't create a new set. It is standard in Python that methods that change the object in place normally return None: list.append set.add list.sort list.reverse dict.update etc. So your examples try: None == s # this is false None == None # but this is true -- Steven From jf_byrnes at comcast.net Sat Apr 5 20:54:01 2014 From: jf_byrnes at comcast.net (Jim Byrnes) Date: Sat, 05 Apr 2014 13:54:01 -0500 Subject: [Tutor] Question about equality of sets In-Reply-To: <20140405181540.GD16466@ando> References: <20140405181540.GD16466@ando> Message-ID: On 04/05/2014 01:15 PM, Steven D'Aprano wrote: > On Sat, Apr 05, 2014 at 12:46:19PM -0500, Jim Byrnes wrote: >> Ubuntu 12.04 python 3.3 >> >> I was working through an exercise about sets. I needed to find the >> duplicates in a list and put them in a set. I figured the solution had >> to do with sets not supporting duplicates. I finally figured it out but >> along the way I was experimenting in idle and got some results I don't >> understand. >> >>>>> s = {1,2,3} >>>>> s >> {1, 2, 3} >>>>> s.add(1) == s # <1> >> False >>>>> s.add(1) == s.add(2) # <2> >> True >>>>> >> >> Neither <1> or <2> changes s, so why is <1> False and <2> True ? > > You're making an assumption about what s.add returns. You're assuming it > returns a new set. It doesn't. Try this: > > > print(s.add(100)) > > > and see what it prints. Actually my assumption was worse than that. I was thinking that because it would not add a dup it would end up being {1,2,3} == {1,2,3} completely forgetting that the left side would return None. Thanks, Jim > set.add modifies the set in place. So calling s.add(1) tries to change > s, it doesn't create a new set. It is standard in Python that methods > that change the object in place normally return None: > > list.append > set.add > list.sort > list.reverse > dict.update > > etc. So your examples try: > > None == s # this is false > None == None # but this is true > > > From welcome.to.eye.o.rama at gmail.com Sat Apr 5 21:53:56 2014 From: welcome.to.eye.o.rama at gmail.com (John Aten) Date: Sat, 5 Apr 2014 14:53:56 -0500 Subject: [Tutor] Storing dictionary value, indexed by key, into a variable In-Reply-To: References: <8D6408E3-43DD-4235-8F78-B6E1F1FB3EA3@gmail.com> <5A5BF28B-2997-4E54-B418-AA235B0FE774@gmail.com> <0b353a1f4ab5d23f733cd8520e0fcac3@sonic.net> Message-ID: <3978384D-9001-4B1E-B405-6FA9E630F75D@gmail.com> I read the article on data driven programming that Danny linked too, and did some additional looking around. I couldn't find anything directly using Python, but I got an idea of the concept and went crazy with it. This may still be off the mark, but I created a complex combination of lists and dictionaries to represent each individual instance of each demonstrative (starting only with one): that_those = [ [ [ {'nom': 'ille', 'clue': 'That/Those, Singular, Masculine Nominative'}, {'gen': 'ill?us', 'clue': 'That/Those, Singular, Masculine Genitive'}, {'dat': 'ill?', 'clue': 'That/Those, Singular, Masculine Dative'}, {'acc': 'illum', 'clue': 'That/Those, Singular, Masculine Accusative'}, {'abl': 'ill?', 'clue': 'That/Those, Singular, Masculine Ablative'} ], [{'nom': 'ill?', 'clue': 'That/Those, Plural, Masculine Nominative'}, {'gen': 'ill?rum', 'clue': 'That/Those, Plural, Masculine Genitive'}, {'dat': 'ill?s', 'clue': 'That/Those, Plural, Masculine Dative'}, {'acc': 'ill?s', 'clue': 'That/Those, Plural, Masculine Accusative'}, {'abl': 'ill?s', 'clue': 'That/Those, Plural, Masculine Ablative'} ] ], [ [ {'nom': 'illa', 'clue': 'That/Those, Singular, Feminine Nominative'}, {'gen': 'ill?us', 'clue': 'That/Those, Singular, Feminine Genitive'}, {'dat': 'ill?', 'clue': 'That/Those, Singular, Feminine Dative'}, {'acc': 'illam', 'clue': 'That/Those, Singular, Feminine Accusative'}, {'abl': 'ill?', 'clue': 'That/Those, Singular, Feminine Ablative'} ], [ {'nom': 'illae', 'clue': 'That/Those, Plural, Feminine Nominative'}, {'gen': 'ill?rum', 'clue': 'That/Those, Plural, Feminine Genitive'}, {'dat': 'ill?s', 'clue': 'That/Those, Plural, Feminine Dative'}, {'acc': 'ill?s', 'clue': 'That/Those, Plural, Feminine Accusative'}, {'abl': 'ill?s', 'clue': 'That/Those, Plural, Feminine Ablative'} ] ] , [ [ {'nom': 'illud', 'clue': 'That/Those, Singular, Neuter Nominative'}, {'gen': 'ill?us', 'clue': 'That/Those, Singular, Neuter Genitive'}, {'dat': 'ill?', 'clue': 'That/Those, Singular, Neuter Dative'}, {'acc': 'illud', 'clue': 'That/Those, Singular, Neuter Accusative'}, {'abl': 'ill?', 'clue': 'That/Those, Singular, Neuter Ablative'} ], [ {'nom': 'illa', 'clue': 'That/Those, Plural, Neuter Nominative'}, {'gen': 'ill?rum', 'clue': 'That/Those, Plural, Neuter Genitive'}, {'dat': 'ill?s', 'clue': 'That/Those, Plural, Neuter Dative'}, {'acc': 'illa', 'clue': 'That/Those, Plural, Neuter Accusative'}, {'abl': 'ill?s', 'clue': 'That/Those, Plural, Neuter Ablative'} ] ] ] Now, this is a big mess, for sure, but it seems possible that it is abstract enough that I could reuse the same logic and just switch out the data to make different types of similar programs. Also, I can write a script to populate this structure to construct a program to drill any type of word that is declined by gender, number and case. I can call each item pretty easily like this: question_to_be_dispalyed_to_user = that_those[gender][number][q_and_a]["clue"] answer_to_that_question = that_those[gender][number][q_and_a][case] Where gender, number, q_and_a, and case follow Peter's suggestion: cases = ['nom', 'gen', 'dat', 'acc', 'abl'] case = random.choice(cases) The complete code will follow, but I have a spooky new problem. When I try to run the following code, I intermittently get the following error: Traceback (most recent call last): File "./latDemTest.py", line 59, in answer = that_those[gender][number][q_and_a][case] KeyError: 'abl' I looked this error up online, and it seems the key error is generated when one attempts to retrieve a value from a dictionary with a key that does not exist. The problem is, it seems to me that the keys should in fact exist. The odd thing is, the code returns this error sometimes, and sometimes it doesn't. I did a bunch of trials, keeping track of which particular entries in the data structures worked and which failed, and I discovered that they overlap. The dictionary {'gen': 'ill?rum', 'clue': 'That/Those, Plural, Neuter Genitive'} for example, happily spits out the proper question and answer sometimes, other times it shoots out the previously mentioned error. I cannot understand how this could happen! THE FULL CODE: #!/Library/Frameworks/Python.framework/Versions/3.2/bin/python3.2 # Don't forget ./ before filename to run from cmnd line! import random import os that_those = [ [ [ {'nom': 'ille', 'clue': 'That/Those, Singular, Masculine Nominative'}, {'gen': 'ill?us', 'clue': 'That/Those, Singular, Masculine Genitive'}, {'dat': 'ill?', 'clue': 'That/Those, Singular, Masculine Dative'}, {'acc': 'illum', 'clue': 'That/Those, Singular, Masculine Accusative'}, {'abl': 'ill?', 'clue': 'That/Those, Singular, Masculine Ablative'} ], [{'nom': 'ill?', 'clue': 'That/Those, Plural, Masculine Nominative'}, {'gen': 'ill?rum', 'clue': 'That/Those, Plural, Masculine Genitive'}, {'dat': 'ill?s', 'clue': 'That/Those, Plural, Masculine Dative'}, {'acc': 'ill?s', 'clue': 'That/Those, Plural, Masculine Accusative'}, {'abl': 'ill?s', 'clue': 'That/Those, Plural, Masculine Ablative'} ] ], [ [ {'nom': 'illa', 'clue': 'That/Those, Singular, Feminine Nominative'}, {'gen': 'ill?us', 'clue': 'That/Those, Singular, Feminine Genitive'}, {'dat': 'ill?', 'clue': 'That/Those, Singular, Feminine Dative'}, {'acc': 'illam', 'clue': 'That/Those, Singular, Feminine Accusative'}, {'abl': 'ill?', 'clue': 'That/Those, Singular, Feminine Ablative'} ], [ {'nom': 'illae', 'clue': 'That/Those, Plural, Feminine Nominative'}, {'gen': 'ill?rum', 'clue': 'That/Those, Plural, Feminine Genitive'}, {'dat': 'ill?s', 'clue': 'That/Those, Plural, Feminine Dative'}, {'acc': 'ill?s', 'clue': 'That/Those, Plural, Feminine Accusative'}, {'abl': 'ill?s', 'clue': 'That/Those, Plural, Feminine Ablative'} ] ] , [ [ {'nom': 'illud', 'clue': 'That/Those, Singular, Neuter Nominative'}, {'gen': 'ill?us', 'clue': 'That/Those, Singular, Neuter Genitive'}, {'dat': 'ill?', 'clue': 'That/Those, Singular, Neuter Dative'}, {'acc': 'illud', 'clue': 'That/Those, Singular, Neuter Accusative'}, {'abl': 'ill?', 'clue': 'That/Those, Singular, Neuter Ablative'} ], [ {'nom': 'illa', 'clue': 'That/Those, Plural, Neuter Nominative'}, {'gen': 'ill?rum', 'clue': 'That/Those, Plural, Neuter Genitive'}, {'dat': 'ill?s', 'clue': 'That/Those, Plural, Neuter Dative'}, {'acc': 'illa', 'clue': 'That/Those, Plural, Neuter Accusative'}, {'abl': 'ill?s', 'clue': 'That/Those, Plural, Neuter Ablative'} ] ] ] # place one: 0 = masculine, 1 = feminine, 2 = neuter # place two: 0 = singular, 1 = plural # place two: # place three: 0 = answer, 1 = clue # place four, 'nom' = answer, 'clue' = description # So, word[0][0][0]["nom"] should produce: ille numbers = [0,1] genders = [0,1,2] cases = ['nom', 'gen', 'dat', 'acc', 'abl'] guess = '' score = 0 tries = 0 while guess != 'exit': os.system('clear') q_and_a = random.choice(numbers) gender = random.choice(genders) number = random.choice(numbers) case = random.choice(cases) print(case) question = that_those[gender][number][q_and_a]["clue"] print(question) answer = that_those[gender][number][q_and_a][case] print(question) guess = input() if guess == 'exit': N = 0 percentage = int((score/tries) * 100) print("You scored", score, "out of", tries, ", or", percentage, "%") tries +=1 if guess == answer: score += 1 print('faster!') else: print(answer) print('slower...') input() From alan.gauld at btinternet.com Sat Apr 5 23:13:47 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 05 Apr 2014 22:13:47 +0100 Subject: [Tutor] Storing dictionary value, indexed by key, into a variable In-Reply-To: <3978384D-9001-4B1E-B405-6FA9E630F75D@gmail.com> References: <8D6408E3-43DD-4235-8F78-B6E1F1FB3EA3@gmail.com> <5A5BF28B-2997-4E54-B418-AA235B0FE774@gmail.com> <0b353a1f4ab5d23f733cd8520e0fcac3@sonic.net> <3978384D-9001-4B1E-B405-6FA9E630F75D@gmail.com> Message-ID: On 05/04/14 20:53, John Aten wrote: > The complete code will follow, but I have a spooky new problem. Welcome to the world of data driven programming. Its seductive but brings its own set of challenges. Structuring the data and how you format it visually makes a huge difference to your ability to debug. I don't know why your code gives the error it does but a few intermediate print statements might help pinpoint what's happening. It could be as simple as a missing comma or bracket/brace somewhere, but you need to be sure the layout it consistent to spot these kinds of bugs. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Sat Apr 5 23:14:22 2014 From: __peter__ at web.de (Peter Otten) Date: Sat, 05 Apr 2014 23:14:22 +0200 Subject: [Tutor] Storing dictionary value, indexed by key, into a variable References: <8D6408E3-43DD-4235-8F78-B6E1F1FB3EA3@gmail.com> <5A5BF28B-2997-4E54-B418-AA235B0FE774@gmail.com> <0b353a1f4ab5d23f733cd8520e0fcac3@sonic.net> <3978384D-9001-4B1E-B405-6FA9E630F75D@gmail.com> Message-ID: John Aten wrote: > I read the article on data driven programming that Danny linked too, and > did some additional looking around. I couldn't find anything directly > using Python, but I got an idea of the concept and went crazy with it. > This may still be off the mark, but I created a complex combination of > lists and dictionaries to represent each individual instance of each > demonstrative (starting only with one): > > that_those = [ [ [ {'nom': 'ille', 'clue': 'That/Those, Singular, > Masculine Nominative'}, {'gen': 'ill?us', 'clue': 'That/Those, Singular, > Masculine Genitive'}, {'dat': 'ill?', 'clue': 'That/Those, Singular, > Masculine Dative'}, {'acc': 'illum', 'clue': 'That/Those, Singular, > Masculine Accusative'}, {'abl': 'ill?', 'clue': 'That/Those, Singular, > Masculine Ablative'} ], [{'nom': 'ill?', 'clue': 'That/Those, Plural, > Masculine Nominative'}, > {'gen': 'ill?rum', 'clue': 'That/Those, Plural, Masculine > {Genitive'}, > {'dat': 'ill?s', 'clue': 'That/Those, Plural, Masculine Dative'}, > {'acc': 'ill?s', 'clue': 'That/Those, Plural, Masculine Accusative'}, > {'abl': 'ill?s', 'clue': 'That/Those, Plural, Masculine Ablative'} ] ], [ > [ {'nom': 'illa', 'clue': 'That/Those, Singular, Feminine Nominative'}, > {'gen': 'ill?us', 'clue': 'That/Those, Singular, Feminine > {Genitive'}, > {'dat': 'ill?', 'clue': 'That/Those, Singular, Feminine Dative'}, > {'acc': 'illam', 'clue': 'That/Those, Singular, Feminine Accusative'}, > {'abl': 'ill?', 'clue': 'That/Those, Singular, Feminine Ablative'} ], [ > {'nom': 'illae', 'clue': 'That/Those, Plural, Feminine Nominative'}, > {'gen': 'ill?rum', 'clue': 'That/Those, Plural, Feminine Genitive'}, > {'dat': 'ill?s', 'clue': 'That/Those, Plural, Feminine Dative'}, {'acc': > 'ill?s', 'clue': 'That/Those, Plural, Feminine Accusative'}, {'abl': > 'ill?s', 'clue': 'That/Those, Plural, Feminine Ablative'} ] ] , [ [ > {'nom': 'illud', 'clue': 'That/Those, Singular, Neuter Nominative'}, > {'gen': 'ill?us', 'clue': 'That/Those, Singular, Neuter > {Genitive'}, > {'dat': 'ill?', 'clue': 'That/Those, Singular, Neuter Dative'}, > {'acc': 'illud', 'clue': 'That/Those, Singular, Neuter Accusative'}, > {'abl': 'ill?', 'clue': 'That/Those, Singular, Neuter Ablative'} ], [ > {'nom': 'illa', 'clue': 'That/Those, Plural, Neuter Nominative'}, > {'gen': 'ill?rum', 'clue': 'That/Those, Plural, Neuter > {Genitive'}, > {'dat': 'ill?s', 'clue': 'That/Those, Plural, Neuter Dative'}, > {'acc': 'illa', 'clue': 'That/Those, Plural, Neuter Accusative'}, > {'abl': 'ill?s', 'clue': 'That/Those, Plural, Neuter Ablative'} ] ] ] > > Now, this is a big mess, for sure, but it seems possible that it is > abstract enough that I could reuse the same logic and just switch out the > data to make different types of similar programs. Also, I can write a > script to populate this structure to construct a program to drill any type > of word that is declined by gender, number and case. I can call each item > pretty easily like this: > > question_to_be_dispalyed_to_user = > that_those[gender][number][q_and_a]["clue"] answer_to_that_question = > that_those[gender][number][q_and_a][case] > > Where gender, number, q_and_a, and case follow Peter's suggestion: > > cases = ['nom', 'gen', 'dat', 'acc', 'abl'] > case = random.choice(cases) > > The complete code will follow, but I have a spooky new problem. When I try > to run the following code, I intermittently get the following error: > > Traceback (most recent call last): > File "./latDemTest.py", line 59, in > answer = that_those[gender][number][q_and_a][case] > KeyError: 'abl' > > I looked this error up online, and it seems the key error is generated > when one attempts to retrieve a value from a dictionary with a key that > does not exist. The problem is, it seems to me that the keys should in > fact exist. The odd thing is, the code returns this error sometimes, and > sometimes it doesn't. I did a bunch of trials, keeping track of which > particular entries in the data structures worked and which failed, and I > discovered that they overlap. The dictionary {'gen': 'ill?rum', 'clue': > 'That/Those, Plural, Neuter Genitive'} for example, happily spits out the > proper question and answer sometimes, other times it shoots out the > previously mentioned error. I cannot understand how this could happen! There are dicts that have a "nom" key, other dicts that have a "gen" key, and so on. I didn't read your code completely, but my guess is that you do not get a KeyError in the rare case (roughly 1 out 5) that you pick a dictionary that handles the same casus as the one you picked independently. How would you go on fixing this? I think this little table is a good start: > # place one: 0 = masculine, 1 = feminine, 2 = neuter > # place two: 0 = singular, 1 = plural > # place two: > # place three: 0 = answer, 1 = clue > # place four, 'nom' = answer, 'clue' = description > # So, word[0][0][0]["nom"] should produce: ille Unfortunately it gets a little fuzzy in the middle. So here's my fixed table: place one, genus: 0=m, 1=f, 2=n place two, numerus: 0=sg, 1=pl place three, casus: 0=nom, 1=gen, 2=dat, ... place four: "answer"=answer, "clue"=description Everything but place four is a list. That allows for a small simplification: As random.choice() works on arbitrary lists you can apply it directly on your data: #untested genus = random.choice(that_those) numerus = random.choice(genus) casus = random.choice(numerus) clue = casus["clue"] answer = casus["answer"] Once you have more pronomina you just wrap the whole thing into place zero, the word: 0=(everything from above for that/those), 1=(the same structure for this), ... # and so on However, before you add more data you might give some thought to a format of the data that is easier to write and proof-read. You can then write another little script that morphs the structure into the one needed by your current script. From denis.spir at gmail.com Sun Apr 6 11:19:27 2014 From: denis.spir at gmail.com (spir) Date: Sun, 06 Apr 2014 11:19:27 +0200 Subject: [Tutor] One on one tutor In-Reply-To: <1AD1283B-DF03-40F6-9BA3-6CAD9527F165@icloud.com> References: <1AD1283B-DF03-40F6-9BA3-6CAD9527F165@icloud.com> Message-ID: <53411C1F.6060503@gmail.com> On 04/05/2014 01:49 AM, Keith Adu wrote: > > > > Hi my name is Keith, am a beginner with no experience in python or computer science. Am looking for someone to work with me one on one, I have many question that I need answered, my question are basic as of the moment because am starting, I don't want to send an email to everyone about my questions because I feel it will be a waste of their time. If you are interested please let me know. Thank you for reading this. I'd say * it is fine to prefere one on one tutoring than collective or public * it is fine to ask here, since indeed that's a place to find a tutor on python But the issue is that the quality of such tutoring essentially depends on the quality of your personal relation (between learner & tutor), and there is no way to know that before trying. So, I guess the best is to start here on the list, and if after a while you note 1, 2, 3 persons you'd really like to deal as a tutor for you in private, then just ask --privately. d From denis.spir at gmail.com Sun Apr 6 11:25:16 2014 From: denis.spir at gmail.com (spir) Date: Sun, 06 Apr 2014 11:25:16 +0200 Subject: [Tutor] Question about equality of sets In-Reply-To: References: Message-ID: <53411D7C.4040800@gmail.com> On 04/05/2014 07:46 PM, Jim Byrnes wrote: > Ubuntu 12.04 python 3.3 > > I was working through an exercise about sets. I needed to find the duplicates in > a list and put them in a set. I figured the solution had to do with sets not > supporting duplicates. I finally figured it out but along the way I was > experimenting in idle and got some results I don't understand. > >>>> s = {1,2,3} >>>> s > {1, 2, 3} >>>> s.add(1) == s # <1> > False >>>> s.add(1) == s.add(2) # <2> > True >>>> > > Neither <1> or <2> changes s, so why is <1> False and <2> True ? The core issue is that set.add() * is not a computation-function that compute a new set, here like 's' but with a possible additional item (1 or 2) * is an action-function that just possibly puts an item in a set This function returns nothing, in fact None. So you are comparing first None with s, second none with None. d From lacation at gmail.com Sun Apr 6 10:46:57 2014 From: lacation at gmail.com (Laura Kauria) Date: Sun, 6 Apr 2014 11:46:57 +0300 Subject: [Tutor] Python & algorithms (Lang line simplification algorithm) Message-ID: Hi all, I'm new with python and have done little coding with Java. At uni I would need to start a coding task with python writing a proper/working code and we don't get much help at school. Can someone help with which kinds of libraries I need to download to start to work with lists consisting coordinates? Here are a pseudocode of the algorithm http://web.cs.sunyit.edu/~poissad/projects/Curve/about_algorithms/lang Thanks for all the answers! Laura -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun Apr 6 20:21:21 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 06 Apr 2014 19:21:21 +0100 Subject: [Tutor] Python & algorithms (Lang line simplification algorithm) In-Reply-To: References: Message-ID: On 06/04/14 09:46, Laura Kauria wrote: > Can someone help with which kinds of libraries I need to download to > start to work with lists consisting coordinates? It depends what you are doing but you may not need to download anything. Like Java Python has a large standard library of modules. Also python built in collection types include tuples which can be used to store point data (x,y,z) And lists for collections of objects, including other lists. And dictionaries for value based lookups. > Here are a pseudocode of the algorithm > http://web.cs.sunyit.edu/~poissad/projects/Curve/about_algorithms/lang A cursory glance suggests you can do all of that using standard Python. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Sun Apr 6 20:30:27 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 6 Apr 2014 11:30:27 -0700 Subject: [Tutor] Python & algorithms (Lang line simplification algorithm) In-Reply-To: References: Message-ID: Hi Laura, Algorithmic code typically is simple enough that standard language features should suffice. I think you might pick up an external library to make it easier to visualize graphical output, but the core algorithms there appear fairly straightforward. To get some familiarity with basic Python programming, take a look at a tutorial like: http://www.greenteapress.com/thinkpython/thinkpython.html and see if you can pick up the basic language features of Python. If you have questions with that tutorial, feel free to ask here. It looks like you need enough to work with structured data (classes) and the basic data structures (lists, numbers). At least from my reading of the "curve simplification" page you pointed us to, I don't see anything there that's too bad. * You'll want a way to represent points. I think a structured value would be appropriate. Think classes. * You'll want to represent a sequence of these points. In Java, you can hold that collection with ArrayLists or some other list implementation. In Python, there's a generic list data structure that serves a similar purpose. For example, in Java, you'd represent structured data with classes, and a collection of these with a List: /////////////////////////////////////////////////////////////// class Person { private String name; public Person(String name) { this.name = name; } public void greet() { System.out.println("Hello, my name is " + name); } } // Usage: Person p = new Person("Laura"); p.sayHello(); Person p2 = new Person("Lydia"); List people = new ArrayList<>(); people.add(p); people.add(p2); /////////////////////////////////////////////////////////////// And in Python, you can do an analogous construction: ##################################### class Person(object): def __init__(self, name): self.name = name def greet(self): print("Hello, my name is " + self.name) ## Usage p = Person("Laura") p.sayHello() p2 = Person("Lydia") people = [] people.append(p) people.append(p2) ##################################### So there should be a lot of transfer of basic knowledge between what you've learned in Java to Python programming. Many of the concepts are the same, but the names are just different because of the Tower of Babel effect. A basic tutorial of Python will cover these general topics. What might be domain-specific here is the visualization part: you'll want to make it easy to visually see the output of these graphical algorithms. You may want to visualize these points on a graphical canvas on screen. You could have your program generate an image file like a .png, .gif, or .svg file. Another approach may be to use a GUI toolkit that opens up a canvas as part of the program, where you can then manipulate the canvas. If you want to take that approach, you might consider Tkinter, which is a GUI library that should come bundled with Python if I'm not mistaken. See: http://effbot.org/tkinterbook/canvas.htm for a canvas example. So once you have the basic algorithms down, you might use a Tkinter canvas to visualize and see that your algorithms are doing something reasonable. From aruprakshit at rocketmail.com Sun Apr 6 20:27:55 2014 From: aruprakshit at rocketmail.com (Arup Rakshit) Date: Mon, 7 Apr 2014 02:27:55 +0800 (SGT) Subject: [Tutor] how to find the maximum length substring with equal 'a' and 'b' ? Message-ID: <1396808875.10170.YahooMailNeo@web193904.mail.sg3.yahoo.com> Suppose, if I have the string 'aababbb', I want to get the output as 'aababb'. I want the output in time complexity O(N) and space complexity O(1). when input string is 'abababa', output should be same as input. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon Apr 7 00:56:13 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 7 Apr 2014 08:56:13 +1000 Subject: [Tutor] how to find the maximum length substring with equal 'a' and 'b' ? In-Reply-To: <1396808875.10170.YahooMailNeo@web193904.mail.sg3.yahoo.com> References: <1396808875.10170.YahooMailNeo@web193904.mail.sg3.yahoo.com> Message-ID: <20140406225613.GF16466@ando> On Mon, Apr 07, 2014 at 02:27:55AM +0800, Arup Rakshit wrote: > Suppose, if I have the string 'aababbb', I want to get the output as > 'aababb'. I want the output in time complexity O(N) and space > complexity O(1). when input string is 'abababa', output should be same > as input. What is your question about? Are you having trouble with learning Python, or about the algorithm? If you tell us the algorithm to use, we can help you with learning Python. This sounds like homework. Can you show us what work you have already done? In your first example, your output has 3 a's and 3 b's, but your second example doesn't have equal a's and b's. You have: input abababa (4 a's and 3 b's) so the output should be ababab, not abababa. -- Steven From alan.gauld at btinternet.com Mon Apr 7 01:38:44 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 07 Apr 2014 00:38:44 +0100 Subject: [Tutor] how to find the maximum length substring with equal 'a' and 'b' ? In-Reply-To: <1396808875.10170.YahooMailNeo@web193904.mail.sg3.yahoo.com> References: <1396808875.10170.YahooMailNeo@web193904.mail.sg3.yahoo.com> Message-ID: On 06/04/14 19:27, Arup Rakshit wrote: > Suppose, if I have the string 'aababbb', I want to get the output as > 'aababb'. I want the output in time complexity O(N) and space complexity > O(1). when input string is 'abababa', output should be same as input. You will need to explain more about what you want to do. Your two examples don't have any obvious link. The first result is 6 characters the second is 7. The first has equal a's and b's the second has differing numbers and doesn't match your subject line. What is the criteria for deciding what the output is? Once we know that we can start worrying about whether the solution is O(N) etc. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From keithadu at live.com Mon Apr 7 06:02:37 2014 From: keithadu at live.com (keith papa) Date: Mon, 7 Apr 2014 00:02:37 -0400 Subject: [Tutor] understanding Functions help Message-ID: Hi my name is keith and am new to python programming, Am learning python for the first time with the help of coursera problem is am starting a new topic call functions in python and am totally lost. can you please help me understand how function work? why it use in python, what are the rules of function etc. this some of the code the teacher used as an example. http://www.codeskulptor.org/#examples-functions.py this is the video explaing functions: https://onedrive.live.com/redir?resid=B91E3E58ABADC574%21863 -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Apr 7 10:40:22 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 07 Apr 2014 09:40:22 +0100 Subject: [Tutor] understanding Functions help In-Reply-To: References: Message-ID: On 07/04/14 05:02, keith papa wrote: > Hi my name is keith and am new to python programming, Am learning python > for the first time with the help of coursera problem is am starting a > new topic call functions in python and am totally lost. can you please > help me understand how function work? why it use in python, what are the > rules of function etc. Hi Keith, Functions are basically mini programs that you can call from inside your program. You have probably used them already lots of times. For example things like print() (In Python v3), len() and range() are functions. There are many more that are built in to Python. But Python also lets you define your own functions. There are several reasons for doing this: 1) Functions make your code easier to understand. By bundling up a bunch of code that does something into a function and giving it a sensible name your code is easier to read. 2) Functions are reusable, they save you having to repeat the same set of code over and over in your program. Once you learn about modules you will also be able to reuse them across different programs. 3) Functions are easier to test. You can write a function and test it independently from the rest of your program. If your program is mostly made up of functions you can test all of the functions separately. Then testing the whole program is easier because you know that each small function works, so you only need to focus on the glue holding them together. The last point will really only make sense to you once you start writing bigger programs. And functions are one of the keys to building bigger programs. As to the rules of how to create functions. you follow a standard pattern. def FunctionName(optional parameter list): code that does the function return some value(s) here You then call it like this aVariable = FunctionName(some values here) Now you probably don't understand all of that (or any of it?) But if you send a reply back to the list(hit Reply ALL) highlighting the bits you don't get. Asking extra questions where needed, then we can focus our replies where you need them. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Mon Apr 7 20:33:42 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 7 Apr 2014 11:33:42 -0700 Subject: [Tutor] understanding Functions help In-Reply-To: References: Message-ID: On Sun, Apr 6, 2014 at 9:02 PM, keith papa wrote: > Hi my name is keith and am new to python programming, Am learning python for > the first time with the help of coursera problem is am starting a new topic > call functions in python and am totally lost. can you please help me > understand how function work? why it use in python, what are the rules of > function etc. Just to check: have you been introduced to the idea of a function in your math class? That is, that a function is something that takes inputs and produces outputs? From stareq13 at yahoo.com Mon Apr 7 20:53:08 2014 From: stareq13 at yahoo.com (S Tareq) Date: Mon, 7 Apr 2014 19:53:08 +0100 (BST) Subject: [Tutor] understanding Functions help In-Reply-To: References: Message-ID: <1396896788.25781.YahooMailNeo@web133105.mail.ir2.yahoo.com> see more on there they explained everything in there this website :? http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/index.html ? On Monday, 7 April 2014, 19:34, Danny Yoo wrote: On Sun, Apr 6, 2014 at 9:02 PM, keith papa wrote: > Hi my name is keith and am new to python programming, Am learning python for > the first time with the help of coursera problem is am starting a new topic > call functions in python and am totally lost. can you please help me > understand how function work? why it use in python, what are the rules of > function etc. Just to check: have you been introduced to the idea of a function in your math class?? That is, that a function is something that takes inputs and produces outputs? _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From vogernewsletters at yahoo.gr Mon Apr 7 22:11:20 2014 From: vogernewsletters at yahoo.gr (voger) Date: Mon, 07 Apr 2014 23:11:20 +0300 Subject: [Tutor] understanding Functions help In-Reply-To: References: Message-ID: <53430668.2040700@yahoo.gr> Hi, I can't speak about python as I am now discovering it my self but I do understand a bit about functions. So let me try to explain as a newbie to newbie. The examples below are not valid python code but they do display the logic. Lets say you want two temperatures in Fahrenheit but you have two temperatures in Celsius instead. The formula to convert the temperature from Celsius to Fahrenheit is x * 1.8 + 32 = y where x is the temperature in C and y is the temperature in F. So now we have var x1 = 30 #first variable in C var x2 = 50 #second variable in C and if you want the temperatures in F var y1 = x1 / 1.8 + 32 var y2 = x2 * 1,8 + 32 For such a small code this works well. It is not a big deal. What if you have a complicated mathematical formula in there or many things to do? It will get quite lengthy and tiresome to type the same things again and again. Also are you sure you can type it always without errors? I purposely inserted a typo in both calls. Did you catch them? What if they were 30 calls like this? Introducing functions. A function is a programming ummm... thing that can take few parameters and perform an action and optionally return a result. Following the example above we can introduce this function celsiusToFahrenheit(temperatureInCelsius=0) var temperatureInFahrenheit = temperatureInCelsius * 1.8 + 32 return temperatureInFahrenheit Above *celsiusToFahrenheit* is the name of the function. *temperatureInCelsius* is the parameter. There can be more than one. ( For example a function that adds 2 numbers could be add(number1, number2) return number1 + number2 ) Notice that it is defined as temperatureInCelsius=0. That means that in case you don't provide any parameters it will use 0 as default. During the workings of the function that parameter can act as a variable. As soon as the function finishes it's job that variable and any other variables defined (e.g. temperatureInFahrenheit) will be destroyed and cease to exist. Only the *value that is returned* survives and you must assign it to a variable in your main program otherwise you will loose that too. *return* is what it will return back where you called it. You may write a function that doesn't need to return anything. In that case you can omit the return statement. Now our code above could be written as celsiusToFahrenheit(temperatureInCelsius=0) var temperatureInFahrenheit = temperatureInCelsius * 1.8 - 32 return temperatureInFahrenheit var x1 = 30 #first variable in C var x2 = 50 #second variable in C and if you want the temperatures in F var y1 = celsiusToFahrenheit(x1) var y2 = celsiusToFahrenheit(x2) var y3 = celsiusToFahrenheit() celsiusToFahrenheit(x1) # kiss the returned variable bye bye The first two y1 and y2 will call celsiusToFahrenheit with x1 or x2 as parameter. Notice that we don't need to use the same name for the parameter in the declaration and in the function call. The third variable y3 calls it without parameters. This is possible because in the definition we have temperatureInCelsius=0 so the parameter is substituted automatically to 0 and that function will return 32. BTW in that function I have made another typo. Did you catch it? In how many places do you have to fix the typo now? I hope this helps. On 04/07/2014 07:02 AM, keith papa wrote:> Hi my name is keith and am new to python programming, Am learning python > for the first time with the help of coursera problem is am starting a > new topic call functions in python and am totally lost. can you please > help me understand how function work? why it use in python, what are the > rules of function etc. > > > this some of the code the teacher used as an example. > > http://www.codeskulptor.org/#examples-functions.py > > > this is the video explaing functions: > > https://onedrive.live.com/redir?resid=B91E3E58ABADC574%21863 > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > On 04/07/2014 07:02 AM, keith papa wrote: > Hi my name is keith and am new to python programming, Am learning python > for the first time with the help of coursera problem is am starting a > new topic call functions in python and am totally lost. can you please > help me understand how function work? why it use in python, what are the > rules of function etc. > > > this some of the code the teacher used as an example. > > http://www.codeskulptor.org/#examples-functions.py > > > this is the video explaing functions: > > https://onedrive.live.com/redir?resid=B91E3E58ABADC574%21863 > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From rhce.san at gmail.com Tue Apr 8 07:40:57 2014 From: rhce.san at gmail.com (Santosh Kumar) Date: Tue, 8 Apr 2014 11:10:57 +0530 Subject: [Tutor] Constructs Message-ID: 1 #!/usr/bin/python 2 3 class shape: 4 def __init__(self,x,y): 5 self.x = x 6 self.y = y 7 description = "This shape has not been described yet" 8 author = "Nobody has claimed to make this shape yet" 9 10 def __init__(self,x,y,z): 11 self.x = x 12 self.y = y 13 self.z = z 14 print "The values are %d,%d,%d" %(self.x,self.y,self.z) 15 16 triangle = shape(100,20,30) 17 rectange = shape(20,30) I am getting NameError exceptions when i am trying to achieve these. python third.py Traceback (most recent call last): File "third.py", line 3, in class shape: File "third.py", line 14, in shape print "The values are %d,%d,%d" %(self.x,self.y,self.z) NameError: name 'self' is not defined can we have two constructs within the same class. My requiment is very simple, whenn i want to pass two values the shape class should take two arguments and when i pass on three it should take 3 arguments . Is this possible ? Thanks, santosh -------------- next part -------------- An HTML attachment was scrubbed... URL: From rhce.san at gmail.com Tue Apr 8 07:44:36 2014 From: rhce.san at gmail.com (Santosh Kumar) Date: Tue, 8 Apr 2014 11:14:36 +0530 Subject: [Tutor] Inheritance in classes Message-ID: Can i mask the parent attibutes in the child. let me give a quick example. In [1]: class a: ...: value1 = 1 ...: value2 = 2 ...: In [2]: class b(a): ...: value3 = 3 ...: In [3]: obj1 = b() In [4]: obj1.value1 Out[4]: 1 In [5]: obj1.value2 Out[5]: 2 In [6]: obj1.value3 Out[6]: 3 If you notice in the below example you will see that the child class object ``obj1`` has inherited all the attibutes of the parent class. Is there a way by which i can make the child class not inherit some of the properites of parent class. -- D. Santosh Kumar -------------- next part -------------- An HTML attachment was scrubbed... URL: From rhce.san at gmail.com Tue Apr 8 07:48:40 2014 From: rhce.san at gmail.com (Santosh Kumar) Date: Tue, 8 Apr 2014 11:18:40 +0530 Subject: [Tutor] Block highlighters in python Message-ID: Is there a way by which we can highlight a block in the python ? i have a huge code and i want to see a block of ``if`` code. How do i achieve this in VIM or any other editor. Note: In perl if i put my cursor on one "{" it will hightlight the other closed "}". Do we have any such facility in python ? -- D. Santosh Kumar -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Apr 8 10:20:53 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 08 Apr 2014 09:20:53 +0100 Subject: [Tutor] Constructs In-Reply-To: References: Message-ID: On 08/04/14 06:40, Santosh Kumar wrote: > 1 #!/usr/bin/python > 2 > 3 class shape: > 4 def __init__(self,x,y): > 5 self.x = x > 6 self.y = y > 7 description = "This shape has not been described yet" > 8 author = "Nobody has claimed to make this shape yet" > 9 > 10 def __init__(self,x,y,z): > 11 self.x = x > 12 self.y = y > 13 self.z = z > 14 print "The values are %d,%d,%d" %(self.x,self.y,self.z) > 15 > 16 triangle = shape(100,20,30) > 17 rectange = shape(20,30) > > I am getting NameError exceptions when i am trying to achieve these. > > python third.py > Traceback (most recent call last): > File "third.py", line 3, in > class shape: > File "third.py", line 14, in shape > print "The values are %d,%d,%d" %(self.x,self.y,self.z) > NameError: name 'self' is not defined > > can we have two constructs within the same class. My requiment is very > simple, when i want to pass two values the shape class should take two > arguments and when i pass on three it should take 3 arguments . Is this > possible ? No, its not possible in Python, there is no overloading of methods. The normal way to accept variable numbers of parameters is to use default values def __init__(self, a, b, c=None): if c is None: # do one thing else: # do another But in your case you should probably be defining subclasses since triangle and rectangle are probably going to need different method implementations for most things. The name error however is nothing to do with that. The problem there is that you have put the print statement outside of the method so it gets executed during class definition. At that stage there is no instance and therefore no self value. I suspect you wanted to have it indented to the same level as the self assignment lines? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Tue Apr 8 10:27:59 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 08 Apr 2014 09:27:59 +0100 Subject: [Tutor] Inheritance in classes In-Reply-To: References: Message-ID: On 08/04/14 06:44, Santosh Kumar wrote: > Can i mask the parent attibutes in the child. let me give a quick example. > > In [1]: class a: > ...: value1 = 1 > ...: value2 = 2 > ...: > > In [2]: class b(a): > ...: value3 = 3 > ...: > Note that these are class variables and not instance variables. > In [3]: obj1 = b() > > In [4]: obj1.value1 > Out[4]: 1 > > In [6]: obj1.value3 > Out[6]: 3 > > If you notice in the below example you will see that the child class > object ``obj1`` has inherited all the attibutes of the parent class. Yes that's what inheritance means. > there a way by which i can make the child class not inherit some of the > properites of parent class. No. But you can change the inherited values by masking them with your local versions, which could be None. class c(a): value1 = None obj2 = c() print(obj2.value1) -> None HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Tue Apr 8 10:47:36 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 08 Apr 2014 09:47:36 +0100 Subject: [Tutor] Block highlighters in python In-Reply-To: References: Message-ID: On 08/04/14 06:48, Santosh Kumar wrote: > Is there a way by which we can highlight a block in the python ? > > i have a huge code and i want to see a block of ``if`` code. How do i > achieve this in VIM or any other editor. > > Note: In perl if i put my cursor on one "{" it will hightlight the other > closed "}". > > Do we have any such facility in python ? This is not a feature of Python but of the IDE or editor. I don't know of any editor that does that although several have folding that can collapse a block (one example is Scite) so it should be possible. Some of the Python specific IDEs or the Python plugins for Netbeans or Eclipse etc might support it. In vim, if you put blank lines between your blocks the {} keys will move yo to beginning/end of block (ie paragraph). That's how I tend to do it... But that doesn't match where you have nested blocks, inside a function say and you want the outer block... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From dpalao.python at gmail.com Tue Apr 8 10:49:43 2014 From: dpalao.python at gmail.com (David Palao) Date: Tue, 8 Apr 2014 10:49:43 +0200 Subject: [Tutor] Inheritance in classes In-Reply-To: References: Message-ID: 2014-04-08 7:44 GMT+02:00 Santosh Kumar : > Can i mask the parent attibutes in the child. let me give a quick example. > > In [1]: class a: > ...: value1 = 1 > ...: value2 = 2 > ...: > > In [2]: class b(a): > ...: value3 = 3 > ...: > > In [3]: obj1 = b() > > In [4]: obj1.value1 > Out[4]: 1 > > In [5]: obj1.value2 > Out[5]: 2 > > In [6]: obj1.value3 > Out[6]: 3 > > If you notice in the below example you will see that the child class object > ``obj1`` has inherited all the attibutes of the parent class. Is there a way > by which i can make the child class not inherit some of the properites of > parent class. > > > -- > D. Santosh Kumar > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Hi, This is the default behaviour. You can ovrcome that, but it will require extra work. For instance you could make use of pseudoprivate attributes (any attribute starting with double underscore, but not ending with dtwo underscores); or some managing attributes tool: *) __getattr__, __getattribute__ are generic ways to manage attribute fetching *) properties and descriptors allow a more specific way to control attributes (one by one) But, be careful. These tools can be very tricky at first. Hope it helps. Best From steve at pearwood.info Tue Apr 8 16:39:59 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 9 Apr 2014 00:39:59 +1000 Subject: [Tutor] Constructs In-Reply-To: References: Message-ID: <20140408143959.GJ16466@ando> On Tue, Apr 08, 2014 at 11:10:57AM +0530, Santosh Kumar wrote: > 1 #!/usr/bin/python > 2 > 3 class shape: > 4 def __init__(self,x,y): > 5 self.x = x > 6 self.y = y > 7 description = "This shape has not been described yet" > 8 author = "Nobody has claimed to make this shape yet" Notice that the body of the __init__ method is indented (by two spaces -- four is recommended) from the method header. The body ends once the indentation returns to the previous level. So this piece of code has three levels of indentation: Level 0: "class shape" is not indented; Level 1: "def __init__" is 1 indent in; Level 2: the body of the method is 2 indents in; Level 1: the "description" and "author" lines are outdented from 2 back to 1. Because "description" and "author" are indented level with the __init__ definition (NOT the body of the method, the def header) that makes them *class attributes*. They are bound to the class itself, not the instance, and are shared by all instances. In Java terms they would be called "static variables". > 9 > 10 def __init__(self,x,y,z): > 11 self.x = x > 12 self.y = y > 13 self.z = z This now overwrites the existing __init__ method with a new method, also called __init__, that takes three arguments instead of two. Because methods are values exactly the same as strings, floats, bools and so forth, you can only have one method with the same name. If you wrote: x = 1 x = 2 of course you would expect that the second assignment to x overwrites the first assignment -- x cannot have two different values at the same time. The same applies to methods: you cannot have __init__ set to a method taking arguments x, y, z and a method taking arguments x, y at the same time. The newest assignment wins. Java methods are fixed at compile-time, so it is easy for the compiler to decide which constructor method to call at compile-time: if there are two arguments, call the first method, if there are three, call the second. But Python methods are values, and can be replaced on the fly at run-time. Python can do run-time polymorphism, but not in the same way that you do it with Java. Instead, you should define the method with default arguments: def __init__(self, x, y, z=None): self.x = x self.y = y if z is not None: self.z = z > 14 print "The values are %d,%d,%d" %(self.x,self.y,self.z) This line is outdented relative to the body of the __init__ method, so it too is at the class scope. That means that the print statement will be executed at class definition time. The problem is, at class definition time, there is no instance yet, even the class doesn't exist yet, so the reference to "self" will fail with NameError. I think what you want is for the print to be indented one more level, so it is inside the method: def __init__(self,x,y,z): self.x = x self.y = y self.z = z print "The values are %d,%d,%d" % (self.x, self.y, self.z) -- Steven From steve at pearwood.info Tue Apr 8 16:49:29 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 9 Apr 2014 00:49:29 +1000 Subject: [Tutor] Inheritance in classes In-Reply-To: References: Message-ID: <20140408144929.GK16466@ando> On Tue, Apr 08, 2014 at 11:14:36AM +0530, Santosh Kumar wrote: > Can i mask the parent attibutes in the child. let me give a quick example. > > In [1]: class a: > ...: value1 = 1 > ...: value2 = 2 > ...: > > In [2]: class b(a): > ...: value3 = 3 > ...: All of value1, value2, value3 here are *class attributes*, bound to the class, not the instance. In Java terms, that is similar to static variables. For the purpose of your example, that is not very important, but it can make a difference. > In [3]: obj1 = b() > > In [4]: obj1.value1 > Out[4]: 1 > > In [5]: obj1.value2 > Out[5]: 2 > > In [6]: obj1.value3 > Out[6]: 3 > > If you notice in the below example you will see that the child class object > ``obj1`` has inherited all the attibutes of the parent class. That is how object oriented programming is supposed to work. If you don't want to inherit the attributes of class "a", you should not inherit from class "a". > Is there a > way by which i can make the child class not inherit some of the properites > of parent class. There is, but *you should not do this*. This is poor design, and violates the Liskov Substitution Principle: https://en.wikipedia.org/wiki/Liskov_substitution_principle http://www.oodesign.com/liskov-s-substitution-principle.html But what we can do is inherit from a, but over-ride access to one of the attributes and fake an attribute error. But really, you should not do this -- it is poor design. py> class A: ... value1 = 23 ... value2 = 42 ... py> class B(A): ... value3 = 17 ... @property ... def value1(self): ... raise AttributeError("Fake!") ... py> obj = B() py> obj.value1 Traceback (most recent call last): File "", line 1, in File "", line 5, in value1 AttributeError: Fake! py> obj.value2 42 py> obj.value3 17 -- Steven From steve at pearwood.info Tue Apr 8 16:55:56 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 9 Apr 2014 00:55:56 +1000 Subject: [Tutor] Block highlighters in python In-Reply-To: References: Message-ID: <20140408145556.GL16466@ando> On Tue, Apr 08, 2014 at 11:18:40AM +0530, Santosh Kumar wrote: > Is there a way by which we can highlight a block in the python ? > > i have a huge code and i want to see a block of ``if`` code. How do i > achieve this in VIM or any other editor. > > Note: In perl if i put my cursor on one "{" it will hightlight the other > closed "}". > > Do we have any such facility in python ? This has nothing to do with Python, it is completely to do with the editing capabilities of your editor. If you are using Windows Notepad, it is very weak and has nearly no functionality. If you are using Emacs, it can do nearly anything, perhaps with a little bit of scripting. I don't use Vim, so I cannot answer your question. But in my case, I always use four spaces for indents so I can easily see changes in indent level: # not this if condition: do_this() while condition: do_something() do_that() # make it obvious if condition: do_this() while condition: do_something() do_that() -- Steven From nielsen.jared at gmail.com Tue Apr 8 22:38:13 2014 From: nielsen.jared at gmail.com (Jared Nielsen) Date: Tue, 8 Apr 2014 14:38:13 -0600 Subject: [Tutor] question about strip() and list comprehension Message-ID: Hello, Could someone explain why and how this list comprehension with strip() works? f = open('file.txt') t = [t for t in f.readlines() if t.strip()] f.close() print "".join(t) I had a very long file of strings filled with blank lines I wanted to remove. I did some Googling and found the above code snippet, but no clear explanation as to why it works. I'm particularly confused by how "if t.strip()" is removing the blank lines. I also don't fully understand the 'print "".join(t)'. The above didn't remove the leading white space on several lines, so I made the following addition: f = open('file.txt') t = [t for t in f.readlines() if t.strip()] f.close() s = [x.lstrip() for x in t] print "".join(s) List comprehensions are still magic to me. How would I go about incorporating lstrip() in the first list comprehension? Many thanks, J. -------------- next part -------------- An HTML attachment was scrubbed... URL: From akleider at sonic.net Tue Apr 8 23:16:18 2014 From: akleider at sonic.net (Alex Kleider) Date: Tue, 08 Apr 2014 14:16:18 -0700 Subject: [Tutor] dictionary keys In-Reply-To: <20140408143959.GJ16466@ando> References: <20140408143959.GJ16466@ando> Message-ID: <372c828ff260cf2ef99a90bbf94d326f@sonic.net> I've got a fairly large script that uses a dictionary (called 'ipDic') each value of which is a dictionary which in turn also has values which are not simple types. Instead of producing a simple list, """ ips = ipDic.keys() print(ips) """ yields """ dict_keys(['61.147.107.120', '76.191.204.54', '187.44.1.153']) """ Searching my code for 'dict_keys' yields nothing. I've no idea where it comes from. I've been unable to reproduce this behaviour using simpler dictionaries which seem to work as I expect: >>> d = dict(a=1, b=2, c=3) >>> d {'a': 1, 'c': 3, 'b': 2} >>> d.keys() ['a', 'c', 'b'] >>> print(d.keys()) ['a', 'c', 'b'] >>> Can anyone shed light on why instead of getting the I get "dict_keys( )"? (Using Python3, on Ubuntu 12.4) Thanks, Alex From __peter__ at web.de Tue Apr 8 23:34:04 2014 From: __peter__ at web.de (Peter Otten) Date: Tue, 08 Apr 2014 23:34:04 +0200 Subject: [Tutor] dictionary keys References: <20140408143959.GJ16466@ando> <372c828ff260cf2ef99a90bbf94d326f@sonic.net> Message-ID: Alex Kleider wrote: > I've got a fairly large script that uses a dictionary (called 'ipDic') > each > value of which is a dictionary which in turn also has values which are > not > simple types. > Instead of producing a simple list, > """ > ips = ipDic.keys() > print(ips) > """ > yields > """ > dict_keys(['61.147.107.120', '76.191.204.54', '187.44.1.153']) > """ > > Searching my code for 'dict_keys' yields nothing. I've no idea where it > comes from. > >>>> > > Can anyone shed light on why instead of getting the > I > get "dict_keys( )"? > > (Using Python3, on Ubuntu 12.4) That's a change in Python 3 where dict.keys() no longer creates a list, but instead creates a view on the underlying dict data thus saving time and space. In the rare case where you actually need a list you can explicitly create one with ips = list(ipDic) > I've been unable to reproduce this behaviour using simpler dictionaries > which seem to work as I expect: >>>> d = dict(a=1, b=2, c=3) >>>> d > {'a': 1, 'c': 3, 'b': 2} >>>> d.keys() > ['a', 'c', 'b'] >>>> print(d.keys()) > ['a', 'c', 'b'] That's because the above is a session using Python 2. Compare: $ python3 Python 3.3.2+ (default, Feb 28 2014, 00:52:16) [GCC 4.8.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> dict(a=1, b=2).keys() dict_keys(['b', 'a']) $ python2 Python 2.7.5+ (default, Feb 27 2014, 19:37:08) [GCC 4.8.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> dict(a=1, b=2).keys() ['a', 'b'] PS: You can get a view in Python 2, too, with dict.viewkeys() From dyoo at hashcollision.org Tue Apr 8 23:44:26 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 8 Apr 2014 14:44:26 -0700 Subject: [Tutor] question about strip() and list comprehension In-Reply-To: References: Message-ID: > Could someone explain why and how this list comprehension with strip() > works? > > f = open('file.txt') > t = [t for t in f.readlines() if t.strip()] > f.close() > print "".join(t) Hi Jared, Let me rewrite this without the list comprehension, while preserving behavior. ###################### inputFile = open('file.txt') lines = [] for line in inputFile.readlines(): if line.strip(): lines.append(line) inputFile.close() print "".join(lines) ###################### I am changing the names of the variables from the original code because I find it very difficult to distinguish 't' from 'f' sometimes, and because those names are very tied in my mind to something else entirely ("true" and "false"). Does the above code make more sense to you than the version using the list comprehension syntax, or is there something there that is still confusing? Good luck to you. From wprins at gmail.com Wed Apr 9 00:11:10 2014 From: wprins at gmail.com (Walter Prins) Date: Wed, 9 Apr 2014 00:11:10 +0200 Subject: [Tutor] question about strip() and list comprehension In-Reply-To: References: Message-ID: Hi, On 8 April 2014 22:38, Jared Nielsen wrote: > Hello, > Could someone explain why and how this list comprehension with strip() > works? > > f = open('file.txt') > t = [t for t in f.readlines() if t.strip()] > f.close() > print "".join(t) > > I had a very long file of strings filled with blank lines I wanted to > remove. I did some Googling and found the above code snippet, but no clear > explanation as to why it works. I'm particularly confused by how "if > t.strip()" is removing the blank lines. I also don't fully understand the > 'print "".join(t)'. The list comprehension loops through each item in f.readlines(), and outputs each item (adds it to the output list being constructed), if and only if the "if" filter condition is true. Now, to understand "if t.strip()" you need to understand that Python allows objects and items which are not explicitly bool types in contexts where a boolean is required, such as in if statements. In the case of strings, a blank/empty string is considered False, while a non-blank/empty string is considered True. As a consequence, if t.strip() is equivalent to writing if t.strip() != '', and so its presence effectively suppresses adding empty lines from the file into the output list t. An empty string is said to be "falsy" while a non-empty string is said to be "truthy". For more see Section 5.1 (Truth value testing), here: https://docs.python.org/2/library/stdtypes.html As for the question about the print statement, firstly read the following documentation page that describes the string str.join() method: https://docs.python.org/2/library/stdtypes.html#str.join >From this, you should be able to infer that what "".join(t) does is to effectively construct a new output string by concatenating all the items in t, using a blank string as the delimiter, e.g. the result is effectively to just concatenate all the strings directly with no additional delimiter. > The above didn't remove the leading white space on several lines, so I made > the following addition: > > f = open('file.txt') > t = [t for t in f.readlines() if t.strip()] > f.close() > s = [x.lstrip() for x in t] > print "".join(s) > > List comprehensions are still magic to me. How would I go about > incorporating lstrip() in the first list comprehension? The output expression for each item output in the list comprehension, that's the bit n front of the "for", is something you specify/control. Now, in your original code, you just output the line that was read from the file verbatim (e.g. "t"), but nothing forces this on you -- instead you can write any expression you like, including calling "lstrip" on t, as in your question, e.g simply. t = [t.lstrip() for t in f.readlines() if t.strip()] HTH, Walter From ben+python at benfinney.id.au Wed Apr 9 00:30:09 2014 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 09 Apr 2014 08:30:09 +1000 Subject: [Tutor] question about strip() and list comprehension References: Message-ID: <85mwfvzc7i.fsf@benfinney.id.au> Jared Nielsen writes: > I had a very long file of strings filled with blank lines I wanted to > remove. I did some Googling and found the above code snippet The code you found is one of several syntactic shortcuts in Python, which allow creating a sequence directly from an expression in your code. For explanations, look at the documentation for ?generator expression? and ?display? (the latter have syntax for list comprehension, set comprehension, and dict comprehension). > I'm particularly confused by how "if t.strip()" is removing the blank > lines. The ?str.strip? method returns a new string, constructed from the original by removing all leading and trailing whitespace . A string, like any object, can be used in a boolean context; that's why ?if some_expression? works for any expression . For an expression that returns a string, the value in a boolean context will be false if the string is empty, and true for any other string. So, in the list comprehension you found: the ?if t.strip()? is getting a new string, testing it in a boolean context which will be false when the string is empty, and that condition is what determines which values will end up in the sequence. > I also don't fully understand the 'print "".join(t)'. Each text string object has a ?join? method, which uses that string and the specified sequence to construct a new string, joining all the items together . > The above didn't remove the leading white space on several lines Right. The new ?stripped? string is not used except in the ?if? clause, to determine which values will end up in the new list. The original values themselves are unchanged by the process, and end up in the new list as they began. > List comprehensions are still magic to me. How would I go about > incorporating lstrip() in the first list comprehension? In general, if something is a mystery to you, look up the formal description of the method or syntax in the documentation and carefully follow what it's telling you. Have a careful read of the documentation for those concepts, experiment based on your reading, and see what questions you have after that. -- \ ?Smoking cures weight problems. Eventually.? ?Steven Wright | `\ | _o__) | Ben Finney From steve at pearwood.info Wed Apr 9 00:50:07 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 9 Apr 2014 08:50:07 +1000 Subject: [Tutor] question about strip() and list comprehension In-Reply-To: References: Message-ID: <20140408225006.GN16466@ando> On Tue, Apr 08, 2014 at 02:38:13PM -0600, Jared Nielsen wrote: > Hello, > Could someone explain why and how this list comprehension with strip() > works? > > f = open('file.txt') > t = [t for t in f.readlines() if t.strip()] > f.close() > print "".join(t) > > I had a very long file of strings filled with blank lines I wanted to > remove. I did some Googling and found the above code snippet, but no clear > explanation as to why it works. I'm particularly confused by how "if > t.strip()" is removing the blank lines. It isn't. Rather, what it is doing is *preserving* the non-blank lines. The call to strip() removes any leading and trailing whitespace, so if the line is blank of contains nothing but whitespace, it reduces down to the empty string: py> ' '.strip() '' Like other empty sequences and containers, the empty string is considered to be "like False", falsey: py> bool('') False So your list cmprehension (re-written to use a more meaningful name) which looks like this: [line for line in f.readlines() if line.strip() iterates over each line in the file, tests if there is anything left over after stripping the leading/trailing whitespace, and only accumulates the lines that are non-blank. It is equivalent to this for-loop: accumulator = [] for line in f.readlines(): if line.strip(): # like "if bool(line.strip())" accumulator.append(line) > I also don't fully understand the 'print "".join(t)'. I presume you understand what print does :-) so it's only the "".join(t) that has you confused. This is where the interactive interpreter is brilliant, you can try things out for yourself and see what they do. Do you know how to start the interactive interpreter? (If not, ask and we'll tell you.) py> t = ['Is', 'this', 'the', 'right', 'place', 'for', 'an', 'argument?'] py> ''.join(t) 'Isthistherightplaceforanargument?' py> ' '.join(t) 'Is this the right place for an argument?' py> '--+--'.join(t) 'Is--+--this--+--the--+--right--+--place--+--for--+--an--+--argument?' In your case, you have a series of lines, so each line will end with a newline: py> t = ['line 1\n', 'line 2\n', 'line 3\n'] py> ''.join(t) 'line 1\nline 2\nline 3\n' py> print ''.join(t) line 1 line 2 line 3 > The above didn't remove the leading white space on several lines, so I made > the following addition: > > f = open('file.txt') > t = [t for t in f.readlines() if t.strip()] > f.close() > s = [x.lstrip() for x in t] > print "".join(s) You can combine those two list comps into a single one: f = open('file.txt') lines = [line.lstrip() for line in f.readlines() if line.strip()] f.close() -- Steven From akleider at sonic.net Wed Apr 9 02:07:27 2014 From: akleider at sonic.net (Alex Kleider) Date: Tue, 08 Apr 2014 17:07:27 -0700 Subject: [Tutor] dictionary keys In-Reply-To: References: <20140408143959.GJ16466@ando> <372c828ff260cf2ef99a90bbf94d326f@sonic.net> Message-ID: On 2014-04-08 14:34, Peter Otten wrote: > That's a change in Python 3 where dict.keys() no longer creates a list, > but > instead creates a view on the underlying dict data thus saving time and > space. In the rare case where you actually need a list you can > explicitly > create one with > > ips = list(ipDic) > > That's because the above is a session using Python 2. Compare: > > $ python3 > Python 3.3.2+ (default, Feb 28 2014, 00:52:16) > [GCC 4.8.1] on linux > Type "help", "copyright", "credits" or "license" for more information. >>>> dict(a=1, b=2).keys() > dict_keys(['b', 'a']) > > $ python2 > Python 2.7.5+ (default, Feb 27 2014, 19:37:08) > [GCC 4.8.1] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> dict(a=1, b=2).keys() > ['a', 'b'] > > PS: You can get a view in Python 2, too, with dict.viewkeys() > Thanks, Peter, for this clarification. I want to present the list sorted so probably this is the rare case of which you spoke where I would need to use l = list(myDict) rather than the view. From dyoo at hashcollision.org Wed Apr 9 03:05:34 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 8 Apr 2014 18:05:34 -0700 Subject: [Tutor] question about strip() and list comprehension In-Reply-To: References: Message-ID: > > > if line.strip() > > Is that stripping the line of white space at the same time that it is > testing it? > > Two features about Python: 1. Strings are immutable, so the above is computing what a whitespace-stripped line would look like. So that means that 'line.strip()' is doing just a computation: it's not mutating the original line, but computing a new string that has its leading and trailing whitespace stripped away. 2. Empty strings are treated as false values. I'm not happy with how loose Python treats truth, and would rather prefer: if line.strip() != "": ... so that the thing being tested is explicitly either True or False. I like my truth to be black and white, but I suppose I'll have to grimace and bear the fuzziness. :P Together, we see those two features allow us to look at the test in the Python code: if line.strip(): ... and rephrase it in English as: "If the line consists of at least one non-whitespace character: ..." -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Wed Apr 9 08:55:20 2014 From: __peter__ at web.de (Peter Otten) Date: Wed, 09 Apr 2014 08:55:20 +0200 Subject: [Tutor] dictionary keys References: <20140408143959.GJ16466@ando> <372c828ff260cf2ef99a90bbf94d326f@sonic.net> Message-ID: Alex Kleider wrote: > On 2014-04-08 14:34, Peter Otten wrote: > >> That's a change in Python 3 where dict.keys() no longer creates a list, >> but >> instead creates a view on the underlying dict data thus saving time and >> space. In the rare case where you actually need a list you can >> explicitly >> create one with >> >> ips = list(ipDic) > Thanks, Peter, for this clarification. I want to present the list > sorted so probably this is the rare case of which you spoke where I > would need to use l = list(myDict) rather than the view. You can create and sort the list in a single step: l = sorted(myDict) From nielsen.jared at gmail.com Wed Apr 9 00:09:15 2014 From: nielsen.jared at gmail.com (Jared Nielsen) Date: Tue, 8 Apr 2014 16:09:15 -0600 Subject: [Tutor] question about strip() and list comprehension In-Reply-To: References: Message-ID: Thank Danny, That's much more clear. But I still don't understand what's happening with: if line.strip() Is that stripping the line of white space at the same time that it is testing it? On Tue, Apr 8, 2014 at 3:44 PM, Danny Yoo wrote: > > Could someone explain why and how this list comprehension with strip() > > works? > > > > f = open('file.txt') > > t = [t for t in f.readlines() if t.strip()] > > f.close() > > print "".join(t) > > > Hi Jared, > > > Let me rewrite this without the list comprehension, while preserving > behavior. > > ###################### > inputFile = open('file.txt') > lines = [] > for line in inputFile.readlines(): > if line.strip(): > lines.append(line) > inputFile.close() > print "".join(lines) > ###################### > > I am changing the names of the variables from the original code > because I find it very difficult to distinguish 't' from 'f' > sometimes, and because those names are very tied in my mind to > something else entirely ("true" and "false"). > > > Does the above code make more sense to you than the version using the > list comprehension syntax, or is there something there that is still > confusing? > > > Good luck to you. > -- http://jarednielsen.com http://thehelloworldprogram.com http://dototot.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From nielsen.jared at gmail.com Wed Apr 9 04:06:50 2014 From: nielsen.jared at gmail.com (Jared Nielsen) Date: Tue, 8 Apr 2014 20:06:50 -0600 Subject: [Tutor] question about strip() and list comprehension In-Reply-To: References: Message-ID: Thanks Danny! That was an awesome explanation. On Tue, Apr 8, 2014 at 7:05 PM, Danny Yoo wrote: > >> if line.strip() >> >> Is that stripping the line of white space at the same time that it is >> testing it? >> >> > > Two features about Python: > > 1. Strings are immutable, so the above is computing what a > whitespace-stripped line would look like. So that means that > 'line.strip()' is doing just a computation: it's not mutating the original > line, but computing a new string that has its leading and trailing > whitespace stripped away. > > > 2. Empty strings are treated as false values. I'm not happy with how > loose Python treats truth, and would rather prefer: > > if line.strip() != "": ... > > so that the thing being tested is explicitly either True or False. I like > my truth to be black and white, but I suppose I'll have to grimace and bear > the fuzziness. :P > > > Together, we see those two features allow us to look at the test in the > Python code: > > if line.strip(): ... > > and rephrase it in English as: > > "If the line consists of at least one non-whitespace character: ..." > -- http://jarednielsen.com http://thehelloworldprogram.com http://dototot.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From niihung at gmail.com Wed Apr 9 07:58:27 2014 From: niihung at gmail.com (Ni hung) Date: Tue, 8 Apr 2014 22:58:27 -0700 Subject: [Tutor] When to use classes Message-ID: Hi I am learning programming using python. I think of solving a problem using functions and for this reason all/most of my code consists of functions and no classes. I have some understanding of classes/Object Oriented Programming. I can write simple classes but I do not understand when to use classes. Any suggestions, pointers to documents/blogs/books are welcome! Many Thanks Ni -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Wed Apr 9 10:07:35 2014 From: __peter__ at web.de (Peter Otten) Date: Wed, 09 Apr 2014 10:07:35 +0200 Subject: [Tutor] question about strip() and list comprehension References: <20140408225006.GN16466@ando> Message-ID: Steven D'Aprano wrote: > On Tue, Apr 08, 2014 at 02:38:13PM -0600, Jared Nielsen wrote: >> Hello, >> Could someone explain why and how this list comprehension with strip() >> works? >> >> f = open('file.txt') >> t = [t for t in f.readlines() if t.strip()] >> f.close() >> print "".join(t) >> >> I had a very long file of strings filled with blank lines I wanted to >> remove. I did some Googling and found the above code snippet, but no >> clear explanation as to why it works. I'm particularly confused by how >> "if t.strip()" is removing the blank lines. > > It isn't. Rather, what it is doing is *preserving* the non-blank lines. > > The call to strip() removes any leading and trailing whitespace, so if > the line is blank of contains nothing but whitespace, it reduces down to > the empty string: > > py> ' '.strip() > '' > > Like other empty sequences and containers, the empty string is > considered to be "like False", falsey: > > py> bool('') > False > > > So your list cmprehension (re-written to use a more meaningful name) > which looks like this: > > [line for line in f.readlines() if line.strip() > > iterates over each line in the file, tests if there is anything left > over after stripping the leading/trailing whitespace, and only > accumulates the lines that are non-blank. It is equivalent to this > for-loop: > > accumulator = [] > for line in f.readlines(): > if line.strip(): # like "if bool(line.strip())" > accumulator.append(line) > > >> I also don't fully understand the 'print "".join(t)'. > > I presume you understand what print does :-) so it's only the "".join(t) > that has you confused. This is where the interactive interpreter is > brilliant, you can try things out for yourself and see what they do. Do > you know how to start the interactive interpreter? > > (If not, ask and we'll tell you.) > > py> t = ['Is', 'this', 'the', 'right', 'place', 'for', 'an', 'argument?'] > py> ''.join(t) > 'Isthistherightplaceforanargument?' > py> ' '.join(t) > 'Is this the right place for an argument?' > py> '--+--'.join(t) > 'Is--+--this--+--the--+--right--+--place--+--for--+--an--+--argument?' > > > In your case, you have a series of lines, so each line will end with a > newline: > > py> t = ['line 1\n', 'line 2\n', 'line 3\n'] > py> ''.join(t) > 'line 1\nline 2\nline 3\n' > py> print ''.join(t) > line 1 > line 2 > line 3 > > >> The above didn't remove the leading white space on several lines, so I >> made the following addition: >> >> f = open('file.txt') >> t = [t for t in f.readlines() if t.strip()] >> f.close() >> s = [x.lstrip() for x in t] >> print "".join(s) > > > You can combine those two list comps into a single one: > > f = open('file.txt') > lines = [line.lstrip() for line in f.readlines() if line.strip()] > f.close() For those readers who are starting out with python but are not absolute beginners let me just mention that in [line.lstrip() for line in f.readlines() if line.strip()] the readlines() call is superfluous -- it reads the lines of the file into a list thus putting them all into memory when you need only one at a time, effectively more than doubling the amount of memory needed. So get into the habit of iterating over a file directly for line in f: .... In the case where you want to filter and modify lines you can omit the list with all modified lines, too: import sys for line in f: line = line.lstrip() sys.stdout.write(line) Here sys.stdout.write() writes to stdout like print, but expects a string and doesn't add a newline. Note that I didn't add if line: sys.stdout.write(line) -- it doesn't matter much if you do or don't write an empty string. Now what about the listcomp? An experienced programmer would use a generator expression which is similar to the listcomp, but just deals with the current line. Your script will never run out of memory no matter whether the file has one billion or one billion billion lines (diskspace and runtime are another matter). A genexp looks similar to a listcomp lines = (line.lstrip() for line in f) but has the limitation here that you can only iterate over the lines while the file is still open. Together with a sophisticated way to close the file with open("file.txt") as f: ... # do something with the file print "f is now closed without an explicit close() call" print "even if an error occured while processing the file" the whole script that removes empty lines and leading whitespace can be written as import sys with open("file.txt") as f: sys.stdout.writelines(line.lstrip() for line in f) What was I saying? Ah: FORGET ABOUT file.readlines(). You hardly ever need it. From akleider at sonic.net Wed Apr 9 11:04:46 2014 From: akleider at sonic.net (Alex Kleider) Date: Wed, 09 Apr 2014 02:04:46 -0700 Subject: [Tutor] dictionary keys In-Reply-To: References: <20140408143959.GJ16466@ando> <372c828ff260cf2ef99a90bbf94d326f@sonic.net> Message-ID: <7f72688d8f4f2eacfba7c2c2d553957d@sonic.net> On 2014-04-08 23:55, Peter Otten wrote: > You can create and sort the list in a single step: > > l = sorted(myDict) > Thank you again; this is a new idiom for me. From alan.gauld at btinternet.com Wed Apr 9 12:22:05 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 09 Apr 2014 11:22:05 +0100 Subject: [Tutor] When to use classes In-Reply-To: References: Message-ID: On 09/04/14 06:58, Ni hung wrote: > functions and no classes. I have some understanding of classes/Object > Oriented Programming. I can write simple classes but I do not understand > when to use classes. If you are just learning it may be that the programs you have written are too small to make classes useful. Classes tend to come into their own on larger programs. As to when to use them: if you see a lot of functions all taking the same set of data inputs then there's a good chance you should turn that into a class that holds the data and has the functions as methods. Alternatively if you need to manipulate several copies of a thing then defining thing as a class and creating multiple instances makes sense (Think of strings in Python - how much harder it would be to work with strings if they weren't classes. Up until Python v2 that's exactly what we had to do.) As projects get bigger still, it starts to become more natural to think of the program in terms of classes because they become more of an expression of your real-world problem domain than an expression of programming data sets. Think about classes like employee, order, bankaccount, building, etc (Although those with a formal math background often find this hardest to do since they have had it beaten into their skulls that things consist of functions and data!) But don't sweat about it, you can go a long way without classes. And when they are needed it usually becomes clear based on what I said in paragraph 2 above. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From davea at davea.name Wed Apr 9 13:01:02 2014 From: davea at davea.name (Dave Angel) Date: Wed, 9 Apr 2014 07:01:02 -0400 (EDT) Subject: [Tutor] When to use classes References: Message-ID: Ni hung Wrote in message: (Please post in text format, not html. It doesn't matter for your particular message, but several things can go wrong, where some or most of us do not see what you meant to post) > I am learning programming using python. I think of solving a > problem using functions and for this reason all/most of my > code consists of functions and no classes. ?I have some understanding of classes/Object Oriented Programming. I can write simple classes but I do not understand when to use classes Welcome to the forum., and to Python. You may not realize it, but you're already using classes in even the simplest Python program. int is a class, list and dict are classes. Even a function is an instance of a class. So what you're really asking is when you should create your own new classes. The short answer is whenever the standard ones (there are many hundreds of them, maybe thousands) is inadequate for your needs. When writing simple programs, compositions of existing classes can take you a long way. When you need something a bit trickier, Python has ways of generating classes that you might not realize, such as namedtuple. At its simplest, a class is a way to bundle up data items and method items so that each instance has all of those attributes with a simple call to the constructor. The simplest class is probably NoneType, which has about 20 attributes, I think all dunder ones. (Use dir () to look at objects and find their attributes, and help () to learn more about most attributes) But sooner or later, you find yourself with a dict of keys and values, where the values are lists of tuples, and you'll throw up your hands and write a nice class to make it easier to keep track of things. -- DaveA From ap235711 at gmail.com Wed Apr 9 11:23:09 2014 From: ap235711 at gmail.com (Alexis Prime) Date: Wed, 9 Apr 2014 17:23:09 +0800 Subject: [Tutor] Delete unwanted rows Message-ID: Hello, My question is whether I should write a loop or a function to delete rows. I'm using pandas. But you may be able to help me as my question is about the reasoning behind programming. I have a pandas dataframe that looks like this, covering all countries in the world, for over 200 rows and many columns: Canada 20 China 112 Germany 10 Japan 12 Martinique 140 Mexico 180 Saint Kitts 90 Saint Martins 133 Saint Helena 166 USA 18 # So I write a list of small countries that I wish to exclude from my analysis. What I want to do is to delete the rows from my dataframe. toexclude = ['Martinique', 'Saint Kitts', 'Saint Martins', 'Saint Helena'] After this, should I write a loop to loop through the dataframe, find the countries that I want to delete, and then delete the rows? Or should I write a function, which deletes those rows, and then returns me a new and trimmed dataframe? Thank you for helping me figure this out. Alexis -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Wed Apr 9 15:38:36 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 09 Apr 2014 14:38:36 +0100 Subject: [Tutor] Delete unwanted rows In-Reply-To: References: Message-ID: On 09/04/2014 10:23, Alexis Prime wrote: > Hello, > > My question is whether I should write a loop or a function to delete rows. > > I'm using pandas. But you may be able to help me as my question is about > the reasoning behind programming. > > I have a pandas dataframe that looks like this, covering all countries > in the world, for over 200 rows and many columns: > > Canada 20 > China 112 > Germany 10 > Japan 12 > Martinique 140 > Mexico 180 > Saint Kitts 90 > Saint Martins 133 > Saint Helena 166 > USA 18 > > # So I write a list of small countries that I wish to exclude from my > analysis. What I want to do is to delete the rows from my dataframe. > > toexclude = ['Martinique', 'Saint Kitts', 'Saint Martins', 'Saint > Helena'] > > After this, should I write a loop to loop through the dataframe, find > the countries that I want to delete, and then delete the rows? > > Or should I write a function, which deletes those rows, and then returns > me a new and trimmed dataframe? > > Thank you for helping me figure this out. > > Alexis > If you are going to do this repeatedly you'd be better off writing a function. I don't know enough about pandas to say whether the rows should be deleted within a loop in the function or a new data frame is returned. But before you write any code, have to checked to see if there is a function or method in pandas that does this for you? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From __peter__ at web.de Wed Apr 9 16:14:01 2014 From: __peter__ at web.de (Peter Otten) Date: Wed, 09 Apr 2014 16:14:01 +0200 Subject: [Tutor] Delete unwanted rows References: Message-ID: Alexis Prime wrote: > Hello, > > My question is whether I should write a loop or a function to delete rows. > > I'm using pandas. But you may be able to help me as my question is about > the reasoning behind programming. > > I have a pandas dataframe that looks like this, covering all countries in > the world, for over 200 rows and many columns: > > Canada 20 > China 112 > Germany 10 > Japan 12 > Martinique 140 > Mexico 180 > Saint Kitts 90 > Saint Martins 133 > Saint Helena 166 > USA 18 > > # So I write a list of small countries that I wish to exclude from my > analysis. What I want to do is to delete the rows from my dataframe. > > toexclude = ['Martinique', 'Saint Kitts', 'Saint Martins', 'Saint > Helena'] > > After this, should I write a loop to loop through the dataframe, find the > countries that I want to delete, and then delete the rows? > > Or should I write a function, which deletes those rows, and then returns > me a new and trimmed dataframe? > > Thank you for helping me figure this out. The dataset is so small that I would not spend much time on efficiency or philosophical considerations, and use the solution that is easiest to code. In generic Python this means typically iterating over the data and building a new list (I'm sorry, I don't know how this translates into pandas): data = [ ("Canada", 20), ("China", 112), ... ] excluded = {"Martinique", "Saint Kitts", ...} cleaned_data = [row for row in data if row[0] not in excluded] However, pandas is a specialist topic and if you expect to work more with it you may want to learn the proper idiomatic way to do it. You should then ask again on a mailing list that is frequented by the pandas experts -- python-tutor is mostly about the basics of generic Python. Finally, as someone who knows Python well, just a little numpy, and nothing about pandas I decided to bang my head against the wall a few times until I came up with the following hack: import pandas data = """\ Canada 20 China 112 Germany 10 Japan 12 Martinique 140 Mexico 180 Saint Kitts 90 Saint Martins 133 Saint Helena 166 USA 18 """ rows = [line.rsplit(None, 1) for line in data.splitlines()] names = [row[0] for row in rows] values = [int(row[1]) for row in rows] df = pandas.DataFrame(dict(name=names, value=values)) class Contain: def __init__(self, items): self.items = set(items) def __ne__(self, other): return other not in self.items def __eq__(self, other): return other in self.items exclude = Contain([ 'Martinique', 'Saint Kitts', 'Saint Martins', 'Saint Helena']) cleaned_df = df[df.name != exclude] print("Before:") print(df) print() print("After:") print(cleaned_df) [The trick here is to convert the "in" into the "==" operator because the latter can return arbitrary objects while the former is limited to bool] From ugajin at talktalk.net Wed Apr 9 16:17:49 2014 From: ugajin at talktalk.net (ugajin at talktalk.net) Date: Wed, 09 Apr 2014 10:17:49 -0400 Subject: [Tutor] masking library files Message-ID: <8D122159B84276F-11A4-1CC77@webmail-vfrr14.sis.aol.com> Is it common for files saved to a working directory to 'mask' library files located in the Python framework? -A -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Wed Apr 9 16:25:25 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 09 Apr 2014 15:25:25 +0100 Subject: [Tutor] Delete unwanted rows In-Reply-To: References: Message-ID: On 09/04/2014 15:14, Peter Otten wrote: > Alexis Prime wrote: > > However, pandas is a specialist topic and if you expect to work more with it > you may want to learn the proper idiomatic way to do it. You should then > ask again on a mailing list that is frequented by the pandas experts -- > python-tutor is mostly about the basics of generic Python. > gmane.comp.python.pydata or https://groups.google.com/forum/#!forum/pydata but I'd definitely recommend the former. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From breamoreboy at yahoo.co.uk Wed Apr 9 16:34:21 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 09 Apr 2014 15:34:21 +0100 Subject: [Tutor] masking library files In-Reply-To: <8D122159B84276F-11A4-1CC77@webmail-vfrr14.sis.aol.com> References: <8D122159B84276F-11A4-1CC77@webmail-vfrr14.sis.aol.com> Message-ID: On 09/04/2014 15:17, ugajin at talktalk.net wrote: > Is it common for files saved to a working directory to 'mask' library > files located in the Python framework? > > -A > Yes. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From alan.gauld at btinternet.com Wed Apr 9 17:56:29 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 09 Apr 2014 16:56:29 +0100 Subject: [Tutor] masking library files In-Reply-To: <8D122159B84276F-11A4-1CC77@webmail-vfrr14.sis.aol.com> References: <8D122159B84276F-11A4-1CC77@webmail-vfrr14.sis.aol.com> Message-ID: On 09/04/14 15:17, ugajin at talktalk.net wrote: > Is it common for files saved to a working directory to 'mask' library > files located in the Python framework? Python looks in the local directory first so if you name a module with the name of one of the standard modules Python will, quite reasonably, assume you want your module to be used rather than the standard one. The best solution is not to name your modules the same as the standard library ones. When it comes to non-standard libraries then it becomes a matter of looking at the sys.path value and the order of search defined there. Which may, in turn, depend on how they were installed. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From cs at zip.com.au Wed Apr 9 13:25:14 2014 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 9 Apr 2014 21:25:14 +1000 Subject: [Tutor] When to use classes In-Reply-To: References: Message-ID: <20140409112514.GA71433@cskk.homeip.net> On 08Apr2014 22:58, Ni hung wrote: > I am learning programming using python. I think of solving a problem using > functions and for this reason all/most of my code consists of functions and > no classes. I have some understanding of classes/Object Oriented > Programming. I can write simple classes but I do not understand when to use > classes. Loosely speaking, you can usefully make a class when there is a particular type of object on which you are make an assortedment of operations. For example, if you have several functions looking like this: def modify_thing1(obj1, ...): def add_something(obj1, ...): and so on, where obj1 is always the same kind of thing, representing some well define idea you have when writing the code. In that circumstance you might make a class looking like this: class ObjName(object): def __init__(self): # "self" here is an "obj1" as used above # set whatever attributes an "obj1" should have to start with # you can pass in some of those values as parameters to the __init__ function def modify(self, ...): # this is the same as "modify_thing1(obj1, ...)" earlier # using "self" as "obj1" def add(self, ...): # this is the same as "add_something(obj1, ...)" earlier Then your code looks like: obj1 = ObjName() ... obj1.modify(5) obj1.add(6) or what have you. This has several advantages: - all the code that affects this kind of object is in one place - you can remove all the specifics of how things are done from the main code and keep them inside the class methods. This makes the main code more readable (if you pick good method names) and shorter (also more readable, usually). - later, if need be, you can change the inner workings of how ObjNames work without changing the main code, or at least not much - sometimes not at all Anyway, that's an ok rule of thumb for when to make a class: when you have a bunch of operations to do to one type of thing. Cheers, -- Cameron Simpson From nielsen.jared at gmail.com Wed Apr 9 18:49:27 2014 From: nielsen.jared at gmail.com (Jared Nielsen) Date: Wed, 9 Apr 2014 10:49:27 -0600 Subject: [Tutor] difference between expressions and statements Message-ID: Hi Pythons, Could someone explain the difference between expressions and statements? I know that expressions are statements that produce a value. I'm unclear on functions and especially strings. Are any of the following expressions? print(42) print("spam") spam = 42 print(spam) Is the first example producing a value or simply displaying an integer? Does a string count as a value? Is a variable assignment considered a value? If I print a variable is that considered production of a value? Thanks! From dyoo at hashcollision.org Wed Apr 9 21:04:42 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 9 Apr 2014 12:04:42 -0700 Subject: [Tutor] difference between expressions and statements In-Reply-To: References: Message-ID: > Could someone explain the difference between expressions and statements? > > I know that expressions are statements that produce a value. Yes, that's pretty much it. If you can point your finger at the thing and say that it produces a value, it's an expression. > Are any of the following expressions? > > print(42) > print("spam") > spam = 42 > print(spam) See: https://docs.python.org/2/reference/expressions.html Technically, the only things that count as expressions have to fit the shape of something described in that documentation link. Your question about assignment being an expression or not is something you can find out by seeing where "Assignment statements" show in: https://docs.python.org/2/reference/index.html Good luck! From cbc at unc.edu Wed Apr 9 22:01:04 2014 From: cbc at unc.edu (Chris Calloway) Date: Wed, 09 Apr 2014 16:01:04 -0400 Subject: [Tutor] 2014 PyCamps Message-ID: <5345A700.3010007@unc.edu> Need some in-person and structured Python tutoring? PyCamp is an ultra-low-cost, five-day, intensive Python boot camp program by a user group for user groups. PyCamp has taught Python fundamentals to thousands of beginners for nine years while sponsoring Python regional conferences, symposia, sprints, scholarships, and user groups. You can get up to speed on the most modern programming language at PyCamp. This year choose from two PyCamps: Wisconsin PyCamp 2014, June 13-17, University of Wisconsin-Oshkosh http://tripython.org/wiscpy14 Wisconsin PyCamp 2014 is a training program of Plone Symposium Midwest. http://midwest.plonesymp.org Wisconsin PyCamp 2014 is all-day catered (breakfast, lunch, snacks). or PyOhio PyCamp 2014, July 21-25, The Ohio State University http://tripython.org/pyohio14 PyOhio PyCamp 2014 is a pre-conference training program of PyOhio http://pyohio.org Scholarships for women and minorities are available for PyOhio PyCamp. -- Sincerely, Chris Calloway http://nccoos.org/Members/cbc office: 3313 Venable Hall phone: (919) 599-3530 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 From ag355 at hotmail.co.uk Wed Apr 9 21:59:38 2014 From: ag355 at hotmail.co.uk (Adam Grierson) Date: Wed, 9 Apr 2014 19:59:38 +0000 Subject: [Tutor] =?utf-8?q?Python_Help?= Message-ID: Hi I'm using 3D climate data (ending in ?.nc?). The cube contains time, longitude and latitude. I would like to look at the average output over the last 20 years. The time field spans back hundreds of years and I only know how to collapse the entire field into a mean value. How can I tell python to collapse just the last 20 years into a mean value? Thank you Adam -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Apr 9 23:01:39 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 09 Apr 2014 22:01:39 +0100 Subject: [Tutor] difference between expressions and statements In-Reply-To: References: Message-ID: On 09/04/14 17:49, Jared Nielsen wrote: > Hi Pythons, > Could someone explain the difference between expressions and statements? > I know that expressions are statements that produce a value. Yep, that's it. > I'm unclear on functions and especially strings. Unclear in what way? Both functions and strings are expressions, in that they produce, or are, values. > Are any of the following expressions? > print(42) > print("spam") > spam = 42 > print(spam) Yes, 3 are expressions, and all 4 are statements containing expressions. > Is the first example producing a value or simply displaying an integer? It does actually produce a value but its not the integer that is displayed. The default value for any function (including print() ) is None... You can prove that by trying: >>> print( print(42) ) 42 None The 42 is the output displayed by the innermost print() The None is the value returned by the inner print function. The Python interpreter normally suppresses the None from a print function but because I explicitly told it to print the return from print it did it in this case. > Does a string count as a value? Yes, certainly. > Is a variable assignment considered a value? No, its a statement but not an expression. (In Python at least, in some other languages the rules are different) > If I print a variable is that considered production of a value? Yes, as above. But the value produced is the None returned by the print function not the value that print displays. HTH And did I just do your homework? hmmm... I'll give you the benefit of the doubt. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Wed Apr 9 23:06:39 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 09 Apr 2014 22:06:39 +0100 Subject: [Tutor] Python Help In-Reply-To: References: Message-ID: On 09/04/14 20:59, Adam Grierson wrote: > I'm using 3D climate data (ending in ?.nc?). The cube contains time, > longitude and latitude. I would like to look at the average output over > the last 20 years. The time field spans back hundreds of years and I > only know how to collapse the entire field into a mean value. How can I > tell python to collapse just the last 20 years into a mean value? This group is for teaching the fundamentals of the Python language and its standard library. You probably can solve this using standard Python but I suspect there will be more specific libraries around that you can install that will help with this problem. Those libraries will likely be a better place to ask. That having been said, if you want a pure Puython solution we can try to help, but we need a lot more detail about what the data looks like and what exactly you expect out. Your description is a bit vague and while I might be able to find out what you mean by Googling a bit, I'm not that keen to spend my time that way. The more you help us the more we can help you... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From cbc at unc.edu Wed Apr 9 23:48:50 2014 From: cbc at unc.edu (Chris Calloway) Date: Wed, 09 Apr 2014 17:48:50 -0400 Subject: [Tutor] Python Help In-Reply-To: References: Message-ID: <5345C042.4000101@unc.edu> On 4/9/2014 3:59 PM, Adam Grierson wrote: > I'm using 3D climate data (ending in ?.nc?). The cube contains time, > longitude and latitude. I would like to look at the average output over > the last 20 years. The time field spans back hundreds of years and I > only know how to collapse the entire field into a mean value. How can I > tell python to collapse just the last 20 years into a mean value? An ".nc" file extension is NetCDF. NetCDF can be served by a DAP server. A DAP server can be sent a DAP request by a Python DAP client to drill for results confined to particular variables, a geographic bounding box, and a stop and start timestamp. See: http://pydap.org Or you can simply subset the desired subarray of NetCDF data using SciPy: http://docs.scipy.org/doc/scipy/reference/generated/scipy.io.netcdf.netcdf_file.html Here's a tutorial: snowball.millersville.edu/~adecaria/ESCI386P/esci386-lesson14-Reading-NetCDF-files.pdf -- Sincerely, Chris Calloway http://nccoos.org/Members/cbc office: 3313 Venable Hall phone: (919) 599-3530 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 From ben+python at benfinney.id.au Thu Apr 10 00:41:04 2014 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 10 Apr 2014 08:41:04 +1000 Subject: [Tutor] difference between expressions and statements References: Message-ID: <85ioqiyvlr.fsf@benfinney.id.au> Jared Nielsen writes: > Could someone explain the difference between expressions and > statements? For general programming terminology, the Wikipedia articles tend to be good. Roughly: * An expression evaluates to some single value. * A statement does some action. > I know that expressions are statements that produce a value. Expressions are not statements. A statement *may* be an expression. Statements in Python can consist of an expression, or can consist of other things. > I'm unclear on functions and especially strings. You're looking for a strict dichotomy which doesn't exist, and I think that's confusing you. > Are any of the following expressions? They can all be statements. > print(42) > print("spam") These are function calls. A function call evaluates to a value, so is always an expression. > spam = 42 Assignment is only a statement in Python. The statement is an instruction to perform the assignment. The left side is an expression evaluating to a reference; the right side is an expression evaluating to a value. > Is a variable assignment considered a value? In Python, assignment (try not to think in terms of ?variable?; assignment is the act of binding a reference to a value) is always a statement. In other languages, assignment can return a value and is therefore an expression. That is not the case in Python. > Is the first example producing a value or simply displaying an > integer? > Does a string count as a value? Yes to all these. Learn more about statements ? especially the fact that statements consist sometimes of expressions alone, sometimes of other things including expressions ? at the Python Language Reference . -- \ ?If nature has made any one thing less susceptible than all | `\ others of exclusive property, it is the action of the thinking | _o__) power called an idea? ?Thomas Jefferson | Ben Finney From davea at davea.name Thu Apr 10 02:59:16 2014 From: davea at davea.name (Dave Angel) Date: Wed, 9 Apr 2014 20:59:16 -0400 (EDT) Subject: [Tutor] difference between expressions and statements References: Message-ID: Jared Nielsen Wrote in message: > Hi Pythons, > Could someone explain the difference between expressions and statements? > I know that expressions are statements that produce a value. > I'm unclear on functions and especially strings. > Are any of the following expressions? > > print(42) > print("spam") > spam = 42 > print(spam) > > Is the first example producing a value or simply displaying an integer? > Does a string count as a value? > Is a variable assignment considered a value? > If I print a variable is that considered production of a value? > Can't answer till you specify Python version. The other answers all seem to assume version 3.x. In version 2, these are all statements, none are expressions. -- DaveA From davea at davea.name Thu Apr 10 03:02:05 2014 From: davea at davea.name (Dave Angel) Date: Wed, 9 Apr 2014 21:02:05 -0400 (EDT) Subject: [Tutor] masking library files References: <8D122159B84276F-11A4-1CC77@webmail-vfrr14.sis.aol.com> Message-ID: ugajin at talktalk.net Wrote in message: > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > A message left in invisible ink. Please post in text form, not html, as html offers too many ways to mess up the message. -- DaveA From ugajin at talktalk.net Thu Apr 10 03:20:16 2014 From: ugajin at talktalk.net (ugajin at talktalk.net) Date: Wed, 09 Apr 2014 21:20:16 -0400 Subject: [Tutor] masking library files In-Reply-To: References: Message-ID: <8D12272264D1C02-11A4-1D51C@webmail-vfrr14.sis.aol.com> Please write in plain English if you want to be understood. -----Original Message----- From: Dave Angel To: tutor at python.org Sent: Thu, 10 Apr 2014 2:00 Subject: Re: [Tutor] masking library files ugajin at talktalk.net Wrote in message: > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > A message left in invisible ink. Please post in text form, not html, as html offers too many ways to mess up the message. -- DaveA _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From connerjoy1994 at hotmail.com Thu Apr 10 04:04:10 2014 From: connerjoy1994 at hotmail.com (Conner Crowe) Date: Wed, 9 Apr 2014 19:04:10 -0700 Subject: [Tutor] Question regarding Python loops Message-ID: To whom this may concern: I am struggling on two questions: Problem 2. Suppose you are given a function leave(minute) which returns the number of students that leave an exam during its mth minute. Write a function already_left(t) that returns the number of students that left at or before minute t of the exam. Example code: # To simplify testing, you may wish to define a stand-in function for leave that # behaves predictably # Here's one possible stand-in: def leave(minute): if minute > 0: return minute return 0 def already_left(t): # Your code goes here Example usage: print already_left(1) print already_left(4) Example output: 1 10 So far for this i have: def leave(minute): if minute > 0: return minute elif minute=0: return 0 def already_left(i): for i in range(0,t): leave(i) print already_left(8) print already_left(4) Problem 3. Using your already_left function, write a function percent_left(t, l) that returns the percent of the class that has left at or before minute t, assuming the exam is l minutes long, and the entire class has left at or before minute l. Return your result as a float between 0 and 1. Example code: def percent_left(t, l): # Your code goes here Example usage: print percent_left(0, 4) print percent_left(1, 4) print percent_left(4, 4) print percent_left(30, 50) Example output: 0.0 0.1 1.0 0.364705882353 -------------- next part -------------- An HTML attachment was scrubbed... URL: From dharmit.dev at gmail.com Thu Apr 10 09:39:18 2014 From: dharmit.dev at gmail.com (Dharmit Shah) Date: Thu, 10 Apr 2014 13:09:18 +0530 Subject: [Tutor] Desktop Notifications on Linux Message-ID: Hi all, I am trying to create a script that will go through the /var/log/secure file on a Linux system and provide desktop notifications for failed login attempts. Here is the code - http://pastebin.com/MXP8Yd91 And here's notification.py - http://pastebin.com/BhsSTP6H I am facing issue in the function "new_attempts_from_last". I am not able to raise a desktop notification from this function. It always fails with this traceback - http://pastebin.com/cgHMScv3 I see this error only when I try to raise a notification from the aforementioned function. If I run test examples under /usr/share/doc/notify-python/examples, it works just fine. Also, if I try to raise a notification from under if __name__ == "__main__":, it works without any issues. So I don't think there's any issue with OS's notification daemon. Running from python shell like below also works fine: In [1]: import notification In [2]: notification.notification("Hey") What am I missing or doing wrong here? If it matters, I am running Fedora 20, python 2.7 and Cinnamon desktop environment. For readability purposes, I have provided pastebin links. Let me know if this is not the correct way. Regards, Dharmit. From niihung at gmail.com Thu Apr 10 07:42:48 2014 From: niihung at gmail.com (Ni hung) Date: Wed, 9 Apr 2014 22:42:48 -0700 Subject: [Tutor] When to use classes In-Reply-To: <20140409112514.GA71433@cskk.homeip.net> References: <20140409112514.GA71433@cskk.homeip.net> Message-ID: Thank you Alan, Dave and Cameron (and folks managing this email group)! Your replies were very helpful. Regards ni On Wed, Apr 9, 2014 at 4:25 AM, Cameron Simpson wrote: > On 08Apr2014 22:58, Ni hung wrote: > > I am learning programming using python. I think of solving a problem > using > > functions and for this reason all/most of my code consists of functions > and > > no classes. I have some understanding of classes/Object Oriented > > Programming. I can write simple classes but I do not understand when to > use > > classes. > > Loosely speaking, you can usefully make a class when there is a > particular type of object on which you are make an assortedment of > operations. > > For example, if you have several functions looking like this: > > def modify_thing1(obj1, ...): > > def add_something(obj1, ...): > > and so on, where obj1 is always the same kind of thing, representing > some well define idea you have when writing the code. > > In that circumstance you might make a class looking like this: > > class ObjName(object): > > def __init__(self): > # "self" here is an "obj1" as used above > # set whatever attributes an "obj1" should have to start with > # you can pass in some of those values as parameters to the __init__ > function > > def modify(self, ...): > # this is the same as "modify_thing1(obj1, ...)" earlier > # using "self" as "obj1" > > def add(self, ...): > # this is the same as "add_something(obj1, ...)" earlier > > Then your code looks like: > > obj1 = ObjName() > ... > obj1.modify(5) > obj1.add(6) > > or what have you. > > This has several advantages: > > - all the code that affects this kind of object is in one place > > - you can remove all the specifics of how things are done from the main > code and keep them inside the class methods. > This makes the main code more readable (if you pick good method names) > and shorter (also more readable, usually). > > - later, if need be, you can change the inner workings of how > ObjNames work without changing the main code, or at least not > much - sometimes not at all > > Anyway, that's an ok rule of thumb for when to make a class: when > you have a bunch of operations to do to one type of thing. > > Cheers, > -- > Cameron Simpson > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Thu Apr 10 10:35:33 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 10 Apr 2014 09:35:33 +0100 Subject: [Tutor] masking library files In-Reply-To: <8D12272264D1C02-11A4-1D51C@webmail-vfrr14.sis.aol.com> References: <8D12272264D1C02-11A4-1D51C@webmail-vfrr14.sis.aol.com> Message-ID: On 10/04/2014 02:20, ugajin at talktalk.net wrote: > Please write in plain English if you want to be understood. > > > -----Original Message----- > From: Dave Angel > To: tutor at python.org > Sent: Thu, 10 Apr 2014 2:00 > Subject: Re: [Tutor] masking library files > > ugajin at talktalk.net Wrote in message: >> > > A message left in invisible ink. Please post in text form, not > html, as html offers too many ways to mess up the message. > > > -- > DaveA > Please don't top post on this list. And don't use language like that if you want answers, especially when replying to a highly respected member of this community such as Dave. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From __peter__ at web.de Thu Apr 10 10:37:54 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 10 Apr 2014 10:37:54 +0200 Subject: [Tutor] Question regarding Python loops References: Message-ID: Conner Crowe wrote: > To whom this may concern: > > I am struggling on two questions: > Problem 2. > Suppose you are given a function leave(minute) which returns the number of > students that leave an exam during its mth minute. Write a function > already_left(t) that returns the number of students that left at or before > minute t of the exam. > > Example code: > # To simplify testing, you may wish to define a stand-in function for > # leave that behaves predictably > # Here's one possible stand-in: > def leave(minute): > if minute > 0: > return minute > return 0 > > def already_left(t): > # Your code goes here > > Example usage: > print already_left(1) > print already_left(4) > > Example output: > 1 > 10 > > So far for this i have: > def leave(minute): > if minute > 0: > return minute > elif minute=0: > return 0 > def already_left(i): > for i in range(0,t): > leave(i) Hint: You are throwing away the result of the leave() call. But what you want is to add up the results of all calls to leave() in the loop. Once you have that total you need already_left() to return it so that > print already_left(8) > print already_left(4) will print it instead of None (the default when you do not explicitly return anything from a function). > Problem 3. How would you solve that with pen and paper? From __peter__ at web.de Thu Apr 10 11:00:06 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 10 Apr 2014 11:00:06 +0200 Subject: [Tutor] Desktop Notifications on Linux References: Message-ID: Dharmit Shah wrote: > I am trying to create a script that will go through the > /var/log/secure file on a Linux system and provide desktop > notifications for failed login attempts. > > Here is the code - http://pastebin.com/MXP8Yd91 > And here's notification.py - http://pastebin.com/BhsSTP6H > > I am facing issue in the function "new_attempts_from_last". I am not > able to raise a desktop notification from this function. It always > fails with this traceback - http://pastebin.com/cgHMScv3 > > I see this error only when I try to raise a notification from the > aforementioned function. If I run test examples under > /usr/share/doc/notify-python/examples, it works just fine. Also, if I > try to raise a notification from under if __name__ == "__main__":, it > works without any issues. So I don't think there's any issue with OS's > notification daemon. Running from python shell like below also works > fine: > > In [1]: import notification > > In [2]: notification.notification("Hey") > > What am I missing or doing wrong here? > > If it matters, I am running Fedora 20, python 2.7 and Cinnamon desktop > environment. > > For readability purposes, I have provided pastebin links. Let me know > if this is not the correct way. Maybe you are running the code as a user that has no "desktop"? Here's a strapped-down demo: $ cat notify_demo.py import pynotify import sys pynotify.init("Notification") n = pynotify.Notification("Notification", " ".join(sys.argv[1:])) n.show() $ python notify_demo.py hello world The notification appeared on my desktop. However, when trying again as root I got the error you are seeing: $ sudo su # python notify_demo.py hello again Traceback (most recent call last): File "notify_demo.py", line 6, in n.show() glib.GError: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.Notifications was not provided by any .service files From ugajin at talktalk.net Thu Apr 10 11:16:34 2014 From: ugajin at talktalk.net (ugajin at talktalk.net) Date: Thu, 10 Apr 2014 05:16:34 -0400 Subject: [Tutor] masking library files In-Reply-To: Message-ID: <8D122B4AFD880B0-CF0-3BFE@webmail-vfrr13.sis.aol.com> -----Original Message----- From: Mark Lawrence To: tutor at python.org Sent: Thu, 10 Apr 2014 9:36 Subject: Re: [Tutor] masking library files On 10/04/2014 02:20, ugajin at talktalk.net wrote: > Please write in plain English if you want to be understood. > > > -----Original Message----- > From: Dave Angel > To: tutor at python.org > Sent: Thu, 10 Apr 2014 2:00 > Subject: Re: [Tutor] masking library files > > ugajin at talktalk.net Wrote in message: >> > > A message left in invisible ink. Please post in text form, not > html, as html offers too many ways to mess up the message. > > > -- > DaveA > Please don't top post on this list. And don't use language like that if you want answers, especially when replying to a highly respected member of this community such as Dave. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor Like this? -------------- next part -------------- An HTML attachment was scrubbed... URL: From ugajin at talktalk.net Thu Apr 10 11:30:59 2014 From: ugajin at talktalk.net (ugajin at talktalk.net) Date: Thu, 10 Apr 2014 05:30:59 -0400 Subject: [Tutor] masking library files In-Reply-To: <8D122B4AFD880B0-CF0-3BFE@webmail-vfrr13.sis.aol.com> References: <8D122B4AFD880B0-CF0-3BFE@webmail-vfrr13.sis.aol.com> Message-ID: <8D122B6B3D02432-CF0-3C53@webmail-vfrr13.sis.aol.com> -----Original Message----- From: ugajin at talktalk.net To: tutor at python.org Sent: Thu, 10 Apr 2014 10:17 Subject: Re: [Tutor] masking library files -----Original Message----- From: Mark Lawrence To: tutor at python.org Sent: Thu, 10 Apr 2014 9:36 Subject: Re: [Tutor] masking library files On 10/04/2014 02:20, ugajin at talktalk.net wrote:? > Please write in plain English if you want to be understood.? >? >? > -----Original Message-----? > From: Dave Angel ? > To: tutor at python.org? > Sent: Thu, 10 Apr 2014 2:00? > Subject: Re: [Tutor] masking library files? >? > ugajin at talktalk.net Wrote in message:? ? >>? >? > A message left in invisible ink. Please post in text form, not? > html, as html offers too many ways to mess up the message.? >? >? > --? > DaveA? >? ? Please don't top post on this list. And don't use language like that if you want answers, especially when replying to a highly respected member of this community such as Dave.? ? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language.? ? Mark Lawrence? ? ---? This email is free from viruses and malware because avast! Antivirus protection is active.? http://www.avast.com? ? _______________________________________________? Tutor maillist - Tutor at python.org? To unsubscribe or change subscription options:? https://mail.python.org/mailman/listinfo/tutor Like this? _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor Sorry, but posting this way does not make sense. The header appears on top, the message at the foot, buried in multiple footers. It is a mess. From alan.gauld at btinternet.com Thu Apr 10 13:15:12 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 10 Apr 2014 12:15:12 +0100 Subject: [Tutor] masking library files In-Reply-To: <8D122B6B3D02432-CF0-3C53@webmail-vfrr13.sis.aol.com> References: <8D122B4AFD880B0-CF0-3BFE@webmail-vfrr13.sis.aol.com> <8D122B6B3D02432-CF0-3C53@webmail-vfrr13.sis.aol.com> Message-ID: On 10/04/14 10:30, ugajin at talktalk.net wrote: > On 10/04/2014 02:20, ugajin at talktalk.net wrote: >> Please write in plain English if you want to be understood. >> >> >> -----Original Message----- >> From: Dave Angel >> To: tutor at python.org >>> >> >> A message left in invisible ink. Please post in text form, not >> html, as html offers too many ways to mess up the message. >> >> >> > > Please don't top post on this list. And don't use language like that > if you want answers, especially when replying to a highly respected > member of this community such as Dave. > > Mark Lawrence > > > Like this? Yes, but delete all the junk such as extraneous headers/footers as I did above. It does actually make the conversation much easier to follow than top posting. I appreciate many modern mail tools do top posting as default (thanks Microsoft! :-( ) but for conversations its much easier to use inline comments. For a good, fairly short, balanced, explanation of how it helps, see this: http://en.wikipedia.org/wiki/Posting_style It helps if you have quoting turned on in your mail tool too - yours doesn't seem to be using quotes at present (Quoting means the >>> bits in the message above - the number of > characters tells you how far back in the conversation it was) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Thu Apr 10 13:22:03 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 10 Apr 2014 12:22:03 +0100 Subject: [Tutor] Question regarding Python loops In-Reply-To: References: Message-ID: On 10/04/14 03:04, Conner Crowe wrote: > *Problem 2.* > # Here's one possible stand-in: > def leave(minute): > if minute > 0: > return minute > return 0 > So far for this i have: > *def* *leave*(minute): > *if* minute > 0: > *return* minute > *elif* minute=0: > *return* 0 Ignoring the messsed yup formatting, presumably due to HTML mail... You have introduced a bug into the example you were given. The elif line should read elif minute == 0: # use double = sign But as the example shows you don't need the explicit check the function returns zero even for negative numbers. Your version returns None for negatives which will likely cause your later code to break. > *def**already_left*(i): > *for* i *in* range(0,t) Where is t defined? > leave(i) You calculate the result but then ignore it... You need to return a value from your function. > *Problem 3.* Lets do one problem at a time. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From ugajin at talktalk.net Thu Apr 10 14:17:07 2014 From: ugajin at talktalk.net (ugajin at talktalk.net) Date: Thu, 10 Apr 2014 08:17:07 -0400 Subject: [Tutor] Fwd: masking library files In-Reply-To: <8D122CDB9C2E0BE-CF0-3F87@webmail-vfrr13.sis.aol.com> Message-ID: <8D122CDE92090E4-CF0-3F8E@webmail-vfrr13.sis.aol.com> But I must remember to cc tutor at python.org when replying to you. -----Original Message----- From: ugajin at talktalk.net To: alan.gauld at btinternet.com Sent: Thu, 10 Apr 2014 13:15 Subject: Re: [Tutor] masking library files It is off, and I don't appear have an option to turn it on in plain text. I have this option, but but only in rich text/html, and as you see it top posts, which I quite like. Yes, but delete all the junk such as extraneous headers/footers as I did above. It does actually make the conversation much easier to follow than top posting. I appreciate many modern mail tools do top posting as default (thanks Microsoft! :-( ) but for conversations its much easier to use inline comments. For a good, fairly short, balanced, explanation of how it helps, see this: http://en.wikipedia.org/wiki/Posting_style It helps if you have quoting turned on in your mail tool too - yours doesn't seem to be using quotes at present (Quoting means the >>> bits in the message above - the number of > characters tells you how far back in the conversation it was) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos -----Original Message----- From: Alan Gauld To: tutor at python.org Sent: Thu, 10 Apr 2014 12:23 Subject: Re: [Tutor] masking library files On 10/04/14 10:30, ugajin at talktalk.net wrote: > On 10/04/2014 02:20, ugajin at talktalk.net wrote: >> Please write in plain English if you want to be understood. >> >> >> -----Original Message----- >> From: Dave Angel >> To: tutor at python.org >>> >> >> A message left in invisible ink. Please post in text form, not >> html, as html offers too many ways to mess up the message. >> >> >> > > Please don't top post on this list. And don't use language like that > if you want answers, especially when replying to a highly respected > member of this community such as Dave. > > Mark Lawrence > > > Like this? Yes, but delete all the junk such as extraneous headers/footers as I did above. It does actually make the conversation much easier to follow than top posting. I appreciate many modern mail tools do top posting as default (thanks Microsoft! :-( ) but for conversations its much easier to use inline comments. For a good, fairly short, balanced, explanation of how it helps, see this: http://en.wikipedia.org/wiki/Posting_style It helps if you have quoting turned on in your mail tool too - yours doesn't seem to be using quotes at present (Quoting means the >>> bits in the message above - the number of > characters tells you how far back in the conversation it was) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From dharmit.dev at gmail.com Thu Apr 10 13:34:52 2014 From: dharmit.dev at gmail.com (Dharmit Shah) Date: Thu, 10 Apr 2014 17:04:52 +0530 Subject: [Tutor] Desktop Notifications on Linux In-Reply-To: References: Message-ID: Hi Peter, On Thu, Apr 10, 2014 at 2:30 PM, Peter Otten <__peter__ at web.de> wrote: > Dharmit Shah wrote: > >> I am trying to create a script that will go through the >> /var/log/secure file on a Linux system and provide desktop >> notifications for failed login attempts. >> >> Here is the code - http://pastebin.com/MXP8Yd91 >> And here's notification.py - http://pastebin.com/BhsSTP6H >> >> I am facing issue in the function "new_attempts_from_last". I am not >> able to raise a desktop notification from this function. It always >> fails with this traceback - http://pastebin.com/cgHMScv3 >> >> I see this error only when I try to raise a notification from the >> aforementioned function. If I run test examples under >> /usr/share/doc/notify-python/examples, it works just fine. Also, if I >> try to raise a notification from under if __name__ == "__main__":, it >> works without any issues. So I don't think there's any issue with OS's >> notification daemon. Running from python shell like below also works >> fine: >> >> In [1]: import notification >> >> In [2]: notification.notification("Hey") >> >> What am I missing or doing wrong here? >> >> If it matters, I am running Fedora 20, python 2.7 and Cinnamon desktop >> environment. >> >> For readability purposes, I have provided pastebin links. Let me know >> if this is not the correct way. > Thanks for your prompt response. > Maybe you are running the code as a user that has no "desktop"? Here's a > strapped-down demo: > > $ cat notify_demo.py > import pynotify > import sys > > pynotify.init("Notification") > n = pynotify.Notification("Notification", " ".join(sys.argv[1:])) > n.show() > $ python notify_demo.py hello world > > The notification appeared on my desktop. However, when trying again as root > I got the error you are seeing: > > $ sudo su > # python notify_demo.py hello again > Traceback (most recent call last): > File "notify_demo.py", line 6, in > n.show() > glib.GError: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name > org.freedesktop.Notifications was not provided by any .service files > That does ring some bells. I am logged into my F20 system as non-root user but since reading /var/log/secure file requires superuser privileges, I am running it as sudo: sudo python secure.py That probably explains the issue I am facing. I will add the user to the root group and see if it helps. Regards, Dharmit. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From gb.gabrielebrambilla at gmail.com Thu Apr 10 17:58:30 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Thu, 10 Apr 2014 11:58:30 -0400 Subject: [Tutor] improving speed using and recalling C functions Message-ID: Hi, I have a program that is reading near 600000 elements from a file. For each element it performs 200 times a particular mathematical operation (a numerical interpolation of a function). Now these process takes near 8 hours. Creating a C function and calling it from the code could improve the speed? It could be useful that it contains both the mathematical operation and the for loop or only the mathematical operation? If it is a reasonable solution to try decreasing the computational time how can I implement this C function? Thanks Gabriele -------------- next part -------------- An HTML attachment was scrubbed... URL: From martin at linux-ip.net Thu Apr 10 19:05:47 2014 From: martin at linux-ip.net (Martin A. Brown) Date: Thu, 10 Apr 2014 13:05:47 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Hi there Gabriele, : I have a program that is reading near 600000 elements from a : file. For each element it performs 200 times a particular : mathematical operation (a numerical interpolation of a function). : Now these process takes near 8 hours. Sounds fun! Here are some thoughts (though I may not have any solid answers, I hope these pointers are useful): Are you sure (from profiling) that the numerical interpolation is the bottleneck? What is the numerical operation? 1: Is the function implemented in Numpy? [0] 2: How about SciPy? [1] Is there a domain-specific Python library that already does the computation you want to achieve? If so, then you have "only" the format question--how to put your data into the format required by the library. If not, then on to the next question you ask ... : Creating a C function and calling it from the code could improve : the speed? It could be useful that it contains both the : mathematical operation and the for loop or only the mathematical : operation? : : If it is a reasonable solution to try decreasing the : computational time how can I implement this C function? It is certainly an option! In the event that you cannot find something that is fast enough in Python already and you cannot find any C library that has Python bindings, then, whether you have to write your own, or you have a third party C library, you have several options for writing bindings. You can write your Python bindings using: * ctypes: https://docs.python.org/2/library/ctypes.html * cffi: http://cffi.readthedocs.org/en/release-0.8/ * cython: http://www.cython.org/ * roll your own C! It might be beyond the scope of this list (certainly beyond my capabilities) to provide recommendations on which avenue to take--but if you are asking, you may already have the tools you need to assess for yourself. There is, however, quite a bit of experience loitering around this list, so perhaps somebody else will have a suggestion. -Martin [0] http://www.numpy.org/ [1] http://www.scipy.org/ -- Martin A. Brown http://linux-ip.net/ From alan.gauld at btinternet.com Thu Apr 10 19:26:12 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 10 Apr 2014 18:26:12 +0100 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: On 10/04/14 16:58, Gabriele Brambilla wrote: > For each element it performs 200 times a particular mathematical > operation (a numerical interpolation of a function). > Now these process takes near 8 hours. The first thing to do in such cases is check that the time is going where you think it is. Run the cProfile tool on the code (with a smaller data set!) to see where the time is being spent. > Creating a C function and calling it from the code could improve the speed? Yes, it could. But it may not be necessary. There are various add on tools that can drastically improve Python efficiency, especially for math type functions so you might want to check that nobody has already written what you need. > If it is a reasonable solution to try decreasing the computational time > how can I implement this C function? If its already written in Python the easiest way might be to use Cython to convert it into C. I'm no expert in Cython however, but others on this list can help. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From gb.gabrielebrambilla at gmail.com Thu Apr 10 19:29:58 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Thu, 10 Apr 2014 13:29:58 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Hi, 2014-04-10 13:05 GMT-04:00 Martin A. Brown : > > Hi there Gabriele, > > : I have a program that is reading near 600000 elements from a > : file. For each element it performs 200 times a particular > : mathematical operation (a numerical interpolation of a function). > : Now these process takes near 8 hours. > > Sounds fun! Here are some thoughts (though I may not have any solid > answers, I hope these pointers are useful): > > Are you sure (from profiling) that the numerical interpolation > is the bottleneck? > > I see that the time needed by the code increase when I increase the number of these operation. It's the only thing I do in the code. (I'm sorry but I don't know very well what profiling is) > What is the numerical operation? > 1: Is the function implemented in Numpy? [0] > 2: How about SciPy? [1] > > Is interp1d from scipy.interpolate. I think is the best one > Is there a domain-specific Python library that already does the > computation you want to achieve? If so, then you have "only" the > format question--how to put your data into the format required by > the library. If not, then on to the next question you ask ... > > No, interp1d do exactly what I want. > : Creating a C function and calling it from the code could improve > : the speed? It could be useful that it contains both the > : mathematical operation and the for loop or only the mathematical > : operation? > : > : If it is a reasonable solution to try decreasing the > : computational time how can I implement this C function? > > It is certainly an option! In the event that you cannot find > something that is fast enough in Python already and you cannot find > any C library that has Python bindings, then, whether you have to > write your own, or you have a third party C library, you have > several options for writing bindings. > You can write your Python bindings using: > > * ctypes: https://docs.python.org/2/library/ctypes.html > * cffi: http://cffi.readthedocs.org/en/release-0.8/ > * cython: http://www.cython.org/ > * roll your own C! > > Do you know if does a C library exist with an already implemented function like interp1d? I use Anaconda, is there an already implemented mode to do it in Anaconda? Cython? > It might be beyond the scope of this list (certainly beyond my > capabilities) to provide recommendations on which avenue to > take--but if you are asking, you may already have the tools you need > to assess for yourself. There is, however, quite a bit of > experience loitering around this list, so perhaps somebody else will > have a suggestion. > > -Martin > > [0] http://www.numpy.org/ > [1] http://www.scipy.org/ > > -- > Martin A. Brown > http://linux-ip.net/ > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ugajin at talktalk.net Thu Apr 10 20:09:42 2014 From: ugajin at talktalk.net (ugajin at talktalk.net) Date: Thu, 10 Apr 2014 14:09:42 -0400 Subject: [Tutor] masking library files In-Reply-To: <5346BB98.8060009@yahoo.co.uk> References: <5346BB98.8060009@yahoo.co.uk> Message-ID: <8D122FF2AC2B26E-CF0-4544@webmail-vfrr13.sis.aol.com> I do have a Reply All command, but as in this case, there is no recipient other than myself to be included. Also, hitting the Reply All command seems to generate an alert warning message, when recipients include a mail-list address, and some mail list replies go to the mail-list as recipient rather than the sender by default. I don't know which mail tool you are using but you should have a ReplyAll command that does that automatically. -----Original Message----- From: Alan Gauld To: ugajin at talktalk.net Sent: Thu, 10 Apr 2014 16:41 Subject: Re: Fwd: masking library files On 10/04/14 13:17, ugajin at talktalk.net wrote: > But I must remember to cc tutor at python.org when replying to you. I don't know which mail tool you are using but you should have a ReplyAll command that does that automatically. If you are using a Desktop mail tool it may even have a mailing list mode that detects lists (or allows you to specify them) and auto uses replyall... Alan g. > -----Original Message----- > From: ugajin at talktalk.net > To: alan.gauld at btinternet.com > Sent: Thu, 10 Apr 2014 13:15 > Subject: Re: [Tutor] masking library files > > > It is off, and I don't appear have an option to turn it on in plain > text. I have this option, but but only in rich text/html, and as you see > it top posts, which I quite like. > > Yes, but delete all the junk such as extraneous headers/footers as > I did above. It does actually make the conversation much easier to > follow than top posting. > > I appreciate many modern mail tools do top posting as default > (thanks Microsoft! :-( ) but for conversations its much easier > to use inline comments. > > For a good, fairly short, balanced, explanation of how it helps, > see this: > > http://en.wikipedia.org/wiki/Posting_style > > It helps if you have quoting turned on in your mail tool too - yours > doesn't seem to be using quotes at present (Quoting means the >>> > bits in the message above - the number of > characters tells you how > far back in the conversation it was) > > -- Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > > > -----Original Message----- > From: Alan Gauld > > To: tutor at python.org > Sent: Thu, 10 Apr 2014 12:23 > Subject: Re: [Tutor] masking library files > > On 10/04/14 10:30, ugajin at talktalk.net wrote: > > > On 10/04/2014 02:20, ugajin at talktalk.net wrote: > >> Please write in plain English if you want to be understood. > >> > >> > >> -----Original Message----- > >> From: Dave Angel > >> To: tutor at python.org > >>> > >> > >> A message left in invisible ink. Please post in text form, not > >> html, as html offers too many ways to mess up the message. > >> > >> > >> > > > > Please don't top post on this list. And don't use language like that > > if you want answers, especially when replying to a highly respected > > member of this community such as Dave. > > > > Mark Lawrence > > > > > > Like this? > > Yes, but delete all the junk such as extraneous headers/footers as > I did above. It does actually make the conversation much easier to > follow than top posting. > > I appreciate many modern mail tools do top posting as default > (thanks Microsoft! :-( ) but for conversations its much easier > to use inline comments. > > For a good, fairly short, balanced, explanation of how it helps, > see this: > > http://en.wikipedia.org/wiki/Posting_style > > It helps if you have quoting turned on in your mail tool too - yours > doesn't seem to be using quotes at present (Quoting means the >>> > bits in the message above - the number of > characters tells you how far > back in the conversation it was) > > -- Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Alan G Author of the Learn To Program web site http://www.alan-g.me.uk http://www.flickr.com/photos/alangauldphotos -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Thu Apr 10 20:29:40 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 10 Apr 2014 11:29:40 -0700 Subject: [Tutor] masking library files In-Reply-To: <8D122FF2AC2B26E-CF0-4544@webmail-vfrr13.sis.aol.com> References: <5346BB98.8060009@yahoo.co.uk> <8D122FF2AC2B26E-CF0-4544@webmail-vfrr13.sis.aol.com> Message-ID: We should get back to the topic. Did you have a Python question? Meta-discussion on mailing list etiquette must not dominate discussion on helping people learning to program with Python. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Thu Apr 10 20:44:43 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 10 Apr 2014 11:44:43 -0700 Subject: [Tutor] masking library files In-Reply-To: <8D122159B84276F-11A4-1CC77@webmail-vfrr14.sis.aol.com> References: <8D122159B84276F-11A4-1CC77@webmail-vfrr14.sis.aol.com> Message-ID: On Wed, Apr 9, 2014 at 7:17 AM, wrote: > Is it common for files saved to a working directory to 'mask' library files > located in the Python framework? Hi ugajin, To come back to your original question: yes, unfortunately this happens. I think it's a flaw in the language. There are steps to mitigate the problem, some by convention, some by using newer features in Python. Certain style guidelines dictate that module import never use relative paths, but rather use one rooted at the toplevel package directory. As a concrete example, see Google Python style guidelines on how to do imports: http://google-styleguide.googlecode.com/svn/trunk/pyguide.html#Packages This is odd at first, but it has the intent of avoiding module collision. The basic idea is to have all your code packaged, and always use the unique package as a namespace to avoid collision with the standard library. So if you have modules m1, m2 in package p, and m1 wants to import m2, then we say: import p.m2 Other mitigation strategies include using explicit-relative imports: https://docs.python.org/2/tutorial/modules.html#intra-package-references but I think the convention approach is a little easier to handle. From breamoreboy at yahoo.co.uk Thu Apr 10 20:54:50 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 10 Apr 2014 19:54:50 +0100 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: On 10/04/2014 18:29, Gabriele Brambilla wrote: > > (I'm sorry but I don't know very well what profiling is) > Take a look at these for some tips http://www.huyng.com/posts/python-performance-analysis/ and https://wiki.python.org/moin/PythonSpeed/PerformanceTips -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From dyoo at hashcollision.org Thu Apr 10 20:51:11 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 10 Apr 2014 11:51:11 -0700 Subject: [Tutor] masking library files In-Reply-To: References: <8D122159B84276F-11A4-1CC77@webmail-vfrr14.sis.aol.com> Message-ID: Apologies for the multiple emails. I'm still paging in from memory how Python imports are working these days. :P If you have the freedom to do so, you may also turn on the absolute import system: https://docs.python.org/2/whatsnew/2.5.html#pep-328-absolute-and-relative-imports so that you can avoid the issue altogether, at least within your own code. This is not the default behavior in Python 2, so you've got to opt-into it. I believe it is the default in Python 3, so it should not be an issue in Python 3 code. From alan.gauld at btinternet.com Thu Apr 10 20:57:40 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 10 Apr 2014 19:57:40 +0100 Subject: [Tutor] masking library files In-Reply-To: <8D122FF2AC2B26E-CF0-4544@webmail-vfrr13.sis.aol.com> References: <5346BB98.8060009@yahoo.co.uk> <8D122FF2AC2B26E-CF0-4544@webmail-vfrr13.sis.aol.com> Message-ID: On 10/04/14 19:09, ugajin at talktalk.net wrote: > I do have a Reply All command, but as in this case, there is no > recipient other than myself to be included. That's because I (deliberately) replied only to you. :-) > commandseems to generate an alert warning message, when recipients > include a mail-list address, and some mail list replies go to the > mail-list as recipient rather than the sender by default. Which tool are you using? Others on the list may be using the same tool and can help with settings... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Thu Apr 10 21:09:55 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 10 Apr 2014 12:09:55 -0700 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Hi Gabriele, Have you profiled your program? Please look at: https://docs.python.org/2/library/profile.html If you can, avoid guessing what is causing performance to drop. Rather, use the tools in the profiling libraries to perform measurements. It may be that your program is taking a long time because of something obvious, but perhaps there is some other factor that's contributing. Please do this. More details would be helpful. Are you using any libraries such as Numpy? Just writing something in C doesn't magically make it go faster. CPython is written in C, for example, and yet people do not say that Python itself is very fast. :P It may be the case that writing the computations in C will allow you to specify enough type information so that the computer can effectively run your computations quickly. But if you're using libraries like Numpy to do vector parallel operations, I would not be surprised if that would outperform native non-parallel C code. See: http://technicaldiscovery.blogspot.com/2011/06/speeding-up-python-numpy-cython-and.html which demonstrates that effective use of NumPy may speed up computations by a significant order of magnitude, if you use the library to take advantage of its vectorizing compuations. More domain-specific details may help folks on the tutor list to give good advice here. From bgailer at gmail.com Thu Apr 10 22:11:33 2014 From: bgailer at gmail.com (bob gailer) Date: Thu, 10 Apr 2014 16:11:33 -0400 Subject: [Tutor] difference between expressions and statements In-Reply-To: References: Message-ID: <5346FAF5.6050709@gmail.com> Caveat: I began this before there were any other responses. So this may be overkill - but I ike to be thorough. On 4/9/2014 12:49 PM, Jared Nielsen wrote: > Hi Pythons, > Could someone explain the difference between expressions and statements? >> I know that expressions are statements that produce a value. > No. Expressions are not statements. These are mutually exclusive. > Expressions do produce values. An attempt at a thorough answer: In the language reference glossary under "expression" you will find: "A piece of syntax which can be evaluated to some value. In other words, an expression is an accumulation of expression elements like literals, names, attribute access, operators or function calls which all return a value.... There are also statements which cannot be used as expressions, such as if. Assignments are also statements, not expressions." Tthe above is a quote; I don't like some of the grammar. In your examples print is a function. So all calls to print are expressions. In the language reference you will also find: 7. Simple statements 7.1. Expression statements 7.2. Assignment statements 7.3. The assert statement 7.4. The pass statement 7.5. The del statement 7.6. The return statement 7.7. The yield statement 7.8. The raise statement 7.9. The break statement 7.10. The continue statement 7.11. The import statement 7.12. The global statement 7.13. The nonlocal statement 8. Compound statements 8.1. The if statement 8.2. The while statement 8.3. The for statement 8.4. The try statement 8.5. The with statement 8.6. Function definitions 8.7. Class definitions With the exception of - 7.1. Expression statements - all of the above are either start with a keyword except 7.2 assignment which is indicated by an equal sign (=) . - all of the above cause something to happen (except pass), and do not return a value. 7.1. Expression statement is either one expression or several separated by commas. Used interactively to display value(s). Used anywhere to make a function call. > I'm unclear on functions and especially strings. > Are any of the following expressions? > > print(42) > print("spam") > spam = 42 > print(spam) > > Is the first example producing a value or simply displaying an integer? All function calls return a value. In the case of print the return value is always None. spam = 42 is a statement. (indicated by the = sign. 42 is a value. > Does a string count as a value? > Yes - however I suspect you are limiting "string" to something within quotes. Those are "string literals". > Is a variable assignment considered a value? No > If I print a variable is that considered production of a value? See above comment on print. Long but comprehensive answer. Feel free to ask questions. Note there are various subtleties here -some keywords may be used to start a statement or in an expression - e.g. if, else, for yield. This also raises the fact that else (inter ala) is neither an expression or a statement; rather it is part of a compound statement. Nothing is simple. Oh there is more but I may never hit send.... From gb.gabrielebrambilla at gmail.com Thu Apr 10 22:51:44 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Thu, 10 Apr 2014 16:51:44 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: I'm trying to profile it adding this code: import cProfile import re import pstats cProfile.run('re.compile("foo|bar")', 'restats') p = pstats.Stats('restats') p.strip_dirs().sort_stats('name') p.sort_stats('time').print_stats(10) but where I have to add this in my code? because I obtain Thu Apr 10 15:23:17 2014 restats 194 function calls (189 primitive calls) in 0.001 seconds Ordered by: internal time List reduced from 34 to 10 due to restriction <10> ncalls tottime percall cumtime percall filename:lineno(function) 3/1 0.000 0.000 0.000 0.000 sre_compile.py:33(_compile) 1 0.000 0.000 0.000 0.000 sre_compile.py:208(_optimize_chars et) 3/1 0.000 0.000 0.000 0.000 sre_parse.py:141(getwidth) 1 0.000 0.000 0.000 0.000 sre_compile.py:362(_compile_info) 2 0.000 0.000 0.000 0.000 sre_parse.py:380(_parse) 1 0.000 0.000 0.000 0.000 sre_parse.py:302(_parse_sub) 1 0.000 0.000 0.001 0.001 re.py:226(_compile) 10 0.000 0.000 0.000 0.000 sre_parse.py:183(__next) 1 0.000 0.000 0.001 0.001 sre_compile.py:496(compile) 15 0.000 0.000 0.000 0.000 {isinstance} but my program take more than 0.001 seconds! I think it's not working as I want. Gabriele 2014-04-10 15:09 GMT-04:00 Danny Yoo : > Hi Gabriele, > > Have you profiled your program? Please look at: > > https://docs.python.org/2/library/profile.html > > If you can, avoid guessing what is causing performance to drop. > Rather, use the tools in the profiling libraries to perform > measurements. > > > It may be that your program is taking a long time because of something > obvious, but perhaps there is some other factor that's contributing. > Please do this. More details would be helpful. Are you using any > libraries such as Numpy? Just writing something in C doesn't > magically make it go faster. CPython is written in C, for example, > and yet people do not say that Python itself is very fast. :P > > It may be the case that writing the computations in C will allow you > to specify enough type information so that the computer can > effectively run your computations quickly. But if you're using > libraries like Numpy to do vector parallel operations, I would not be > surprised if that would outperform native non-parallel C code. > > See: > > > http://technicaldiscovery.blogspot.com/2011/06/speeding-up-python-numpy-cython-and.html > > which demonstrates that effective use of NumPy may speed up > computations by a significant order of magnitude, if you use the > library to take advantage of its vectorizing compuations. > > > More domain-specific details may help folks on the tutor list to give > good advice here. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Thu Apr 10 23:14:25 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 10 Apr 2014 14:14:25 -0700 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Hi Gabriele, I should probably have pointed you to: https://docs.python.org/2/library/profile.html#instant-user-s-manual instead. Here is an example that uses the cProfile module. Let's say that I'm trying to pinpoint where something is going slow in some_program(): ######################################################### import cProfile def slow_string_mul(w, n): s = "" for x in xrange(n): s = slow_append(s, w) return s def slow_append(s, w): return s + w def fast_string_mul(w, n): return w * n def some_program(w, n): print slow_string_mul(w, n) == fast_string_mul(w, n) ## Try running the operation, and let cProfile report stats." cProfile.run('some_program("testing", 50000)', None, 'time') ######################################################### We tell cProfile.run to execute some_program(), sorting its measurements by time. You might save the collected statistics to a file, but here I have not, and have cProfile.run() just report the results after the program finishes. Here's what comes back from cProfile's report: ######################################################### $ python test_profile.py True 50005 function calls in 1.422 seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno(function) 50000 1.225 0.000 1.225 0.000 test_profile.py:9(slow_append) 1 0.197 0.197 1.422 1.422 test_profile.py:3(slow_string_mul) 1 0.000 0.000 1.422 1.422 test_profile.py:15(some_program) 1 0.000 0.000 0.000 0.000 test_profile.py:12(fast_string_mul) 1 0.000 0.000 1.422 1.422 :1() 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} ######################################################### This is telling me that slow_append is being called 50000 times, which in hindsight sounds right. It's taking the majority of the time of this program, which is intuitively true, as I wrote it in a way to do a very naive string-appending operation, which gets more expensive the longer the string grows. The point of examples is to show simple uses of the libraries. But you will have to adapt the examples to work for your context. From davea at davea.name Fri Apr 11 00:55:33 2014 From: davea at davea.name (Dave Angel) Date: Thu, 10 Apr 2014 18:55:33 -0400 (EDT) Subject: [Tutor] masking library files References: <8D12272264D1C02-11A4-1D51C@webmail-vfrr14.sis.aol.com> Message-ID: ugajin at talktalk.net Wrote in message: > Please write in plain English if you want to be understood Like the op, you post in html. There are a bunch of things that can wrong when you do that, so you should use text mail instead. In the case of the op, he apparently changed the text color to the same as the background. I called it invisible ink, because I couldn't read a word. -- DaveA From gb.gabrielebrambilla at gmail.com Fri Apr 11 00:51:48 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Thu, 10 Apr 2014 18:51:48 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Hi, I get this result: Thu Apr 10 17:35:53 2014 restats 21071736 function calls in 199.883 seconds Ordered by: internal time List reduced from 188 to 10 due to restriction <10> ncalls tottime percall cumtime percall filename:lineno(function) 1 149.479 149.479 199.851 199.851 skymaps5.py:16(mymain) 18101000 28.682 0.000 28.682 0.000 {method 'write' of 'file' objects} 33044 5.470 0.000 6.444 0.000 interpolate.py:394(_call_linear) 230000 2.272 0.000 21.279 0.000 instruments.py:10(kappa) 231328 2.120 0.000 2.120 0.000 {numpy.core.multiarray.array} 33044 1.719 0.000 3.836 0.000 interpolate.py:454(_check_bounds) 66088 1.611 0.000 1.611 0.000 {method 'reduce' of 'numpy.ufunc' objects} 33044 1.146 0.000 11.623 0.000 interpolate.py:443(_evaluate) 33044 1.120 0.000 5.542 0.000 interpolate.py:330(__init__) 33044 0.659 0.000 2.329 0.000 polyint.py:82(_set_yi) the major time is required by mymain that is the whole program. the write on file is an operation that I do in the end but if I increase the number of data it doesn't increase (I tried doubing the sample of values and I know why it's behaving in this way) the third are interpolate and kappa: the two functions (one called inside the other one) So they are the ones that are taking time. Now what can I do? thanks Gabriele 2014-04-10 17:14 GMT-04:00 Danny Yoo : > Hi Gabriele, > > > I should probably have pointed you to: > > https://docs.python.org/2/library/profile.html#instant-user-s-manual > > instead. > > > Here is an example that uses the cProfile module. Let's say that I'm > trying to pinpoint where something is going slow in some_program(): > > ######################################################### > import cProfile > > def slow_string_mul(w, n): > s = "" > for x in xrange(n): > s = slow_append(s, w) > return s > > def slow_append(s, w): > return s + w > > def fast_string_mul(w, n): > return w * n > > def some_program(w, n): > print slow_string_mul(w, n) == fast_string_mul(w, n) > > ## Try running the operation, and let cProfile report stats." > cProfile.run('some_program("testing", 50000)', None, 'time') > ######################################################### > > > We tell cProfile.run to execute some_program(), sorting its > measurements by time. You might save the collected statistics to a > file, but here I have not, and have cProfile.run() just report the > results after the program finishes. > > > > Here's what comes back from cProfile's report: > > ######################################################### > $ python test_profile.py > True > 50005 function calls in 1.422 seconds > > Ordered by: internal time > > ncalls tottime percall cumtime percall filename:lineno(function) > 50000 1.225 0.000 1.225 0.000 > test_profile.py:9(slow_append) > 1 0.197 0.197 1.422 1.422 > test_profile.py:3(slow_string_mul) > 1 0.000 0.000 1.422 1.422 > test_profile.py:15(some_program) > 1 0.000 0.000 0.000 0.000 > test_profile.py:12(fast_string_mul) > 1 0.000 0.000 1.422 1.422 :1() > 1 0.000 0.000 0.000 0.000 {method 'disable' of > '_lsprof.Profiler' objects} > ######################################################### > > This is telling me that slow_append is being called 50000 times, which > in hindsight sounds right. It's taking the majority of the time of > this program, which is intuitively true, as I wrote it in a way to do > a very naive string-appending operation, which gets more expensive the > longer the string grows. > > > > The point of examples is to show simple uses of the libraries. But > you will have to adapt the examples to work for your context. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From nielsen.jared at gmail.com Thu Apr 10 23:48:01 2014 From: nielsen.jared at gmail.com (Jared Nielsen) Date: Thu, 10 Apr 2014 15:48:01 -0600 Subject: [Tutor] difference between expressions and statements In-Reply-To: <5346FAF5.6050709@gmail.com> References: <5346FAF5.6050709@gmail.com> Message-ID: Thanks for the thorough answer, Bob. I now understand the difference. On Apr 10, 2014 2:11 PM, "bob gailer" wrote: > Caveat: I began this before there were any other responses. So this may be > overkill - but I ike to be thorough. > > On 4/9/2014 12:49 PM, Jared Nielsen wrote: > >> Hi Pythons, >> Could someone explain the difference between expressions and statements? >> >>> I know that expressions are statements that produce a value. >>> >> No. Expressions are not statements. These are mutually exclusive. >> Expressions do produce values. >> > An attempt at a thorough answer: > > In the language reference glossary under "expression" you will find: > > "A piece of syntax which can be evaluated to some value. In other words, > an expression is an accumulation of expression elements like literals, > names, attribute access, operators or function calls which all return a > value.... There are also statements which cannot be used as expressions, > such as if. Assignments are also statements, not expressions." > > Tthe above is a quote; I don't like some of the grammar. > > In your examples print is a function. So all calls to print are > expressions. > > In the language reference you will also find: > > 7. Simple statements > 7.1. Expression statements > 7.2. Assignment statements > 7.3. The assert statement > 7.4. The pass statement > 7.5. The del statement > 7.6. The return statement > 7.7. The yield statement > 7.8. The raise statement > 7.9. The break statement > 7.10. The continue statement > 7.11. The import statement > 7.12. The global statement > 7.13. The nonlocal statement > 8. Compound statements > 8.1. The if statement > 8.2. The while statement > 8.3. The for statement > 8.4. The try statement > 8.5. The with statement > 8.6. Function definitions > 8.7. Class definitions > > With the exception of > - 7.1. Expression statements > - all of the above are either start with a keyword except 7.2 assignment > which is indicated by an equal sign (=) . > - all of the above cause something to happen (except pass), and do not > return a value. > > 7.1. Expression statement is either one expression or several separated by > commas. > Used interactively to display value(s). > Used anywhere to make a function call. > >> I'm unclear on functions and especially strings. >> Are any of the following expressions? >> >> print(42) >> print("spam") >> spam = 42 >> print(spam) >> >> Is the first example producing a value or simply displaying an integer? >> > All function calls return a value. In the case of print the return value > is always None. > spam = 42 is a statement. (indicated by the = sign. 42 is a value. > >> Does a string count as a value? >> Yes - however I suspect you are limiting "string" to something within >> quotes. Those are "string literals". >> Is a variable assignment considered a value? >> > No > >> If I print a variable is that considered production of a value? >> > See above comment on print. > > Long but comprehensive answer. Feel free to ask questions. > > Note there are various subtleties here -some keywords may be used to > start a statement or in an expression - e.g. if, else, for yield. > > This also raises the fact that else (inter ala) is neither an expression > or a statement; rather it is part of a compound statement. Nothing is > simple. > > Oh there is more but I may never hit send.... > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sabausmani at outlook.com Fri Apr 11 00:26:09 2014 From: sabausmani at outlook.com (Saba Usmani) Date: Thu, 10 Apr 2014 23:26:09 +0100 Subject: [Tutor] Refining Code Message-ID: My task is :A food vending machine accepts 10p, 20p, 50p and ?1 coins. One or more coins are inserted and the current credit is calculated and displayed. A product is selected from those available. The system checks to see if there is enough credit to purchase the product chosen. If there is not enough credit the system displays an error message. If there is enough credit it dispenses the product, updates the credit available and displays the remaining credit. Further selections can be made if there is enough credit. The vending machine simulation should have five products and prices. Design, code, test and evaluate a program for this simulation. I have designed the following code, but would like to know how to make it more efficient without making it too complex as I am a beginner or is this fine? Also, how do I add a loop to this so that once one product has been dispensed the program asks the user if they would like to continue and purchase another product? Code: print "Welcome to Snack Attack" snack1 = 0.40snack2 = 0.75snack3 = 1.20snack4 = 0.99snack5 = 0.50insert = 0 change = 0currentCredit = 0.00A = 0.10B = 0.20C = 0.50D = 1.00a = 0.10b = 0.20c = 0.50d = 1.00 print "Menu"print "Snack 1: Snickers - ?0.40"print "Snack 2: Doritos - ?0.75 "print "Snack 3: J20 - ?1.20"print "Snack 4: Oreos - ?0.99"print "Snack 5: M&M's - ?0.50" print "Exit?" - how do I make this a Boolean expression, so the user can respond with either yes or no? choice = input("Select your snack: ") if choice==1: print " " print "You have selected Snickers, which cost ?0.40" print "Please insert ?0.40" while currentCredit < snack1: print "Please select which of these coins to insert; A:10p,B:20p,C:50p and D:?1" insert_coins = input("Insert coins: ") currentCredit = insert_coins + currentCredit print "Your current credit is ?",currentCredit else: change_given=currentCredit-snack1 print " " print "Your change is ?",change_given print "Your Snickers have been dispensed...Enjoy!" elif choice==2: print "You have selected Doritos, which cost ?0.75" print "Please insert ?0.75" while currentCredit From martin at linux-ip.net Fri Apr 11 01:41:52 2014 From: martin at linux-ip.net (Martin A. Brown) Date: Thu, 10 Apr 2014 16:41:52 -0700 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Gabriele, > 21071736 function calls in 199.883 seconds The 21 million function calls isn't really a surprise to me, given 18 million calls to file.write(). Given that the majority of the time is still spent in skymaps5.py, I think you'll need to instrument that a bit more to figure out where the hotspot is. > Ordered by: internal time > List reduced from 188 to 10 due to restriction <10> > > ncalls tottime percall cumtime percall filename:lineno(function) > 1 149.479 149.479 199.851 199.851 skymaps5.py:16(mymain) > 18101000 28.682 0.000 28.682 0.000 {method 'write' of 'file' > objects} > > the major time is required by mymain that is the whole program. > the write on file is an operation that I do in the end but if I > increase the number of data it doesn't increase (I tried doubing > the sample of values and I know why it's behaving in this way) the > third are interpolate and kappa: the two functions (one called > inside the other one) This is a good finding, in fact. Now, you know which module contains the bottleneck. Is your CPU pegged when you run that skymap5.py code? Can you run the profiler on the skymaps5.py code, too, to locate the specific hotspot? Great to see that you are using the profiler, effectively, too! > So they are the ones that are taking time. > Now what can I do? I think you now need to profile skymaps5.py code? Specifically the stuff in main(). OK, so I have not myself used scipy.interpolate.interp1d before, but I went to have a look at it. So, you feed interp1d() an x and a y (e.g. a plot line on a diagram), and it essentially produces its best guess of a function which will fit that curve, correct? Well, this is how it performs with random input and an identity function: element count 100, duration 0.000 element count 1000, duration 0.001 element count 10000, duration 0.005 element count 100000, duration 0.055 element count 1000000, duration 0.858 element count 10000000, duration 30.404 So, with 10 million inputs on an admittedly brain-dead function, there's not a performance bottleneck. If you can find the parts of your skymaps5.py code that are the bottleneck, then you could post it here. I hadn't thought of using interpolate, myself, as I didn't even know it existed. Thanks and good luck, -Martin -- Martin A. Brown http://linux-ip.net/ From dyoo at hashcollision.org Fri Apr 11 01:55:13 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 10 Apr 2014 16:55:13 -0700 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: > ncalls tottime percall cumtime percall filename:lineno(function) > 1 149.479 149.479 199.851 199.851 skymaps5.py:16(mymain) > 18101000 28.682 0.000 28.682 0.000 {method 'write' of 'file' objects} > > 33044 5.470 0.000 6.444 0.000 interpolate.py:394(_call_linear) > 230000 2.272 0.000 21.279 0.000 instruments.py:10(kappa) > 231328 2.120 0.000 2.120 0.000 {numpy.core.multiarray.array} > 33044 1.719 0.000 3.836 0.000 interpolate.py:454(_check_bounds) > 66088 1.611 0.000 1.611 0.000 {method 'reduce' of 'numpy.ufunc' > objects} > 33044 1.146 0.000 11.623 0.000 interpolate.py:443(_evaluate) > 33044 1.120 0.000 5.542 0.000 interpolate.py:330(__init__) > 33044 0.659 0.000 2.329 0.000 polyint.py:82(_set_yi) > > the major time is required by mymain that is the whole program. Good! Profiles like this allow us to pinpoint issues. Wait... ?! The profiler is saying that the majority of time is in my main, but _not_ in auxiliary functions. That's surprising. Am I misreading the profile? Can you show what mymain is doing? -------------- next part -------------- An HTML attachment was scrubbed... URL: From gb.gabrielebrambilla at gmail.com Fri Apr 11 01:53:37 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Thu, 10 Apr 2014 19:53:37 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: but main is the program that contains everything. I used the profile in this way: import cProfile import pstats def mymain(): #all the code #end of main indentation cProfile.run('mymain()', 'restats', 'time') p = pstats.Stats('restats') p.strip_dirs().sort_stats('name') p.sort_stats('time').print_stats(10) So all the function I used are contained in main(), so even all the others that are appeared. Gabriele 2014-04-10 19:41 GMT-04:00 Martin A. Brown : > > Gabriele, > > > 21071736 function calls in 199.883 seconds >> > > The 21 million function calls isn't really a surprise to me, given 18 > million calls to file.write(). Given that the majority of the time is > still spent in skymaps5.py, I think you'll need to instrument that a bit > more to figure out where the hotspot is. > > Ordered by: internal time >> List reduced from 188 to 10 due to restriction <10> >> >> ncalls tottime percall cumtime percall filename:lineno(function) >> 1 149.479 149.479 199.851 199.851 skymaps5.py:16(mymain) >> 18101000 28.682 0.000 28.682 0.000 {method 'write' of 'file' >> objects} >> >> the major time is required by mymain that is the whole program. the write >> on file is an operation that I do in the end but if I increase the number >> of data it doesn't increase (I tried doubing the sample of values and I >> know why it's behaving in this way) the third are interpolate and kappa: >> the two functions (one called inside the other one) >> > > This is a good finding, in fact. Now, you know which module contains the > bottleneck. Is your CPU pegged when you run that skymap5.py code? Can you > run the profiler on the skymaps5.py code, too, to locate the specific > hotspot? Great to see that you are using the profiler, effectively, too! > > > So they are the ones that are taking time. >> Now what can I do? >> > > I think you now need to profile skymaps5.py code? Specifically the stuff > in main(). > > OK, so I have not myself used scipy.interpolate.interp1d before, but I > went to have a look at it. So, you feed interp1d() an x and a y (e.g. a > plot line on a diagram), and it essentially produces its best guess of a > function which will fit that curve, correct? > > Well, this is how it performs with random input and an identity function: > > element count 100, duration 0.000 > element count 1000, duration 0.001 > element count 10000, duration 0.005 > element count 100000, duration 0.055 > element count 1000000, duration 0.858 > element count 10000000, duration 30.404 > > So, with 10 million inputs on an admittedly brain-dead function, there's > not a performance bottleneck. If you can find the parts of your > skymaps5.py code that are the bottleneck, then you could post it here. > > I hadn't thought of using interpolate, myself, as I didn't even know it > existed. > > Thanks and good luck, > > -Martin > > > -- > Martin A. Brown > http://linux-ip.net/ > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gb.gabrielebrambilla at gmail.com Fri Apr 11 02:03:16 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Thu, 10 Apr 2014 20:03:16 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: sure. def mymain(): def LEstep(n): Emin=10**6 Emax=5*(10**10) Lemin=log10(Emin) Lemax=log10(Emax) stepE=(Lemax-Lemin)/n return (stepE, n, Lemin, Lemax) if __name__ == "__main__": import sys if len(sys.argv)<=1: stepENE, nex, Lemin, Lemax = LEstep(200) elif len(sys.argv)<=2: stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) else: stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) freq=float(sys.argv[2]) eel = list(range(nex)) eels = np.logspace(Lemin, Lemax, num=nex, endpoint=False) indpha = list(range(npha)) indobs = list(range(nobs)) rlc = c/(2*pi*freq) MYMAP1 = np.zeros([npha, nobs, nex], dtype=float) MYMAP2 = np.zeros([npha, nobs, nex], dtype=float) MYMAP3 = np.zeros([npha, nobs, nex], dtype=float) MYMAP4 = np.zeros([npha, nobs, nex], dtype=float) MYMAP5 = np.zeros([npha, nobs, nex], dtype=float) count=0 omegacliston = [] alpha = '60_' for my_line in open('datasm0_60_5s.dat'): myinternet = [] gmlis = [] print('reading the line', count, '/599378') my_parts = [float(i) for i in my_line.split()] phase = my_parts[4] zobs = my_parts[5] rho = my_parts[6] gammar1 = my_parts[7] gammar2 = my_parts[8] gammar3 = my_parts[9] gammar4 = my_parts[10] gammar5 = my_parts[11] gmlis.append(gammar1) gmlis.append(gammar2) gmlis.append(gammar3) gmlis.append(gammar4) gmlis.append(gammar5) i = int((phase-phamin)/stepPHA) j = int((zobs-obamin)/stepOB) for gammar in gmlis: omC = (1.5)*(gammar**3)*c/(rho*rlc) gig = omC*hcut/eVtoErg omegacliston.append(omC) for w in eel[:]: omega = (10**(w*stepENE+Lemin))*eVtoErg/hcut x = omega/omC kap = instruments.kappa(x) Iom = (1.732050808/c)*(e**2)*gammar*kap P = Iom*(c/(rho*rlc))/(2*pi) phps = P/(hcut*omega ) www = phps/(stepPHA*sin(zobs)*stepOB) myinternet.append(www) count = count + 1 oo = 0 for k in eel[:]: MYMAP1[i, j, k] = MYMAP1[i, j, k] + myinternet[oo] oo = oo + 1 for k in eel[:]: MYMAP2[i, j, k] = MYMAP2[i, j, k] + myinternet[oo] oo = oo + 1 for k in eel[:]: MYMAP3[i, j, k] = MYMAP3[i, j, k] + myinternet[oo] oo = oo + 1 for k in eel[:]: MYMAP4[i, j, k] = MYMAP4[i, j, k] + myinternet[oo] oo = oo + 1 for k in eel[:]: MYMAP5[i, j, k] = MYMAP5[i, j, k] + myinternet[oo] oo = oo + 1 BIGMAPS = [MYMAP1, MYMAP2, MYMAP3, MYMAP4, MYMAP5] sigmas = [1, 3, 5, 10, 30] fiq1 = plt.figure() fiq2 = plt.figure() fiq3 = plt.figure() fiq4 = plt.figure() fiq5 = plt.figure() fiqqs = [fiq1, fiq2, fiq3, fiq4, fiq5] multis = zip(sigmas, BIGMAPS, fiqqs) for sigma, MYMAP, fiq in multis: filename=alpha+'_'+str(sigma)+'_'+str(npha)+'_'+str(phamin)+'_'+str(phamax)+'_'+str(nobs)+'_'+str(obamin)+'_'+str(obamax)+'_'+str(nex)+'_'+str(Lemin)+'_'+str(Lemax)+'_.dat' MYfile = open(filename, 'a') for k in eel[:]: for j in indobs[:]: for i in indpha[:]: A=MYMAP[i, j, k] stringa = str(A) + ',' MYfile.write(stringa) accapo = '\n' MYfile.write(accapo) MYfile.close() 2014-04-10 19:55 GMT-04:00 Danny Yoo : > > > ncalls tottime percall cumtime percall filename:lineno(function) > > 1 149.479 149.479 199.851 199.851 skymaps5.py:16(mymain) > > 18101000 28.682 0.000 28.682 0.000 {method 'write' of 'file' > objects} > > > > 33044 5.470 0.000 6.444 0.000 > interpolate.py:394(_call_linear) > > 230000 2.272 0.000 21.279 0.000 instruments.py:10(kappa) > > 231328 2.120 0.000 2.120 0.000 > {numpy.core.multiarray.array} > > 33044 1.719 0.000 3.836 0.000 > interpolate.py:454(_check_bounds) > > 66088 1.611 0.000 1.611 0.000 {method 'reduce' of > 'numpy.ufunc' > > objects} > > 33044 1.146 0.000 11.623 0.000 > interpolate.py:443(_evaluate) > > 33044 1.120 0.000 5.542 0.000 > interpolate.py:330(__init__) > > 33044 0.659 0.000 2.329 0.000 polyint.py:82(_set_yi) > > > > the major time is required by mymain that is the whole program. > > Good! Profiles like this allow us to pinpoint issues. > > Wait... ?! > > The profiler is saying that the majority of time is in my main, but _not_ > in auxiliary functions. That's surprising. Am I misreading the profile? > > Can you show what mymain is doing? > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Fri Apr 11 02:10:53 2014 From: bgailer at gmail.com (bob gailer) Date: Thu, 10 Apr 2014 20:10:53 -0400 Subject: [Tutor] difference between expressions and statements In-Reply-To: References: <5346FAF5.6050709@gmail.com> Message-ID: <5347330D.10308@gmail.com> On 4/10/2014 5:48 PM, Jared Nielsen wrote: > > Thanks for the thorough answer, Bob. I now understand the difference. > Thanks for the ACK. It helps me remember I have something to contribute. From alan.gauld at btinternet.com Fri Apr 11 02:21:06 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 11 Apr 2014 01:21:06 +0100 Subject: [Tutor] Refining Code In-Reply-To: References: Message-ID: On 10/04/14 23:26, Saba Usmani wrote: > My task is : > A food vending machine accepts 10p, 20p, 50p and ?1 coins.... > */I have designed the following code, but would like to know how to make > it more efficient without making it too complex as I am a beginner Have you covered functions yet? If so you can use functions to remove a lot of duplication from the code which will make it clearer. But if you haven't covered functions then what you have is not too bad. Thee is one bad habit you should really avoid. You should not use input() in Python 2. It is a security risk and although your program is not going to be used in earnest anywhere its a bad habit to get into. Better to use raw_input() and then convert to a number using int() - or float if thats what you need. [In v3 raw_input has been renamed as input and the v2 input removed.] > this fine? Also, how do I add a loop to this so that once one product > has been dispensed the program asks the user if they would like to > continue and purchase another product? /* You should probably use a while loop. You could use this pattern: while True: # means loop forever display menu get choice if choice is Exit: break # drop out of the loop elif choice == .... The final thing is that your coin counting code could be made more concise by using a dictionary to store the mapping of menu choice(A-D) to value: coins = {'A': 0.1, 'B':0.2...} Rather than all the individual variables. Then the input code becomes: print "Please select which ...." choice = raw_input("Enter coin: ") insert_coins = coins[choice] Its a trade off of more code to define the data or more code to read the input. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From martin at linux-ip.net Fri Apr 11 02:59:15 2014 From: martin at linux-ip.net (Martin A. Brown) Date: Thu, 10 Apr 2014 17:59:15 -0700 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Gabriele, > but main is the program that contains everything. And, that is precisely the point of profiling the thing that contains 'everything'. Because the bottleneck is almost always somewher inside of 'everything'. But, you have to keep digging until you find it. I saw that you replied to Danny Yoo with your code, and I have to say that this is rather domain-specific, so it may be quite difficult for somebody to glance at it and figure out where the hotspot is. It is for this reason that we were asking about profiling. Some follow-on questions: Code: for my_line in open('datasm0_60_5s.dat') Q: How big is datasm0_60_5s.dat? Unless there's a whitespace pasting issue, it looks like you are reading that file for each run through mymain(). Has this file changed in size recently? Code: kap = instruments.kappa(x) Q: What is instruments? A module? Is the performance hit there? Code: for k in eel[:]: MYMAP1[i, j, k] = MYMAP1[i, j, k] + myinternet[oo] oo = oo + 1 for k in eel[:]: MYMAP,[i, j, k] = MYMAP1[i, j, k] + myinternet[oo] oo = oo + 1 ... Comment: You are looping over your sliced eel five times. Do you need to? I like eel salad a great deal, as well, but, how about: for k in eel: MYMAP1[i, j, k] = MYMAP1[i, j, k] + myinternet[oo] MYMAP2[i, j, k] = MYMAP2[i, j, k] + myinternet[oo] MYMAP3[i, j, k] = MYMAP3[i, j, k] + myinternet[oo] MYMAP4[i, j, k] = MYMAP4[i, j, k] + myinternet[oo] MYMAP5[i, j, k] = MYMAP5[i, j, k] + myinternet[oo] oo = oo + 1 That should cut down a bit of looping time. Especially as the eel grows longer. Another suggestion, that is more along the lines of "how do I figure out what's broken this time in my code". I almost always add the logging module to any program larger than a few lines. Why? Because then, I can simply add logger lines and see what's going on. Since I'm a perfect programmer and, like you, I don't make mistakes, I never need this, but I do it anyway to look good around my colleagues (best practices and all). In seriousness, using logging [0] is not at all tricky for standalone scripts. (It does get a bit more involved when you are importing modules and libraries), but,) Consider the following: import sys import logging logformat='%(asctime)s %(name)s %(levelname)s %(message)s' logging.basicConfig(format=logformat, stream=sys.stderr, level=logging.INFO) logger = logging.getLogger({ '__main__': None }.get(__name__, __name__)) With that setup at the top of the program, now you can sprinkle lines like this throughout your code with impunity. import os # -- calling logger.info() will print stuff to STDERR logger.info("silly example %r", os.environ) # -- calling logger.debug() will not print to STDERR # using, above config logger.debug("debug example %d", 1) # -- Ok, set it so anything that is set to logging.DEBUG (or # higher) is shown logger.setLevel(logging.DEBUG) logger.debug("debug example %d", 2) # -- and restore the prior pattern; restting so newer .debug lines # are not shown logger.setLevel(logging.INFO) logger.debug("debug example %d", 3) OK, so why is this useful? Well, timestamps in log lines is one reason. Another reason is the typical diagnostic technique.... "What is the value of variable x, y, z, oo, text_contens Good luck tracking down your peformance issue! -Martin [0] https://docs.python.org/2/library/logging.html https://docs.python.org/3/library/logging.html -- Martin A. Brown http://linux-ip.net/ From steve at pearwood.info Fri Apr 11 02:59:05 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 11 Apr 2014 10:59:05 +1000 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: <20140411005904.GE11385@ando> On Thu, Apr 10, 2014 at 11:58:30AM -0400, Gabriele Brambilla wrote: > Hi, > > I have a program that is reading near 600000 elements from a file. > For each element it performs 200 times a particular mathematical operation > (a numerical interpolation of a function). > Now these process takes near 8 hours. Why are you repeating each operation 200 times? Surely you don't mean something like this? for element in elements_from_file(): for i in range(200): result = function(element) Before spending your time re-writing the function in C, it may help checking that there are no inefficencies in the code. Calculating the function may not be what is slowing your code down. It might help if you show us your code. -- Steven From steve at pearwood.info Fri Apr 11 03:02:05 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 11 Apr 2014 11:02:05 +1000 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: <20140411005904.GE11385@ando> References: <20140411005904.GE11385@ando> Message-ID: <20140411010205.GF11385@ando> On Fri, Apr 11, 2014 at 10:59:05AM +1000, Steven D'Aprano wrote: > It might help if you show us your code. Oops, never mind, I see you have done so. -- Steven From dyoo at hashcollision.org Fri Apr 11 03:21:10 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 10 Apr 2014 18:21:10 -0700 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Ok, good. There's a few things you'll want to fix in your mymain() in order for the profiler to work more effectively in pinpointing issues. 1. Move functionality outside of "if __name__ == '__main__':" At the moment, you've put the entire functionality of your program in the body of that if statement within mymain. That structure is probably not right. I see that this block is computing values for stepENE, nex, Lemin, Lemax, and, conditionally, freq. This should be lifted out into its own function. You should also note that, because 'freq' is computed conditionally, there are certain code paths in which your mymain() will fail. This is most likely a bad thing. Recommendation: have mymain() take in parameters. Move LEstep() toplevel. Restructure to: ################################################ import sys def mymain(stepENE, nex, Lemin, Lemax, freq): ## everything starting after "eel = list(range(nex))..." ## if __name__ == '__main__': if len(sys.argv)<=1: stepENE, nex, Lemin, Lemax = LEstep(200) elif len(sys.argv) <= 2: stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) else: stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) freq=float(sys.argv[2]) mymain(stepENE, nex, Lemin, Lemax, freq) ################################################ 2. Do not slice lists unless you really mean to do so. I see a lot of slices that do not look right. Examples like: ################################################ for k in eel[:]: # code cut ... ################################################ Just loop over eel. No copy necessary. This is doing a lot more memory copying over and over again. Instead: ################################################ for k in eel: # code cut ... ################################################ Do this everywhere you're creating slices with [:], unless you really need to copy. This is happening in multiple places in the code. 3. Be sure to remove dead variables. There are variables here that are not used. omegacliston is dead code, for example. You're appending to it, but doing nothing with its value. 4. Watch for repetitive code. There's something about the use of MYMAP1, MYMAP2, etc. that looks very suspicious. That is, the block here: ################################################ oo = 0 for k in eel: MYMAP1[i, j, k] = MYMAP1[i, j, k] + myinternet[oo] oo = oo + 1 for k in eel: MYMAP2[i, j, k] = MYMAP2[i, j, k] + myinternet[oo] oo = oo + 1 for k in eel: MYMAP3[i, j, k] = MYMAP3[i, j, k] + myinternet[oo] oo = oo + 1 for k in eel: MYMAP4[i, j, k] = MYMAP4[i, j, k] + myinternet[oo] oo = oo + 1 for k in eel: MYMAP5[i, j, k] = MYMAP5[i, j, k] + myinternet[oo] oo = oo + 1 ################################################ feels repetitive and strange. Martin Brown identifies this problem as well, so at least we're on the same page. Solving this problem takes a little bit of work. If you have five maps, with some kind of relationship, represent and use that. Ah. You're already doing this by representing BIGMAPS at reporting time. Then move the definition of an array of maps to the front of the code. Use a container holding those five maps in a single variable. Call it MYMAPS. ################################################ MYMAPS = [np.zeros([npha, nobs, nex], dtype=float), np.zeros([npha, nobs, nex], dtype=float), np.zeros([npha, nobs, nex], dtype=float), np.zeros([npha, nobs, nex], dtype=float), np.zeros([npha, nobs, nex], dtype=float)] ################################################ Wen you're tempted to say MYMAP1, use MYMAPS[0]. MYMAP2 --> MYMAPS[1], and so on. This will allow you to dissolve a lot of complexity out of the code. We'll see this in a moment. 5. Change the computational structure. The computation of MYMAPS being done after the processing of gmlis is not right. It's the whole reason why there's this awkward intermediate myinternet structure that's used just to fill in each MYMAP later. Do the processing as part of your earlier loop. We know that gmlis is exactly five elements long. Just say so: ################################################ gmlis = [my_parts[7], my_parts[8], my_parts[9], my_parts[10], my_parts[11]] ################################################ Once you have this, and once you have a definition of MYMAPS, then you can kill a lot of the code by doing a zip loop across them both: for gammar, MYMAP in zip(gmlis, MYMAPS): ## NOTE 1 omC = (1.5)*(gammar**3)*c/(rho*rlc) gig = omC*hcut/eVtoErg for w in eel: omega = (10**(w*stepENE+Lemin))*eVtoErg/hcut x = omega/omC kap = instruments.kappa(x) Iom = (1.732050808/c)*(e**2)*gammar*kap P = Iom*(c/(rho*rlc))/(2*pi) phps = P/(hcut*omega) www = phps/(stepPHA*sin(zobs)*stepOB) MYMAP[i, j, w] += www ## NOTE 2 This lets you get rid of the second half of your program, essentially. Rather than put intermediate values packed into myinternet, and then follow that with a loop that unpacks those values into MYMAP, just put the values in. There's no need for the intermediate myinternet accumulation variable. You know exactly where the values are supposed to go in MYMAP, so just do it! :P myinternet is a red herring: it does no useful work. Note: this generalization works if you start thinking of arrays rather than individual variables. As a general practice, if you find yourself naming variables as x_1, x_2, x_3, x_4, ... x_n and dealing with them case by case, you probably want to make an array instead and deal with it uniformly. From dyoo at hashcollision.org Fri Apr 11 03:38:17 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 10 Apr 2014 18:38:17 -0700 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: > Comment: You are looping over your sliced eel five times. Do you > need to? I like eel salad a great deal, as well, but, how about: > > > for k in eel: > MYMAP1[i, j, k] = MYMAP1[i, j, k] + myinternet[oo] > MYMAP2[i, j, k] = MYMAP2[i, j, k] + myinternet[oo] > MYMAP3[i, j, k] = MYMAP3[i, j, k] + myinternet[oo] > MYMAP4[i, j, k] = MYMAP4[i, j, k] + myinternet[oo] > MYMAP5[i, j, k] = MYMAP5[i, j, k] + myinternet[oo] > oo = oo + 1 Hi Gabriele, Also note that, when Martin looked at this part of the code, he unfortunately misinterpreted its effect; Martin's proposed rewrite here does not preserve the meaning of the original code. But rather than wag my finger at how Martin interpreted the code, I'd rather make the observation that this is a warning sign that the original code here was not easy to understand. From dyoo at hashcollision.org Fri Apr 11 04:05:21 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 10 Apr 2014 19:05:21 -0700 Subject: [Tutor] Refining Code In-Reply-To: References: Message-ID: Hi Saba, Do you see any similarities between each of the snack choices? Do you see any differences? (Did you happen to use copy-and-paste at any time when you wrote the program?) From gb.gabrielebrambilla at gmail.com Fri Apr 11 05:30:20 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Thu, 10 Apr 2014 23:30:20 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Hi Danny, I followed your suggestion. Tomorrow morning I will run this new version of the code. Now using a sample of 81 elements (instead of 600000) the profile returns: Thu Apr 10 23:25:59 2014 restats 18101188 function calls in 1218.626 seconds Ordered by: internal time List reduced from 13 to 10 due to restriction <10> ncalls tottime percall cumtime percall filename:lineno(function) 1 1015.803 1015.803 1218.334 1218.334 skymaps5.py:44(mymain) 18101000 202.490 0.000 202.490 0.000 {method 'write' of 'file' objects} 1 0.292 0.292 1218.626 1218.626 :1() 6 0.029 0.005 0.029 0.005 {open} 5 0.010 0.002 0.010 0.002 {method 'close' of 'file' objects} 81 0.002 0.000 0.002 0.000 {method 'split' of 'str' objects} 82 0.001 0.000 0.001 0.000 {zip} 1 0.000 0.000 0.000 0.000 function_base.py:8(linspace) 1 0.000 0.000 0.000 0.000 function_base.py:93(logspace) 5 0.000 0.000 0.000 0.000 {numpy.core.multiarray.zeros} Anyway I would like to try to speed it up using C functions (and maybe comparing the resuts of the two profile in the end) How can I do it now? Can I use Cython? Thanks Gabriele 2014-04-10 21:38 GMT-04:00 Danny Yoo : > > Comment: You are looping over your sliced eel five times. Do you > > need to? I like eel salad a great deal, as well, but, how about: > > > > > > for k in eel: > > MYMAP1[i, j, k] = MYMAP1[i, j, k] + myinternet[oo] > > MYMAP2[i, j, k] = MYMAP2[i, j, k] + myinternet[oo] > > MYMAP3[i, j, k] = MYMAP3[i, j, k] + myinternet[oo] > > MYMAP4[i, j, k] = MYMAP4[i, j, k] + myinternet[oo] > > MYMAP5[i, j, k] = MYMAP5[i, j, k] + myinternet[oo] > > oo = oo + 1 > > > Hi Gabriele, > > Also note that, when Martin looked at this part of the code, he > unfortunately misinterpreted its effect; Martin's proposed rewrite > here does not preserve the meaning of the original code. But rather > than wag my finger at how Martin interpreted the code, I'd rather make > the observation that this is a warning sign that the original code > here was not easy to understand. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Fri Apr 11 09:02:56 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Fri, 11 Apr 2014 00:02:56 -0700 Subject: [Tutor] Fwd: Refining Code In-Reply-To: References: Message-ID: Forwarding to tutor; need to sleep tonight. ---------- Forwarded message ---------- From: Saba Usmani Date: Thu, Apr 10, 2014 at 11:35 PM Subject: Re: [Tutor] Refining Code To: Danny Yoo Hi, Yes I did use copy and paste sometimes- is that bad? How could you tell and what are the similarities between the snacks- why? Thanks Saba Sent from my iPhone > On 11 Apr 2014, at 03:05, "Danny Yoo" wrote: > > Hi Saba, > > Do you see any similarities between each of the snack choices? Do you > see any differences? > > (Did you happen to use copy-and-paste at any time when you wrote the program?) From gregg.martinson at gmail.com Fri Apr 11 01:27:41 2014 From: gregg.martinson at gmail.com (Gregg Martinson) Date: Thu, 10 Apr 2014 18:27:41 -0500 Subject: [Tutor] How to make comparison work Message-ID: I have been working through a fairly simple process to teach myself python and I am running into a problem with a comparison. Can anyone tell me where I am going wrong? #!/usr/bin/env python class Team(object): code = "" opponents_debated=[] wins=0 losses=0 competitors=[] def __init__(self, code): self.code = code self.competitors.append(code) #self.school_teams.append(code) ####HERE'S THE LOGIC PROBLEM def havedebated(self, otherTeam): print (self.code, "compares ", otherTeam, "is in",self.competitors) if otherTeam in self.competitors: return 1 else: return 0 def giveCode(self): return self.code def debates(self,otherteam): self.competitors.append(otherteam) def make_team(code): team = Team(code) return team #MAIN Program# myTeamCodes = ["a", "aa", "b", "bb", "c", "cc", "d"] # Make teams myTeams = [] #list of teams for x in myTeamCodes: myteam = make_team(x) myTeams.append(myteam) for x in myTeams: x.print_team() for x in myTeams: for y in myTeams: affteam=x.giveCode() negteam=y.giveCode() print (affteam," vs. ",negteam) #have the two teams debated? if x.havedebated(negteam): print("they have debated...") continue else: print("DEBATE!") #NEVER HAPPENS! x.debates(negteam) thiscode=x.giveCode(); othercode=y.giveCode(); print(thiscode,"debates ",othercode) -- http://about.me/greggmartinson -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Apr 11 10:20:15 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 11 Apr 2014 09:20:15 +0100 Subject: [Tutor] How to make comparison work In-Reply-To: References: Message-ID: On 11/04/14 00:27, Gregg Martinson wrote: > I have been working through a fairly simple process to teach myself > python and I am running into a problem with a comparison. Can anyone > tell me where I am going wrong? > > #!/usr/bin/env python > > class Team(object): > code = "" > opponents_debated=[] > wins=0 > losses=0 > competitors=[] > All of these variables are class variables rather than instance variables. That means they are *shared* by all instances. > def __init__(self, code): > self.code = code > self.competitors.append(code) > #self.school_teams.append(code) So when you run init() you are changing the values for all your teams. I strongly suspect you want those variables inside init so that each object has its own value? > ####HERE'S THE LOGIC PROBLEM > def havedebated(self, otherTeam): > print (self.code, "compares ", otherTeam, "is in",self.competitors) > if otherTeam in self.competitors: > return 1 > else: > return 0 When you do the comparison you are checking against the shared collection which will, I think, have all the teams in it, so it will always be true. I haven't studied that in detail but that's what a quick glance suggests to me. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Fri Apr 11 10:32:21 2014 From: __peter__ at web.de (Peter Otten) Date: Fri, 11 Apr 2014 10:32:21 +0200 Subject: [Tutor] How to make comparison work References: Message-ID: Gregg Martinson wrote: > I have been working through a fairly simple process to teach myself python > and I am running into a problem with a comparison. Can anyone tell me > where I am going wrong? > > #!/usr/bin/env python > > class Team(object): > code = "" > opponents_debated=[] > wins=0 > losses=0 > competitors=[] Defining the 'competitors' list here means that it is shared by all Team instances. As soon as any team A has debated with a team B B is added to this list. As any team immediately adds itself to the list no debate will ever take place. Solution: instead of a class attribute make the list an instance attribute by moving the definition into the initialiser: > > def __init__(self, code): self.competitors = [] > self.code = code > self.competitors.append(code) > #self.school_teams.append(code) Note that the difference between class and instance attributes exists for all attributes, but may not lead to an error when you rebind instead of mutating the attribute: >>> class T: ... wins = 0 ... def win(self): ... self.wins = self.wins + 1 ... >>> a = T() >>> b = T() >>> a.win() >>> a.win() >>> b.win() >>> a.wins 2 >>> b.wins 1 >>> T.wins 0 That is because the first time win() is called on an instance self.wins = self.wins + 1 The instance attribute is not found and the right side falls back to look up self.wins in the class, i. e. the first time you are effectively running self.wins = T.wins + 1 The left-hand side always denotes an assignment to the instance, so T.wins will always remain 0. It is still good practice to define all attributes that are meant to be instance attributes in the initialiser: class Team: def __init__(self, code): self.wins = 0 self.losses = 0 ... From __peter__ at web.de Fri Apr 11 10:59:18 2014 From: __peter__ at web.de (Peter Otten) Date: Fri, 11 Apr 2014 10:59:18 +0200 Subject: [Tutor] improving speed using and recalling C functions References: Message-ID: Gabriele Brambilla wrote: > Anyway I would like to try to speed it up using C functions (and maybe > comparing the resuts of the two profile in the end) I can't help you on your chosen path, but let me emphasise that the code you posted looks like it has great potential for speed-up by replacing the inner loops with numpy array operations. If you post a small dataset somewhere and a version of the code that can run standalone (no undefined variables or libraries, no commandline arguments) I might even tinker with it myself to demonstrate this potential... From fomcl at yahoo.com Fri Apr 11 14:16:12 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 11 Apr 2014 05:16:12 -0700 (PDT) Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: <1397218572.22975.YahooMailNeo@web163801.mail.gq1.yahoo.com> ________________________________ > From: Gabriele Brambilla >To: Danny Yoo >Cc: python tutor >Sent: Friday, April 11, 2014 5:30 AM >Subject: Re: [Tutor] improving speed using and recalling C functions >?? > > >Hi Danny, >I followed your suggestion. >Tomorrow morning I will run this new version of the code. > > >Now using a sample of 81 elements (instead of 600000) the profile returns: > > >Thu Apr 10 23:25:59 2014 ? ?restats > > >? ? ? ? ?18101188 function calls in 1218.626 seconds > > >? ?Ordered by: internal time >? ?List reduced from 13 to 10 due to restriction <10> > > >? ?ncalls ?tottime ?percall ?cumtime ?percall filename:lineno(function) >? ? ? ? 1 1015.803 1015.803 1218.334 1218.334 skymaps5.py:44(mymain) >?18101000 ?202.490 ? ?0.000 ?202.490 ? ?0.000 {method 'write' of 'file' objects} > > >? ? ? ? 1 ? ?0.292 ? ?0.292 1218.626 1218.626 :1() >? ? ? ? 6 ? ?0.029 ? ?0.005 ? ?0.029 ? ?0.005 {open} >? ? ? ? 5 ? ?0.010 ? ?0.002 ? ?0.010 ? ?0.002 {method 'close' of 'file' objects} > > >? ? ? ?81 ? ?0.002 ? ?0.000 ? ?0.002 ? ?0.000 {method 'split' of 'str' objects} >? ? ? ?82 ? ?0.001 ? ?0.000 ? ?0.001 ? ?0.000 {zip} >? ? ? ? 1 ? ?0.000 ? ?0.000 ? ?0.000 ? ?0.000 function_base.py:8(linspace) >? ? ? ? 1 ? ?0.000 ? ?0.000 ? ?0.000 ? ?0.000 function_base.py:93(logspace) >? ? ? ? 5 ? ?0.000 ? ?0.000 ? ?0.000 ? ?0.000 {numpy.core.multiarray.zeros} > > >Anyway I would like to try to speed it up using C functions (and maybe comparing the resuts of the two profile in the end) >How can I do it now? Can I use Cython? If you have a compiler installed already it's just easy_install cython. Writing Cython is not hard. That is, you easily get speed improvements. It's in a .pyx file and once you're done you generate the .c and .so/.dll files with a setup.py like below. Reason why I am posting this snippet is the way to generate an annotated html file of your cython code. The whiter, the more in C, the better. Yellow means stuff might still be?improved more. ? * setup.py from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext import Cython.Compiler.Options Cython.Compiler.Options.annotate = True?? # <---- really handy setup( ??? cmdclass = {'build_ext': build_ext}, ??? ext_modules = [Extension("myModule", ["myModule.pyx"])] ) From gb.gabrielebrambilla at gmail.com Fri Apr 11 15:00:53 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Fri, 11 Apr 2014 09:00:53 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: <1397218572.22975.YahooMailNeo@web163801.mail.gq1.yahoo.com> References: <1397218572.22975.YahooMailNeo@web163801.mail.gq1.yahoo.com> Message-ID: I think I have Cython already installed with Anaconda. How it works? Thanks Gabriele 2014-04-11 8:16 GMT-04:00 Albert-Jan Roskam : > ________________________________ > > From: Gabriele Brambilla > >To: Danny Yoo > >Cc: python tutor > >Sent: Friday, April 11, 2014 5:30 AM > >Subject: Re: [Tutor] improving speed using and recalling C functions > > > > > > > >Hi Danny, > >I followed your suggestion. > >Tomorrow morning I will run this new version of the code. > > > > > >Now using a sample of 81 elements (instead of 600000) the profile returns: > > > > > >Thu Apr 10 23:25:59 2014 restats > > > > > > 18101188 function calls in 1218.626 seconds > > > > > > Ordered by: internal time > > List reduced from 13 to 10 due to restriction <10> > > > > > > ncalls tottime percall cumtime percall filename:lineno(function) > > 1 1015.803 1015.803 1218.334 1218.334 skymaps5.py:44(mymain) > > 18101000 202.490 0.000 202.490 0.000 {method 'write' of 'file' > objects} > > > > > > 1 0.292 0.292 1218.626 1218.626 :1() > > 6 0.029 0.005 0.029 0.005 {open} > > 5 0.010 0.002 0.010 0.002 {method 'close' of 'file' > objects} > > > > > > 81 0.002 0.000 0.002 0.000 {method 'split' of 'str' > objects} > > 82 0.001 0.000 0.001 0.000 {zip} > > 1 0.000 0.000 0.000 0.000 function_base.py:8(linspace) > > 1 0.000 0.000 0.000 0.000 > function_base.py:93(logspace) > > 5 0.000 0.000 0.000 0.000 > {numpy.core.multiarray.zeros} > > > > > >Anyway I would like to try to speed it up using C functions (and maybe > comparing the resuts of the two profile in the end) > >How can I do it now? Can I use Cython? > > If you have a compiler installed already it's just easy_install cython. > Writing Cython is not hard. That is, you easily get speed improvements. > It's in a .pyx file and once you're done you generate the .c and .so/.dll > files with a setup.py like below. Reason why I am posting this snippet is > the way to generate an annotated html file of your cython code. The whiter, > the more in C, the better. Yellow means stuff might still be improved more. > > * setup.py > from distutils.core import setup > from distutils.extension import Extension > from Cython.Distutils import build_ext > import Cython.Compiler.Options > Cython.Compiler.Options.annotate = True # <---- really handy > > setup( > cmdclass = {'build_ext': build_ext}, > ext_modules = [Extension("myModule", ["myModule.pyx"])] > > ) > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gb.gabrielebrambilla at gmail.com Fri Apr 11 15:20:07 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Fri, 11 Apr 2014 09:20:07 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Hi Danny, I'm quiet impressed. the program takes near 30 minutes instead of more than 8 hours! this is the profile: Fri Apr 11 09:14:04 2014 restats 19532732 function calls in 2105.024 seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno(function) 1 2087.606 2087.606 2105.006 2105.006 skymapsI.py:44(mymain) 18101000 12.757 0.000 12.757 0.000 {method 'write' of 'file' objects} 715853 3.473 0.000 3.473 0.000 {method 'split' of 'str' objects} 715854 1.162 0.000 1.162 0.000 {zip} 1 0.018 0.018 2105.024 2105.024 :1() 6 0.006 0.001 0.006 0.001 {open} 5 0.002 0.000 0.002 0.000 {method 'close' of 'file' objects} 1 0.000 0.000 0.000 0.000 function_base.py:8(linspace) 5 0.000 0.000 0.000 0.000 {numpy.core.multiarray.zeros} 1 0.000 0.000 0.000 0.000 function_base.py:93(logspace) 1 0.000 0.000 0.000 0.000 {numpy.core.multiarray.arange} 3 0.000 0.000 0.000 0.000 {range} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Prof iler' objects} I hope to have similar problems in the future to learn better how to do with them! but in the profile I don't see any operation regarding reading the file or the mathematical operations...are them hidden in mymain()? thanks Gabriele 2014-04-10 21:38 GMT-04:00 Danny Yoo : > > Comment: You are looping over your sliced eel five times. Do you > > need to? I like eel salad a great deal, as well, but, how about: > > > > > > for k in eel: > > MYMAP1[i, j, k] = MYMAP1[i, j, k] + myinternet[oo] > > MYMAP2[i, j, k] = MYMAP2[i, j, k] + myinternet[oo] > > MYMAP3[i, j, k] = MYMAP3[i, j, k] + myinternet[oo] > > MYMAP4[i, j, k] = MYMAP4[i, j, k] + myinternet[oo] > > MYMAP5[i, j, k] = MYMAP5[i, j, k] + myinternet[oo] > > oo = oo + 1 > > > Hi Gabriele, > > Also note that, when Martin looked at this part of the code, he > unfortunately misinterpreted its effect; Martin's proposed rewrite > here does not preserve the meaning of the original code. But rather > than wag my finger at how Martin interpreted the code, I'd rather make > the observation that this is a warning sign that the original code > here was not easy to understand. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gb.gabrielebrambilla at gmail.com Fri Apr 11 15:56:54 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Fri, 11 Apr 2014 09:56:54 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Hi, I'm sorry but there is a big problem. the code is producing empty file.dat. I think it's because of this that previously I have done that strange trick of myinternet... So: for my_line in open('data.dat'): myinternet = [] gmlis = [] print('reading the line', count, '/599378') my_parts = [float(i) for i in my_line.split()] phase = my_parts[4] zobs = my_parts[5] rho = my_parts[6] gmils=[my_parts[7], my_parts[8], my_parts[9], my_parts[10], my_parts[11]] i = int((phase-phamin)/stepPHA) j = int((zobs-obamin)/stepOB) for gammar, MYMAP in zip(gmlis, MYMAPS): omC = (1.5)*(gammar**3)*c/(rho*rlc) gig = omC*hcut/eVtoErg #check the single emission for w in eel: omega = (10**(w*stepENE+Lemin))*eVtoErg/hcut x = omega/omC kap = instruments.kappa(x) Iom = (1.732050808/c)*(e**2)*gammar*kap #jackson dI/domega P = Iom*(c/(rho*rlc))/(2*pi) #jackson P phps = P/(hcut*omega) #photons per second www = phps/(stepPHA*sin(zobs)*stepOB) MYMAP[i,j,w] += www count = count + 1 when I exit here the MYMAP matrix has all the cells = 0. Now I will try to fiugre it out why. Thanks Gabriele 2014-04-11 9:20 GMT-04:00 Gabriele Brambilla : > Hi Danny, > I'm quiet impressed. > the program takes near 30 minutes instead of more than 8 hours! > > this is the profile: > Fri Apr 11 09:14:04 2014 restats > > 19532732 function calls in 2105.024 seconds > > Ordered by: internal time > > ncalls tottime percall cumtime percall filename:lineno(function) > 1 2087.606 2087.606 2105.006 2105.006 skymapsI.py:44(mymain) > 18101000 12.757 0.000 12.757 0.000 {method 'write' of 'file' > objects} > > 715853 3.473 0.000 3.473 0.000 {method 'split' of 'str' > objects} > 715854 1.162 0.000 1.162 0.000 {zip} > 1 0.018 0.018 2105.024 2105.024 :1() > 6 0.006 0.001 0.006 0.001 {open} > 5 0.002 0.000 0.002 0.000 {method 'close' of 'file' > objects} > > 1 0.000 0.000 0.000 0.000 function_base.py:8(linspace) > 5 0.000 0.000 0.000 0.000 {numpy.core.multiarray.zeros} > 1 0.000 0.000 0.000 0.000 function_base.py:93(logspace) > 1 0.000 0.000 0.000 0.000 > {numpy.core.multiarray.arange} > 3 0.000 0.000 0.000 0.000 {range} > 1 0.000 0.000 0.000 0.000 {method 'disable' of > '_lsprof.Prof > iler' objects} > > I hope to have similar problems in the future to learn better how to do > with them! > but in the profile I don't see any operation regarding reading the file or > the mathematical operations...are them hidden in mymain()? > > thanks > > Gabriele > > > > > 2014-04-10 21:38 GMT-04:00 Danny Yoo : > > > Comment: You are looping over your sliced eel five times. Do you >> > need to? I like eel salad a great deal, as well, but, how about: >> > >> > >> > for k in eel: >> > MYMAP1[i, j, k] = MYMAP1[i, j, k] + myinternet[oo] >> > MYMAP2[i, j, k] = MYMAP2[i, j, k] + myinternet[oo] >> > MYMAP3[i, j, k] = MYMAP3[i, j, k] + myinternet[oo] >> > MYMAP4[i, j, k] = MYMAP4[i, j, k] + myinternet[oo] >> > MYMAP5[i, j, k] = MYMAP5[i, j, k] + myinternet[oo] >> > oo = oo + 1 >> >> >> Hi Gabriele, >> >> Also note that, when Martin looked at this part of the code, he >> unfortunately misinterpreted its effect; Martin's proposed rewrite >> here does not preserve the meaning of the original code. But rather >> than wag my finger at how Martin interpreted the code, I'd rather make >> the observation that this is a warning sign that the original code >> here was not easy to understand. >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gb.gabrielebrambilla at gmail.com Fri Apr 11 16:05:18 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Fri, 11 Apr 2014 10:05:18 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: ok, it seems that the code don't enter in this for loop for gammar, MYMAP in zip(gmlis, MYMAPS): I don't understand why. Thanks Gabriele 2014-04-11 9:56 GMT-04:00 Gabriele Brambilla : > Hi, I'm sorry but there is a big problem. > the code is producing empty file.dat. > > I think it's because of this that previously I have done that strange > trick of myinternet... > > So: > > for my_line in open('data.dat'): > > myinternet = [] > > gmlis = [] > > print('reading the line', count, '/599378') > > my_parts = [float(i) for i in my_line.split()] > > phase = my_parts[4] > > zobs = my_parts[5] > > rho = my_parts[6] > > > > gmils=[my_parts[7], my_parts[8], my_parts[9], > my_parts[10], my_parts[11]] > > > > i = int((phase-phamin)/stepPHA) > > j = int((zobs-obamin)/stepOB) > > > > for gammar, MYMAP in zip(gmlis, MYMAPS): > > > > omC = (1.5)*(gammar**3)*c/(rho*rlc) > > gig = omC*hcut/eVtoErg > > #check the single emission > > > > for w in eel: > > omega = > (10**(w*stepENE+Lemin))*eVtoErg/hcut > > x = omega/omC > > kap = instruments.kappa(x) > > Iom = (1.732050808/c)*(e**2)*gammar*kap > #jackson dI/domega > > P = Iom*(c/(rho*rlc))/(2*pi) #jackson P > > phps = P/(hcut*omega) #photons per second > > www = phps/(stepPHA*sin(zobs)*stepOB) > > MYMAP[i,j,w] += www > > > > count = count + 1 > > when I exit here the MYMAP matrix has all the cells = 0. > > Now I will try to fiugre it out why. > > Thanks > > Gabriele > > > > 2014-04-11 9:20 GMT-04:00 Gabriele Brambilla < > gb.gabrielebrambilla at gmail.com>: > > Hi Danny, >> I'm quiet impressed. >> the program takes near 30 minutes instead of more than 8 hours! >> >> this is the profile: >> Fri Apr 11 09:14:04 2014 restats >> >> 19532732 function calls in 2105.024 seconds >> >> Ordered by: internal time >> >> ncalls tottime percall cumtime percall filename:lineno(function) >> 1 2087.606 2087.606 2105.006 2105.006 skymapsI.py:44(mymain) >> 18101000 12.757 0.000 12.757 0.000 {method 'write' of 'file' >> objects} >> >> 715853 3.473 0.000 3.473 0.000 {method 'split' of 'str' >> objects} >> 715854 1.162 0.000 1.162 0.000 {zip} >> 1 0.018 0.018 2105.024 2105.024 :1() >> 6 0.006 0.001 0.006 0.001 {open} >> 5 0.002 0.000 0.002 0.000 {method 'close' of 'file' >> objects} >> >> 1 0.000 0.000 0.000 0.000 function_base.py:8(linspace) >> 5 0.000 0.000 0.000 0.000 >> {numpy.core.multiarray.zeros} >> 1 0.000 0.000 0.000 0.000 >> function_base.py:93(logspace) >> 1 0.000 0.000 0.000 0.000 >> {numpy.core.multiarray.arange} >> 3 0.000 0.000 0.000 0.000 {range} >> 1 0.000 0.000 0.000 0.000 {method 'disable' of >> '_lsprof.Prof >> iler' objects} >> >> I hope to have similar problems in the future to learn better how to do >> with them! >> but in the profile I don't see any operation regarding reading the file >> or the mathematical operations...are them hidden in mymain()? >> >> thanks >> >> Gabriele >> >> >> >> >> 2014-04-10 21:38 GMT-04:00 Danny Yoo : >> >> > Comment: You are looping over your sliced eel five times. Do you >>> > need to? I like eel salad a great deal, as well, but, how about: >>> > >>> > >>> > for k in eel: >>> > MYMAP1[i, j, k] = MYMAP1[i, j, k] + myinternet[oo] >>> > MYMAP2[i, j, k] = MYMAP2[i, j, k] + myinternet[oo] >>> > MYMAP3[i, j, k] = MYMAP3[i, j, k] + myinternet[oo] >>> > MYMAP4[i, j, k] = MYMAP4[i, j, k] + myinternet[oo] >>> > MYMAP5[i, j, k] = MYMAP5[i, j, k] + myinternet[oo] >>> > oo = oo + 1 >>> >>> >>> Hi Gabriele, >>> >>> Also note that, when Martin looked at this part of the code, he >>> unfortunately misinterpreted its effect; Martin's proposed rewrite >>> here does not preserve the meaning of the original code. But rather >>> than wag my finger at how Martin interpreted the code, I'd rather make >>> the observation that this is a warning sign that the original code >>> here was not easy to understand. >>> >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gb.gabrielebrambilla at gmail.com Fri Apr 11 16:18:23 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Fri, 11 Apr 2014 10:18:23 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: ok modifying the for in this way (zipping an array of matrix drive it crazy) it works dko=0 for gammar in gmils: omC = (1.5)*(gammar**3)*c/(rho*rlc) gig = omC*hcut/eVtoErg #check the single emission for w in eel: omega = (10**(w*stepENE+Lemin))*eVtoErg/hcut x = omega/omC kap = instruments.kappa(x) Iom = (1.732050808/c)*(e**2)*gammar*kap #jackson dI/domega P = Iom*(c/(rho*rlc))/(2*pi) #jackson P phps = P/(hcut*omega) #photons per second www = phps/(stepPHA*sin(zobs)*stepOB) MYMAPS[dko][i,j,w] += www dko += 1 count = count + 1 Now I will tell you how much it takes. Thanks Gabriele 2014-04-11 10:05 GMT-04:00 Gabriele Brambilla < gb.gabrielebrambilla at gmail.com>: > ok, it seems that the code don't enter in this for loop > > for gammar, MYMAP in zip(gmlis, MYMAPS): > > I don't understand why. > > Thanks > > Gabriele > > > 2014-04-11 9:56 GMT-04:00 Gabriele Brambilla < > gb.gabrielebrambilla at gmail.com>: > > Hi, I'm sorry but there is a big problem. >> the code is producing empty file.dat. >> >> I think it's because of this that previously I have done that strange >> trick of myinternet... >> >> So: >> >> for my_line in open('data.dat'): >> >> myinternet = [] >> >> gmlis = [] >> >> print('reading the line', count, '/599378') >> >> my_parts = [float(i) for i in my_line.split()] >> >> phase = my_parts[4] >> >> zobs = my_parts[5] >> >> rho = my_parts[6] >> >> >> >> gmils=[my_parts[7], my_parts[8], my_parts[9], >> my_parts[10], my_parts[11]] >> >> >> >> i = int((phase-phamin)/stepPHA) >> >> j = int((zobs-obamin)/stepOB) >> >> >> >> for gammar, MYMAP in zip(gmlis, MYMAPS): >> >> >> >> omC = (1.5)*(gammar**3)*c/(rho*rlc) >> >> gig = omC*hcut/eVtoErg >> >> #check the single emission >> >> >> >> for w in eel: >> >> omega = >> (10**(w*stepENE+Lemin))*eVtoErg/hcut >> >> x = omega/omC >> >> kap = instruments.kappa(x) >> >> Iom = (1.732050808/c)*(e**2)*gammar*kap >> #jackson dI/domega >> >> P = Iom*(c/(rho*rlc))/(2*pi) #jackson P >> >> phps = P/(hcut*omega) #photons per second >> >> www = phps/(stepPHA*sin(zobs)*stepOB) >> >> MYMAP[i,j,w] += www >> >> >> >> count = count + 1 >> >> when I exit here the MYMAP matrix has all the cells = 0. >> >> Now I will try to fiugre it out why. >> >> Thanks >> >> Gabriele >> >> >> >> 2014-04-11 9:20 GMT-04:00 Gabriele Brambilla < >> gb.gabrielebrambilla at gmail.com>: >> >> Hi Danny, >>> I'm quiet impressed. >>> the program takes near 30 minutes instead of more than 8 hours! >>> >>> this is the profile: >>> Fri Apr 11 09:14:04 2014 restats >>> >>> 19532732 function calls in 2105.024 seconds >>> >>> Ordered by: internal time >>> >>> ncalls tottime percall cumtime percall filename:lineno(function) >>> 1 2087.606 2087.606 2105.006 2105.006 skymapsI.py:44(mymain) >>> 18101000 12.757 0.000 12.757 0.000 {method 'write' of 'file' >>> objects} >>> >>> 715853 3.473 0.000 3.473 0.000 {method 'split' of 'str' >>> objects} >>> 715854 1.162 0.000 1.162 0.000 {zip} >>> 1 0.018 0.018 2105.024 2105.024 :1() >>> 6 0.006 0.001 0.006 0.001 {open} >>> 5 0.002 0.000 0.002 0.000 {method 'close' of 'file' >>> objects} >>> >>> 1 0.000 0.000 0.000 0.000 >>> function_base.py:8(linspace) >>> 5 0.000 0.000 0.000 0.000 >>> {numpy.core.multiarray.zeros} >>> 1 0.000 0.000 0.000 0.000 >>> function_base.py:93(logspace) >>> 1 0.000 0.000 0.000 0.000 >>> {numpy.core.multiarray.arange} >>> 3 0.000 0.000 0.000 0.000 {range} >>> 1 0.000 0.000 0.000 0.000 {method 'disable' of >>> '_lsprof.Prof >>> iler' objects} >>> >>> I hope to have similar problems in the future to learn better how to do >>> with them! >>> but in the profile I don't see any operation regarding reading the file >>> or the mathematical operations...are them hidden in mymain()? >>> >>> thanks >>> >>> Gabriele >>> >>> >>> >>> >>> 2014-04-10 21:38 GMT-04:00 Danny Yoo : >>> >>> > Comment: You are looping over your sliced eel five times. Do you >>>> > need to? I like eel salad a great deal, as well, but, how about: >>>> > >>>> > >>>> > for k in eel: >>>> > MYMAP1[i, j, k] = MYMAP1[i, j, k] + myinternet[oo] >>>> > MYMAP2[i, j, k] = MYMAP2[i, j, k] + myinternet[oo] >>>> > MYMAP3[i, j, k] = MYMAP3[i, j, k] + myinternet[oo] >>>> > MYMAP4[i, j, k] = MYMAP4[i, j, k] + myinternet[oo] >>>> > MYMAP5[i, j, k] = MYMAP5[i, j, k] + myinternet[oo] >>>> > oo = oo + 1 >>>> >>>> >>>> Hi Gabriele, >>>> >>>> Also note that, when Martin looked at this part of the code, he >>>> unfortunately misinterpreted its effect; Martin's proposed rewrite >>>> here does not preserve the meaning of the original code. But rather >>>> than wag my finger at how Martin interpreted the code, I'd rather make >>>> the observation that this is a warning sign that the original code >>>> here was not easy to understand. >>>> >>> >>> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gb.gabrielebrambilla at gmail.com Fri Apr 11 16:23:47 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Fri, 11 Apr 2014 10:23:47 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: this is the profile for a sample of 1000 elements Fri Apr 11 10:21:21 2014 restats 31594963 function calls in 103.708 seconds Ordered by: internal time List reduced from 47 to 20 due to restriction <20> ncalls tottime percall cumtime percall filename:lineno(function) 1 57.133 57.133 103.692 103.692 skymapsI.py:44(mymain) 176832 9.898 0.000 11.710 0.000 interpolate.py:394(_call_linear) 18101000 7.808 0.000 7.808 0.000 {method 'write' of 'file' objects} 1237824 3.794 0.000 3.794 0.000 {numpy.core.multiarray.array} 1001000 3.610 0.000 38.383 0.000 instruments.py:10(kappa) 353664 3.314 0.000 3.314 0.000 {method 'reduce' of 'numpy.ufunc' objects} 176832 3.157 0.000 7.428 0.000 interpolate.py:454(_check_bounds) 176832 2.074 0.000 10.213 0.000 interpolate.py:330(__init__) 176832 2.053 0.000 21.522 0.000 interpolate.py:443(_evaluate) 176832 1.253 0.000 4.404 0.000 polyint.py:82(_set_yi) 176832 0.769 0.000 0.769 0.000 {method 'clip' of 'numpy.ndarray' objects} 353664 0.706 0.000 0.706 0.000 {method 'reshape' of 'numpy.ndarra y' objects} 353664 0.667 0.000 1.205 0.000 numerictypes.py:735(issubdtype) 707328 0.637 0.000 2.451 0.000 numeric.py:392(asarray) 176832 0.601 0.000 23.555 0.000 polyint.py:37(__call__) 353664 0.569 0.000 3.883 0.000 _methods.py:31(_any) 176832 0.504 0.000 1.429 0.000 polyint.py:74(_reshape_yi) 176832 0.473 0.000 0.473 0.000 {method 'searchsorted' of 'numpy.n darray' objects} 176832 0.440 0.000 1.645 0.000 polyint.py:102(_set_dtype) 176832 0.426 0.000 4.830 0.000 polyint.py:30(__init__) thanks Gabriele 2014-04-11 10:18 GMT-04:00 Gabriele Brambilla < gb.gabrielebrambilla at gmail.com>: > ok > modifying the for in this way (zipping an array of matrix drive it crazy) > it works > > dko=0 > > for gammar in gmils: > > > omC = (1.5)*(gammar**3)*c/(rho*rlc) > > gig = omC*hcut/eVtoErg > > > #check the single emission > > > > for w in eel: > > omega = > (10**(w*stepENE+Lemin))*eVtoErg/hcut > > x = omega/omC > > kap = instruments.kappa(x) > > Iom = (1.732050808/c)*(e**2)*gammar*kap > #jackson dI/domega > > P = Iom*(c/(rho*rlc))/(2*pi) #jackson P > > phps = P/(hcut*omega) #photons per second > > www = phps/(stepPHA*sin(zobs)*stepOB) > > MYMAPS[dko][i,j,w] += www > > dko += 1 > > > > count = count + 1 > > Now I will tell you how much it takes. > > Thanks > > Gabriele > > > > 2014-04-11 10:05 GMT-04:00 Gabriele Brambilla < > gb.gabrielebrambilla at gmail.com>: > > ok, it seems that the code don't enter in this for loop >> >> for gammar, MYMAP in zip(gmlis, MYMAPS): >> >> I don't understand why. >> >> Thanks >> >> Gabriele >> >> >> 2014-04-11 9:56 GMT-04:00 Gabriele Brambilla < >> gb.gabrielebrambilla at gmail.com>: >> >> Hi, I'm sorry but there is a big problem. >>> the code is producing empty file.dat. >>> >>> I think it's because of this that previously I have done that strange >>> trick of myinternet... >>> >>> So: >>> >>> for my_line in open('data.dat'): >>> >>> myinternet = [] >>> >>> gmlis = [] >>> >>> print('reading the line', count, '/599378') >>> >>> my_parts = [float(i) for i in my_line.split()] >>> >>> phase = my_parts[4] >>> >>> zobs = my_parts[5] >>> >>> rho = my_parts[6] >>> >>> >>> >>> gmils=[my_parts[7], my_parts[8], my_parts[9], >>> my_parts[10], my_parts[11]] >>> >>> >>> >>> i = int((phase-phamin)/stepPHA) >>> >>> j = int((zobs-obamin)/stepOB) >>> >>> >>> >>> for gammar, MYMAP in zip(gmlis, MYMAPS): >>> >>> >>> >>> omC = (1.5)*(gammar**3)*c/(rho*rlc) >>> >>> gig = omC*hcut/eVtoErg >>> >>> #check the single emission >>> >>> >>> >>> for w in eel: >>> >>> omega = >>> (10**(w*stepENE+Lemin))*eVtoErg/hcut >>> >>> x = omega/omC >>> >>> kap = instruments.kappa(x) >>> >>> >>> Iom = (1.732050808/c)*(e**2)*gammar*kap >>> #jackson dI/domega >>> >>> P = Iom*(c/(rho*rlc))/(2*pi) #jackson P >>> >>> phps = P/(hcut*omega) #photons per second >>> >>> www = phps/(stepPHA*sin(zobs)*stepOB) >>> >>> MYMAP[i,j,w] += www >>> >>> >>> >>> count = count + 1 >>> >>> when I exit here the MYMAP matrix has all the cells = 0. >>> >>> Now I will try to fiugre it out why. >>> >>> Thanks >>> >>> Gabriele >>> >>> >>> >>> 2014-04-11 9:20 GMT-04:00 Gabriele Brambilla < >>> gb.gabrielebrambilla at gmail.com>: >>> >>> Hi Danny, >>>> I'm quiet impressed. >>>> the program takes near 30 minutes instead of more than 8 hours! >>>> >>>> this is the profile: >>>> Fri Apr 11 09:14:04 2014 restats >>>> >>>> 19532732 function calls in 2105.024 seconds >>>> >>>> Ordered by: internal time >>>> >>>> ncalls tottime percall cumtime percall filename:lineno(function) >>>> 1 2087.606 2087.606 2105.006 2105.006 skymapsI.py:44(mymain) >>>> 18101000 12.757 0.000 12.757 0.000 {method 'write' of 'file' >>>> objects} >>>> >>>> 715853 3.473 0.000 3.473 0.000 {method 'split' of 'str' >>>> objects} >>>> 715854 1.162 0.000 1.162 0.000 {zip} >>>> 1 0.018 0.018 2105.024 2105.024 :1() >>>> 6 0.006 0.001 0.006 0.001 {open} >>>> 5 0.002 0.000 0.002 0.000 {method 'close' of 'file' >>>> objects} >>>> >>>> 1 0.000 0.000 0.000 0.000 >>>> function_base.py:8(linspace) >>>> 5 0.000 0.000 0.000 0.000 >>>> {numpy.core.multiarray.zeros} >>>> 1 0.000 0.000 0.000 0.000 >>>> function_base.py:93(logspace) >>>> 1 0.000 0.000 0.000 0.000 >>>> {numpy.core.multiarray.arange} >>>> 3 0.000 0.000 0.000 0.000 {range} >>>> 1 0.000 0.000 0.000 0.000 {method 'disable' of >>>> '_lsprof.Prof >>>> iler' objects} >>>> >>>> I hope to have similar problems in the future to learn better how to do >>>> with them! >>>> but in the profile I don't see any operation regarding reading the file >>>> or the mathematical operations...are them hidden in mymain()? >>>> >>>> thanks >>>> >>>> Gabriele >>>> >>>> >>>> >>>> >>>> 2014-04-10 21:38 GMT-04:00 Danny Yoo : >>>> >>>> > Comment: You are looping over your sliced eel five times. Do you >>>>> > need to? I like eel salad a great deal, as well, but, how about: >>>>> > >>>>> > >>>>> > for k in eel: >>>>> > MYMAP1[i, j, k] = MYMAP1[i, j, k] + myinternet[oo] >>>>> > MYMAP2[i, j, k] = MYMAP2[i, j, k] + myinternet[oo] >>>>> > MYMAP3[i, j, k] = MYMAP3[i, j, k] + myinternet[oo] >>>>> > MYMAP4[i, j, k] = MYMAP4[i, j, k] + myinternet[oo] >>>>> > MYMAP5[i, j, k] = MYMAP5[i, j, k] + myinternet[oo] >>>>> > oo = oo + 1 >>>>> >>>>> >>>>> Hi Gabriele, >>>>> >>>>> Also note that, when Martin looked at this part of the code, he >>>>> unfortunately misinterpreted its effect; Martin's proposed rewrite >>>>> here does not preserve the meaning of the original code. But rather >>>>> than wag my finger at how Martin interpreted the code, I'd rather make >>>>> the observation that this is a warning sign that the original code >>>>> here was not easy to understand. >>>>> >>>> >>>> >>> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Apr 11 20:47:24 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 11 Apr 2014 19:47:24 +0100 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: On 11/04/14 09:59, Peter Otten wrote: > Gabriele Brambilla wrote: > >> Anyway I would like to try to speed it up using C functions >... > posted looks like it has great potential for speed-up by replacing the inner > loops with numpy array operations. And in case its not obvious much(most?) of numPy consists of C functions. So by using NumPy you are usually using C code not native Python. That's what I alluded to in my first post on this thread: there are other libraries who have trod this route before and done the work for you. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From dinoandoni at yahoo.com Fri Apr 11 19:13:25 2014 From: dinoandoni at yahoo.com (Andoni Gorostiza) Date: Fri, 11 Apr 2014 10:13:25 -0700 (PDT) Subject: [Tutor] Range within a range Message-ID: <1397236405.7785.YahooMailNeo@web163006.mail.bf1.yahoo.com> Hi tutor. I need your help with something I don't understand. In the tutorial, it mentions an example of a range within a range. I'll keep it simplified. How exactly does this work? I'll provide a few examples. >>> for x in range(0,5): ...for n in range(0,5): ...? ? ? ? ? print(x) >>> for x in range(0,5): ...for n in range(0,5) ...? ? ? ? ? print(n) This one comes from the tutorial: >>> for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print(n, 'equals', x, '*', n//x) ... break ... else: ... # loop fell through without finding a factor ... print(n, 'is a prime number') ... 2 is a prime number 3 is a prime number 4 equals 2 * 2 5 is a prime number 6 equals 2 * 3 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3 Can you explain what is going on? -------------- next part -------------- An HTML attachment was scrubbed... URL: From gregg.martinson at gmail.com Fri Apr 11 13:47:07 2014 From: gregg.martinson at gmail.com (Gregg Martinson) Date: Fri, 11 Apr 2014 06:47:07 -0500 Subject: [Tutor] How to make comparison work In-Reply-To: References: Message-ID: Excellent. I guess I never read through the class stuff in learning python(its a great book, but very detailed....) Now I know better! -- http://about.me/greggmartinson On Fri, Apr 11, 2014 at 3:32 AM, Peter Otten <__peter__ at web.de> wrote: > Gregg Martinson wrote: > > > I have been working through a fairly simple process to teach myself > python > > and I am running into a problem with a comparison. Can anyone tell me > > where I am going wrong? > > > > #!/usr/bin/env python > > > > class Team(object): > > code = "" > > opponents_debated=[] > > wins=0 > > losses=0 > > competitors=[] > > Defining the 'competitors' list here means that it is shared by all Team > instances. As soon as any team A has debated with a team B B is added to > this list. As any team immediately adds itself to the list no debate will > ever take place. > > Solution: instead of a class attribute make the list an instance attribute > by moving the definition into the initialiser: > > > > > def __init__(self, code): > self.competitors = [] > > self.code = code > > self.competitors.append(code) > > #self.school_teams.append(code) > > Note that the difference between class and instance attributes exists for > all attributes, but may not lead to an error when you rebind instead of > mutating the attribute: > > >>> class T: > ... wins = 0 > ... def win(self): > ... self.wins = self.wins + 1 > ... > >>> a = T() > >>> b = T() > >>> a.win() > >>> a.win() > >>> b.win() > >>> a.wins > 2 > >>> b.wins > 1 > >>> T.wins > 0 > > That is because the first time win() is called on an instance > > self.wins = self.wins + 1 > > The instance attribute is not found and the right side falls back to look > up > self.wins in the class, i. e. the first time you are effectively running > > self.wins = T.wins + 1 > > The left-hand side always denotes an assignment to the instance, so T.wins > will always remain 0. > > It is still good practice to define all attributes that are meant to be > instance attributes in the initialiser: > > class Team: > def __init__(self, code): > self.wins = 0 > self.losses = 0 > ... > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Fri Apr 11 21:30:23 2014 From: bgailer at gmail.com (bob gailer) Date: Fri, 11 Apr 2014 15:30:23 -0400 Subject: [Tutor] Range within a range In-Reply-To: <1397236405.7785.YahooMailNeo@web163006.mail.bf1.yahoo.com> References: <1397236405.7785.YahooMailNeo@web163006.mail.bf1.yahoo.com> Message-ID: <534842CF.3050503@gmail.com> On 4/11/2014 1:13 PM, Andoni Gorostiza wrote: > Hi tutor. I need your help with something I don't understand. In the > tutorial, it mentions an example of a range within a range. I'll keep > it simplified. How exactly does this work? I'll provide a few examples. > > >>> for x in range(0,5): > ...for n in range(0,5): > ... print(x) > > >>> for x in range(0,5): > ...for n in range(0,5) > ... print(n) > > This one comes from the tutorial: > > >>>for n in range(2, 10): > ... for x in range(2, n): > ... if n % x == 0: > ... print(n, 'equals', x, '*', n//x) > ... break > ... else: > ... # loop fell through without finding a factor > ... print(n, 'is a prime number') > ... > 2 is a prime number > 3 is a prime number > 4 equals 2 * 2 > 5 is a prime number > 6 equals 2 * 3 > 7 is a prime number > 8 equals 2 * 4 > 9 equals 3 * 3 > Can you explain what is going on? We could but that will not help you learn. Instead I recommend with pencil and paper you play computer - "execute" (write) the code one step at a time and write down what happens. Very simple example: execute: change: for x in range(0,2): x == 0 for n in range(0,2): n == 0 print(x) output == 0 next for n n == 1 print(x) output == 0 next for x x == 1 for n in range(0,2): n == 0 print(x) output == 1 keep going - at each step write what executes and what changes or happens When you run into an operation you don't understand stop and either - look it up - ask this list From __peter__ at web.de Fri Apr 11 21:35:47 2014 From: __peter__ at web.de (Peter Otten) Date: Fri, 11 Apr 2014 21:35:47 +0200 Subject: [Tutor] improving speed using and recalling C functions References: Message-ID: Gabriele Brambilla wrote: > ok, it seems that the code don't enter in this for loop > > for gammar, MYMAP in zip(gmlis, MYMAPS): > > I don't understand why. You have two variables with similar names, gmlis and gmils: >> gmlis = [] >> gmils=[my_parts[7], my_parts[8], my_parts[9], >> my_parts[10], my_parts[11]] >> for gammar, MYMAP in zip(gmlis, MYMAPS): I assume you wanted for gammar, MYMAP in zip(gmils, MYMAPS): From gb.gabrielebrambilla at gmail.com Fri Apr 11 22:01:57 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Fri, 11 Apr 2014 16:01:57 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Yes, but I want to make a C extension to run faster a function from scipy.interpolate (interp1d) It woulldn't change anything? thanks Gabriele 2014-04-11 14:47 GMT-04:00 Alan Gauld : > On 11/04/14 09:59, Peter Otten wrote: > >> Gabriele Brambilla wrote: >> >> Anyway I would like to try to speed it up using C functions >>> >> ... >> >> posted looks like it has great potential for speed-up by replacing the >> inner >> loops with numpy array operations. >> > > And in case its not obvious much(most?) of numPy consists > of C functions. So by using NumPy you are usually using > C code not native Python. > > That's what I alluded to in my first post on this thread: > there are other libraries who have trod this route before > and done the work for you. > > HTH > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gb.gabrielebrambilla at gmail.com Fri Apr 11 22:03:37 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Fri, 11 Apr 2014 16:03:37 -0400 Subject: [Tutor] Fwd: improving speed using and recalling C functions In-Reply-To: References: Message-ID: I forget the reply all ---------- Forwarded message ---------- From: Gabriele Brambilla Date: 2014-04-11 16:03 GMT-04:00 Subject: Re: [Tutor] improving speed using and recalling C functions To: Peter Otten <__peter__ at web.de> you are right. probably this is the problem. thanks Gabriele 2014-04-11 15:35 GMT-04:00 Peter Otten <__peter__ at web.de>: Gabriele Brambilla wrote: > > > ok, it seems that the code don't enter in this for loop > > > > for gammar, MYMAP in zip(gmlis, MYMAPS): > > > > I don't understand why. > > You have two variables with similar names, gmlis and gmils: > > >> gmlis = [] > > >> gmils=[my_parts[7], my_parts[8], my_parts[9], > >> my_parts[10], my_parts[11]] > > >> for gammar, MYMAP in zip(gmlis, MYMAPS): > > I assume you wanted > > for gammar, MYMAP in zip(gmils, MYMAPS): > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Fri Apr 11 23:00:23 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Fri, 11 Apr 2014 14:00:23 -0700 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: On Fri, Apr 11, 2014 at 1:01 PM, Gabriele Brambilla wrote: > Yes, > but I want to make a C extension to run faster a function from > scipy.interpolate (interp1d) Just to emphasis: I believe your goal should be: "I want to make my program fast." Your goal should probably not be: "I want to write a C extension". I'm not saying that writing a C extension is necessarily wrong, and it may be that writing a C extension will make your program fast. But this approach may not be the easiest or most maintainable approach to improving your program's performance. Using C is not without its costs and risks. As soon as you are in C territory, the seat belts are off. Just recall the craziness that happened this week with regards to programs written in low-level languages like C. Explicitly: http://heartbleed.com. If you are writing with C, you have to be very, very delicate with your code. Experts get it wrong, with severe consequences. This is why the focus on C extensions to get speed disturbs me so much: it assumes that C is a safe language to use. It's not, especially for beginners. We should strongly discourage low-level languages unless there is some overriding concern. For scientific calculations like the ones you are doing, you should place a premium on getting a right answer, and not just a fast answer. From dyoo at hashcollision.org Fri Apr 11 23:27:59 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Fri, 11 Apr 2014 14:27:59 -0700 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: On Fri, Apr 11, 2014 at 1:01 PM, Gabriele Brambilla wrote: > Yes, > but I want to make a C extension to run faster a function from > scipy.interpolate (interp1d) > > It woulldn't change anything? This is precisely why you want to drive your optimization based on what the profiler is telling you. Look at the profiler's output again, closely: --- 31594963 function calls in 103.708 seconds Ordered by: internal time List reduced from 47 to 20 due to restriction <20> ncalls tottime percall cumtime percall filename:lineno(function) 1 57.133 57.133 103.692 103.692 skymapsI.py:44(mymain) 176832 9.898 0.000 11.710 0.000 interpolate.py:394(_call_linear) 18101000 7.808 0.000 7.808 0.000 {method 'write' of 'file' objects} 1237824 3.794 0.000 3.794 0.000 {numpy.core.multiarray.array} 1001000 3.610 0.000 38.383 0.000 instruments.py:10(kappa) 353664 3.314 0.000 3.314 0.000 {method 'reduce' of 'numpy.ufunc' [cutting some content] --- About 8% of the time in your program is being spent in interpolate.py. But this is in SciPy code, so it is likely difficult to rewrite. Also note that the amount of time being spent on merely writing the output is about that much time too! That's what the profile is saying, qualitatively. And on the other hand, the code in skymapsI.mymain is still a target worthy of your attention. Compare how much time it was taking before we started investigating it. Before, it took 75% of the total runtime of your program. We improved upon that a lot with a few small changes. But it's still taking 55% of the total time of your program's running. If you look at the rest of the profiler's output, we know that everything else is fairly inconsequential. That's why we're pushing you to look at the data. Trust the profiler. Work on the thing that is contributing most to the cost of your program: continue trying to improve the code in skymapsI.mymain. I am fairly certain there is still some low-hanging fruit there. The profile you want to see, eventually, is one where the computation is being done mostly in numpy code. But as we can see now, the numpy code is barely contributing to the runtime. That's a situation that needs improvement. Peter Otten's offer to help you use NumPy more effectively is one you should take seriously. Good luck! From gb.gabrielebrambilla at gmail.com Sat Apr 12 01:41:48 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Fri, 11 Apr 2014 19:41:48 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Ok guys, when I wrote that email I was excited for the apparent speed increasing (it was jumping the bottleneck for loop for the reason peter otten outlined). Now, instead the changes, the speed is not improved (the code still running from this morning and it's at one forth of the dataset). What can I do to speed it up? Thanks Gabriele sent from Samsung Mobile Il giorno 11/apr/2014 17:00, "Danny Yoo" ha scritto: > On Fri, Apr 11, 2014 at 1:01 PM, Gabriele Brambilla > wrote: > > Yes, > > but I want to make a C extension to run faster a function from > > scipy.interpolate (interp1d) > > > Just to emphasis: I believe your goal should be: "I want to make my > program fast." > > Your goal should probably not be: "I want to write a C extension". > I'm not saying that writing a C extension is necessarily wrong, and it > may be that writing a C extension will make your program fast. But > this approach may not be the easiest or most maintainable approach to > improving your program's performance. > > Using C is not without its costs and risks. As soon as you are in C > territory, the seat belts are off. Just recall the craziness that > happened this week with regards to programs written in low-level > languages like C. Explicitly: http://heartbleed.com. If you are > writing with C, you have to be very, very delicate with your code. > Experts get it wrong, with severe consequences. > > This is why the focus on C extensions to get speed disturbs me so > much: it assumes that C is a safe language to use. It's not, > especially for beginners. We should strongly discourage low-level > languages unless there is some overriding concern. For scientific > calculations like the ones you are doing, you should place a premium > on getting a right answer, and not just a fast answer. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sabausmani at outlook.com Fri Apr 11 22:58:13 2014 From: sabausmani at outlook.com (Saba Usmani) Date: Fri, 11 Apr 2014 21:58:13 +0100 Subject: [Tutor] Creating an Invalid message for user Message-ID: Hi, I am meant to design code for a program that converts from binary number to decimal and vice versa. This is what i have so far: print "Welcome to the binary and decimal converter"loop = Truewhile loop: bord = raw_input("Enter b for binary or d decimal or exit to exit") if bord == "b": d = 0 b = 0 factor = 1; b = raw_input ("Enter Binary Number:") b=b.lstrip("0") b = int(b) while(b > 0): if((int(b) % 10) == 1): d += factor b /= 10 factor = factor * 2 print "The Decimal Number is: ", d elif bord == "d": x=0 n=int(input('Enter Decimal Number: ')) x=n k=[] # array while (n>0): a=int(float(n%2)) k.append(a) n=(n-a)/2 k.append(0) string="" for j in k[::-1]: string=string+str(j) print('The binary Number for %d is %s'%(x, string)) elif bord == "exit" : print "Goodbye" loop = False - This code does not recognize invalid inputs e.g in the binary to decimal conversion, if I enter 10021 it will not inform me,the user, that the input is invalid. The same problem occurs with the decimal to binary conversion - if i enter 123&&gf I am not told to try again with a valid input - how do I implement this in the code above ThanksSaba -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Sat Apr 12 02:09:10 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 12 Apr 2014 01:09:10 +0100 Subject: [Tutor] Creating an Invalid message for user In-Reply-To: References: Message-ID: On 11/04/2014 21:58, Saba Usmani wrote: > Hi, > > I am meant to design code for a program that converts from binary number > to decimal and vice versa. > > This is what i have so far: > > print "Welcome to the binary and decimal converter" > loop = True > while loop: > bord = raw_input("Enter b for binary or d decimal or exit to exit") > if bord == "b": > d = 0 > b = 0 > factor = 1; > b = raw_input ("Enter Binary Number:") > b=b.lstrip("0") > b = int(b) > while(b > 0): > if((int(b) % 10) == 1): > d += factor > b /= 10 > factor = factor * 2 > print "The Decimal Number is: ", d > elif bord == "d": > x=0 > n=int(input('Enter Decimal Number: ')) > x=n > k=[] # array > while (n>0): > a=int(float(n%2)) > k.append(a) > n=(n-a)/2 > k.append(0) > string="" > for j in k[::-1]: > string=string+str(j) > print('The binary Number for %d is %s'%(x, string)) > elif bord == "exit" : > print "Goodbye" > loop = False > > - This code does not recognize invalid inputs e.g in the binary to > decimal conversion, if I enter 10021 it will not inform me,the user, > that the input is invalid. The same problem occurs with the decimal to > binary conversion - if i enter 123&&gf I am not told to try again with a > valid input - how do I implement this in the code above > > Thanks > Saba > https://docs.python.org/3/tutorial/errors.html#handling-exceptions is as good a starting point as any. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From davea at davea.name Sat Apr 12 02:24:00 2014 From: davea at davea.name (Dave Angel) Date: Fri, 11 Apr 2014 20:24:00 -0400 (EDT) Subject: [Tutor] Creating an Invalid message for user References: Message-ID: Saba Usmani Wrote in message: > > You posted in html so I can't quote your code, but why aren't you using int() to convert in one call? Second argument is the base to be used. value = int ("10011", 2) othervalue = int ("234") # default to decimal -- DaveA From bgailer at gmail.com Sat Apr 12 03:13:02 2014 From: bgailer at gmail.com (bob gailer) Date: Fri, 11 Apr 2014 21:13:02 -0400 Subject: [Tutor] Refining Code In-Reply-To: References: Message-ID: <5348931E.1070202@gmail.com> On 4/10/2014 6:26 PM, Saba Usmani wrote: > > My task is : Welcome to the tutor list. In what school are you learning Python? What version of Python? What operating system? What do you use to write and run your code? What Python elements have you studied so far? Your code can be greatly simplified by the application of tuples, lists, dictionaries, functions and/or classes. Requests: post in plain text rather than html (no formatting). This guarantees that all of us will be able to read your posts with ease. Also reply in such a way that a copy goes to tutor at python.org, to keep all of us in the loop. The rest of my comments follow the relevant part of your post. Please also reply in similar fashion. Avoid as much as possible "top posting". > > A food vending machine accepts 10p, 20p, 50p and ?1 coins. One or more > coins are inserted and the current credit is calculated and displayed. > A product is selected from those available. The system checks to see > if there is enough credit to purchase the product chosen. If there is > not enough credit the system displays an error message. If there is > enough credit it dispenses the product, updates the credit available > and displays the remaining credit. Further selections can be made if > there is enough credit. The vending machine simulation should have > five products and prices. Design, code, test and evaluate a program > for this simulation. I am glad to see this problem appear again. Recently it was sent to me privately by someone who claimed it was not homework! Critique of the specification: it is a bit vague (imprecise). I assume the instructor has a broad tolerance for what is delivered. In a business environment I'd want it a lot more precise. Often I have to help the user accomplish this, as many users don't know how to do this. it is a good idea to first develop a sample dialog from the specification, review that with the user, then code to reproduce that sample. Do this. Either review it with the instructor or forget that step. > > I have designed the following code, but would like to know how to make > it more efficient without making it too complex as I am a beginner or > is this fine? Efficient? Do you mean execution time? With today's processor speeds that is rarely an issue to be concerned about when first writing code. Complexity does not necessarily create efficiency. > Also, how do I add a loop to this so that once one product has been > dispensed the program asks the user if they would like to continue and > purchase another product? Alan has given a suggestion already. Based on your code you already know how to use a while loop. What is mysterious about using it here? > > > Code: > > print "Welcome to Snack Attack" > > snack1 = 0.40 > snack2 = 0.75 > snack3 = 1.20 > snack4 = 0.99 > snack5 = 0.50 > insert = 0 You never use this variable! > > change = 0 This machine does not dispense change! > currentCredit = 0.00 > A = 0.10 > B = 0.20 > C = 0.50 > D = 1.00 > a = 0.10 > b = 0.20 > c = 0.50 > d = 1.00 Never Never use floating values for money, as floating point cannot in general represent fractional values exactly. > > print "Menu" > print "Snack 1: Snickers - ?0.40" > print "Snack 2: Doritos - ?0.75 " > print "Snack 3: J20 - ?1.20" > print "Snack 4: Oreos - ?0.99" > print "Snack 5: M&M's - ?0.50" > print "Exit?" - how do I make this a Boolean > expression, so the user can respond with either yes or no? > You don't. Better (as Alan suggested) use raw_input and treat users entries as character. There is no advantage to using input and integers and a lot of room for errors. > choice = input("Select your snack: ") This does not agree with the specification - enter coin(s) first. The ensuing dialog also does not agree with the specification. You deposit one or more coins first, see the available credit. then choose a > > if choice==1: > print " " > print "You have selected Snickers, which cost ?0.40" > print "Please insert ?0.40" > while currentCredit < snack1: > print "Please select which of these coins to insert; > A:10p,B:20p,C:50p and D:?1" > insert_coins = input("Insert coins: ") > currentCredit = insert_coins + currentCredit Major problem here. As A user I'd enter (say) 20p. How does that get translated to a numeric value for adding? What should happen if I enter 30p, or 20x, or foo? > print "Your current credit is ?",currentCredit This assumes that credit less than ?1 will be reported as a fraction of a ?. How will you handle this fraction? > else: > change_given=currentCredit-snack1 > print " " > print "Your change is ?",change_given > print "Your Snickers have been dispensed...Enjoy!" > > elif choice==2: > print "You have selected Doritos, which cost ?0.75" > print "Please insert ?0.75" > while currentCredit print "Please select which of these coins to insert; > A:10p,B:20p,C:50p and D:?1" > insert_coins = input("Enter coins: ") > currentCredit = insert_coins + currentCredit > print "Your current credit is ?",currentCredit > else: > change_given=currentCredit-snack2 > print " " > print "Your change is ?",change_given > print "Your Doritos have been dispensed...Enjoy!" > > elif choice==3: > print "You have selected J20, which costs ?1.20" > print "Please insert ?1.20" > while currentCredit print "Please select which of these coins to insert; > A:10p,B:20p,C:50p and D:?1" > insert_coins = input("Enter coins: ") > currentCredit = insert_coins + currentCredit > print "Your current credit is ?",currentCredit > else: > change_given=currentCredit-snack3 > print " " > print "Your change is ?",change_given > print "Your J2O has been dispensed...Enjoy!" > > elif choice==4: > print "You have selcetd Oreos, which cost ?0.99" > print "Please insert ?0.99" > while currentCredit print "Please select which of these coins to insert; > A:10p,B:20p,C:50p and D:?1" > insert_coins = input("Enter coins: ") > currentCredit = insert_coins + currentCredit > print "Your current credit is ?",currentCredit > else: > change_given=currentCredit-snack4 > print " " > print "Your change is ?",change_given > print "Your Oreos have been dispensed...Enjoy!" > > elif choice==5: > print "You have selected M&M's, which cost ?0.50" > print "Please insert ?0.50" > while currentCredit print "Please select which of these coins to insert; > A:10p,B:20p,C:50p and D:?1" > insert_coins = input("Enter coins: ") > currentCredit = insert_coins + currentCredit > print "Your current credit is ?",currentCredit > else: > change_given=currentCredit-snack5 > print " " > print "Your change is ?",change_given > print "Your M&M's have been dispensed...Enjoy!" > > elif choice=="Exit": > print "Thank You and Have a Nice Day" > else: > print " " > print "Invalid choice" > Otherwise it is an OK program, for a beginner. You should carefully develop a sample dialog, by taking the specification item by item, translating each piece into a bit of dialog, then write code from that. It will be quite different. If you have studied lists or tuples, consider putting all the data for one product in a list, and create another list of these lists. By doing this you enter the data once, and refer to it more than once. From __peter__ at web.de Sat Apr 12 12:21:22 2014 From: __peter__ at web.de (Peter Otten) Date: Sat, 12 Apr 2014 12:21:22 +0200 Subject: [Tutor] improving speed using and recalling C functions References: Message-ID: Gabriele Brambilla wrote: > Ok guys, when I wrote that email I was excited for the apparent speed > increasing (it was jumping the bottleneck for loop for the reason peter > otten outlined). > Now, instead the changes, the speed is not improved (the code still > running from this morning and it's at one forth of the dataset). > > What can I do to speed it up? Not as easy as I had hoped and certainly not as pretty, here's my modification of the code you sent me. What makes it messy is that I had to inline your kappa() function; my first attempt with numpy.vectorize() didn't help much. There is still stuff in the 'for gammar...' loop that doesn't belong there, but I decided it was time for me to stop ;) Note that it may still be worthwhile to consult a numpy expert (which I'm not!). from scipy import stats import matplotlib.pyplot as plt from scipy import optimize from matplotlib import colors, ticker, cm import numpy as np phamin = 0 phamax = 2*pi obamin = 0 obamax = pi npha = 100 nobs = 181 stepPHA = (phamax-phamin)/npha stepOB = (obamax-obamin)/nobs freq = 10 c = 2.9979*(10**(10)) e = 4.8032*(10**(-10)) hcut = 1.0546*(10**(-27)) eVtoErg = 1.6022*(10**(-12)) from math import * import numpy as np from scipy.interpolate import interp1d kaparg = [ -3.0, -2.0, -1.52287875, -1.22184875, -1.0, -0.69897, -0.52287875, -0.39794001, -0.30103, -0.22184875, -0.15490196, 0.0, 0.30103, 0.60205999, 0.69897, 0.77815125, 0.90308999, 1.0] kapval = [ -0.6716204 , -0.35163999, -0.21183163, -0.13489603, -0.0872467 , -0.04431225, -0.03432803, -0.04335142, -0.05998184, -0.08039898, -0.10347378, -0.18641901, -0.52287875, -1.27572413, -1.66958623, -2.07314329, -2.88941029, -3.7212464 ] my_inter = interp1d(kaparg, kapval) def LEstep(n): Emin = 10**6 Emax = 5*(10**10) Lemin = log10(Emin) Lemax = log10(Emax) stepE = (Lemax-Lemin)/n return stepE, n, Lemin, Lemax def mymain(stepENE, nex, Lemin, Lemax, freq): eel = np.array(list(range(nex))) eels = np.logspace(Lemin, Lemax, num=nex, endpoint=False) rlc = c/(2*pi*freq) sigmas = [1, 3, 5, 10, 30] MYMAPS = [ np.zeros([npha, nobs, nex], dtype=float) for _ in sigmas] alpha = '60_' ALPHA = (1.732050808/c)*(e**2) for count, my_line in enumerate(open('datasm0_60_5s.dat')): myinternet = [] print('reading the line', count, '/599378') my_parts = np.array(my_line.split(), dtype=float) phase = my_parts[4] zobs = my_parts[5] rho = my_parts[6] gmils = my_parts[7:12] i = int((phase-phamin)/stepPHA) j = int((zobs-obamin)/stepOB) for gammar, MYMAP in zip(gmils, MYMAPS): omC = (1.5)*(gammar**3)*c/(rho*rlc) gig = omC*hcut/eVtoErg omega = (10**(eel*stepENE+Lemin))*eVtoErg/hcut x = omega/omC kap = np.empty(x.shape) sel = x >= 10.0 zsel = x[sel] kap[sel] = 1.2533 * np.sqrt(zsel)*np.exp(-zsel) sel = x < 0.001 zsel = x[sel] kap[sel] = (2.1495 * np.exp(0.333333333 * np.log(zsel)) - 1.8138 * zsel) sel = ~ ((x >= 10.0) | (x < 0.001)) zsel = x[sel] result = my_inter(np.log10(zsel)) kap[sel] = 10**result Iom = ALPHA*gammar*kap P = Iom*(c/(rho*rlc))/(2*pi) phps = P/(hcut*omega) www = phps/(stepPHA*sin(zobs)*stepOB) MYMAP[i,j] += www for sigma, MYMAP in zip(sigmas, MYMAPS): print(sigma) filename = "_".join(str(p) for p in ["skymap", alpha, sigma, npha, phamin, phamax, nobs, obamin, obamax, nex, Lemin, Lemax, '.dat'] ) x, y, z = MYMAP.shape with open(filename, 'ab') as MYfile: np.savetxt( MYfile, MYMAP.reshape(x*y, z, order="F").T, delimiter=",", fmt="%s", newline=",\n") if __name__ == "__main__": if len(sys.argv)<=1: stepENE, nex, Lemin, Lemax = LEstep(200) elif len(sys.argv)<=2: stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) else: stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) freq=float(sys.argv[2]) mymain(stepENE, nex, Lemin, Lemax, freq) For reference here is the original (with the loop over gmlis instead of gmils): > import sys > > from math import * > from scipy import ndimage > from scipy import stats > import matplotlib.pyplot as plt > from scipy import optimize > from matplotlib import colors, ticker, cm > import numpy as np > import cProfile > import pstats > > phamin=0 > phamax=2*pi > obamin=0 > obamax=pi > npha=100 > nobs=181 > stepPHA=(phamax-phamin)/npha > stepOB=(obamax-obamin)/nobs > freq=10 > c=2.9979*(10**(10)) > e=4.8032*(10**(-10)) > hcut=1.0546*(10**(-27)) > eVtoErg=1.6022*(10**(-12)) > > > from math import * > import numpy as np > from scipy.interpolate import interp1d > > > def kappa(z): > N=18 > kaparg = [-3.0, -2.0, -1.52287875, -1.22184875, -1.0, -0.69897, -0.52287875, -0.39794001, -0.30103, -0.22184875, -0.15490196, 0.0, 0.30103, 0.60205999, 0.69897, 0.77815125, 0.90308999, 1.0] > kapval = [-0.6716204 , -0.35163999, -0.21183163, -0.13489603, -0.0872467 , -0.04431225, -0.03432803, -0.04335142, -0.05998184, -0.08039898, -0.10347378, -0.18641901, -0.52287875, -1.27572413, -1.66958623, -2.07314329, -2.88941029, -3.7212464 ] > zlog=log10(z) > if z < 0.001: > k = 2.1495 * exp (0.333333333 * log (z)) - 1.8138 * z > return (k) > elif z >= 10.0: > k = 1.2533 * sqrt (z) * exp (-z) > return (k) > else: > my_inter = interp1d(kaparg, kapval) > my_z = np.array([zlog]) > result = my_inter(my_z) > valuelog = result[0] > k=10**valuelog > return(k) > > > > > def LEstep(n): > Emin=10**6 > Emax=5*(10**10) > Lemin=log10(Emin) > Lemax=log10(Emax) > stepE=(Lemax-Lemin)/n > return (stepE, n, Lemin, Lemax) > > > def mymain(stepENE, nex, Lemin, Lemax, freq): > > > eel = list(range(nex)) > eels = np.logspace(Lemin, Lemax, num=nex, endpoint=False) > > indpha = list(range(npha)) > indobs = list(range(nobs)) > rlc = c/(2*pi*freq) > > #creating an empty 3D vector > MYMAPS = [np.zeros([npha, nobs, nex], dtype=float), np.zeros([npha, nobs, nex], dtype=float), np.zeros([npha, nobs, nex], dtype=float), np.zeros([npha, nobs, nex], dtype=float), np.zeros([npha, nobs, nex], dtype=float)] > > > count=0 > > > alpha = '60_' > > for my_line in open('datasm0_60_5s.dat'): > myinternet = [] > gmlis = [] > print('reading the line', count, '/599378') > my_parts = [float(i) for i in my_line.split()] > phase = my_parts[4] > zobs = my_parts[5] > rho = my_parts[6] > > gmils=[my_parts[7], my_parts[8], my_parts[9], my_parts[10], my_parts[11]] > > i = int((phase-phamin)/stepPHA) > j = int((zobs-obamin)/stepOB) > > for gammar, MYMAP in zip(gmils, MYMAPS): > > omC = (1.5)*(gammar**3)*c/(rho*rlc) > gig = omC*hcut/eVtoErg > > for w in eel: > omega = (10**(w*stepENE+Lemin))*eVtoErg/hcut > x = omega/omC > kap = kappa(x) > Iom = (1.732050808/c)*(e**2)*gammar*kap > P = Iom*(c/(rho*rlc))/(2*pi) > phps = P/(hcut*omega) > www = phps/(stepPHA*sin(zobs)*stepOB) > MYMAP[i,j,w] += www > > count = count + 1 > > > > sigmas = [1, 3, 5, 10, 30] > > multis = zip(sigmas, MYMAPS) > > for sigma, MYMAP in multis: > > print(sigma) > filename='skymap_'+alpha+'_'+str(sigma)+'_'+str(npha)+'_'+str(phamin)+'_'+str(phamax)+'_'+str(nobs)+'_'+str(obamin)+'_'+str(obamax)+'_'+str(nex)+'_'+str(Lemin)+'_'+str(Lemax)+'_.dat' > > MYfile = open(filename, 'a') > for k in eel: > for j in indobs: > for i in indpha: > A=MYMAP[i, j, k] > stringa = str(A) + ',' > MYfile.write(stringa) > accapo = '\n' > MYfile.write(accapo) > > MYfile.close() > > > if __name__ == "__main__": > if len(sys.argv)<=1: > stepENE, nex, Lemin, Lemax = LEstep(200) > elif len(sys.argv)<=2: > stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) > else: > stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) > freq=float(sys.argv[2]) > > > #mymain(stepENE, nex, Lemin, Lemax, freq) > > #print('profile') > cProfile.run('mymain(stepENE, nex, Lemin, Lemax, freq)', 'restats', 'time') > > p = pstats.Stats('restats') > p.strip_dirs().sort_stats('name') > p.sort_stats('time').print_stats(20) > From gb.gabrielebrambilla at gmail.com Sat Apr 12 14:22:34 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Sat, 12 Apr 2014 08:22:34 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Ok guys, I'm not expert about profile but help me to look at it. this one is for 715853 elements (to multiply by 5, and for each of this N*5 there is a loop of 200 times) Sat Apr 12 04:58:50 2014 restats 9636507991 function calls in 66809.764 seconds Ordered by: internal time List reduced from 47 to 20 due to restriction <20> ncalls tottime percall cumtime percall filename:lineno(function) 1 13548.507 13548.507 66809.692 66809.692 skymapsI.py:44(mymain) 125800544 13539.337 0.000 15998.925 0.000 interpolate.py:394(_call_linear) 880603808 5353.382 0.000 5353.382 0.000 {numpy.core.multiarray.array} 715853000 4998.740 0.000 52861.634 0.000 instruments.py:10(kappa) 251601088 4550.940 0.000 4550.940 0.000 {method 'reduce' of 'numpy.ufunc' objects} 125800544 4312.078 0.000 10163.614 0.000 interpolate.py:454(_check_bounds) 125800544 2944.126 0.000 14182.917 0.000 interpolate.py:330(__init__) 125800544 2846.577 0.000 29484.248 0.000 interpolate.py:443(_evaluate) 125800544 1665.852 0.000 6000.603 0.000 polyint.py:82(_set_yi) 125800544 1039.455 0.000 1039.455 0.000 {method 'clip' of 'numpy.ndarray' objects} 251601088 944.848 0.000 944.848 0.000 {method 'reshape' of 'numpy.ndarray' objects} 251601088 922.928 0.000 1651.218 0.000 numerictypes.py:735(issubdtype) 503202176 897.044 0.000 3434.768 0.000 numeric.py:392(asarray) 125800544 816.401 0.000 32242.481 0.000 polyint.py:37(__call__) 251601088 787.593 0.000 5338.533 0.000 _methods.py:31(_any) 125800544 689.779 0.000 1989.101 0.000 polyint.py:74(_reshape_yi) 125800544 638.946 0.000 638.946 0.000 {method 'searchsorted' of 'numpy.ndarray' objects} 125800544 606.778 0.000 2257.996 0.000 polyint.py:102(_set_dtype) 125800544 598.000 0.000 6598.602 0.000 polyint.py:30(__init__) 629002720 549.358 0.000 549.358 0.000 {issubclass} looking at tottime it seems that skymaps mymain() and interpolate take the same big amount of time...right? So it's true that I have to slow down mymain() but interpolate is a problem too! do you agree with me? Now I will read Peter Otten's code and run the new simulation with it thanks Gabriele 2014-04-12 6:21 GMT-04:00 Peter Otten <__peter__ at web.de>: > Gabriele Brambilla wrote: > > > Ok guys, when I wrote that email I was excited for the apparent speed > > increasing (it was jumping the bottleneck for loop for the reason peter > > otten outlined). > > Now, instead the changes, the speed is not improved (the code still > > running from this morning and it's at one forth of the dataset). > > > > What can I do to speed it up? > > Not as easy as I had hoped and certainly not as pretty, here's my > modification of the code you sent me. What makes it messy is that > I had to inline your kappa() function; my first attempt with > numpy.vectorize() didn't help much. There is still stuff in the > 'for gammar...' loop that doesn't belong there, but I decided it > was time for me to stop ;) > > Note that it may still be worthwhile to consult a numpy expert > (which I'm not!). > > from scipy import stats > import matplotlib.pyplot as plt > from scipy import optimize > from matplotlib import colors, ticker, cm > import numpy as np > > phamin = 0 > phamax = 2*pi > obamin = 0 > obamax = pi > npha = 100 > nobs = 181 > stepPHA = (phamax-phamin)/npha > stepOB = (obamax-obamin)/nobs > freq = 10 > c = 2.9979*(10**(10)) > e = 4.8032*(10**(-10)) > hcut = 1.0546*(10**(-27)) > eVtoErg = 1.6022*(10**(-12)) > > from math import * > import numpy as np > from scipy.interpolate import interp1d > > kaparg = [ > -3.0, -2.0, -1.52287875, -1.22184875, -1.0, -0.69897, > -0.52287875, -0.39794001, -0.30103, -0.22184875, > -0.15490196, 0.0, 0.30103, 0.60205999, 0.69897, > 0.77815125, 0.90308999, 1.0] > > kapval = [ > -0.6716204 , -0.35163999, -0.21183163, -0.13489603, > -0.0872467 , -0.04431225, -0.03432803, -0.04335142, > -0.05998184, -0.08039898, -0.10347378, -0.18641901, > -0.52287875, -1.27572413, -1.66958623, -2.07314329, > -2.88941029, -3.7212464 ] > > my_inter = interp1d(kaparg, kapval) > > def LEstep(n): > Emin = 10**6 > Emax = 5*(10**10) > Lemin = log10(Emin) > Lemax = log10(Emax) > stepE = (Lemax-Lemin)/n > return stepE, n, Lemin, Lemax > > def mymain(stepENE, nex, Lemin, Lemax, freq): > eel = np.array(list(range(nex))) > eels = np.logspace(Lemin, Lemax, num=nex, endpoint=False) > > rlc = c/(2*pi*freq) > > sigmas = [1, 3, 5, 10, 30] > MYMAPS = [ > np.zeros([npha, nobs, nex], dtype=float) for _ in sigmas] > > alpha = '60_' > ALPHA = (1.732050808/c)*(e**2) > for count, my_line in enumerate(open('datasm0_60_5s.dat')): > myinternet = [] > print('reading the line', count, '/599378') > my_parts = np.array(my_line.split(), dtype=float) > phase = my_parts[4] > zobs = my_parts[5] > rho = my_parts[6] > > gmils = my_parts[7:12] > > i = int((phase-phamin)/stepPHA) > j = int((zobs-obamin)/stepOB) > > for gammar, MYMAP in zip(gmils, MYMAPS): > > omC = (1.5)*(gammar**3)*c/(rho*rlc) > gig = omC*hcut/eVtoErg > > omega = (10**(eel*stepENE+Lemin))*eVtoErg/hcut > x = omega/omC > > kap = np.empty(x.shape) > sel = x >= 10.0 > zsel = x[sel] > kap[sel] = 1.2533 * np.sqrt(zsel)*np.exp(-zsel) > > sel = x < 0.001 > zsel = x[sel] > kap[sel] = (2.1495 * np.exp(0.333333333 * np.log(zsel)) > - 1.8138 * zsel) > > sel = ~ ((x >= 10.0) | (x < 0.001)) > zsel = x[sel] > result = my_inter(np.log10(zsel)) > kap[sel] = 10**result > > Iom = ALPHA*gammar*kap > P = Iom*(c/(rho*rlc))/(2*pi) > phps = P/(hcut*omega) > www = phps/(stepPHA*sin(zobs)*stepOB) > MYMAP[i,j] += www > > for sigma, MYMAP in zip(sigmas, MYMAPS): > print(sigma) > filename = "_".join(str(p) for p in > ["skymap", alpha, sigma, npha, phamin, phamax, nobs, > obamin, obamax, nex, Lemin, Lemax, '.dat'] > ) > > x, y, z = MYMAP.shape > with open(filename, 'ab') as MYfile: > np.savetxt( > MYfile, > MYMAP.reshape(x*y, z, order="F").T, > delimiter=",", fmt="%s", newline=",\n") > > if __name__ == "__main__": > if len(sys.argv)<=1: > stepENE, nex, Lemin, Lemax = LEstep(200) > elif len(sys.argv)<=2: > stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) > else: > stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) > freq=float(sys.argv[2]) > > mymain(stepENE, nex, Lemin, Lemax, freq) > > > For reference here is the original (with the loop over gmlis > instead of gmils): > > > import sys > > > > from math import * > > from scipy import ndimage > > from scipy import stats > > import matplotlib.pyplot as plt > > from scipy import optimize > > from matplotlib import colors, ticker, cm > > import numpy as np > > import cProfile > > import pstats > > > > phamin=0 > > phamax=2*pi > > obamin=0 > > obamax=pi > > npha=100 > > nobs=181 > > stepPHA=(phamax-phamin)/npha > > stepOB=(obamax-obamin)/nobs > > freq=10 > > c=2.9979*(10**(10)) > > e=4.8032*(10**(-10)) > > hcut=1.0546*(10**(-27)) > > eVtoErg=1.6022*(10**(-12)) > > > > > > from math import * > > import numpy as np > > from scipy.interpolate import interp1d > > > > > > def kappa(z): > > N=18 > > kaparg = [-3.0, -2.0, -1.52287875, -1.22184875, -1.0, -0.69897, > -0.52287875, -0.39794001, -0.30103, -0.22184875, -0.15490196, 0.0, > 0.30103, 0.60205999, 0.69897, 0.77815125, 0.90308999, 1.0] > > kapval = [-0.6716204 , -0.35163999, -0.21183163, -0.13489603, > -0.0872467 , -0.04431225, -0.03432803, -0.04335142, -0.05998184, > -0.08039898, -0.10347378, -0.18641901, -0.52287875, -1.27572413, > -1.66958623, -2.07314329, -2.88941029, -3.7212464 ] > > zlog=log10(z) > > if z < 0.001: > > k = 2.1495 * exp (0.333333333 * log (z)) - 1.8138 * z > > return (k) > > elif z >= 10.0: > > k = 1.2533 * sqrt (z) * exp (-z) > > return (k) > > else: > > my_inter = interp1d(kaparg, kapval) > > my_z = np.array([zlog]) > > result = my_inter(my_z) > > valuelog = result[0] > > k=10**valuelog > > return(k) > > > > > > > > > > def LEstep(n): > > Emin=10**6 > > Emax=5*(10**10) > > Lemin=log10(Emin) > > Lemax=log10(Emax) > > stepE=(Lemax-Lemin)/n > > return (stepE, n, Lemin, Lemax) > > > > > > def mymain(stepENE, nex, Lemin, Lemax, freq): > > > > > > eel = list(range(nex)) > > eels = np.logspace(Lemin, Lemax, num=nex, endpoint=False) > > > > indpha = list(range(npha)) > > indobs = list(range(nobs)) > > rlc = c/(2*pi*freq) > > > > #creating an empty 3D vector > > MYMAPS = [np.zeros([npha, nobs, nex], dtype=float), np.zeros([npha, > nobs, nex], dtype=float), np.zeros([npha, nobs, nex], dtype=float), > np.zeros([npha, nobs, nex], dtype=float), np.zeros([npha, nobs, > nex], dtype=float)] > > > > > > count=0 > > > > > > alpha = '60_' > > > > for my_line in open('datasm0_60_5s.dat'): > > myinternet = [] > > gmlis = [] > > print('reading the line', count, '/599378') > > my_parts = [float(i) for i in my_line.split()] > > phase = my_parts[4] > > zobs = my_parts[5] > > rho = my_parts[6] > > > > gmils=[my_parts[7], my_parts[8], my_parts[9], my_parts[10], > my_parts[11]] > > > > i = int((phase-phamin)/stepPHA) > > j = int((zobs-obamin)/stepOB) > > > > for gammar, MYMAP in zip(gmils, MYMAPS): > > > > omC = (1.5)*(gammar**3)*c/(rho*rlc) > > gig = omC*hcut/eVtoErg > > > > for w in eel: > > omega = (10**(w*stepENE+Lemin))*eVtoErg/hcut > > x = omega/omC > > kap = kappa(x) > > Iom = (1.732050808/c)*(e**2)*gammar*kap > > P = Iom*(c/(rho*rlc))/(2*pi) > > phps = P/(hcut*omega) > > www = phps/(stepPHA*sin(zobs)*stepOB) > > MYMAP[i,j,w] += www > > > > count = count + 1 > > > > > > > > sigmas = [1, 3, 5, 10, 30] > > > > multis = zip(sigmas, MYMAPS) > > > > for sigma, MYMAP in multis: > > > > print(sigma) > > > filename='skymap_'+alpha+'_'+str(sigma)+'_'+str(npha)+'_'+str(phamin)+'_'+str(phamax)+'_'+str(nobs)+'_'+str(obamin)+'_'+str(obamax)+'_'+str(nex)+'_'+str(Lemin)+'_'+str(Lemax)+'_.dat' > > > > MYfile = open(filename, 'a') > > for k in eel: > > for j in indobs: > > for i in indpha: > > A=MYMAP[i, j, k] > > stringa = str(A) + ',' > > MYfile.write(stringa) > > accapo = '\n' > > MYfile.write(accapo) > > > > MYfile.close() > > > > > > if __name__ == "__main__": > > if len(sys.argv)<=1: > > stepENE, nex, Lemin, Lemax = LEstep(200) > > elif len(sys.argv)<=2: > > stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) > > else: > > stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) > > freq=float(sys.argv[2]) > > > > > > #mymain(stepENE, nex, Lemin, Lemax, freq) > > > > #print('profile') > > cProfile.run('mymain(stepENE, nex, Lemin, Lemax, freq)', 'restats', > 'time') > > > > p = pstats.Stats('restats') > > p.strip_dirs().sort_stats('name') > > p.sort_stats('time').print_stats(20) > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gb.gabrielebrambilla at gmail.com Sat Apr 12 15:34:08 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Sat, 12 Apr 2014 09:34:08 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: Ok, i just run Peter's code and it seems really faster...I hope to don't mistake this time! Thanks Gabriele sent from Samsung Mobile Il giorno 12/apr/2014 08:22, "Gabriele Brambilla" < gb.gabrielebrambilla at gmail.com> ha scritto: > Ok guys, > I'm not expert about profile but help me to look at it. > this one is for 715853 elements (to multiply by 5, and for each of this > N*5 there is a loop of 200 times) > > Sat Apr 12 04:58:50 2014 restats > > 9636507991 function calls in 66809.764 seconds > > Ordered by: internal time > List reduced from 47 to 20 due to restriction <20> > > ncalls tottime percall cumtime percall > filename:lineno(function) > 1 13548.507 13548.507 66809.692 66809.692 > skymapsI.py:44(mymain) > 125800544 13539.337 0.000 15998.925 0.000 > interpolate.py:394(_call_linear) > 880603808 5353.382 0.000 5353.382 0.000 > {numpy.core.multiarray.array} > 715853000 4998.740 0.000 52861.634 0.000 > instruments.py:10(kappa) > 251601088 4550.940 0.000 4550.940 0.000 {method 'reduce' > of 'numpy.ufunc' objects} > 125800544 4312.078 0.000 10163.614 0.000 > interpolate.py:454(_check_bounds) > 125800544 2944.126 0.000 14182.917 0.000 > interpolate.py:330(__init__) > 125800544 2846.577 0.000 29484.248 0.000 > interpolate.py:443(_evaluate) > 125800544 1665.852 0.000 6000.603 0.000 polyint.py:82(_set_yi) > 125800544 1039.455 0.000 1039.455 0.000 {method 'clip' of > 'numpy.ndarray' objects} > 251601088 944.848 0.000 944.848 0.000 {method 'reshape' > of 'numpy.ndarray' objects} > 251601088 922.928 0.000 1651.218 0.000numerictypes.py:735(issubdtype) > 503202176 897.044 0.000 3434.768 0.000 > numeric.py:392(asarray) > 125800544 816.401 0.000 32242.481 0.000 > polyint.py:37(__call__) > 251601088 787.593 0.000 5338.533 0.000 _methods.py:31(_any) > 125800544 689.779 0.000 1989.101 0.000 > polyint.py:74(_reshape_yi) > 125800544 638.946 0.000 638.946 0.000 {method > 'searchsorted' of 'numpy.ndarray' objects} > 125800544 606.778 0.000 2257.996 0.000 > polyint.py:102(_set_dtype) > 125800544 598.000 0.000 6598.602 0.000 > polyint.py:30(__init__) > 629002720 549.358 0.000 549.358 0.000 {issubclass} > > > looking at tottime it seems that skymaps mymain() and interpolate take the > same big amount of time...right? > > So it's true that I have to slow down mymain() but interpolate is a > problem too! > > do you agree with me? > > Now I will read Peter Otten's code and run the new simulation with it > > thanks > > Gabriele > > > 2014-04-12 6:21 GMT-04:00 Peter Otten <__peter__ at web.de>: > >> Gabriele Brambilla wrote: >> >> > Ok guys, when I wrote that email I was excited for the apparent speed >> > increasing (it was jumping the bottleneck for loop for the reason peter >> > otten outlined). >> > Now, instead the changes, the speed is not improved (the code still >> > running from this morning and it's at one forth of the dataset). >> > >> > What can I do to speed it up? >> >> Not as easy as I had hoped and certainly not as pretty, here's my >> modification of the code you sent me. What makes it messy is that >> I had to inline your kappa() function; my first attempt with >> numpy.vectorize() didn't help much. There is still stuff in the >> 'for gammar...' loop that doesn't belong there, but I decided it >> was time for me to stop ;) >> >> Note that it may still be worthwhile to consult a numpy expert >> (which I'm not!). >> >> from scipy import stats >> import matplotlib.pyplot as plt >> from scipy import optimize >> from matplotlib import colors, ticker, cm >> import numpy as np >> >> phamin = 0 >> phamax = 2*pi >> obamin = 0 >> obamax = pi >> npha = 100 >> nobs = 181 >> stepPHA = (phamax-phamin)/npha >> stepOB = (obamax-obamin)/nobs >> freq = 10 >> c = 2.9979*(10**(10)) >> e = 4.8032*(10**(-10)) >> hcut = 1.0546*(10**(-27)) >> eVtoErg = 1.6022*(10**(-12)) >> >> from math import * >> import numpy as np >> from scipy.interpolate import interp1d >> >> kaparg = [ >> -3.0, -2.0, -1.52287875, -1.22184875, -1.0, -0.69897, >> -0.52287875, -0.39794001, -0.30103, -0.22184875, >> -0.15490196, 0.0, 0.30103, 0.60205999, 0.69897, >> 0.77815125, 0.90308999, 1.0] >> >> kapval = [ >> -0.6716204 , -0.35163999, -0.21183163, -0.13489603, >> -0.0872467 , -0.04431225, -0.03432803, -0.04335142, >> -0.05998184, -0.08039898, -0.10347378, -0.18641901, >> -0.52287875, -1.27572413, -1.66958623, -2.07314329, >> -2.88941029, -3.7212464 ] >> >> my_inter = interp1d(kaparg, kapval) >> >> def LEstep(n): >> Emin = 10**6 >> Emax = 5*(10**10) >> Lemin = log10(Emin) >> Lemax = log10(Emax) >> stepE = (Lemax-Lemin)/n >> return stepE, n, Lemin, Lemax >> >> def mymain(stepENE, nex, Lemin, Lemax, freq): >> eel = np.array(list(range(nex))) >> eels = np.logspace(Lemin, Lemax, num=nex, endpoint=False) >> >> rlc = c/(2*pi*freq) >> >> sigmas = [1, 3, 5, 10, 30] >> MYMAPS = [ >> np.zeros([npha, nobs, nex], dtype=float) for _ in sigmas] >> >> alpha = '60_' >> ALPHA = (1.732050808/c)*(e**2) >> for count, my_line in enumerate(open('datasm0_60_5s.dat')): >> myinternet = [] >> print('reading the line', count, '/599378') >> my_parts = np.array(my_line.split(), dtype=float) >> phase = my_parts[4] >> zobs = my_parts[5] >> rho = my_parts[6] >> >> gmils = my_parts[7:12] >> >> i = int((phase-phamin)/stepPHA) >> j = int((zobs-obamin)/stepOB) >> >> for gammar, MYMAP in zip(gmils, MYMAPS): >> >> omC = (1.5)*(gammar**3)*c/(rho*rlc) >> gig = omC*hcut/eVtoErg >> >> omega = (10**(eel*stepENE+Lemin))*eVtoErg/hcut >> x = omega/omC >> >> kap = np.empty(x.shape) >> sel = x >= 10.0 >> zsel = x[sel] >> kap[sel] = 1.2533 * np.sqrt(zsel)*np.exp(-zsel) >> >> sel = x < 0.001 >> zsel = x[sel] >> kap[sel] = (2.1495 * np.exp(0.333333333 * np.log(zsel)) >> - 1.8138 * zsel) >> >> sel = ~ ((x >= 10.0) | (x < 0.001)) >> zsel = x[sel] >> result = my_inter(np.log10(zsel)) >> kap[sel] = 10**result >> >> Iom = ALPHA*gammar*kap >> P = Iom*(c/(rho*rlc))/(2*pi) >> phps = P/(hcut*omega) >> www = phps/(stepPHA*sin(zobs)*stepOB) >> MYMAP[i,j] += www >> >> for sigma, MYMAP in zip(sigmas, MYMAPS): >> print(sigma) >> filename = "_".join(str(p) for p in >> ["skymap", alpha, sigma, npha, phamin, phamax, nobs, >> obamin, obamax, nex, Lemin, Lemax, '.dat'] >> ) >> >> x, y, z = MYMAP.shape >> with open(filename, 'ab') as MYfile: >> np.savetxt( >> MYfile, >> MYMAP.reshape(x*y, z, order="F").T, >> delimiter=",", fmt="%s", newline=",\n") >> >> if __name__ == "__main__": >> if len(sys.argv)<=1: >> stepENE, nex, Lemin, Lemax = LEstep(200) >> elif len(sys.argv)<=2: >> stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) >> else: >> stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) >> freq=float(sys.argv[2]) >> >> mymain(stepENE, nex, Lemin, Lemax, freq) >> >> >> For reference here is the original (with the loop over gmlis >> instead of gmils): >> >> > import sys >> > >> > from math import * >> > from scipy import ndimage >> > from scipy import stats >> > import matplotlib.pyplot as plt >> > from scipy import optimize >> > from matplotlib import colors, ticker, cm >> > import numpy as np >> > import cProfile >> > import pstats >> > >> > phamin=0 >> > phamax=2*pi >> > obamin=0 >> > obamax=pi >> > npha=100 >> > nobs=181 >> > stepPHA=(phamax-phamin)/npha >> > stepOB=(obamax-obamin)/nobs >> > freq=10 >> > c=2.9979*(10**(10)) >> > e=4.8032*(10**(-10)) >> > hcut=1.0546*(10**(-27)) >> > eVtoErg=1.6022*(10**(-12)) >> > >> > >> > from math import * >> > import numpy as np >> > from scipy.interpolate import interp1d >> > >> > >> > def kappa(z): >> > N=18 >> > kaparg = [-3.0, -2.0, -1.52287875, -1.22184875, -1.0, -0.69897, >> -0.52287875, -0.39794001, -0.30103, -0.22184875, -0.15490196, 0.0, >> 0.30103, 0.60205999, 0.69897, 0.77815125, 0.90308999, 1.0] >> > kapval = [-0.6716204 , -0.35163999, -0.21183163, -0.13489603, >> -0.0872467 , -0.04431225, -0.03432803, -0.04335142, -0.05998184, >> -0.08039898, -0.10347378, -0.18641901, -0.52287875, -1.27572413, >> -1.66958623, -2.07314329, -2.88941029, -3.7212464 ] >> > zlog=log10(z) >> > if z < 0.001: >> > k = 2.1495 * exp (0.333333333 * log (z)) - 1.8138 * z >> > return (k) >> > elif z >= 10.0: >> > k = 1.2533 * sqrt (z) * exp (-z) >> > return (k) >> > else: >> > my_inter = interp1d(kaparg, kapval) >> > my_z = np.array([zlog]) >> > result = my_inter(my_z) >> > valuelog = result[0] >> > k=10**valuelog >> > return(k) >> > >> > >> > >> > >> > def LEstep(n): >> > Emin=10**6 >> > Emax=5*(10**10) >> > Lemin=log10(Emin) >> > Lemax=log10(Emax) >> > stepE=(Lemax-Lemin)/n >> > return (stepE, n, Lemin, Lemax) >> > >> > >> > def mymain(stepENE, nex, Lemin, Lemax, freq): >> > >> > >> > eel = list(range(nex)) >> > eels = np.logspace(Lemin, Lemax, num=nex, endpoint=False) >> > >> > indpha = list(range(npha)) >> > indobs = list(range(nobs)) >> > rlc = c/(2*pi*freq) >> > >> > #creating an empty 3D vector >> > MYMAPS = [np.zeros([npha, nobs, nex], dtype=float), np.zeros([npha, >> nobs, nex], dtype=float), np.zeros([npha, nobs, nex], dtype=float), >> np.zeros([npha, nobs, nex], dtype=float), np.zeros([npha, nobs, >> nex], dtype=float)] >> > >> > >> > count=0 >> > >> > >> > alpha = '60_' >> > >> > for my_line in open('datasm0_60_5s.dat'): >> > myinternet = [] >> > gmlis = [] >> > print('reading the line', count, '/599378') >> > my_parts = [float(i) for i in my_line.split()] >> > phase = my_parts[4] >> > zobs = my_parts[5] >> > rho = my_parts[6] >> > >> > gmils=[my_parts[7], my_parts[8], my_parts[9], my_parts[10], >> my_parts[11]] >> > >> > i = int((phase-phamin)/stepPHA) >> > j = int((zobs-obamin)/stepOB) >> > >> > for gammar, MYMAP in zip(gmils, MYMAPS): >> > >> > omC = (1.5)*(gammar**3)*c/(rho*rlc) >> > gig = omC*hcut/eVtoErg >> > >> > for w in eel: >> > omega = (10**(w*stepENE+Lemin))*eVtoErg/hcut >> > x = omega/omC >> > kap = kappa(x) >> > Iom = (1.732050808/c)*(e**2)*gammar*kap >> > P = Iom*(c/(rho*rlc))/(2*pi) >> > phps = P/(hcut*omega) >> > www = phps/(stepPHA*sin(zobs)*stepOB) >> > MYMAP[i,j,w] += www >> > >> > count = count + 1 >> > >> > >> > >> > sigmas = [1, 3, 5, 10, 30] >> > >> > multis = zip(sigmas, MYMAPS) >> > >> > for sigma, MYMAP in multis: >> > >> > print(sigma) >> > >> filename='skymap_'+alpha+'_'+str(sigma)+'_'+str(npha)+'_'+str(phamin)+'_'+str(phamax)+'_'+str(nobs)+'_'+str(obamin)+'_'+str(obamax)+'_'+str(nex)+'_'+str(Lemin)+'_'+str(Lemax)+'_.dat' >> > >> > MYfile = open(filename, 'a') >> > for k in eel: >> > for j in indobs: >> > for i in indpha: >> > A=MYMAP[i, j, k] >> > stringa = str(A) + ',' >> > MYfile.write(stringa) >> > accapo = '\n' >> > MYfile.write(accapo) >> > >> > MYfile.close() >> > >> > >> > if __name__ == "__main__": >> > if len(sys.argv)<=1: >> > stepENE, nex, Lemin, Lemax = LEstep(200) >> > elif len(sys.argv)<=2: >> > stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) >> > else: >> > stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) >> > freq=float(sys.argv[2]) >> > >> > >> > #mymain(stepENE, nex, Lemin, Lemax, freq) >> > >> > #print('profile') >> > cProfile.run('mymain(stepENE, nex, Lemin, Lemax, freq)', 'restats', >> 'time') >> > >> > p = pstats.Stats('restats') >> > p.strip_dirs().sort_stats('name') >> > p.sort_stats('time').print_stats(20) >> > >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gb.gabrielebrambilla at gmail.com Sat Apr 12 17:52:09 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Sat, 12 Apr 2014 11:52:09 -0400 Subject: [Tutor] improving speed using and recalling C functions In-Reply-To: References: Message-ID: ok Peter Otten code works (very fast), and this is the profile Sat Apr 12 11:15:39 2014 restats 92834776 function calls in 6218.782 seconds Ordered by: internal time List reduced from 41 to 20 due to restriction <20> ncalls tottime percall cumtime percall filename:lineno(function) 1 5301.641 5301.641 6218.763 6218.763 skymapsIO.py:50(mymain) 3489985 380.469 0.000 452.478 0.000 interpolate.py:394(_call_linear) 3489985 98.186 0.000 227.229 0.000 interpolate.py:454(_check_bounds) 6979970 96.567 0.000 96.567 0.000 {method 'reduce' of 'numpy.ufunc'objects} 3489985 44.853 0.000 738.135 0.000 interpolate.py:443(_evaluate) 7677978 41.010 0.000 41.010 0.000 {numpy.core.multiarray.array} 5 40.430 8.086 40.621 8.124 npyio.py:882(savetxt) 3489985 26.952 0.000 26.952 0.000 {method 'clip' of 'numpy.ndarray'objects} 3489985 24.749 0.000 24.749 0.000 {method 'searchsorted' of 'numpy.ndarray' objects} 3489985 22.457 0.000 828.238 0.000 polyint.py:37(__call__) 6979970 19.720 0.000 116.287 0.000 _methods.py:31(_any) 6979980 15.330 0.000 35.092 0.000 numeric.py:392(asarray) 3489985 14.847 0.000 45.039 0.000 polyint.py:57(_prepare_x) 3489990 12.904 0.000 12.904 0.000 {method 'reshape' of 'numpy.ndarray' objects} 6979970 12.757 0.000 129.044 0.000 {method 'any' of 'numpy.ndarray' objects} 3489985 11.624 0.000 11.624 0.000 {method 'astype' of 'numpy.ndarray' objects} 3489985 10.077 0.000 10.077 0.000 {numpy.core.multiarray.empty} 3489985 9.945 0.000 22.607 0.000 polyint.py:63(_finish_y) 3489985 7.051 0.000 7.051 0.000 {method 'ravel' of 'numpy.ndarray' objects} 697998 6.746 0.000 6.746 0.000 {zip} So I think that in this way it's ok. Thank you all very much, Gabriele p.s: I didn't know this way to write: is there a tutorial for this kind of operations? kap = np.empty(x.shape) sel = x >= 10.0 zsel = x[sel] kap[sel] = 1.2533 * np.sqrt(zsel)*np.exp(-zsel) sel = x < 0.001 zsel = x[sel] kap[sel] = (2.1495 * np.exp(0.333333333 * np.log(zsel)) - 1.8138 * zsel) sel = ~ ((x >= 10.0) | (x < 0.001)) zsel = x[sel] result = my_inter(np.log10(zsel)) kap[sel] = 10**result 2014-04-12 9:34 GMT-04:00 Gabriele Brambilla : > Ok, i just run Peter's code and it seems really faster...I hope to don't > mistake this time! > > Thanks > > Gabriele > > sent from Samsung Mobile > Il giorno 12/apr/2014 08:22, "Gabriele Brambilla" < > gb.gabrielebrambilla at gmail.com> ha scritto: > > Ok guys, >> I'm not expert about profile but help me to look at it. >> this one is for 715853 elements (to multiply by 5, and for each of this >> N*5 there is a loop of 200 times) >> >> Sat Apr 12 04:58:50 2014 restats >> >> 9636507991 function calls in 66809.764 seconds >> >> Ordered by: internal time >> List reduced from 47 to 20 due to restriction <20> >> >> ncalls tottime percall cumtime percall >> filename:lineno(function) >> 1 13548.507 13548.507 66809.692 66809.692 >> skymapsI.py:44(mymain) >> 125800544 13539.337 0.000 15998.925 0.000 >> interpolate.py:394(_call_linear) >> 880603808 5353.382 0.000 5353.382 0.000 >> {numpy.core.multiarray.array} >> 715853000 4998.740 0.000 52861.634 0.000 >> instruments.py:10(kappa) >> 251601088 4550.940 0.000 4550.940 0.000 {method 'reduce' >> of 'numpy.ufunc' objects} >> 125800544 4312.078 0.000 10163.614 0.000 >> interpolate.py:454(_check_bounds) >> 125800544 2944.126 0.000 14182.917 0.000 >> interpolate.py:330(__init__) >> 125800544 2846.577 0.000 29484.248 0.000 >> interpolate.py:443(_evaluate) >> 125800544 1665.852 0.000 6000.603 0.000 >> polyint.py:82(_set_yi) >> 125800544 1039.455 0.000 1039.455 0.000 {method 'clip' of >> 'numpy.ndarray' objects} >> 251601088 944.848 0.000 944.848 0.000 {method 'reshape' >> of 'numpy.ndarray' objects} >> 251601088 922.928 0.000 1651.218 0.000numerictypes.py:735(issubdtype) >> 503202176 897.044 0.000 3434.768 0.000 >> numeric.py:392(asarray) >> 125800544 816.401 0.000 32242.481 0.000 >> polyint.py:37(__call__) >> 251601088 787.593 0.000 5338.533 0.000 >> _methods.py:31(_any) >> 125800544 689.779 0.000 1989.101 0.000 >> polyint.py:74(_reshape_yi) >> 125800544 638.946 0.000 638.946 0.000 {method >> 'searchsorted' of 'numpy.ndarray' objects} >> 125800544 606.778 0.000 2257.996 0.000 >> polyint.py:102(_set_dtype) >> 125800544 598.000 0.000 6598.602 0.000 >> polyint.py:30(__init__) >> 629002720 549.358 0.000 549.358 0.000 {issubclass} >> >> >> looking at tottime it seems that skymaps mymain() and interpolate take >> the same big amount of time...right? >> >> So it's true that I have to slow down mymain() but interpolate is a >> problem too! >> >> do you agree with me? >> >> Now I will read Peter Otten's code and run the new simulation with it >> >> thanks >> >> Gabriele >> >> >> 2014-04-12 6:21 GMT-04:00 Peter Otten <__peter__ at web.de>: >> >>> Gabriele Brambilla wrote: >>> >>> > Ok guys, when I wrote that email I was excited for the apparent speed >>> > increasing (it was jumping the bottleneck for loop for the reason peter >>> > otten outlined). >>> > Now, instead the changes, the speed is not improved (the code still >>> > running from this morning and it's at one forth of the dataset). >>> > >>> > What can I do to speed it up? >>> >>> Not as easy as I had hoped and certainly not as pretty, here's my >>> modification of the code you sent me. What makes it messy is that >>> I had to inline your kappa() function; my first attempt with >>> numpy.vectorize() didn't help much. There is still stuff in the >>> 'for gammar...' loop that doesn't belong there, but I decided it >>> was time for me to stop ;) >>> >>> Note that it may still be worthwhile to consult a numpy expert >>> (which I'm not!). >>> >>> from scipy import stats >>> import matplotlib.pyplot as plt >>> from scipy import optimize >>> from matplotlib import colors, ticker, cm >>> import numpy as np >>> >>> phamin = 0 >>> phamax = 2*pi >>> obamin = 0 >>> obamax = pi >>> npha = 100 >>> nobs = 181 >>> stepPHA = (phamax-phamin)/npha >>> stepOB = (obamax-obamin)/nobs >>> freq = 10 >>> c = 2.9979*(10**(10)) >>> e = 4.8032*(10**(-10)) >>> hcut = 1.0546*(10**(-27)) >>> eVtoErg = 1.6022*(10**(-12)) >>> >>> from math import * >>> import numpy as np >>> from scipy.interpolate import interp1d >>> >>> kaparg = [ >>> -3.0, -2.0, -1.52287875, -1.22184875, -1.0, -0.69897, >>> -0.52287875, -0.39794001, -0.30103, -0.22184875, >>> -0.15490196, 0.0, 0.30103, 0.60205999, 0.69897, >>> 0.77815125, 0.90308999, 1.0] >>> >>> kapval = [ >>> -0.6716204 , -0.35163999, -0.21183163, -0.13489603, >>> -0.0872467 , -0.04431225, -0.03432803, -0.04335142, >>> -0.05998184, -0.08039898, -0.10347378, -0.18641901, >>> -0.52287875, -1.27572413, -1.66958623, -2.07314329, >>> -2.88941029, -3.7212464 ] >>> >>> my_inter = interp1d(kaparg, kapval) >>> >>> def LEstep(n): >>> Emin = 10**6 >>> Emax = 5*(10**10) >>> Lemin = log10(Emin) >>> Lemax = log10(Emax) >>> stepE = (Lemax-Lemin)/n >>> return stepE, n, Lemin, Lemax >>> >>> def mymain(stepENE, nex, Lemin, Lemax, freq): >>> eel = np.array(list(range(nex))) >>> eels = np.logspace(Lemin, Lemax, num=nex, endpoint=False) >>> >>> rlc = c/(2*pi*freq) >>> >>> sigmas = [1, 3, 5, 10, 30] >>> MYMAPS = [ >>> np.zeros([npha, nobs, nex], dtype=float) for _ in sigmas] >>> >>> alpha = '60_' >>> ALPHA = (1.732050808/c)*(e**2) >>> for count, my_line in enumerate(open('datasm0_60_5s.dat')): >>> myinternet = [] >>> print('reading the line', count, '/599378') >>> my_parts = np.array(my_line.split(), dtype=float) >>> phase = my_parts[4] >>> zobs = my_parts[5] >>> rho = my_parts[6] >>> >>> gmils = my_parts[7:12] >>> >>> i = int((phase-phamin)/stepPHA) >>> j = int((zobs-obamin)/stepOB) >>> >>> for gammar, MYMAP in zip(gmils, MYMAPS): >>> >>> omC = (1.5)*(gammar**3)*c/(rho*rlc) >>> gig = omC*hcut/eVtoErg >>> >>> omega = (10**(eel*stepENE+Lemin))*eVtoErg/hcut >>> x = omega/omC >>> >>> kap = np.empty(x.shape) >>> sel = x >= 10.0 >>> zsel = x[sel] >>> kap[sel] = 1.2533 * np.sqrt(zsel)*np.exp(-zsel) >>> >>> sel = x < 0.001 >>> zsel = x[sel] >>> kap[sel] = (2.1495 * np.exp(0.333333333 * np.log(zsel)) >>> - 1.8138 * zsel) >>> >>> sel = ~ ((x >= 10.0) | (x < 0.001)) >>> zsel = x[sel] >>> result = my_inter(np.log10(zsel)) >>> kap[sel] = 10**result >>> >>> Iom = ALPHA*gammar*kap >>> P = Iom*(c/(rho*rlc))/(2*pi) >>> phps = P/(hcut*omega) >>> www = phps/(stepPHA*sin(zobs)*stepOB) >>> MYMAP[i,j] += www >>> >>> for sigma, MYMAP in zip(sigmas, MYMAPS): >>> print(sigma) >>> filename = "_".join(str(p) for p in >>> ["skymap", alpha, sigma, npha, phamin, phamax, nobs, >>> obamin, obamax, nex, Lemin, Lemax, '.dat'] >>> ) >>> >>> x, y, z = MYMAP.shape >>> with open(filename, 'ab') as MYfile: >>> np.savetxt( >>> MYfile, >>> MYMAP.reshape(x*y, z, order="F").T, >>> delimiter=",", fmt="%s", newline=",\n") >>> >>> if __name__ == "__main__": >>> if len(sys.argv)<=1: >>> stepENE, nex, Lemin, Lemax = LEstep(200) >>> elif len(sys.argv)<=2: >>> stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) >>> else: >>> stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) >>> freq=float(sys.argv[2]) >>> >>> mymain(stepENE, nex, Lemin, Lemax, freq) >>> >>> >>> For reference here is the original (with the loop over gmlis >>> instead of gmils): >>> >>> > import sys >>> > >>> > from math import * >>> > from scipy import ndimage >>> > from scipy import stats >>> > import matplotlib.pyplot as plt >>> > from scipy import optimize >>> > from matplotlib import colors, ticker, cm >>> > import numpy as np >>> > import cProfile >>> > import pstats >>> > >>> > phamin=0 >>> > phamax=2*pi >>> > obamin=0 >>> > obamax=pi >>> > npha=100 >>> > nobs=181 >>> > stepPHA=(phamax-phamin)/npha >>> > stepOB=(obamax-obamin)/nobs >>> > freq=10 >>> > c=2.9979*(10**(10)) >>> > e=4.8032*(10**(-10)) >>> > hcut=1.0546*(10**(-27)) >>> > eVtoErg=1.6022*(10**(-12)) >>> > >>> > >>> > from math import * >>> > import numpy as np >>> > from scipy.interpolate import interp1d >>> > >>> > >>> > def kappa(z): >>> > N=18 >>> > kaparg = [-3.0, -2.0, -1.52287875, -1.22184875, -1.0, -0.69897, >>> -0.52287875, -0.39794001, -0.30103, -0.22184875, -0.15490196, 0.0, >>> 0.30103, 0.60205999, 0.69897, 0.77815125, 0.90308999, 1.0] >>> > kapval = [-0.6716204 , -0.35163999, -0.21183163, -0.13489603, >>> -0.0872467 , -0.04431225, -0.03432803, -0.04335142, -0.05998184, >>> -0.08039898, -0.10347378, -0.18641901, -0.52287875, -1.27572413, >>> -1.66958623, -2.07314329, -2.88941029, -3.7212464 ] >>> > zlog=log10(z) >>> > if z < 0.001: >>> > k = 2.1495 * exp (0.333333333 * log (z)) - 1.8138 * z >>> > return (k) >>> > elif z >= 10.0: >>> > k = 1.2533 * sqrt (z) * exp (-z) >>> > return (k) >>> > else: >>> > my_inter = interp1d(kaparg, kapval) >>> > my_z = np.array([zlog]) >>> > result = my_inter(my_z) >>> > valuelog = result[0] >>> > k=10**valuelog >>> > return(k) >>> > >>> > >>> > >>> > >>> > def LEstep(n): >>> > Emin=10**6 >>> > Emax=5*(10**10) >>> > Lemin=log10(Emin) >>> > Lemax=log10(Emax) >>> > stepE=(Lemax-Lemin)/n >>> > return (stepE, n, Lemin, Lemax) >>> > >>> > >>> > def mymain(stepENE, nex, Lemin, Lemax, freq): >>> > >>> > >>> > eel = list(range(nex)) >>> > eels = np.logspace(Lemin, Lemax, num=nex, endpoint=False) >>> > >>> > indpha = list(range(npha)) >>> > indobs = list(range(nobs)) >>> > rlc = c/(2*pi*freq) >>> > >>> > #creating an empty 3D vector >>> > MYMAPS = [np.zeros([npha, nobs, nex], dtype=float), >>> np.zeros([npha, nobs, nex], dtype=float), np.zeros([npha, nobs, nex], >>> dtype=float), np.zeros([npha, nobs, nex], dtype=float), np.zeros([npha, >>> nobs, >>> nex], dtype=float)] >>> > >>> > >>> > count=0 >>> > >>> > >>> > alpha = '60_' >>> > >>> > for my_line in open('datasm0_60_5s.dat'): >>> > myinternet = [] >>> > gmlis = [] >>> > print('reading the line', count, '/599378') >>> > my_parts = [float(i) for i in my_line.split()] >>> > phase = my_parts[4] >>> > zobs = my_parts[5] >>> > rho = my_parts[6] >>> > >>> > gmils=[my_parts[7], my_parts[8], my_parts[9], my_parts[10], >>> my_parts[11]] >>> > >>> > i = int((phase-phamin)/stepPHA) >>> > j = int((zobs-obamin)/stepOB) >>> > >>> > for gammar, MYMAP in zip(gmils, MYMAPS): >>> > >>> > omC = (1.5)*(gammar**3)*c/(rho*rlc) >>> > gig = omC*hcut/eVtoErg >>> > >>> > for w in eel: >>> > omega = (10**(w*stepENE+Lemin))*eVtoErg/hcut >>> > x = omega/omC >>> > kap = kappa(x) >>> > Iom = (1.732050808/c)*(e**2)*gammar*kap >>> > P = Iom*(c/(rho*rlc))/(2*pi) >>> > phps = P/(hcut*omega) >>> > www = phps/(stepPHA*sin(zobs)*stepOB) >>> > MYMAP[i,j,w] += www >>> > >>> > count = count + 1 >>> > >>> > >>> > >>> > sigmas = [1, 3, 5, 10, 30] >>> > >>> > multis = zip(sigmas, MYMAPS) >>> > >>> > for sigma, MYMAP in multis: >>> > >>> > print(sigma) >>> > >>> filename='skymap_'+alpha+'_'+str(sigma)+'_'+str(npha)+'_'+str(phamin)+'_'+str(phamax)+'_'+str(nobs)+'_'+str(obamin)+'_'+str(obamax)+'_'+str(nex)+'_'+str(Lemin)+'_'+str(Lemax)+'_.dat' >>> > >>> > MYfile = open(filename, 'a') >>> > for k in eel: >>> > for j in indobs: >>> > for i in indpha: >>> > A=MYMAP[i, j, k] >>> > stringa = str(A) + ',' >>> > MYfile.write(stringa) >>> > accapo = '\n' >>> > MYfile.write(accapo) >>> > >>> > MYfile.close() >>> > >>> > >>> > if __name__ == "__main__": >>> > if len(sys.argv)<=1: >>> > stepENE, nex, Lemin, Lemax = LEstep(200) >>> > elif len(sys.argv)<=2: >>> > stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) >>> > else: >>> > stepENE, nex, Lemin, Lemax = LEstep(int(sys.argv[1])) >>> > freq=float(sys.argv[2]) >>> > >>> > >>> > #mymain(stepENE, nex, Lemin, Lemax, freq) >>> > >>> > #print('profile') >>> > cProfile.run('mymain(stepENE, nex, Lemin, Lemax, freq)', 'restats', >>> 'time') >>> > >>> > p = pstats.Stats('restats') >>> > p.strip_dirs().sort_stats('name') >>> > p.sort_stats('time').print_stats(20) >>> > >>> >>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> https://mail.python.org/mailman/listinfo/tutor >>> >> >> -------------- next part -------------- An HTML attachment was scrubbed... URL: From badouglas at gmail.com Sun Apr 13 08:54:38 2014 From: badouglas at gmail.com (bruce) Date: Sun, 13 Apr 2014 02:54:38 -0400 Subject: [Tutor] cdata/aml question.. Message-ID: Hi. The following text contains sample data. I'm simply trying to parse it using libxml2dom as the lib to extract data. As an example, to get the name/desc test data d = libxml2dom.parseString(s, html=1) p1="//department/name" p2="//department/desc" pcount_ = d.xpath(p1) p2_ = d.xpath(p2) print str(len(pcount_)) nba=0 for a in pcount_: abbrv=a.nodeValue print abbrv abbrv=a.toString() print abbrv abbrv=a.textContent print abbrv neither of the above generates any of the CML name/desc data.. any pointers on what I'm missing??? I can/have created a quick parse/split process to get the data, but I thought there'd be a straight forward process to extract the data using one of the py/libs.. thanks From keithadu at live.com Sun Apr 13 06:39:36 2014 From: keithadu at live.com (keith papa) Date: Sun, 13 Apr 2014 00:39:36 -0400 Subject: [Tutor] A Byte of Python or Learn python the hard way Message-ID: I want to start learning python today and I wanted to know which book I should start with: A Byte of Python or Learn python the hard way? -------------- next part -------------- An HTML attachment was scrubbed... URL: From ben+python at benfinney.id.au Sun Apr 13 10:29:43 2014 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 13 Apr 2014 18:29:43 +1000 Subject: [Tutor] A Byte of Python or Learn python the hard way References: Message-ID: <854n1xy6mg.fsf@benfinney.id.au> keith papa writes: > I want to start learning python today Welcome. What is your current level of programming knowledge? What langauges, if any, do you already use? What are your goals for learning Python? What will you use it for? -- \ ?Few things are harder to put up with than the annoyance of a | `\ good example.? ?Mark Twain, _Pudd'n'head Wilson_ | _o__) | Ben Finney From petluke at gmail.com Sun Apr 13 10:31:47 2014 From: petluke at gmail.com (Luke Pettit) Date: Sun, 13 Apr 2014 18:31:47 +1000 Subject: [Tutor] A Byte of Python or Learn python the hard way In-Reply-To: <854n1xy6mg.fsf@benfinney.id.au> References: <854n1xy6mg.fsf@benfinney.id.au> Message-ID: https://www.coursera.org/course/interactivepython?from_restricted_preview=1&course_id=971041&r=https%3A%2F%2Fclass.coursera.org%2Finteractivepython-003%2Fclass On 13 April 2014 18:29, Ben Finney wrote: > keith papa writes: > > > I want to start learning python today > > Welcome. > > What is your current level of programming knowledge? What langauges, if > any, do you already use? > > What are your goals for learning Python? What will you use it for? > > -- > \ "Few things are harder to put up with than the annoyance of a | > `\ good example." --Mark Twain, _Pudd'n'head Wilson_ | > _o__) | > Ben Finney > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Luke Pettit ,,, ^..^,,, http://lukepettit-3d.blogspot.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sun Apr 13 10:56:29 2014 From: __peter__ at web.de (Peter Otten) Date: Sun, 13 Apr 2014 10:56:29 +0200 Subject: [Tutor] cdata/aml question.. References: Message-ID: bruce wrote: > The following text contains sample data. I'm simply trying to parse it > using libxml2dom as the lib to extract data. > > As an example, to get the name/desc > > test data > HTG]]> Heritage]]> > > d = libxml2dom.parseString(s, html=1) > > p1="//department/name" > p2="//department/desc" > > pcount_ = d.xpath(p1) > p2_ = d.xpath(p2) > print str(len(pcount_)) > nba=0 > > for a in pcount_: > abbrv=a.nodeValue > print abbrv > abbrv=a.toString() > print abbrv > abbrv=a.textContent > print abbrv > > neither of the above generates any of the CML name/desc data.. > > any pointers on what I'm missing??? Your example seems to work here when I omit the html=1 d = libxml2dom.parseString(s) ... > I can/have created a quick parse/split process to get the data, but I > thought there'd be a straight forward process to extract the data > using one of the py/libs.. One way using the stdlib: from xml.etree import ElementTree as ET #root = ET.parse(filename).getroot() root = ET.fromstring(data) for department in root.findall(".//department"): name = department.find("name").text desc = department.find("desc").text print("{}: {}".format(name, desc)) From stefan_ml at behnel.de Sun Apr 13 12:45:08 2014 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 13 Apr 2014 12:45:08 +0200 Subject: [Tutor] cdata/aml question.. In-Reply-To: References: Message-ID: Peter Otten, 13.04.2014 10:56: > from xml.etree import ElementTree as ET > #root = ET.parse(filename).getroot() > root = ET.fromstring(data) > for department in root.findall(".//department"): > name = department.find("name").text > desc = department.find("desc").text name = department.findtext("name") desc = department.findtext("desc") > print("{}: {}".format(name, desc)) Stefan From keithadu at live.com Mon Apr 14 13:53:36 2014 From: keithadu at live.com (keith papa) Date: Mon, 14 Apr 2014 07:53:36 -0400 Subject: [Tutor] python errors Message-ID: Hi am a new to programming and I reading the book "Think python" am on chapter one and it mentioned some errors I need to look out for like: Syntax errors, Runtime errors and semantic errors. I wanted to know if you guys have some examples of the errors? -------------- next part -------------- An HTML attachment was scrubbed... URL: From lacation at gmail.com Mon Apr 14 17:30:03 2014 From: lacation at gmail.com (Laura Kauria) Date: Mon, 14 Apr 2014 18:30:03 +0300 Subject: [Tutor] Python & algorithms (Lang line simplification algorithm) In-Reply-To: References: Message-ID: Thanks a lot for all the help! I got the courage to start at least.. I started by converting the pseudocode I had to python. Still I have problems with perpendicular distance and creating a line with python. I need to create a line between two points and then check what is the distance between a line and intermediate points which were between lines start and end point. If someone could help me with this? I could not understand can I do this without math library or not? I'm a geographer student so all possible library suggestions concerning spatial data is also appreciated. Laura 2014-04-06 21:30 GMT+03:00 Danny Yoo : > Hi Laura, > > > Algorithmic code typically is simple enough that standard language > features should suffice. I think you might pick up an external > library to make it easier to visualize graphical output, but the core > algorithms there appear fairly straightforward. > > To get some familiarity with basic Python programming, take a look at > a tutorial like: > > http://www.greenteapress.com/thinkpython/thinkpython.html > > and see if you can pick up the basic language features of Python. If > you have questions with that tutorial, feel free to ask here. > > > It looks like you need enough to work with structured data (classes) > and the basic data structures (lists, numbers). At least from my > reading of the "curve simplification" page you pointed us to, I don't > see anything there that's too bad. > > * You'll want a way to represent points. I think a structured value > would be appropriate. Think classes. > > * You'll want to represent a sequence of these points. In Java, you > can hold that collection with ArrayLists or some other list > implementation. In Python, there's a generic list data structure that > serves a similar purpose. > > > For example, in Java, you'd represent structured data with classes, > and a collection of these with a List: > > /////////////////////////////////////////////////////////////// > class Person { > private String name; > public Person(String name) { this.name = name; } > public void greet() { System.out.println("Hello, my name is " + name); > } > } > > // Usage: > Person p = new Person("Laura"); > p.sayHello(); > Person p2 = new Person("Lydia"); > List people = new ArrayList<>(); > people.add(p); > people.add(p2); > /////////////////////////////////////////////////////////////// > > > And in Python, you can do an analogous construction: > > ##################################### > class Person(object): > def __init__(self, name): > self.name = name > def greet(self): > print("Hello, my name is " + self.name) > > ## Usage > p = Person("Laura") > p.sayHello() > p2 = Person("Lydia") > people = [] > people.append(p) > people.append(p2) > ##################################### > > > So there should be a lot of transfer of basic knowledge between what > you've learned in Java to Python programming. Many of the concepts > are the same, but the names are just different because of the Tower of > Babel effect. A basic tutorial of Python will cover these general > topics. > > > What might be domain-specific here is the visualization part: you'll > want to make it easy to visually see the output of these graphical > algorithms. You may want to visualize these points on a graphical > canvas on screen. You could have your program generate an image file > like a .png, .gif, or .svg file. > > Another approach may be to use a GUI toolkit that opens up a canvas as > part of the program, where you can then manipulate the canvas. If you > want to take that approach, you might consider Tkinter, which is a GUI > library that should come bundled with Python if I'm not mistaken. > See: > > http://effbot.org/tkinterbook/canvas.htm > > for a canvas example. So once you have the basic algorithms down, you > might use a Tkinter canvas to visualize and see that your algorithms > are doing something reasonable. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Apr 14 18:25:09 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 14 Apr 2014 17:25:09 +0100 Subject: [Tutor] python errors In-Reply-To: References: Message-ID: On 14/04/14 12:53, keith papa wrote: > Hi am a new to programming and I reading the book "Think python" am on > chapter one and it mentioned some errors I need to look out for like: > Syntax errors, Runtime errors and semantic errors. I wanted to know > if you guys have some examples of the errors? You will create your own examples soon enough! :-) But if you want to see what some look like try the following (assuming you use Python v3) Syntax error (The code is not valid python): >>> print "Python rocks!" Runtime error (The code is valid but the result is not due to runtime issues): >>> foo = [] >>> print(foo[2]) Semantic error (The code/design is logically wrong even if valid syntactically) >>> x = "foo" - 4 Try those in the interpreter and see what happens. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From bgailer at gmail.com Mon Apr 14 21:21:48 2014 From: bgailer at gmail.com (bob gailer) Date: Mon, 14 Apr 2014 15:21:48 -0400 Subject: [Tutor] Python & algorithms (Lang line simplification algorithm) In-Reply-To: References: Message-ID: <534C354C.5020102@gmail.com> On 4/14/2014 11:30 AM, Laura Kauria wrote: > Thanks a lot for all the help! I got the courage to start at least.. > Some requests regarding posts. 1) put your comments following the relevant text rather than at the top. 2) delete old (irrelevant) text. 4) be more clear with your questions. > I started by converting the pseudocode I had to python. 3) post your code > Still I have problems with perpendicular distance and creating a line > with python. > I need to create a line between two points what does that mean (apart from the obvious) in terms of your code> > and then check what is the distance between a line and intermediate > points which were between lines start and end point. If someone could > help me with this? > I'm a geographer student so all possible library suggestions > concerning spatial data is also appreciated. > 4) be more clear with your questions. I can make guesses about what you want, but that wastes time. " distance between a line and intermediate points which were between lines start and end point" as I read this the answer seems to be zero. ( a point that is between lines start and end point is on the line). I don't think you mean that. We are here to help. The more you tell us the easier it is for us to help. From alan.gauld at btinternet.com Mon Apr 14 22:55:32 2014 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Mon, 14 Apr 2014 21:55:32 +0100 (BST) Subject: [Tutor] python errors In-Reply-To: References: , Message-ID: <1397508932.26632.YahooMailNeo@web186005.mail.ir2.yahoo.com> From: keith papa >To: Alan Gauld >Sent: Monday, 14 April 2014, 18:19 >Subject: RE: [Tutor] python errors > > > > >>>> x = "foo" - 4 > > >Traceback (most recent call last): >? File "", line 1, in >? ? x = "foo" - 4 >TypeError: unsupported operand type(s) for -: 'str' and 'int' >>>>? > >That's right, its a semantic error. You ?shouldn't be trying to subtract a number? from a string. It's not a meaningful thing to do, The Python errors are not categorised into syntactic, semantic and runtime error types, (although there is a syntax error exception). Those are computer? science concepts not explicitly exposed by Python. Python just reports an? error as above. Alan g. -------------- next part -------------- An HTML attachment was scrubbed... URL: From lacation at gmail.com Mon Apr 14 22:21:17 2014 From: lacation at gmail.com (Laura Kauria) Date: Mon, 14 Apr 2014 23:21:17 +0300 Subject: [Tutor] Python & algorithms (Lang line simplification algorithm) In-Reply-To: <534C354C.5020102@gmail.com> References: <534C354C.5020102@gmail.com> Message-ID: Hi, sorry I don't know the conventions yet. I'd need to know how to create a line between two points. I don't know after many hours checking, how to convert this pseudocode to python. line = new Line( pointList[startPoint], pointList[endPoint]) I've done point list and indexes already, put don't know how to create the line. Laura 2014-04-14 22:21 GMT+03:00 bob gailer : > On 4/14/2014 11:30 AM, Laura Kauria wrote: > >> Thanks a lot for all the help! I got the courage to start at least.. >> >> Some requests regarding posts. > > 1) put your comments following the relevant text rather than at the top. > 2) delete old (irrelevant) text. > 4) be more clear with your questions. > > I started by converting the pseudocode I had to python. >> > > 3) post your code > > Still I have problems with perpendicular distance and creating a line >> with python. >> I need to create a line between two points >> > what does that mean (apart from the obvious) in terms of your code> > >> and then check what is the distance between a line and intermediate >> points which were between lines start and end point. If someone could help >> me with this? >> I'm a geographer student so all possible library suggestions concerning >> spatial data is also appreciated. >> >> 4) be more clear with your questions. > > I can make guesses about what you want, but that wastes time. > > " distance between a line and intermediate points which were between lines > start and end point" as I read this the answer seems to be zero. ( a point > that is between lines start and end point is on the line). I don't think > you mean that. > > We are here to help. The more you tell us the easier it is for us to help. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Apr 15 01:22:12 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 15 Apr 2014 00:22:12 +0100 Subject: [Tutor] Python & algorithms (Lang line simplification algorithm) In-Reply-To: References: Message-ID: On 14/04/14 16:30, Laura Kauria wrote: > I need to create a line between two points and then check what is the > distance between a line and intermediate points which were between lines > start and end point. When you say you want to "create a line" what do you mean? Do you want to - create a line in data as a set of points(at what precision?) - plot or draw a line on screen (with axes?) - build a mathematical model of a line? Checking distances/intersections etc only requires the math model. But if you want to visualise it you may prefer a combination of all three. We need a bit more detail about what you are expecting as a result. The three options above all have solutions that are potentially very different. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Tue Apr 15 01:24:30 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 15 Apr 2014 00:24:30 +0100 Subject: [Tutor] Python & algorithms (Lang line simplification algorithm) In-Reply-To: References: <534C354C.5020102@gmail.com> Message-ID: On 14/04/14 21:21, Laura Kauria wrote: > line = new Line( pointList[startPoint], pointList[endPoint]) line = Line(pointList[startPoint], pointList[endPoint]) is the translation. But that probably doesn't help much. > I've done point list and indexes already, put don't know how to create > the line. See my other post. What do you mean by "create a line"? What is your expectation of the end result? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From ben+python at benfinney.id.au Tue Apr 15 02:23:09 2014 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 15 Apr 2014 10:23:09 +1000 Subject: [Tutor] Python & algorithms (Lang line simplification algorithm) References: Message-ID: <85r44zwidu.fsf@benfinney.id.au> Laura Kauria writes: > Thanks a lot for all the help! I got the courage to start at least.. Congratulations! Courage is a necessary ingredient when starting :-) Could you please avoid top-posting, and instead use interleaved style for your replies, so the conversation is in a natural order. > I started by converting the pseudocode I had to python. If it's short and simple, please post it here so we can discuss it in context. > Still I have problems with perpendicular distance and creating a line > with python. > I need to create a line between two points and then check what is the > distance between a line and intermediate points which were between lines > start and end point. If someone could help me with this? I could not > understand can I do this without math library or not? The ?math? module in the standard library has trigonometric functions . If you have co-ordinate data and know how to use trigonometry, then those functions will do what you expect. -- \ ?If I melt dry ice, can I swim without getting wet?? ?Steven | `\ Wright | _o__) | Ben Finney From brianjamesarb at gmail.com Tue Apr 15 04:09:55 2014 From: brianjamesarb at gmail.com (brian arb) Date: Mon, 14 Apr 2014 22:09:55 -0400 Subject: [Tutor] Why should modules or packages should define their own domain-specific base exception class? Message-ID: I don't quite understand why the google python style guide recommends that packages and modules we write should avoid using the catch-all except. Instead the guide encourages you to write domain specific exception classes. class Error(Exception): """...""" class ThisSpecificError(Error): """...""" class ThatSpecificError(Error): """...""" try: ... except mylib.Error: ... REF. http://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=Exceptions#Exceptions Thoughts? -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Tue Apr 15 04:29:37 2014 From: bgailer at gmail.com (bob gailer) Date: Mon, 14 Apr 2014 22:29:37 -0400 Subject: [Tutor] Why should modules or packages should define their own domain-specific base exception class? In-Reply-To: References: Message-ID: <534C9991.4080804@gmail.com> On 4/14/2014 10:09 PM, brian arb wrote: > I don't quite understand why the google python style guide recommends > that packages and modules we write should avoid using the catch-all > except. Instead the guide encourages you to write domain specific > exception classes. > > class Error(Exception): > """...""" > > class ThisSpecificError(Error): > """...""" > > class ThatSpecificError(Error): > """...""" > > > try: > ... > except mylib.Error: > ... > | > | > |REF. > |http://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=Exceptions#Exceptions > > Thoughts? Let's say I write a program to process some user input that should follow some syntatic / semantic rules. When my program finds a violation I want to raise an exception that is specific to these rules, rather than a built-in exception. Raising such exceptions is a useful way to exit from some possibly deeply nested series of calls, ifs, whiles, fors, etc. I write except statements for my error classes, then let the python-specific ones be caught by a different except. The python-specific exceptions mean something is wrong with my progam, which I handle differently than those related to user input. From sunil.techspk at gmail.com Tue Apr 15 08:06:42 2014 From: sunil.techspk at gmail.com (Sunil Tech) Date: Tue, 15 Apr 2014 11:36:42 +0530 Subject: [Tutor] reading a csv file Message-ID: Hi, #!/usr/bin/python import csv import sys def main(): cr = csv.reader(open("data.csv","rb")) for row in cr: print row if __name__ == "__main__": sys.exit(main()) when i run this... i get Traceback (most recent call last): File "data.py", line 14, in sys.exit(main()) File "data.py", line 9, in main cr = csv.reader(open("data.csv","rb")) AttributeError: 'module' object has no attribute 'reader' can anyone correct me please. thank you in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From sunil.techspk at gmail.com Tue Apr 15 08:18:49 2014 From: sunil.techspk at gmail.com (Sunil Tech) Date: Tue, 15 Apr 2014 11:48:49 +0530 Subject: [Tutor] reading a csv file In-Reply-To: References: Message-ID: Please ignore previous email. this error occurred as i had previously created .pyc file... after deleting that .pyc file.. now it is working fine. Thank you. On Tue, Apr 15, 2014 at 11:36 AM, Sunil Tech wrote: > Hi, > > #!/usr/bin/python > > import csv > import sys > > def main(): > cr = csv.reader(open("data.csv","rb")) > for row in cr: > print row > > if __name__ == "__main__": > sys.exit(main()) > > > > when i run this... > > i get > > Traceback (most recent call last): > File "data.py", line 14, in > sys.exit(main()) > File "data.py", line 9, in main > cr = csv.reader(open("data.csv","rb")) > AttributeError: 'module' object has no attribute 'reader' > > > can anyone correct me please. > > thank you in advance. > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sunil.techspk at gmail.com Tue Apr 15 08:24:45 2014 From: sunil.techspk at gmail.com (Sunil Tech) Date: Tue, 15 Apr 2014 11:54:45 +0530 Subject: [Tutor] Fwd: Puzzle - Next Step to our interviewing process - Pramati Technologies! In-Reply-To: References: <06a601cf0b86$aefe5360$0cfafa20$@pramati.com> <03d301cf0c34$e3796450$aa6c2cf0$@pramati.com> <01e801cf0cee$98e359e0$caaa0da0$@pramati.com> <011f01cf0db5$5fc59660$1f50c320$@pramati.com> <076401cf0df4$8f481180$add83480$@pramati.com> <053c01cf1032$11b7c860$35275920$@pramati.com> <067401cf1044$a88717c0$f9954740$@pramati.com> <044601cf11b1$03387930$09a96b90$@pramati.com> <05f001cf11bf$619b09d0$24d11d70$@pramati.com> <07a901cf11d7$d2927c50$77b774f0$@pramati.com> <019801cf1276$6583a180$308ae480$@pramati.com> <07bd01cf12a0$4dd621a0$e98264e0$@pramati.com> <01e201cf133b$30ce7a30$926b6e90$@pramati.com> <025801cf1596$6bc7fd20$4357f760$@pramati.com> <027d01cf1665$40045f10$c00d1d30$@pramati.com> <01ac01cf1728$22d68ad0$6883a070$@pramati.com> <019001cf18c9$305629c0$91027d40$@pramati.com> <047c01cf1b20$d946e840$8bd4b8c0$@pramati.com> <076b01cf1b3e$b0ece070$12c6a150$@pramati.com> <0bf601cf1b5e$aa067760$fe136620$@pramati.com> <085701cf1c0c$bbda1290$338e37b0$@pramati.com> <074701cf1cde$77f89150$67e9b3f0$@pramati.com> <027401cf1d76$6c6f65a0$454e30e0$@pramati.com> <088501cf1d9f$128a41d0$379ec570$@pramati.com> <09af01cf1dab$7e68e350$7b3aa9f0$@pramati.com> <01e801cf1e36$e6fea8b0$b4fbfa10$@pramati.com> <042501cf20b1$c00a1460$401e3d20$@pramati.com> <000301cf20ca$da170ca0$8e4525e0$@pramati.com> <023b01cf22fd$0a2bd9f0$1e838dd0$@pramati.com> <06ce01cf2645$48559180$d900b480$@pramati.com> <084301cf2654$f74757f0$e5d607d0$@pramati.com> <00eb01cf26db$fa213f90$ee63beb0$@pramati.com> <04ea01cf26f0$ccc4a680$664df380$@pramati.com> <076901cf2705$54e187e0$fea497a0$@pramati.com> <023c01cf27ae$0652b4b0$12f81e10$@pramati.com> <024801cf27ae$24ea4780$6ebed680$@pramati.com> <084301cf27e2$2b2d8790$818896b0$@pramati.com> <04bc01cf288d$a72b0d30$f5812790$@pramati.com> <040301cf2bb5$8d57cc10$a8076430$@pramati.com> <058c01cf2bc6$2ef3ead0$8cdbc070$@pramati.com> <04ba01cf345e$114ec9a0$33ec5ce0$@pramati.com> <051601cf3463$6f4c9410$4de5bc30$@pramati.com> <04b601cf3c24$5c48ae30$14da0a90$@pramati.com> <08c901cf3ea1$e512e030$af38a090$@pramati.com> <01dd01cf434a$e0869130$a193b390$@pramati.com> <02d001cf44c9$a27e3890$e77aa9b0$@pramati.com> <02a701cf4715$2d165f80$87431e80$@pramati.com> <04a701cf47f9$32bf7270$983e5750$@pramati.com> <033401cf48bd$ce0b2750$6a2175f0$@pramati.com> <03fb01cf48c4$66f86b20$34e94160$@pramati.com> <022101cf4978$ba8475e0$2f8d61a0$@pramati.com> <014201cf4ef1$3aeba3c0$b0c2eb40$@pramati.com> <015c01cf4ef1$b0978a80$11c69f80$@pramati.com> <01bd01cf4fbc$c256fb40$4704f1c0$@pramati.com> <036601cf52f9$aaf76cb0$00e64610$@pramati.com> <052d01cf5311$10d9c390$328d4ab0$@pramati.com> <02b301cf5478$b1d21e60$15765b20$@pramati.com> <03e101cf57a9$227caf80$67760e80$@pramati.com> Message-ID: Kindly assess the problem carefully including all the possibilities, including but not limited to: 1. Menu directly available(1 item or all items). 2. Menu available but distributed over multiple items. 3. Menu need not be present in all restaurants listed. 4. Menu not available at all. Please complete the puzzle using the development language that you are being interviewed for. Most people have send responses that work for the dataset and the test cases described along with the problem. However we do use a different dataset and test cases that try and check some boundary conditions. We have seen many solutions that work for the test cases below but fail with our internally used test cases. Because it is the Internet Age, but also it is a recession, the Comptroller of the town of Jurgensville has decided to publish the prices of every item on every menu of every restaurant in town, all in a single CSV file (Jurgensville is not quite up to date with modern data serializationmethods). In addition, the restaurants of Jurgensville also offer Value Meals, which are groups of several items, at a discounted price. The Comptroller has also included these Value Meals in the file. The file's format is: for lines that define a price for a single item: restaurant ID, price, item label for lines that define the price for a Value Meal (there can be any number of items in a value meal) restaurant ID, price, item 1 label, item 2 label, ... All restaurant IDs are integers, all item labels are lower case letters and underscores, and the price is a decimal number. Because you are an expert software engineer, you decide to write a program that accepts the town's price file, and a list of item labels that someone wants to eat for dinner, and outputs the restaurant they should go to, and the total price it will cost them. It is okay to purchase extra items, as long as the total cost is minimized. Here are some sample data sets, program inputs, and the expected result: ---------------------------- Data File data.csv 1, 4.00, burger 1, 8.00, tofu_log 2, 5.00, burger 2, 6.50, tofu_log Program Input program data.csv burger tofu_log Expected Output => 2, 11.5 --------------------------- ---------------------------- Data File data.csv 3, 4.00, chef_salad 3, 8.00, steak_salad_sandwich 4, 5.00, steak_salad_sandwich 4, 2.50, wine_spritzer Program Input program data.csv chef_salad wine_spritzer Expected Output => nil (or null or false or something to indicate that no matching restaurant could be found) --------------------------- ---------------------------- Data File data.csv 5, 4.00, extreme_fajita 5, 8.00, fancy_european_water 6, 5.00, fancy_european_water 6, 6.00, extreme_fajita, jalapeno_poppers, extra_salsa Program Input program data.csv fancy_european_water extreme_fajita Expected Output => 6, 11.0 --------------------------- We have included all these samples in a single data file, sample_data.csv. Please include instructions for how to run your program with your submission. -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: sample_data.csv Type: application/vnd.ms-excel Size: 322 bytes Desc: not available URL: From alan.gauld at btinternet.com Tue Apr 15 10:10:44 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 15 Apr 2014 09:10:44 +0100 Subject: [Tutor] reading a csv file In-Reply-To: References: Message-ID: On 15/04/14 07:06, Sunil Tech wrote: > Hi, > > #!/usr/bin/python > > import csv > import sys > > def main(): > cr = csv.reader(open("data.csv","rb")) > for row in cr: > print row > when i run this... > > cr = csv.reader(open("data.csv","rb")) > AttributeError: 'module' object has no attribute 'reader' > The most common cause of this error is that you have created a file called csv.py which is masking the library module. Could that be the case here? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From lacation at gmail.com Tue Apr 15 08:51:38 2014 From: lacation at gmail.com (Laura Kauria) Date: Tue, 15 Apr 2014 09:51:38 +0300 Subject: [Tutor] Python & algorithms (Lang line simplification algorithm) In-Reply-To: <85r44zwidu.fsf@benfinney.id.au> References: <85r44zwidu.fsf@benfinney.id.au> Message-ID: Okay I'll try. What the main job is to do an algorithm which simples a line. Algorithm deletes points which are unnecessary accurate according to tolerance distance user gives. >> I started by converting the pseudocode I had to python. Pseudocode: http://web.cs.sunyit.edu/~poissad/projects/Curve/about_algorithms/lang 1. function lang(PointList[], Tolerance) 2. key=0 3. endP= PointList.length-1 4. do { 5. endP= PointList.length-1 6. if (key+1 != endP) // If there are intermediate points 7. line= new Line( PointList[key], PointList[endP]) 8. /* Find the point with the furthest perpendicular distance */ 9. maxIndex= key+1 10. maxD= perpendicularDistance(line, PointList[maxIndex]) 11. for (i=maxIndex+1; i maxD) 14. maxIndex=i 15. maxD=d 16. if (maxD > Tolerance) 17. endP--; 18. else 19. for (i=key+1; i> Still I have problems with perpendicular distance and creating a line >> with python. What I mean by creating a line is what Alan answered "checking distances/intersections etc only requires the math model." I need the mathematical model not a picture/graph. I'll try move forward with Bens suggestion of ?math? module which probably help me with both mathematical line model between two points and its distance between the intermediate points. > . Here is a picture of the lang algorithm, where you can see distance of interest by purple http://psimpl.sourceforge.net/lang.html. I'll ask later if I can't get this work. Cheers! 2014-04-15 3:23 GMT+03:00 Ben Finney : > Laura Kauria writes: > > > Thanks a lot for all the help! I got the courage to start at least.. > > Congratulations! Courage is a necessary ingredient when starting :-) > > Could you please avoid top-posting, and instead use interleaved style > for > your replies, so the conversation is in a natural order. > > > I started by converting the pseudocode I had to python. > > If it's short and simple, please post it here so we can discuss it in > context. > > > Still I have problems with perpendicular distance and creating a line > > with python. > > I need to create a line between two points and then check what is the > > distance between a line and intermediate points which were between lines > > start and end point. If someone could help me with this? I could not > > understand can I do this without math library or not? > > The ?math? module in the standard library has trigonometric functions > . > If you have co-ordinate data and know how to use trigonometry, then > those functions will do what you expect. > > -- > \ ?If I melt dry ice, can I swim without getting wet?? ?Steven | > `\ Wright | > _o__) | > Ben Finney > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From vkuritza at gmail.com Tue Apr 15 07:06:13 2014 From: vkuritza at gmail.com (Victoria Kuritza) Date: Tue, 15 Apr 2014 00:06:13 -0500 Subject: [Tutor] Quick Question Message-ID: <400FA11A-2D0E-4EC1-81D0-A3EAE56B61BC@aol.com> How can I search in a corpus for three or more occurrences of capitalized words in a line? What would I input as my search? Cheers, Victoria K. -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Tue Apr 15 10:21:25 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 15 Apr 2014 09:21:25 +0100 Subject: [Tutor] Quick Question In-Reply-To: <400FA11A-2D0E-4EC1-81D0-A3EAE56B61BC@aol.com> References: <400FA11A-2D0E-4EC1-81D0-A3EAE56B61BC@aol.com> Message-ID: On 15/04/2014 06:06, Victoria Kuritza wrote: > How can I search in a corpus for three or more occurrences of > capitalized words in a line? What would I input as my search? > > > Cheers, > > Victoria K. > What does your code currently look like? What problems are you actually having? Last and by no means least, where is the Python question here? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From sunil.techspk at gmail.com Tue Apr 15 10:36:53 2014 From: sunil.techspk at gmail.com (Sunil Tech) Date: Tue, 15 Apr 2014 14:06:53 +0530 Subject: [Tutor] reading a csv file In-Reply-To: References: Message-ID: yes Alan, what you said is true. Thank you. On Tue, Apr 15, 2014 at 1:40 PM, Alan Gauld wrote: > On 15/04/14 07:06, Sunil Tech wrote: > >> Hi, >> >> #!/usr/bin/python >> >> import csv >> import sys >> >> def main(): >> cr = csv.reader(open("data.csv","rb")) >> for row in cr: >> print row >> > > when i run this... >> >> >> cr = csv.reader(open("data.csv","rb")) >> AttributeError: 'module' object has no attribute 'reader' >> >> > The most common cause of this error is that you have > created a file called csv.py which is masking the > library module. > > Could that be the case here? > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Tue Apr 15 14:25:44 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 15 Apr 2014 22:25:44 +1000 Subject: [Tutor] Quick Question In-Reply-To: <400FA11A-2D0E-4EC1-81D0-A3EAE56B61BC@aol.com> References: <400FA11A-2D0E-4EC1-81D0-A3EAE56B61BC@aol.com> Message-ID: <20140415122539.GH11385@ando> Hi Victoria, On Tue, Apr 15, 2014 at 12:06:13AM -0500, Victoria Kuritza wrote: > How can I search in a corpus for three or more occurrences of > capitalized words in a line? What would I input as my search? I'm afraid that question is a bit *too* quick. What are you using for search? My guess is that you're using NLTK, but I'm afraid I don't know a lot about that. It would help if you showed us what you're using to search for a single capitalized word. Regards, Steve From qianyunguo at gmail.com Tue Apr 15 14:56:16 2014 From: qianyunguo at gmail.com (Qianyun Guo) Date: Tue, 15 Apr 2014 14:56:16 +0200 Subject: [Tutor] questions when define a class Message-ID: Hi all, I am trying to get a suffix tree from a string. I use three classes, Node, Edge, SuffixTree. I have two questions when implementing: ?1? >>> a = Edge(1,2,3,4) >>> a.length 1 if I remove '@property' in my code, it returns as below: >>> a = Edge(1,2,3,4) >>> a.length >>> a.length() 1 I don't really understand the differences w/ @property, and the differences of a.length and a.length(), could you explain? ?2? In SuffixTree, I define two functions, _get_str_from_edge, _get_str_from_node, the latter depend on the first one (please see my code). Then I have this problem: >>> a = SuffixTree('abcd') >>> a._get_str_from_edge(a.edges[(0,1)]) 'abcd$' >>> a._get_str_from_node(0,1) TypeError: _get_str_from_edge() takes exactly 2 arguments (3 given) Could you tell me what's wrong here? below is my code, __repr__ are removed for convenience. ################################### class Node: def __init__(self, parent_node): self.suffix_node = -1 self.parent = parent_node self.children = [] def add_child(self, child_node_index): self.children.append(child_node_index) class Edge: def __init__(self, first_char_index, last_char_index,\ source_node_index, dest_node_index): self.first_char_index = first_char_index self.last_char_index = last_char_index self.source_node_index = source_node_index self.dest_node_index = dest_node_index @property def length(self): return self.last_char_index - self.first_char_index class SuffixTree: def __init__(self, string): self.string = string+'$' self.N = len(self.string) self.nodes = [] self.num_nodes = 0 self.edges = {} #initialize two node tree root = Node(-1) root.suffix_node = -1 self._add_node(root) leaf = Node(0) leaf.suffix_node = 0 self._add_node(leaf) self.nodes[0].add_child(1) edge = Edge(0, self.N, 0, 1) self._add_edge(edge) def _get_str_from_edge(self, edge): return self.string[edge.first_char_index : edge.last_char_index] def _get_str_from_node(self, source_node_index, dest_node_index): return self._get_str_from_edge(self, self.edges[(source_node_index, \ dest_node_index)]) def _add_node(self, node): self.nodes.append(node) self.num_nodes += 1 def _add_edge(self, edge): self.edges[(edge.source_node_index, edge.dest_node_index)] = edge self.nodes[edge.source_node_index].add_child(edge.dest_node_index) self.nodes[edge.dest_node_index].parent = edge.source_node_index -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Tue Apr 15 15:46:39 2014 From: __peter__ at web.de (Peter Otten) Date: Tue, 15 Apr 2014 15:46:39 +0200 Subject: [Tutor] questions when define a class References: Message-ID: Qianyun Guo wrote: > Hi all, I am trying to get a suffix tree from a string. I use three > classes, Node, Edge, SuffixTree. I have two questions when implementing: > > ?1? > >>>> a = Edge(1,2,3,4) > >>>> a.length > > 1 > if I remove '@property' in my code, it returns as below: > >>>> a = Edge(1,2,3,4) > >>>> a.length > > > >>>> a.length() > > 1 > > > > I don't really understand the differences w/ @property, and the > differences of a.length and a.length(), could you explain? Properties are a way to calculate attributes: > @property > def length(self): > return self.last_char_index - self.first_char_index In client code obj.length looks like an attribute, but does a calculation under the hood. This is particularly useful to keep a changing interface compatible to prior versions: version 1, everybody uses cartesian coordinates: class Point: def __init__(self, x, y): self.x = x self.y = y version 2, polar coordinates are so much better ;) class Point: def __init__(self, r, phi): self.r = r self.phi = phi @property def x(self): return self.r * math.cos(self.phi) @property def y(self): return self.r * math.sin(self.phi) Thanks to properties our new point can be passed to functions that expect cartesian coords. Without properties we'd either end up writing a trivial wrapper def get_x(self): return self.x for every attribute (the Java way) and never use attributes directly or we'd have to change all occurences of point.x to point.x(). > ?2? > > In SuffixTree, I define two functions, _get_str_from_edge, > _get_str_from_node, the latter depend on the first one (please see my > code). Then I have this problem: > >>>> a = SuffixTree('abcd') > >>>> a._get_str_from_edge(a.edges[(0,1)]) > > 'abcd$' > >>>> a._get_str_from_node(0,1) > > TypeError: _get_str_from_edge() takes exactly 2 arguments (3 given) > > Could you tell me what's wrong here? > def _get_str_from_node(self, source_node_index, dest_node_index): > return self._get_str_from_edge(self, > self.edges[(source_node_index, > \ > dest_node_index)]) self._get_str_from_edge gives you a "bound method", i. e. one that already knows about self. Invoke it as return self._get_str_from_edge( self.edges[(source_node_index, dest_node_index)]) By the way, the backslash is not necessesary as long as there are open parens. Preferred: (1 + # no backslash 2) Obsolete alternative: 1 + \ 2 From dyoo at hashcollision.org Tue Apr 15 16:35:09 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 15 Apr 2014 07:35:09 -0700 Subject: [Tutor] Fwd: Puzzle - Next Step to our interviewing process - Pramati Technologies! In-Reply-To: References: <06a601cf0b86$aefe5360$0cfafa20$@pramati.com> <03d301cf0c34$e3796450$aa6c2cf0$@pramati.com> <01e801cf0cee$98e359e0$caaa0da0$@pramati.com> <011f01cf0db5$5fc59660$1f50c320$@pramati.com> <076401cf0df4$8f481180$add83480$@pramati.com> <053c01cf1032$11b7c860$35275920$@pramati.com> <067401cf1044$a88717c0$f9954740$@pramati.com> <044601cf11b1$03387930$09a96b90$@pramati.com> <05f001cf11bf$619b09d0$24d11d70$@pramati.com> <07a901cf11d7$d2927c50$77b774f0$@pramati.com> <019801cf1276$6583a180$308ae480$@pramati.com> <07bd01cf12a0$4dd621a0$e98264e0$@pramati.com> <01e201cf133b$30ce7a30$926b6e90$@pramati.com> <025801cf1596$6bc7fd20$4357f760$@pramati.com> <027d01cf1665$40045f10$c00d1d30$@pramati.com> <01ac01cf1728$22d68ad0$6883a070$@pramati.com> <019001cf18c9$305629c0$91027d40$@pramati.com> <047c01cf1b20$d946e840$8bd4b8c0$@pramati.com> <076b01cf1b3e$b0ece070$12c6a150$@pramati.com> <0bf601cf1b5e$aa067760$fe136620$@pramati.com> <085701cf1c0c$bbda1290$338e37b0$@pramati.com> <074701cf1cde$77f89150$67e9b3f0$@pramati.com> <027401cf1d76$6c6f65a0$454e30e0$@pramati.com> <088501cf1d9f$128a41d0$379ec570$@pramati.com> <09af01cf1dab$7e68e350$7b3aa9f0$@pramati.com> <01e801cf1e36$e6fea8b0$b4fbfa10$@pramati.com> <042501cf20b1$c00a1460$401e3d20$@pramati.com> <000301cf20ca$da170ca0$8e4525e0$@pramati.com> <023b01cf22fd$0a2bd9f0$1e838dd0$@pramati.com> <06ce01cf2645$48559180$d900b480$@pramati.com> <084301cf2654$f74757f0$e5d607d0$@pramati.com> <00eb01cf26db$fa213f90$ee63beb0$@pramati.com> <04ea01cf26f0$ccc4a680$664df380$@pramati.com> <076901cf2705$54e187e0$fea497a0$@pramati.com> <023c01cf27ae$0652b4b0$12f81e10$@pramati.com> <024801cf27ae$24ea4780$6ebed680$@pramati.com> <084301cf27e2$2b2d8790$818896b0$@pramati.com> <04bc01cf288d$a72b0d30$f5812790$@pramati.com> <040301cf2bb5$8d57cc10$a8076430$@pramati.com> <058c01cf2bc6$2ef3ead0$8cdbc070$@pramati.com> <04ba01cf345e$114ec9a0$33ec5ce0$@pramati.com> <051601cf3463$6f4c9410$4de5bc30$@pramati.com> <04b601cf3c24$5c48ae30$14da0a90$@pramati.com> <08c901cf3ea1$e512e030$af38a090$@pramati.com> <01dd01cf434a$e0869130$a193b390$@pramati.com> <02d001cf44c9$a27e3890$e77aa9b0$@pramati.com> <02a701cf4715$2d165f80$87431e80$@pramati.com> <04a701cf47f9$32bf7270$983e5750$@pramati.com> <033401cf48bd$ce0b2750$6a2175f0$@pramati.com> <03fb01cf48c4$66f86b20$34e94160$@pramati.com> <022101cf4978$ba8475e0$2f8d61a0$@pramati.com> <014201cf4ef1$3aeba3c0$b0c2eb40$@pramati.com> <015c01cf4ef1$b0978a80$11c69f80$@pramati.com> <01bd01cf4fbc$c256fb40$4704f1c0$@pramati.com> <036601cf52f9$aaf76cb0$00e64610$@pramati.com> <052d01cf5311$10d9c390$328d4ab0$@pramati.com> <02b301cf5478$b1d21e60$15765b20$@pramati.com> <03e101cf57a9$227caf80$67760e80$@pramati.com> Message-ID: So, what is your question? -------------- next part -------------- An HTML attachment was scrubbed... URL: From sunil.techspk at gmail.com Tue Apr 15 16:49:47 2014 From: sunil.techspk at gmail.com (Sunil Tech) Date: Tue, 15 Apr 2014 20:19:47 +0530 Subject: [Tutor] Fwd: Puzzle - Next Step to our interviewing process - Pramati Technologies! In-Reply-To: References: <06a601cf0b86$aefe5360$0cfafa20$@pramati.com> <03d301cf0c34$e3796450$aa6c2cf0$@pramati.com> <01e801cf0cee$98e359e0$caaa0da0$@pramati.com> <011f01cf0db5$5fc59660$1f50c320$@pramati.com> <076401cf0df4$8f481180$add83480$@pramati.com> <053c01cf1032$11b7c860$35275920$@pramati.com> <067401cf1044$a88717c0$f9954740$@pramati.com> <044601cf11b1$03387930$09a96b90$@pramati.com> <05f001cf11bf$619b09d0$24d11d70$@pramati.com> <07a901cf11d7$d2927c50$77b774f0$@pramati.com> <019801cf1276$6583a180$308ae480$@pramati.com> <07bd01cf12a0$4dd621a0$e98264e0$@pramati.com> <01e201cf133b$30ce7a30$926b6e90$@pramati.com> <025801cf1596$6bc7fd20$4357f760$@pramati.com> <027d01cf1665$40045f10$c00d1d30$@pramati.com> <01ac01cf1728$22d68ad0$6883a070$@pramati.com> <019001cf18c9$305629c0$91027d40$@pramati.com> <047c01cf1b20$d946e840$8bd4b8c0$@pramati.com> <076b01cf1b3e$b0ece070$12c6a150$@pramati.com> <0bf601cf1b5e$aa067760$fe136620$@pramati.com> <085701cf1c0c$bbda1290$338e37b0$@pramati.com> <074701cf1cde$77f89150$67e9b3f0$@pramati.com> <027401cf1d76$6c6f65a0$454e30e0$@pramati.com> <088501cf1d9f$128a41d0$379ec570$@pramati.com> <09af01cf1dab$7e68e350$7b3aa9f0$@pramati.com> <01e801cf1e36$e6fea8b0$b4fbfa10$@pramati.com> <042501cf20b1$c00a1460$401e3d20$@pramati.com> <000301cf20ca$da170ca0$8e4525e0$@pramati.com> <023b01cf22fd$0a2bd9f0$1e838dd0$@pramati.com> <06ce01cf2645$48559180$d900b480$@pramati.com> <084301cf2654$f74757f0$e5d607d0$@pramati.com> <00eb01cf26db$fa213f90$ee63beb0$@pramati.com> <04ea01cf26f0$ccc4a680$664df380$@pramati.com> <076901cf2705$54e187e0$fea497a0$@pramati.com> <023c01cf27ae$0652b4b0$12f81e10$@pramati.com> <024801cf27ae$24ea4780$6ebed680$@pramati.com> <084301cf27e2$2b2d8790$818896b0$@pramati.com> <04bc01cf288d$a72b0d30$f5812790$@pramati.com> <040301cf2bb5$8d57cc10$a8076430$@pramati.com> <058c01cf2bc6$2ef3ead0$8cdbc070$@pramati.com> <04ba01cf345e$114ec9a0$33ec5ce0$@pramati.com> <051601cf3463$6f4c9410$4de5bc30$@pramati.com> <04b601cf3c24$5c48ae30$14da0a90$@pramati.com> <08c901cf3ea1$e512e030$af38a090$@pramati.com> <01dd01cf434a$e0869130$a193b390$@pramati.com> <02d001cf44c9$a27e3890$e77aa9b0$@pramati.com> <02a701cf4715$2d165f80$87431e80$@pramati.com> <04a701cf47f9$32bf7270$983e5750$@pramati.com> <033401cf48bd$ce0b2750$6a2175f0$@pramati.com> <03fb01cf48c4$66f86b20$34e94160$@pramati.com> <022101cf4978$ba8475e0$2f8d61a0$@pramati.com> <014201cf4ef1$3aeba3c0$b0c2eb40$@pramati.com> <015c01cf4ef1$b0978a80$11c69f80$@pramati.com> <01bd01cf4fbc$c256fb40$4704f1c0$@pramati.com> <036601cf52f9$aaf76cb0$00e64610$@pramati.com> <052d01cf5311$10d9c390$328d4ab0$@pramati.com> <02b301cf5478$b1d21e60$15765b20$@pramati.com> <03e101cf57a9$227caf80$67760e80$@pramati.com> Message-ID: Hi Danny, Thank you for replying.. I need the python program which takes the attached csv & on running the program which will give me the results as ---------------------------- Data File data.csv 1, 4.00, burger 1, 8.00, tofu_log 2, 5.00, burger 2, 6.50, tofu_log Program Input program data.csv burger tofu_log Expected Output => 2, 11.5 --------------------------- ---------------------------- Data File data.csv 3, 4.00, chef_salad 3, 8.00, steak_salad_sandwich 4, 5.00, steak_salad_sandwich 4, 2.50, wine_spritzer Program Input program data.csv chef_salad wine_spritzer Expected Output => nil (or null or false or something to indicate that no matching restaurant could be found) --------------------------- ---------------------------- Data File data.csv 5, 4.00, extreme_fajita 5, 8.00, fancy_european_water 6, 5.00, fancy_european_water 6, 6.00, extreme_fajita, jalapeno_poppers, extra_salsa Program Input program data.csv fancy_european_water extreme_fajita Expected Output => 6, 11.0 On Tue, Apr 15, 2014 at 8:05 PM, Danny Yoo wrote: > So, what is your question? > -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Wed Apr 16 02:05:19 2014 From: wprins at gmail.com (Walter Prins) Date: Wed, 16 Apr 2014 01:05:19 +0100 Subject: [Tutor] Fwd: Puzzle - Next Step to our interviewing process - Pramati Technologies! In-Reply-To: References: <06a601cf0b86$aefe5360$0cfafa20$@pramati.com> <03d301cf0c34$e3796450$aa6c2cf0$@pramati.com> <01e801cf0cee$98e359e0$caaa0da0$@pramati.com> <011f01cf0db5$5fc59660$1f50c320$@pramati.com> <076401cf0df4$8f481180$add83480$@pramati.com> <053c01cf1032$11b7c860$35275920$@pramati.com> <067401cf1044$a88717c0$f9954740$@pramati.com> <044601cf11b1$03387930$09a96b90$@pramati.com> <05f001cf11bf$619b09d0$24d11d70$@pramati.com> <07a901cf11d7$d2927c50$77b774f0$@pramati.com> <019801cf1276$6583a180$308ae480$@pramati.com> <07bd01cf12a0$4dd621a0$e98264e0$@pramati.com> <01e201cf133b$30ce7a30$926b6e90$@pramati.com> <025801cf1596$6bc7fd20$4357f760$@pramati.com> <027d01cf1665$40045f10$c00d1d30$@pramati.com> <01ac01cf1728$22d68ad0$6883a070$@pramati.com> <019001cf18c9$305629c0$91027d40$@pramati.com> <047c01cf1b20$d946e840$8bd4b8c0$@pramati.com> <076b01cf1b3e$b0ece070$12c6a150$@pramati.com> <0bf601cf1b5e$aa067760$fe136620$@pramati.com> <085701cf1c0c$bbda1290$338e37b0$@pramati.com> <074701cf1cde$77f89150$67e9b3f0$@pramati.com> <027401cf1d76$6c6f65a0$454e30e0$@pramati.com> <088501cf1d9f$128a41d0$379ec570$@pramati.com> <09af01cf1dab$7e68e350$7b3aa9f0$@pramati.com> <01e801cf1e36$e6fea8b0$b4fbfa10$@pramati.com> <042501cf20b1$c00a1460$401e3d20$@pramati.com> <000301cf20ca$da170ca0$8e4525e0$@pramati.com> <023b01cf22fd$0a2bd9f0$1e838dd0$@pramati.com> <06ce01cf2645$48559180$d900b480$@pramati.com> <084301cf2654$f74757f0$e5d607d0$@pramati.com> <00eb01cf26db$fa213f90$ee63beb0$@pramati.com> <04ea01cf26f0$ccc4a680$664df380$@pramati.com> <076901cf2705$54e187e0$fea497a0$@pramati.com> <023c01cf27ae$0652b4b0$12f81e10$@pramati.com> <024801cf27ae$24ea4780$6ebed680$@pramati.com> <084301cf27e2$2b2d8790$818896b0$@pramati.com> <04bc01cf288d$a72b0d30$f5812790$@pramati.com> <040301cf2bb5$8d57cc10$a8076430$@pramati.com> <058c01cf2bc6$2ef3ead0$8cdbc070$@pramati.com> <04ba01cf345e$114ec9a0$33ec5ce0$@pramati.com> <051601cf3463$6f4c9410$4de5bc30$@pramati.com> <04b601cf3c24$5c48ae30$14da0a90$@pramati.com> <08c901cf3ea1$e512e030$af38a090$@pramati.com> <01dd01cf434a$e0869130$a193b390$@pramati.com> <02d001cf44c9$a27e3890$e77aa9b0$@pramati.com> <02a701cf4715$2d165f80$87431e80$@pramati.com> <04a701cf47f9$32bf7270$983e5750$@pramati.com> <033401cf48bd$ce0b2750$6a2175f0$@pramati.com> <03fb01cf48c4$66f86b20$34e94160$@pramati.com> <022101cf4978$ba8475e0$2f8d61a0$@pramati.com> <014201cf4ef1$3aeba3c0$b0c2eb40$@pramati.com> <015c01cf4ef1$b0978a80$11c69f80$@pramati.com> <01bd01cf4fbc$c256fb40$4704f1c0$@pramati.com> <036601cf52f9$aaf76cb0$00e64610$@pramati.com> <052d01cf5311$10d9c390$328d4ab0$@pramati.com> <02b301cf5478$b1d21e60$15765b20$@pramati.com> <03e101cf57a9$227caf80$67760e80$@pramati.com> Message-ID: Hi, On 15 April 2014 15:49, Sunil Tech wrote: > Hi Danny, > > > Thank you for replying.. > > I need the python program which takes the attached csv & on running the > program which will give me the results as That is still not a question. It's a requirements statement. I felt tempted to point out to you that this is not a freelancer board where you get to post your software requirements and then we go and develop your software for you for free, but seeing as you obviously must know this already and presumably are actually in the process of trying to learn Python and are presumably using this apparent interview question as an interesting learning problem, I'll instead ask you to clarify how far you've gotten and where exactly you're stuck in developing a solution? We'll try and help you along and answer specific concrete questions about problems you're having. (Please provide code and full error messages including stack traces where relevant.) If you're stuck even just getting started, try breaking down the problem into smaller sub problems and solving them first, for example it's obvious from the descriptions you'll need to be able to: a) Interpret command line parameters to pick up the menu file name and products required b) Read a/the menu file into memory c) Perform some sort of search/scan of the list of menu items to return a list of restaurants carrying all the required items d) Evaluate the total costs for the items for each of the candidate restaurants to find the cheapest one e) Output/Print the result in the required format Do you have an idea/know how you might tackle each of these sub-tasks? If not, do you have an idea where to start looking to learn what you might use? Walter From ben+python at benfinney.id.au Wed Apr 16 04:33:19 2014 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 16 Apr 2014 12:33:19 +1000 Subject: [Tutor] Fwd: Puzzle - Next Step to our interviewing process - Pramati Technologies! References: <06a601cf0b86$aefe5360$0cfafa20$@pramati.com> <01dd01cf434a$e0869130$a193b390$@pramati.com> <02d001cf44c9$a27e3890$e77aa9b0$@pramati.com> <02a701cf4715$2d165f80$87431e80$@pramati.com> <04a701cf47f9$32bf7270$983e5750$@pramati.com> <033401cf48bd$ce0b2750$6a2175f0$@pramati.com> <03fb01cf48c4$66f86b20$34e94160$@pramati.com> <022101cf4978$ba8475e0$2f8d61a0$@pramati.com> <014201cf4ef1$3aeba3c0$b0c2eb40$@pramati.com> <015c01cf4ef1$b0978a80$11c69f80$@pramati.com> <01bd01cf4fbc$c256fb40$4704f1c0$@pramati.com> <036601cf52f9$aaf76cb0$00e64610$@pramati.com> <052d01cf5311$10d9c390$328d4ab0$@pramati.com> <02b301cf5478$b1d21e60$15765b20$@pramati.com> <03e101cf57a9$227caf80$67760e80$@pramati.com> Message-ID: <85a9bmvw9c.fsf@benfinney.id.au> Sunil Tech writes: > I need the python program This is a resource for tutoring, not for others to write your program. Please approach this as an opportunity to do the work yourself *while* learning. It's still you that needs to write the program. -- \ ?If I held you any closer I would be on the other side of you.? | `\ ?Groucho Marx | _o__) | Ben Finney From wheelerg at seattleu.edu Thu Apr 17 03:10:20 2014 From: wheelerg at seattleu.edu (Wheeler, Gabriel) Date: Thu, 17 Apr 2014 01:10:20 +0000 Subject: [Tutor] List issues Message-ID: <523941C1CCF5F04EB3A821EAFBE3A97130F92795@OITEX22.seattleu.edu> Hi Im having trouble completing this function with lists. Im supposed to create a function that will let me know if there are repeating elements so I wrote this and am not sure where the error lies. It is supposed to count the number of times a number appears and if its greater than 1 then it will say True. #Problem 3 list = [1,2,2,2,3,4] def duplicate(list): for i in range(len[list]): if list.count(i) > 1: return True print duplicate(list) Gabe Wheeler (512)964-5421 -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Apr 17 10:17:49 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 17 Apr 2014 10:17:49 +0200 Subject: [Tutor] List issues References: <523941C1CCF5F04EB3A821EAFBE3A97130F92795@OITEX22.seattleu.edu> Message-ID: Wheeler, Gabriel wrote: > Im having trouble completing this function with lists. Im supposed to > create a function that will let me know if there are repeating elements so > I wrote this and am not sure where the error lies. It helps you (and us) a lot if you clearly state the error you are seeing. If your script bails out with an error post the complete traceback. If all appears to be working, but you get a wrong or unexpected result say what you get and what you expected. Running your code I get $ cat check_dupes.py list = [1,2,2,2,3,4] def duplicate(list): for i in range(len[list]): if list.count(i) > 1: return True print duplicate(list) $ python check_dupes.py Traceback (most recent call last): File "check_dupes.py", line 8, in print duplicate(list) File "check_dupes.py", line 4, in duplicate for i in range(len[list]): TypeError: 'builtin_function_or_method' object has no attribute '__getitem__' Looking at the line shown in the traceback for i in range(len[list]): what could be the function you are not calling but asking for that strange __getitem__ attribute? Hint: >>> def hello(name): ... print "Hello,", name ... >>> hello("Gabriel") Hello, Gabriel >>> hello["Gabriel"] Traceback (most recent call last): File "", line 1, in TypeError: 'function' object has no attribute '__getitem__' That sure looks similar to your error message. Once you have fixed that I recommend that you add a print statement def duplicate(list): for i in range(len[list]): # must be fixed print "looking for", i if list.count(i) > 1: return True You'll see that you are looking for items that are not in the list? Can you figure out why? If not, call the function with another list duplicate(["a", "b", "b", "b", "c", "d"]) > It is supposed to count > the number of times a number appears and if its greater than 1 then it > will say True. > > > #Problem 3 > > list = [1,2,2,2,3,4] > > def duplicate(list): > for i in range(len[list]): > if list.count(i) > 1: > return True > > > print duplicate(list) From davea at davea.name Thu Apr 17 12:03:56 2014 From: davea at davea.name (Dave Angel) Date: Thu, 17 Apr 2014 06:03:56 -0400 (EDT) Subject: [Tutor] List issues References: <523941C1CCF5F04EB3A821EAFBE3A97130F92795@OITEX22.seattleu.edu> Message-ID: "Wheeler, Gabriel" Wrote in message: > (not much I could read there. This is a text mailing list, so please tell your mail program to send in text mode, not html. Only parts of your code were visible here, and your question not at all. Fortunately, Peter quoted all or most of your message. His comments are all good. Mine are in addition, not instead. Your code: list = [1,2,2,2,3,4] def duplicate(list): ? ?for i in range(len[list]): ? ? ? ?if list.count(i) > 1: ? ? ? ? ? ?return True print duplicate(list) ............ Peter's hints should fix your main bugs. Then: When an if-test doesn't seem to be doing what you expect, add a print statement right before it of the exact expression it's testing. Or even make a new variable of the expression so you can be sure you're looking at the same thing. When a standard method seems to be misbehaving, look up its definition. What should the argument to count be? list is a typename in the standard library, so it really shouldn't be used to name your own objects. I'd use something like mylist. Whenever you see a loop like for i in range(len[list]): be suspicious of it. Usually you want the items, not the indices. for item in mylist: This function doesn't return any value if the if test always fails. Is that what you wanted? -- DaveA From fomcl at yahoo.com Thu Apr 17 14:07:28 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 17 Apr 2014 05:07:28 -0700 (PDT) Subject: [Tutor] List issues In-Reply-To: References: <523941C1CCF5F04EB3A821EAFBE3A97130F92795@OITEX22.seattleu.edu> Message-ID: <1397736448.7885.YahooMailNeo@web163804.mail.gq1.yahoo.com> ? ----- Original Message ----- > From: Dave Angel > To: tutor at python.org > Cc: > Sent: Thursday, April 17, 2014 12:03 PM > Subject: Re: [Tutor] List issues > >& quot;Wheeler, Gabriel" Wrote in message: >> > > (not much I could read there. This is a text mailing list, so > please tell your mail program to send in text mode, not html. > Only parts of your code were visible here, and your question not > at all. Fortunately, Peter quoted all or most of your message. > His comments are all good.? Mine are in addition,? not instead. > > > Your code: > > list = [1,2,2,2,3,4] > > def duplicate(list): > ? ?for i in range(len[list]): > ? ? ? ?if list.count(i) > 1: > ? ? ? ? ? ?return True > > print duplicate(list) > > ............ > > Peter's hints should fix your main bugs. Then: > > When an if-test doesn't seem to be doing what you expect,? add a > print statement right before it of the exact expression it's > testing.? and/or use the pdb debugger (still on my own todo list, but I know it is useful!): http://pymotw.com/2/pdb/ From dyoo at hashcollision.org Thu Apr 17 19:12:03 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 17 Apr 2014 10:12:03 -0700 Subject: [Tutor] List issues In-Reply-To: <523941C1CCF5F04EB3A821EAFBE3A97130F92795@OITEX22.seattleu.edu> References: <523941C1CCF5F04EB3A821EAFBE3A97130F92795@OITEX22.seattleu.edu> Message-ID: Hi Gabriel, Try lists of non-numbers as your input, and the error should be a little clearer to see. You should see the conceptual error you're making if not everything in your program is numeric. Try: words = ['hello', 'world', 'hello'] print(words.count(0)) print(words.count('hello')) First write down what you'd expect to see from this. Then execute this snippet. Does your expectations match what you see? --- A main bug in your program is that parts of your program are using numbers for list indices, and other parts of your program are using numbers as elements, but there's a little confusion in using one notion for the other. --- Good luck! From sabausmani at outlook.com Fri Apr 18 23:19:52 2014 From: sabausmani at outlook.com (Saba Usmani) Date: Fri, 18 Apr 2014 22:19:52 +0100 Subject: [Tutor] Creating an Invalid Message for user Message-ID: Hi, I am meant to design code for a program that converts from binary number to decimal and vice versa. This is what i have so far: print "Welcome to the binary and decimal converter"loop = Truewhile loop: bord = raw_input("Enter b for binary or d decimal or exit to exit") if bord == "b": d = 0 b = 0 factor = 1; b = raw_input ("Enter Binary Number:") b=b.lstrip("0") b = int(b) while(b > 0): if((int(b) % 10) == 1): d += factor b /= 10 factor = factor * 2 print "The Decimal Number is: ", d elif bord == "d": x=0 n=int(input('Enter Decimal Number: ')) x=n k=[] # array while (n>0): a=int(float(n%2)) k.append(a) n=(n-a)/2 k.append(0) string="" for j in k[::-1]: string=string+str(j) print('The binary Number for %d is %s'%(x, string)) elif bord == "exit" : print "Goodbye" loop = False - This code does not recognize invalid inputs e.g in the binary to decimal conversion, so if I enter e.g 10021, not a binary number, it will not inform me,the user, that the input is invalid. The same problem occurs with the decimal to binary conversion - if i enter e.g 123&&gf I am not told to try again with a valid input - how do I implement this in the code above ThanksSaba -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Apr 19 02:18:08 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 19 Apr 2014 01:18:08 +0100 Subject: [Tutor] Creating an Invalid Message for user In-Reply-To: References: Message-ID: On 18/04/14 22:19, Saba Usmani wrote: > I am meant to design code for a program that converts from binary number > to decimal and vice versa. First, you know that python includes functions for doing that already right? So you are just doing this as a learning exercise? > while loop: > bord = raw_input("Enter b for binary or d decimal or exit to exit") > if bord == "b": > d = 0 > b = 0 > factor = 1; > b = raw_input ("Enter Binary Number:") > b=b.lstrip("0") Before converting to an int check each character is in the numeric range you need. For binary thats like for ch in inputstring: if ch not in "01": # process invalid input For decimal its for ch in inputstring: if ch not in "0123456789": # process bad input. You could put that in a function and pass in the numbers as a parameter... If you accept floats as input then it gets a whole lot more complicated. but you are converting to int so I assume its ints you expect. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Sat Apr 19 03:58:18 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 19 Apr 2014 11:58:18 +1000 Subject: [Tutor] Creating an Invalid Message for user In-Reply-To: References: Message-ID: <20140419015817.GH28400@ando> Hello Saba, and welcome, Saba, unfortunately your email is almost unreadable to me. To me, your code looks like this: On Fri, Apr 18, 2014 at 10:19:52PM +0100, Saba Usmani wrote: > print "Welcome to the binary and decimal converter"loop = Truewhile > loop: bord = raw_input("Enter b for binary or d decimal or exit to > exit") if bord == "b": d = 0 b = 0 factor = 1; b = raw_input ("Enter > Binary Number:") b=b.lstrip("0") b = int(b) while(b > 0): if((int(b) % > 10) == 1): d += factor b /= 10 factor = factor * 2 print "The Decimal > Number is: ", d elif bord == "d": x=0 n=int(input('Enter Decimal > Number: ')) x=n k=[] # array while (n>0): a=int(float(n%2)) > k.append(a) n=(n-a)/2 k.append(0) string="" for j in k[::-1]: > string=string+str(j) print('The binary Number for %d is %s'%(x, > string)) elif bord == "exit" : print "Goodbye" loop = False A complete mess! Unfortunately, I have neither the time nor the inclination to spend a lot of effort trying to unmangle the code to see what you intended it to be. As a programmer, you will often be dealing with text formats, and with text it is very important that your email program (Outlook, it seems) doesn't mess up the layout. Especially with Python. Unfortunately, if your email program is configured to send so-called "Rich Text" (actually HTML, exactly the same format that web pages use) a side-effect is that it may mess up the layout as above. I recommend that, when posting to technical forums like this tutor mailing list, you turn off "Rich Text" posting so we can see the code the way it is meant to be seen. If you help us to see your code the way it should be seen, we can help you with your code. Regards, -- Steven From fomcl at yahoo.com Sat Apr 19 17:23:29 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sat, 19 Apr 2014 08:23:29 -0700 (PDT) Subject: [Tutor] Creating an Invalid Message for user In-Reply-To: <20140419015817.GH28400@ando> References: <20140419015817.GH28400@ando> Message-ID: <1397921009.60754.YahooMailNeo@web163804.mail.gq1.yahoo.com> ----- Original Message ----- > From: Steven D'Aprano > To: tutor at python.org > Cc: > Sent: Saturday, April 19, 2014 3:58 AM > Subject: Re: [Tutor] Creating an Invalid Message for user > > Hello Saba, and welcome, > > Saba, unfortunately your email is almost unreadable to me. To me, your > code looks like this: > > On Fri, Apr 18, 2014 at 10:19:52PM +0100, Saba Usmani wrote: > >> print "Welcome to the binary and decimal converter"loop = > Truewhile >> loop: bord = raw_input("Enter b for binary or d decimal or exit to >> exit") if bord == "b": d = 0 b = 0 factor = 1; b = raw_input > ("Enter >> Binary Number:") b=b.lstrip("0") b = int(b) while(b > 0): > if((int(b) % >> 10) == 1): d += factor b /= 10 factor = factor * 2 print "The Decimal >> Number is: ", d elif bord == "d": x=0 n=int(input('Enter > Decimal >> Number: ')) x=n k=[] # array while (n>0): a=int(float(n%2)) >> k.append(a) n=(n-a)/2 k.append(0) string="" for j in k[::-1]: >> string=string+str(j) print('The binary Number for %d is %s'%(x, >> string)) elif bord == "exit" : print "Goodbye" loop = > False > > A complete mess! Unfortunately, I have neither the time nor the > inclination to spend a lot of effort trying to unmangle the code to see > what you intended it to be. Hmmm, this indeed looks like Perl. Or worse yet: Brainf*ck, http://www.hevanet.com/cristofd/brainfuck/dbf2c.b ;-) From vipul.sharma20 at gmail.com Sat Apr 19 22:48:28 2014 From: vipul.sharma20 at gmail.com (Vipul Sharma) Date: Sun, 20 Apr 2014 02:18:28 +0530 Subject: [Tutor] equality check difference Message-ID: Hello, Suppose we want some block of code to be executed when both '*a'* and '*b'*are equal to say 5. Then we can write like : *if a == 5 and b == 5:* * # do something* But a few days ago, I just involuntarily wrote a similar condition check as : *if a == b and b == 5:* * # do something * which made me think, is there any difference between the two ? Is there any difference, any difference in the process of evaluation or execution ? and also which one is the better ? I think this is a general programming question and not specifically for python. P.S. : Newbie here :) Thanks. Regards, Vipul Sharma -------------- next part -------------- An HTML attachment was scrubbed... URL: From fomcl at yahoo.com Sat Apr 19 23:31:39 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sat, 19 Apr 2014 14:31:39 -0700 (PDT) Subject: [Tutor] equality check difference In-Reply-To: References: Message-ID: <1397943099.66526.YahooMailNeo@web163804.mail.gq1.yahoo.com> ________________________________ > From: Vipul Sharma >To: tutor at python.org >Sent: Saturday, April 19, 2014 10:48 PM >Subject: [Tutor]??equality check difference > > > >Hello, > > >Suppose we want some block of code to be executed when both 'a' and 'b' are equal to say 5. Then we can write like : > > >if a == 5 and b == 5: >? ? # do something > > >But a few days ago, I just involuntarily wrote a similar condition check as : > > >if a == b and b == 5: >? ? # do something? > > >which made me think, is there any difference between the two ? > > >Is there any difference, any difference in the process of evaluation or execution ? and also which one is the better ? > > >I think this is a general programming question and not specifically for python. > > >P.S. : Newbie here :) I used timeit and it does not seem to make a big difference. The dis module could help give insight what Python is doing behind the scenes (I am not so familiar with that module). $ipython Python 2.7.3 (default, Feb 27 2014, 19:39:10) Type "copyright", "credits" or "license" for more information. IPython 0.13.1 -- An enhanced Interactive Python. In [1]: a, b = 1, 2 In [2]: %timeit if a == 5 and b == 5: pass 1000000 loops, best of 3: 1.1 us per loop In [3]: %timeit if a == b and b == 5: pass 1000000 loops, best of 3: 1.26 us per loop In [4]: %timeit if a == b == 5: pass 100000 loops, best of 3: 1.97 us per loop From __peter__ at web.de Sat Apr 19 23:48:00 2014 From: __peter__ at web.de (Peter Otten) Date: Sat, 19 Apr 2014 23:48 +0200 Subject: [Tutor] equality check difference References: Message-ID: Vipul Sharma wrote: > Hello, > > Suppose we want some block of code to be executed when both '*a'* and > '*b'*are equal to say 5. Then we can write like : > > *if a == 5 and b == 5:* > * # do something* > > But a few days ago, I just involuntarily wrote a similar condition check > as > : > > *if a == b and b == 5:* > * # do something * > > which made me think, is there any difference between the two ? > > Is there any difference, any difference in the process of evaluation or > execution ? and also which one is the better ? > > I think this is a general programming question and not specifically for > python. > > P.S. : Newbie here :) In mathematics there is a property called "transitivity" which basically says that an operation op is transitive if from (a op b) and (a op c) follows b op c Integer equality is such an operation, and this holds for Python, too. If a and b are integers you can safely assume that both conditions are equivalent. There is even a third way to spell this: if a == b == 5: ... But for arbitrary objects transitivity is not guaranteed, and you may see different outcomes. Here is a simple class that implements non-transitive equality: >>> class A: ... def __eq__(self, other): ... return isinstance(other, int) ... >>> a = A() >>> b = A() >>> if a == 5 and b == 5: ... print("yes") ... else: ... print("no") ... yes >>> if a == b and b == 5: ... print("yes") ... else: ... print("no") ... no When you write a == b Python under the hood translates that to a.__eq__(b), and when you implement your own __eq__() method you are free to do anything you like. You can even vary the output between calls: >>> import random >>> class B: ... def __eq__(self, other): ... return random.choice([True, False]) ... >>> c = B() >>> c == 42 False >>> c == 42 False >>> c == 42 True >>> c == 42 False >>> c == 42 True Of course nobody (I hope) would recommend that you actually write such code. PS: Can you guess what a == a prints? From __peter__ at web.de Sun Apr 20 00:04:21 2014 From: __peter__ at web.de (Peter Otten) Date: Sun, 20 Apr 2014 00:04:21 +0200 Subject: [Tutor] equality check difference References: Message-ID: Peter Otten wrote: > In mathematics there is a property called "transitivity" which basically > says that an operation op is transitive if from > > (a op b) and (a op c) > > follows > > b op c I opened the wikipedia article for the english word, but didn't start reading it until after I had hit send :( The above should be from (a op b) and (b op c) follows (a op c) For equality this doesn't matter, but for other operations, e. g. a > b and b > c --> a > c but not WRONG! a > b and a > c --> b > c From alan.gauld at btinternet.com Sun Apr 20 00:07:00 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 19 Apr 2014 23:07:00 +0100 Subject: [Tutor] equality check difference In-Reply-To: References: Message-ID: On 19/04/14 21:48, Vipul Sharma wrote: > *if a == 5 and b == 5:* > * # do something* > > *if a == b and b == 5:* > * # do something * > > which made me think, is there any difference between the two ? Yes. I don't know how python actually does it but in a general sense there is not much if any difference when the conditions are true but if, for example, a is 5 and b is 4 then consider what happens: a==5 and b==5 1) check if a==5 -> True 2) check if b==5 -> False 3) skip if block a==b and b==5 1) check if a == b -> False 2) skip if block So the second version notionally does one less test for a false condition. But its arguable less readable so do you trade readability for a (marginal) speed improvement? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From davea at davea.name Sun Apr 20 03:43:32 2014 From: davea at davea.name (Dave Angel) Date: Sat, 19 Apr 2014 21:43:32 -0400 (EDT) Subject: [Tutor] equality check difference References: Message-ID: Alan Gauld Wrote in message: > On 19/04/14 21:48, Vipul Sharma wrote: > >> *if a == 5 and b == 5:* >> * # do something* >> >> *if a == b and b == 5:* >> * # do something * >> >> which made me think, is there any difference between the two ? > > Yes. > I don't know how python actually does it but in a general > sense there is not much if any difference when the conditions > are true but if, for example, a is 5 and b is 4 then consider > what happens: > > a==5 and b==5 > 1) check if a==5 -> True > 2) check if b==5 -> False > 3) skip if block > > a==b and b==5 > 1) check if a == b -> False > 2) skip if block > > So the second version notionally does one less test for a > false condition. But its arguable less readable so do you > trade readability for a (marginal) speed improvement? > You're right, the second expression is faster for 5,4. But the first is faster for 4,4. So choosing for speed depends on what you know about probable values. -- DaveA From cfuller084 at thinkingplanet.net Sun Apr 20 02:50:42 2014 From: cfuller084 at thinkingplanet.net (Chris Fuller) Date: Sat, 19 Apr 2014 19:50:42 -0500 Subject: [Tutor] equality check difference In-Reply-To: References: Message-ID: <201404191950.42907.cfuller084@thinkingplanet.net> On Saturday, April 19, 2014, Vipul Sharma wrote: > Hello, > > Suppose we want some block of code to be executed when both '*a'* and > '*b'*are equal to say 5. Then we can write like : > > *if a == 5 and b == 5:* > * # do something* > > But a few days ago, I just involuntarily wrote a similar condition check as > > > *if a == b and b == 5:* > * # do something * > > which made me think, is there any difference between the two ? Transitivity is one factor, but another one I don't think got mentioned is short-circuiting. When two conditions are joined by an "and" or an "or", sometimes only the first condition is actually executed, if the result is sufficient to determine the outcome. If the first condition of an "or" is True, then the result must be True, and no further computation is necessary. For "and", if the first condition is False, then the outcome must be False, and evaluation of the other condition does not occur. Most of the time, this doesn't matter. But if the expressions are expensive to compute, or contain "side effects", it can become significant. Side effects are things computations do that have some effect beyond the immediate result. Print statements are familiar examples. Changing an internal variable in a class instance is another. Cheers From cfuller084 at thinkingplanet.net Sun Apr 20 04:30:43 2014 From: cfuller084 at thinkingplanet.net (Chris Fuller) Date: Sat, 19 Apr 2014 21:30:43 -0500 Subject: [Tutor] equality check difference In-Reply-To: References: Message-ID: <201404192130.43609.cfuller084@thinkingplanet.net> Something I forgot to add. This idea of side-effects with the conditionals is actually the whole point of the Python built-in functinos any() and all(). any() will evaluate a generator expression and stops when one of them evaluates to True, and all() works the same, but stops when a False is encountered. If you google these, you are bound to find some helpful examples and explanations. For example, https://docs.python.org/2.7/howto/functional.html Cheers From sabausmani at outlook.com Sun Apr 20 20:06:26 2014 From: sabausmani at outlook.com (Saba Usmani) Date: Sun, 20 Apr 2014 19:06:26 +0100 Subject: [Tutor] Recognising Errors Message-ID: Hi, I have designed some code, but I want it to be able to recognize invalid inputs - such as a binary number with more than 8 digits or non-binary values. What do I have to add and where do I add it? print "Welcome to the binary -> decimal / decimal -> binary converter!"loop = Truewhile loop: choice = raw_input("Enter b to convert from binary to decimal, d to convert from decimal to binary or e to exit") if choice == "b": decimal_num = 0 binary_num = 0 factor = 1; binary_num = raw_input ("Enter Binary Number:") binary_num=binary_num.lstrip("0") binary_num = int(binary_num) while(binary_num > 0): if((int(binary_num) % 10) == 1): decimal_num += factor binary_num /= 10 factor = factor * 2 print "The Decimal Equivalent is: ", decimal_num elif choice == "d": z=0 n=int(input('Enter Decimal Number: ')) z=n k=[] # array while (n>0): a=int(float(n%2)) k.append(a) n=(n-a)/2 k.append(0) string="" for j in k[::-1]: string=string+str(j) print('The Binary Equivalent is %d is %s'%(z, string)) elif choice == "e" : print "Thanks For Using This Converter!" loop = False If for some reason you can't read this code properly as outlook has formatted it to look messy/cluttered; you do not have to respond. Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Apr 21 01:15:14 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 21 Apr 2014 00:15:14 +0100 Subject: [Tutor] Recognising Errors In-Reply-To: References: Message-ID: On 20/04/14 19:06, Saba Usmani wrote: > ...such as a binary number with more than 8 digits or non-binary > values. What do I have to add and where do I add it? You need to either write a function to check the inputs - have you learned about functions yet? - or write some if/else checks after you read the input but before you convert it to a number. Alternatively you can try to convert it and if it can't be converted catch the error - have you covered try/except yet? > print "Welcome to the binary -> decimal / decimal -> binary converter!" > loop = True > while loop: > choice = raw_input("Enter b to convert from binary to decimal, d to > convert from decimal to binary or e to exit") > if choice == "b": > decimal_num = 0 > binary_num = 0 > factor = 1; > binary_num = raw_input ("Enter Binary Number:") > binary_num=binary_num.lstrip("0") Here is where you can test for the length of the string and whether any of the characters are non binary. > binary_num = int(binary_num) Or you can wrap the above line in a try/except: try: binary_num = int(binary_num) except ValueError, TypeError: # deal with error here > while(binary_num > 0): > if((int(binary_num) % 10) == 1): you don't need int() here, you already converted it above. But it's an unusual way to convert a binary string to a decimal number. Even if you don't use the built in conversion tools. Its more usual to just iterate over the characters adding powers of two. > decimal_num += factor > binary_num /= 10 > factor = factor * 2 > print "The Decimal Equivalent is: ", decimal_num > elif choice == "d": > z=0 > n=int(input('Enter Decimal Number: ')) You should not user input() in this way its extremely insecure and even if its only you using the program you could still accidentally type in something that causes harm. Use raw_input() and int() as you did above, it is much safer. > z=n > k=[] # array > while (n>0): > a=int(float(n%2)) modulo two on an integer will always return 0 or 1. There's no need to make it a float then convert back to an int, its already an int. > k.append(a) > n=(n-a)/2 > k.append(0) > string="" > for j in k[::-1]: > string=string+str(j) The join() method of strings will do this all in one step and be much faster. > print('The Binary Equivalent is %d is %s'%(z, string)) > elif choice == "e" : > print "Thanks For Using This Converter!" > loop = False > If for some reason you can't read this code properly as outlook has > formatted it to look messy/cluttered; you do not have to respond. True, but if you send unreadable code you reduce the number of people who can help you. Many of them experts in their field. That's a lot of help to ignore! Its not just Outlook that is the culprit - its how you have set your Outlook options. Of course it would be good if Microsoft made the defaults internet friendly but Microsoft have always struggled with the idea that people don't all use their products :-) Fortunately my mail reader could cope! HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From davea at davea.name Mon Apr 21 08:20:11 2014 From: davea at davea.name (Dave Angel) Date: Mon, 21 Apr 2014 02:20:11 -0400 (EDT) Subject: [Tutor] Recognising Errors References: Message-ID: Saba Usmani Wrote in message: > > If for some reason you can't read this code properly as outlook has formatted it to > look messy/cluttered; you do not have to respond It'd save trouble if you continued in the same thread you started, instead of repeatedly starting a new one. And you clearly have read at least one of the other responses, as you have the nerve to dis those who give you good advice about posting in html. It should be a simple menu choice, unless Outlook is more busted than I remember. If there's something about the programming advice you've received that you don't understand, ask specifically, as a reply all, rather than posting nearly identical code with new wording for your question. -- DaveA From breamoreboy at yahoo.co.uk Mon Apr 21 15:38:45 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 21 Apr 2014 14:38:45 +0100 Subject: [Tutor] Recognising Errors In-Reply-To: References: Message-ID: On 21/04/2014 07:20, Dave Angel wrote: > Saba Usmani Wrote in message: >> >> > If for some reason you can't read this code properly as outlook has formatted it to >> look messy/cluttered; you do not have to respond > > > > It'd save trouble if you continued in the same thread you > started, instead of repeatedly starting a new one. And you > clearly have read at least one of the other responses, as you > have the nerve to dis those who give you good advice about > posting in html. It should be a simple menu choice, unless > Outlook is more busted than I remember. > Depends on whether you're talking about Outlook the Office application or Outlook the new name for Hotmail. I think :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From ne0stigmine at 163.com Mon Apr 21 16:13:08 2014 From: ne0stigmine at 163.com (lee) Date: Mon, 21 Apr 2014 22:13:08 +0800 (CST) Subject: [Tutor] which book to read next?? Message-ID: <2e926609.d361.14584a21eae.Coremail.ne0stigmine@163.com> Hi, I have read the book 'a byte of python' and now I want to read another book. But I just get confused about which one to read next. There is a book list below? 1, pro python 2, python algorithms 3, python cookbook 4, the python standard library by examples which one is suitable for me?? Or I need to start a project with pygame or flask? Thanks for your help! -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Mon Apr 21 18:08:38 2014 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 21 Apr 2014 12:08:38 -0400 Subject: [Tutor] which book to read next?? In-Reply-To: References: <2e926609.d361.14584a21eae.Coremail.ne0stigmine@163.com> Message-ID: On Mon, Apr 21, 2014 at 11:41 AM, Alan Gauld wrote: > On 21/04/14 15:13, lee wrote: > >> Hi, I have read the book 'a byte of python' and now I want to read >> another book. But I just get confused about which one to read next. >> There is a book list below? >> 1, pro python >> 2, python algorithms >> 3, python cookbook >> 4, the python standard library by examples >> which one is suitable for me?? >> > > We would need to know a lot more about you. > What is your skill level in programming (as opposed to python)? > What are your areas of interest? > What is your preferred teaching style? In depth background > detail or surface level but hands-on style? > > Book choice is always a very personal thing. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > -- > https://mail.python.org/mailman/listinfo/python-list > Don't forget to look at the python.org site: https://wiki.python.org/moin/BeginnersGuide -- Joel Goldstick http://joelgoldstick.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Mon Apr 21 19:40:08 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 21 Apr 2014 10:40:08 -0700 Subject: [Tutor] Recognising Errors In-Reply-To: References: Message-ID: > If for some reason you can't read this code properly as outlook has > formatted it to look messy/cluttered; you do not have to respond. You are missing the point of people point this out. Look at what the email archive thinks of your previous messages: https://mail.python.org/pipermail/tutor/2014-April/100904.html https://mail.python.org/pipermail/tutor/2014-April/100940.html https://mail.python.org/pipermail/tutor/2014-April/100985.html https://mail.python.org/pipermail/tutor/2014-April/100997.html When folks are saying that we can't read your programs, we're not trying to insult you. Rather, we're making a very technical observation: your email client is interfering with the indentation of your program. In Python, indentation and formatting _changes_ the meaning of your program, so we really can not tell what your program _means_. Under those conditions, we can't help very effectively. If you really can not fix your email client, then post your programs on a "pastebin" which should preserve formatting and meaning. For example, http://gist.github.com. But please do not treat the advice you're been getting as personal insults. They are not intended to be such. From akleider at sonic.net Mon Apr 21 21:37:50 2014 From: akleider at sonic.net (Alex Kleider) Date: Mon, 21 Apr 2014 12:37:50 -0700 Subject: [Tutor] =?utf-8?q?which_book_to_read_next=3F=3F?= In-Reply-To: <2e926609.d361.14584a21eae.Coremail.ne0stigmine@163.com> References: <2e926609.d361.14584a21eae.Coremail.ne0stigmine@163.com> Message-ID: <7010ec0d67111dcbac7ff09376cad7ca@sonic.net> On 2014-04-21 07:13, lee wrote: > Hi, I have read the book 'a byte of python' and now I want to read > another book. But I just get confused about which one to read next. > There is a book list below? > 1, pro python > 2, python algorithms > 3, python cookbook > 4, the python standard library by examples > which one is suitable for me?? > Or I need to start a project with pygame or flask? > Thanks for your help! If you aren't already a programmer, I would strongly recommend Downey's book: http://www.greenteapress.com/thinkpython/thinkpython.html It covers Python v2 but I found it fairly easy to transition to Python v3. The book I currently keep close at hand as a reference is Programming in Python 3 by Mark Summerfield (2nd Ed) but I would not recommend it as an introductory book about Python. From fomcl at yahoo.com Mon Apr 21 21:59:46 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Mon, 21 Apr 2014 12:59:46 -0700 (PDT) Subject: [Tutor] which book to read next?? In-Reply-To: <7010ec0d67111dcbac7ff09376cad7ca@sonic.net> References: <2e926609.d361.14584a21eae.Coremail.ne0stigmine@163.com> <7010ec0d67111dcbac7ff09376cad7ca@sonic.net> Message-ID: <1398110386.10294.YahooMailNeo@web163802.mail.gq1.yahoo.com> ________________________________ > From: Alex Kleider >To: tutor at python.org >Sent: Monday, April 21, 2014 9:37 PM >The book I currently keep close at hand as a reference is >Programming in Python 3 by Mark Summerfield (2nd Ed) but I would not >recommend it as an introductory book about Python. That's an awesome book. It does contain some very/too advanced chapters, but it is absolutely worth buying. Here is a sample chapter of it, about regexes: http://www.informit.com/content/images/9780137129294/samplepages/0137129297_Sample.pdf From rail.shafigulin at gmail.com Mon Apr 21 23:05:49 2014 From: rail.shafigulin at gmail.com (rail shafigulin) Date: Mon, 21 Apr 2014 17:05:49 -0400 Subject: [Tutor] Groups of mutually exclusive options Message-ID: Does anybody know if there is a way to specify groups of mutually exclusive options using argparse module? Currently argpase allows to specify mutually exclusive options in the following way (taken from https://docs.python.org/release/3.4.0/library/argparse.html#argparse.ArgumentParser.add_mutually_exclusive_group ) >>> parser = argparse.ArgumentParser(prog='PROG')>>> group = parser.add_mutually_exclusive_group()>>> group.add_argument('--foo', action='store_true')>>> group.add_argument('--bar', action='store_false')>>> parser.parse_args(['--foo'])Namespace(bar=True, foo=True)>>> parser.parse_args(['--bar'])Namespace(bar=False, foo=False)>>> parser.parse_args(['--foo', '--bar'])usage: PROG [-h] [--foo | --bar]PROG: error: argument --bar: not allowed with argument --foo What I need is a way to specify groups of mutually exclusive options. In other words >>> parser = argparse.ArgumentParser(prog='PROG')>>> group1 = parser.add_argument_group()>>> group2 = parser.add_argument_group()>>> group1.add_argument('--foo', action='store_true')>>> group1.add_argument('--bar1', action='store_false')>>> group2.add_argument('--foo2', action = 'store_true') >>> group2.add_argument('--bar2', action = 'store_false') >>> mutually_exclusive_group = parser.add_mutually_exclusive_argument_group() >>> mutually_exclusive_group.add_group(group1) >>> mutually_exclusive_group.add_group(group2)>>> parser.parse_args(['--foo1', '--bar1', '--bar2'])usage: PROG [-h] [--foo1, --bar1] | [--foo2, --bar2]PROG: error: argument --foo1 or bar1 not allowed with argument --foo2 or bar2 -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Apr 22 01:46:16 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 22 Apr 2014 00:46:16 +0100 Subject: [Tutor] Groups of mutually exclusive options In-Reply-To: References: Message-ID: On 21/04/14 22:05, rail shafigulin wrote: > Does anybody know if there is a way to specify groups of mutually > exclusive options using argparse module? > Sorry, I didn't follow your example. Can you explain what you mean in English? What would be the outcome if you succeeded? What could the user do and not do? > What I need is a way to specify groups of mutually exclusive options. > In other words > >>>>parser = argparse.ArgumentParser(prog='PROG') >>>>group1 = parser.add_argument_group() >>>> group2 = parser.add_argument_group() >>>>group1.add_argument('--foo', action='store_true') >>>>group1.add_argument('--bar1', action='store_false') >>>> group2.add_argument('--foo2', action = 'store_true') >>>> group2.add_argument('--bar2', action = 'store_false') >>>> mutually_exclusive_group = parser.add_mutually_exclusive_argument_group() >>>> mutually_exclusive_group.add_group(group1) >>>> mutually_exclusive_group.add_group(group2) >>>>parser.parse_args(['--foo1', '--bar1', '--bar2']) > usage: PROG [-h] [--foo1, --bar1] | [--foo2, --bar2] > PROG: error: argument --foo1 or bar1 not allowed with argument --foo2 or bar2 -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From geocrafterserver at gmail.com Mon Apr 21 20:41:52 2014 From: geocrafterserver at gmail.com (Geocrafter .) Date: Mon, 21 Apr 2014 14:41:52 -0400 Subject: [Tutor] Error Message-ID: im trying to make a board, and is detecting the pieces. Here is my code: http://pastebin.com/L3tQLV2g And here is the error: http://pastebin.com/4FiJmywL Do you knwo hwo to fix it? -------------- next part -------------- An HTML attachment was scrubbed... URL: From sabausmani at outlook.com Mon Apr 21 20:46:43 2014 From: sabausmani at outlook.com (Saba Usmani) Date: Mon, 21 Apr 2014 19:46:43 +0100 Subject: [Tutor] Recognising Errors In-Reply-To: References: Message-ID: Thanks for the advice and tips. I wasn't taking anything as an insult; I just have a problem with one of the staffs attitude responses. He needs sleep. Alan Gauld has been very helpful- a special thanks to you. Kind regards Saba On 21 Apr 2014, at 18:40, "Danny Yoo" wrote: >> If for some reason you can't read this code properly as outlook has >> formatted it to look messy/cluttered; you do not have to respond. > > You are missing the point of people point this out. Look at what the > email archive thinks of your previous messages: > > https://mail.python.org/pipermail/tutor/2014-April/100904.html > https://mail.python.org/pipermail/tutor/2014-April/100940.html > https://mail.python.org/pipermail/tutor/2014-April/100985.html > https://mail.python.org/pipermail/tutor/2014-April/100997.html > > > When folks are saying that we can't read your programs, we're not > trying to insult you. Rather, we're making a very technical > observation: your email client is interfering with the indentation of > your program. In Python, indentation and formatting _changes_ the > meaning of your program, so we really can not tell what your program > _means_. Under those conditions, we can't help very effectively. > > If you really can not fix your email client, then post your programs > on a "pastebin" which should preserve formatting and meaning. For > example, http://gist.github.com. But please do not treat the advice > you're been getting as personal insults. They are not intended to be > such. From mik.stephen at yahoo.com Mon Apr 21 20:12:17 2014 From: mik.stephen at yahoo.com (Stephen Mik) Date: Mon, 21 Apr 2014 11:12:17 -0700 (PDT) Subject: [Tutor] Beginning Python 3.4.0 Programmer:Stephen Mik: Cannot get input variable to make While Loop conditional to work Message-ID: <1398103937.65340.YahooMailNeo@web124702.mail.ne1.yahoo.com> Dear Python Community: ??? I am new to Python,with only about a month's experience. I am writing Python 3.4.0 code that apparently isn't doing what it should be doing. Specifically, I am inputting or trying to input,a Sentry Variable to a While Loop. I want to test out the Main program" While" Loop before I add an inner "While" Loop. The program I have written,when run on the Python 3.4.0 Shell,does not stop for input of the "While" Sentry Variable,it just gives a program error: "Value of smv_grandVariable undefined". What am I doing wrong here? I'll try to post that part of the Code that is malfunctioning as well as Python Shell 3.4.0 Traceback Analysis. Please help if you can,this program is due on Thursday the 22nd of April,2014. -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: aSSNMT4Python_Error.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: PythAsst4ShellResult.py URL: From alan.gauld at btinternet.com Tue Apr 22 02:08:59 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 22 Apr 2014 01:08:59 +0100 Subject: [Tutor] Error In-Reply-To: References: Message-ID: On 21/04/14 19:41, Geocrafter . wrote: > im trying to make a board, and is detecting the pieces. Here is my > code:http://pastebin.com/L3tQLV2g And here is the error: > http://pastebin.com/4FiJmywL Do you knwo hwo to fix it? You are passing in a cell location that results in an index out of range. Try figuring out what happens when x is 6 for example. BTW Cant you combine those two enormous if statements into one by just passing ionm the test character(x or y) as an parameter? Like this: def check_around(x, y, test='x'): if test == 'x': check = 'o' elif test == 'o': check = 'x' else: raise ValueError if board[x][y] == test: if board[x - 3][y - 3] == check or board[x - 2][y - 3] == check or board[x - 1][y - 3] == check or... However even better would be to get rid of the huge if statement and replace it with loops to generate the indices. Pythons range function can deal with negatives too: Try: >>> print list(range(-3,4)) to see if that gives you any ideas. Finally your mqain code doesn't appear to do anything very much... for x in range (0, 7): for y in range (0, 7): check_aroundx(x, y) This only checks the x cells. It would be better practice to have the function return a result that you can print externally. Maybe return a list of touching cells say? # sys.stdout.write ("%c" % (board[x][y])) print And these two lines don't do anything much. Just some thoughts. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From wprins at gmail.com Tue Apr 22 02:12:09 2014 From: wprins at gmail.com (Walter Prins) Date: Tue, 22 Apr 2014 01:12:09 +0100 Subject: [Tutor] Beginning Python 3.4.0 Programmer:Stephen Mik: Cannot get input variable to make While Loop conditional to work In-Reply-To: <1398103937.65340.YahooMailNeo@web124702.mail.ne1.yahoo.com> References: <1398103937.65340.YahooMailNeo@web124702.mail.ne1.yahoo.com> Message-ID: Hi, On 21 April 2014 19:12, Stephen Mik wrote: > Dear Python Community: > I am new to Python,with only about a month's experience. I am writing > Python 3.4.0 code that apparently isn't doing what it should be doing. > Specifically, I am inputting or trying to input,a Sentry Variable to a While > Loop. I want to test out the Main program" While" Loop before I add an inner > "While" Loop. The program I have written,when run on the Python 3.4.0 > Shell,does not stop for input of the "While" Sentry Variable,it just gives a > program error: "Value of smv_grandVariable undefined". What am I doing wrong > here? You are misunderstanding how input() works. It is a function, which means it returns a result, and takes one parameter which is a prompt/message to display, e.g you should have something like this: result = input('Input a string:') This displays 'Input a string:' to the user and then waits for input, after which it puts the inputted value into the variable 'result'. That also hopefully explains the error message -- it's telling you that 'smv_grandVariable', which you've given to input() and which Python's duly trying to display is undefined. (By the way, try to pick a better name for that variable which suggests what its role is supposed to be.) Also see here: https://docs.python.org/3.4/library/functions.html#input Walter From alan.gauld at btinternet.com Tue Apr 22 02:18:19 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 22 Apr 2014 01:18:19 +0100 Subject: [Tutor] Beginning Python 3.4.0 Programmer:Stephen Mik: Cannot get input variable to make While Loop conditional to work In-Reply-To: <1398103937.65340.YahooMailNeo@web124702.mail.ne1.yahoo.com> References: <1398103937.65340.YahooMailNeo@web124702.mail.ne1.yahoo.com> Message-ID: On 21/04/14 19:12, Stephen Mik wrote: > ...I am inputting or trying to input,a Sentry Variable > to a While Loop. I want to test out the Main program" While" Loop before > I add an inner "While" Loop. The program I have written,when run on the > Python 3.4.0 Shell,does not stop for input of the "While" Sentry > Variable,it just gives a program error: "Value of smv_grandVariable > undefined". What am I doing wrong here? > import random > ... > print("Do you want to play the game?\n") > print("Enter a 1 to play or 0 to exit:") > input(smv_grandVariable) You have completely misunderstood input... input takes as an argument a prompt string and returns the value input by the user so your usage should look like: smv_grandVariable("Enter a 1 to play or 0 to exit:") But that's a terrible name for a variable. You should name variables after their purpose. What does this variable represent? You say its a sentry? So call it sentry... Having the word "variable" in a variable name is nearly always a mistake. > while (smv_grandVariable == 1 and smv_grandVariable != 0): And your second mistake is that you have not converted the string typed by the user to a number(specifically an int) but you are comparing the variable to the numbers 0,1 Finally the logic of your test can be replaced by the simpler while int(smv_grandVariable) != 0: since 1 is also not zero. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From rail.shafigulin at gmail.com Tue Apr 22 02:26:41 2014 From: rail.shafigulin at gmail.com (rail shafigulin) Date: Mon, 21 Apr 2014 20:26:41 -0400 Subject: [Tutor] Groups of mutually exclusive options In-Reply-To: References: Message-ID: > > >> > Sorry, I didn't follow your example. Can you explain what you mean in > English? What would be the outcome if you succeeded? > What could the user do and not do? The idea is to use two groups of parameters. The user can use only one group of parameters. For example say I have a the script with called myscript.py which can take two groups of options group1 option1a option1b option1c group2 option2a option2b otpion2c I can run this script only with the following options myscript.py --option1a --option1b --option1c or myscript.py --option2a --option2b --option2c I cannot run run a script with the following options myscript --option1a --option2a So it is similar to having mutually exclusive options, however this is sort of on a larger scale. Instead of having mutually exclusive options, we would have mutually exclusive sets of options. Let me know if it didn't clarify the details. I will try to come up with a better explanation. -------------- next part -------------- An HTML attachment was scrubbed... URL: From akleider at sonic.net Tue Apr 22 02:37:28 2014 From: akleider at sonic.net (Alex Kleider) Date: Mon, 21 Apr 2014 17:37:28 -0700 Subject: [Tutor] Groups of mutually exclusive options In-Reply-To: References: Message-ID: <8f1c5af55b9058b847e019fa63f61262@sonic.net> On 2014-04-21 14:05, rail shafigulin wrote: > Does anybody know if there is a way to specify groups of mutually > exclusive > options using argparse module? > As someone pointed out on this list some months ago, you might want to consider using docopt instead of argparse. It is much more in keeping with the SPoL philosophy of Unix. http://docopt.org/ SPoL: Single Point of Light- see Eric Raymond's book http://en.wikipedia.org/wiki/The_Art_of_Unix_Programming From steve at pearwood.info Tue Apr 22 04:35:44 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 22 Apr 2014 12:35:44 +1000 Subject: [Tutor] Groups of mutually exclusive options In-Reply-To: <8f1c5af55b9058b847e019fa63f61262@sonic.net> References: <8f1c5af55b9058b847e019fa63f61262@sonic.net> Message-ID: <20140422023544.GQ28400@ando> On Mon, Apr 21, 2014 at 05:37:28PM -0700, Alex Kleider wrote: > On 2014-04-21 14:05, rail shafigulin wrote: > >Does anybody know if there is a way to specify groups of mutually > >exclusive > >options using argparse module? > > > > As someone pointed out on this list some months ago, you might want to > consider using docopt instead of argparse. It is much more in keeping > with the SPoL philosophy of Unix. Does docopt solve the Original Poster's question? If not, that advice is not terribly helpful. By the way, I think you mean Single Point Of Truth, not Light. http://www.faqs.org/docs/artu/ch04s02.html SPOT, also known as DRY (Don't Repeat Yourself), is an excellent principle to follow, but in my experience it is often too difficult to follow religiously. -- Steven From steve at pearwood.info Tue Apr 22 05:17:28 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 22 Apr 2014 13:17:28 +1000 Subject: [Tutor] Groups of mutually exclusive options In-Reply-To: References: Message-ID: <20140422031727.GR28400@ando> On Mon, Apr 21, 2014 at 05:05:49PM -0400, rail shafigulin wrote: > Does anybody know if there is a way to specify groups of mutually exclusive > options using argparse module? I'm not an expert on argparse, but I think not. If I've understand correctly, this seems to suggest that argparse does not support what you want: http://stackoverflow.com/questions/4770576/does-argparse-python-support-mutually-exclusive-groups-of-arguments If I've misunderstood what you are after, there are very many variations on the theme of mutually exclusive groups of arguments on Stackoverflow, perhaps you can find something more appropriate. -- Steven From steve at pearwood.info Tue Apr 22 05:23:08 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 22 Apr 2014 13:23:08 +1000 Subject: [Tutor] Groups of mutually exclusive options In-Reply-To: References: Message-ID: <20140422032308.GS28400@ando> On Mon, Apr 21, 2014 at 08:26:41PM -0400, rail shafigulin wrote: > For example say I have a the script with called myscript.py which can take > two groups of options > > group1 > option1a > option1b > option1c > group2 > option2a > option2b > otpion2c > > I can run this script only with the following options > > myscript.py --option1a --option1b --option1c > or > myscript.py --option2a --option2b --option2c Normally the way to handle that with argparse is to define subcommands, and write something like this: myscript.py spam --option1a --option1b --option1c myscript.py eggs --option2a --option2b --option2c http://stackoverflow.com/questions/18046540/add-a-second-group-of-parameters-that-is-totally-mutually-exclusive-from-the-fir I don't think there is any directly supported way to handle it in argparse without the subcommands. -- Steven From suhanavidyarthi at gmail.com Tue Apr 22 03:16:20 2014 From: suhanavidyarthi at gmail.com (Suhana Vidyarthi) Date: Mon, 21 Apr 2014 18:16:20 -0700 Subject: [Tutor] Help needed with Python programming Message-ID: My knowledge of coding is fairly limited and I am having a hard time writing a Python code which might be pretty simple for you :-) Here is what I am doing and I need help with: I have a python code that shows a set of shortest paths between nodes A and B. Now I have to select the least risky path among them. To do that I have to consider the risk values of each link. I know how to calculate the path's risk using its link value. For example: There is a path between node A and B wiht two links. Probability of failure for link 1 is 0.001 and for link 2 is 0.003. Here is the link with its risk values: A o--------------------o---------------------o B 0.001 0.003 So the probability of the link being down will be: 1 - (0.999 x 0.997) = 0.996003 You can find the attached file with disaster risk values of each link. For instance; first line is : 1,3,5,0.03 --> this means, first disaster affects links 1-3 and 5-0 and its occurrence rate is 0.03. So you need to assign link (1-3)'s risk to 0.03. Then you will continue with the next disaster which is the one in the next line. Note that, if a link gets affected by 2 disasters, you will add the probability of those 2 disasters to find that link's risk. If anyone can help me code the first line, I will be able to do the rest. You need use "array list" and some functions like "file reader" and "delimiter" I guess. Thanks in advance. -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- 1,3,5,0.03 2,3,5,5,4,0.11 3,3,5,5,4,5,8,0.04 2,5,8,7,8,0.04 3,14,10,14,13,17,13,0.04 1,14,18,0.06 4,10,13,14,13,17,13,12,13,0.04 4,11,6,11,9,11,12,11,19,0.08 3,19,20,15,20,21,20,0.24 1,21,20,0.05 3,20,21,21,16,21,22,0.27 From alan.gauld at btinternet.com Tue Apr 22 10:03:46 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 22 Apr 2014 09:03:46 +0100 Subject: [Tutor] Beginning Python 3.4.0 Programmer:Stephen Mik: Cannot get input variable to make While Loop conditional to work In-Reply-To: References: <1398103937.65340.YahooMailNeo@web124702.mail.ne1.yahoo.com> Message-ID: On 22/04/14 01:18, Alan Gauld wrote: > input takes as an argument a prompt string and returns the value > input by the user so your usage should look like: > > smv_grandVariable("Enter a 1 to play or 0 to exit:") Whoops, something went badly wrong in an edit there. It should read: smv_grandVariable = input("Enter a 1 to play or 0 to exit:") apologies for that. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Tue Apr 22 10:09:55 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 22 Apr 2014 09:09:55 +0100 Subject: [Tutor] Help needed with Python programming In-Reply-To: References: Message-ID: On 22/04/14 02:16, Suhana Vidyarthi wrote: > I have a python code that shows a set of shortest paths between nodes A > and B. It would help if you showed us this code. Otherwise we are just making wild guesses about how you are modelling this. Also knowing which Python version you are using would be good. > If anyone can help me code the first line, I will be able to do the > rest. You need use "array list" and some functions like "file reader" > and "delimiter" I guess. Have you written these functions already? Are they part of some module or library you are using? Or is it the writing of these functions you want help with? Graph or network analysis is a fairly standard math problem. There are probably algorithms (or even solutions) in other languages (or even in Python if you are lucky) that you can convert if you do a search. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Tue Apr 22 13:41:51 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 22 Apr 2014 21:41:51 +1000 Subject: [Tutor] Help needed with Python programming In-Reply-To: References: Message-ID: <20140422114151.GA17388@ando> On Mon, Apr 21, 2014 at 06:16:20PM -0700, Suhana Vidyarthi wrote: [...] > I have a python code that shows a set of shortest paths between nodes A and > B. Now I have to select the least risky path among them. To do that I have > to consider the risk values of each link. I know how to calculate the > path's risk using its link value. > > For example: There is a path between node A and B wiht two links. > Probability of failure for link 1 is 0.001 and for link 2 is 0.003. Here is > the link with its risk values: > A o--------------------o---------------------o B > 0.001 0.003 > So the probability of the link being down will be: 1 - (0.999 x 0.997) = > 0.996003 I don't think that calculation is correct. I think you mean that the probability of the link being UP is (0.999 x 0.997) = 0.996003, and the prob of it being DOWN is 1-0.996003 = 0.003997. So that path has a risk of 0.003997. > You can find the attached file with disaster risk values of each link. > > For instance; first line is : 1,3,5,0.03 --> this means, first disaster > affects links 1-3 and 5-0 and its occurrence rate is 0.03. So you need to > assign link (1-3)'s risk to 0.03. > Then you will continue with the next disaster which is the one in the next > line. Note that, if a link gets affected by 2 disasters, you will add the > probability of those 2 disasters to find that link's risk. > > If anyone can help me code the first line, I will be able to do the rest. > You need use "array list" and some functions like "file reader" and > "delimiter" I guess. Okay, let's start with reading the file. filename = "path/to/file.txt" Notice that I use forward slashes. Even if you are on Windows, you should code your paths with forward slashes. Either that, or you have to double every backslash: # on Windows either of these will be okay filename = "C:/path/to/file.txt" filename = "C:\\path\\to\\file.txt" Now let's read the file, one line at a time: filename = "path/to/file.txt" fp = open(filename, "r") for line in fp: # process that single line ... How might we process the line? I'm not sure what your requirements are, but at a guess you'll want something like this: - ignore leading and trailing whitespace, including the end of line marker at the end of each line; - skip blank lines; - split non-blank lines into four fields; - convert the first three into integers; - and the last field into a float. filename = "path/to/file.txt" fp = open(filename, "r") for line in fp: # process that single line line = line.strip() # ignore leading and trailing whitespace if not line: continue # skip blank lines a, b, c, d = line.split(",") # Split on commas a = int(a) # convert to an int instead of string b, c = int(b), int(c) d = float(d) # And now you can handle the values a, b, c, d ... And finally, when you are done, close the file: fp.close() Does this help? -- Steven From jorge.a.leon.g at gmail.com Tue Apr 22 15:48:51 2014 From: jorge.a.leon.g at gmail.com (Jorge Leon) Date: Tue, 22 Apr 2014 09:48:51 -0400 Subject: [Tutor] inheritance and super() function in python Message-ID: Good day, I have programmed a base class for an environment I have with no problem, but when it comes to referencing the base class's constructor in the derived class's constructor I have been getting errors: *TypeError: Error when calling the metaclass bases* * module.__init__() takes at most 2 arguments (3 given)* Here's how my base class' constructor looks like (position = [x, y, z]): *class Obstacle:* * def __init__(self,position):* * self.position = position* Here's how my derived class's constructor looks like *class Cylinder(Obstacle):* * def __init__(self,position, height, radius):* * super(Obstacle,self).__init__(position)* I have no idea where the 3 given arguments are being taken from. I have modified the code on the super line just in case I missed something but that has not changed a thing. I have read that in Python you may be able to double reference, but there are no other classes interfacing the base and derived class. If anyone has had some prior experience with this I'd appreciate your input. Regards, Jorge -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Tue Apr 22 16:51:46 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 23 Apr 2014 00:51:46 +1000 Subject: [Tutor] inheritance and super() function in python In-Reply-To: References: Message-ID: <20140422145146.GB17388@ando> On Tue, Apr 22, 2014 at 09:48:51AM -0400, Jorge Leon wrote: > Good day, > > > I have programmed a base class for an environment I have with no problem, > but when it comes to referencing the base class's constructor in the > derived class's constructor I have been getting errors: What version of Python are you using? With super, that is actually critical. > *TypeError: Error when calling the metaclass bases* > * module.__init__() takes at most 2 arguments (3 given)* Read the error message. Why is it refering to *module*.__init__? My guess is that you have a module called Obstacle, and a class called Obstacle, and you have mixed them up. Maybe you are doing this: # file Obstacle.py class Obstacle: # code goes here # Another file import Obstacle class Cylinder(Obstacle) I can reproduce your error that way: py> import math py> class X(math): ... pass ... Traceback (most recent call last): File "", line 1, in TypeError: module.__init__() takes at most 2 arguments (3 given) You need to say class Cylinder(Obstacle.Obstacle) Better still, use the naming convention that modules are in lowercase, and classes in CamelCase: import obstacle class Cylinder(obstacle.Obstacle): ... Even better still, Python is not Java. There is no need to put every class in its own file. > Here's how my base class' constructor looks like (position = [x, y, z]): > *class Obstacle:* > * def __init__(self,position):* > * self.position = position* In Python 2, that is a "classic class", or old-style class, and super will not work correctly. You need to inherit from object: class Obstacle(object) In Python 3, there is no difference and it should be fine. -- Steven From ag4ve.us at gmail.com Tue Apr 22 11:14:27 2014 From: ag4ve.us at gmail.com (shawn wilson) Date: Tue, 22 Apr 2014 05:14:27 -0400 Subject: [Tutor] subprocess not returning Message-ID: This works when I have a class for ldd and nothing else, but when I run it like this: https://gist.github.com/ag4ve/11171201 I don't get any of the libraries and I can't figure out where it's failing. ['fattr', [['/testroot', 0, 0, 777], ['/bin/dash']]] HERE1 [/testroot] HERE2 [/bin/dash] ['ldd', ['/lib/x86_64-linux-gnu/libc.so.6', '/lib64/ld-linux-x86-64.so.2']] HERE2 [/lib/x86_64-linux-gnu/libc.so.6] ['ldd', ['/lib64/ld-linux-x86-64.so.2']] HERE2 [/lib64/ld-linux-x86-64.so.2] ['ldd', ['statically']] HERE1 [statically] HERE2 [/lib64/ld-linux-x86-64.so.2] ['ldd', ['statically']] HERE1 [statically] [ 'filelist', [['/testroot', 0, 0, 777], ['/bin/dash', 0, 0, '755'], [[[]], []]]] Obviously it's returning something - but no usable info. From akleider at sonic.net Tue Apr 22 19:47:09 2014 From: akleider at sonic.net (Alex Kleider) Date: Tue, 22 Apr 2014 10:47:09 -0700 Subject: [Tutor] Groups of mutually exclusive options In-Reply-To: <20140422023544.GQ28400@ando> References: <8f1c5af55b9058b847e019fa63f61262@sonic.net> <20140422023544.GQ28400@ando> Message-ID: On 2014-04-21 19:35, Steven D'Aprano wrote: > Does docopt solve the Original Poster's question? If not, that advice > is > not terribly helpful. I don't pretend to fully understand the Original Poster's requirement but I believe mutual exclusivity is supported. Here's a short excerpt. """ Example uses brackets "[ ]", parens "( )", pipes "|" and ellipsis "..." to describe optional, required, mutually exclusive, and repeating elements. """ > > By the way, I think you mean Single Point Of Truth, not Light. > > http://www.faqs.org/docs/artu/ch04s02.html > > SPOT, also known as DRY (Don't Repeat Yourself), is an excellent > principle to follow, but in my experience it is often too difficult to > follow religiously. Thanks for the correction. I too have found it difficult but have always considered it a failure on my part when I'm forced to put it aside. The point I was making is that docopt makes it much easier. From breamoreboy at yahoo.co.uk Tue Apr 22 20:58:30 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 22 Apr 2014 19:58:30 +0100 Subject: [Tutor] Help needed with Python programming In-Reply-To: <20140422114151.GA17388@ando> References: <20140422114151.GA17388@ando> Message-ID: On 22/04/2014 12:41, Steven D'Aprano wrote: > On Mon, Apr 21, 2014 at 06:16:20PM -0700, Suhana Vidyarthi wrote: > [...] > > # on Windows either of these will be okay > filename = "C:/path/to/file.txt" > filename = "C:\\path\\to\\file.txt" > Or a raw string r'C:\path\to\file.txt' -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From alan.gauld at btinternet.com Tue Apr 22 21:10:29 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 22 Apr 2014 20:10:29 +0100 Subject: [Tutor] subprocess not returning In-Reply-To: References: Message-ID: On 22/04/14 10:14, shawn wilson wrote: > This works when I have a class for ldd and nothing else, but when I > run it like this: > https://gist.github.com/ag4ve/11171201 > > I don't get any of the libraries and I can't figure out where it's failing. > ['fattr', [['/testroot', 0, 0, 777], ['/bin/dash']]] > HERE1 [/testroot] > HERE2 [/bin/dash] > ['ldd', ['/lib/x86_64-linux-gnu/libc.so.6', '/lib64/ld-linux-x86-64.so.2']] > HERE2 [/lib/x86_64-linux-gnu/libc.so.6] > ['ldd', ['/lib64/ld-linux-x86-64.so.2']] > HERE2 [/lib64/ld-linux-x86-64.so.2] > ['ldd', ['statically']] > HERE1 [statically] > HERE2 [/lib64/ld-linux-x86-64.so.2] > ['ldd', ['statically']] > HERE1 [statically] > [ 'filelist', > [['/testroot', 0, 0, 777], ['/bin/dash', 0, 0, '755'], [[[]], []]]] > > Obviously it's returning something - but no usable info. We have no clue what you are doing. You say "this works" but we can't see what 'this' is. Is the code on the pastebin link the working or the broken version? It's also a very long listing. Can you produce a shorter example, perhaps with hard coded values that exhibits the problem? HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Tue Apr 22 21:37:24 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 22 Apr 2014 12:37:24 -0700 Subject: [Tutor] Help needed with Python programming In-Reply-To: References: Message-ID: Unfortunately, we can't give too much specific help on your particular problem because it's homework. You should use the knowledge you learned in your introductory programming class about designing programs. In particular, give a name to the function or functions your are designing. Be rigorous in the terms you are using when you talk about the problem. Formalize what the types of inputs and outputs are. Probably most importantly, express test cases that will demonstrate what you want the output to be. And not hand-wavy things, but actual test cases that you can execute. What's the expected result of parsing the first line? That is, you're saying that the string: "1,3,5,0.03" has some kind of meaning that can be parsed. Can you express this meaning as a data structure? Can you give that data structure a name? Can you write a unit test that can test that your parser is behaving properly? From davea at davea.name Tue Apr 22 21:54:17 2014 From: davea at davea.name (Dave Angel) Date: Tue, 22 Apr 2014 15:54:17 -0400 (EDT) Subject: [Tutor] inheritance and super() function in python References: Message-ID: Jorge Leon Wrote in message: > I think Steven has nailed your main problem, but I have two other suggestions: Use text mail, not html. This is a text list, and it can make a difference in half a dozen ways. Any decent email program has a way to select that. When showing an error, include the whole traceback. Steven would not have had to guess if you had. The file names would show him/us for sure. -- DaveA From ag4ve.us at gmail.com Tue Apr 22 21:49:47 2014 From: ag4ve.us at gmail.com (shawn wilson) Date: Tue, 22 Apr 2014 15:49:47 -0400 Subject: [Tutor] subprocess not returning In-Reply-To: References: Message-ID: On Tue, Apr 22, 2014 at 3:10 PM, Alan Gauld wrote: > We have no clue what you are doing. You say "this works" > but we can't see what 'this' is. Is the code on the > pastebin link the working or the broken version? > Per what is expected output (which I forgot to provide - sorry about that). Should be something like this: [ ['/testroot', '0', '0', '777'], ['/bin/dash', 0, 0, '755'], ['/lib/x86_64-linux-gnu/libc.so.6', '0', '0', '777'], ['/lib64/ld-linux-x86-64.so.2', '0', '0', '777'], ['/lib/x86_64-linux-gnu/libc-2.17.so', '0', '0', '755'], ['/lib/x86_64-linux-gnu/ld-2.17.so', '0', '0', '755'] ] Ie, find libraries a program is linked against (just try ldd against any file because I'm not caring about optimizing at this point) and then find the permissions of them and follow symlinks and do the same. Though, what I'm asking specifically is why __ldd isn't returning any values in my module. The best I can simplify to show the part working that should also be working in the gist code is: import subprocess import sys import pprint pp = pprint.PrettyPrinter(indent=4) class T: def ldd(filename): libs = [] for x in filename: p = subprocess.Popen(["ldd", x], universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) for line in p.stdout: s = line.split() pp.pprint(s) if "=>" in s: if len(s) == 3: # virtual library continue else: libs.append(s[2]) else: if len(s) == 2: libs.append(s[0]) return libs if __name__ == "__main__": t = T fattr = [ '/bin/dash' ] pp.pprint(["OUT", t.ldd(fattr)]) Past this, I can see that the ldd method is being called in my actual code but nothing is being returned from it like it is here. > It's also a very long listing. Can you produce a shorter > example, perhaps with hard coded values that exhibits > the problem? I really did try to simplify t.py (where all of the data is included in the script). I guess the reason for my question is because I'm not sure what's not working or what to try next? From baidusandy at 126.com Tue Apr 22 18:13:18 2014 From: baidusandy at 126.com (baidusandy) Date: Wed, 23 Apr 2014 00:13:18 +0800 (CST) Subject: [Tutor] loop couldn't work well and file couldn't close Message-ID: <4300c98a.e907.1458a367e37.Coremail.baidusandy@126.com> I'm a newbie to Python. I want to make python useful to my work, to write a script for get some data out. But there are some setbacks. I really needs your help. Please, help me. -------the first one-------------- for x in range(1,a): tem=open('results\\temp'+str(x)) tem.seek(0) i_line=tem.readline() while i_line: i_list=i_line.split('\t') if float(i_list[1]) From pscott_74 at yahoo.com Wed Apr 23 01:18:38 2014 From: pscott_74 at yahoo.com (Patti Scott) Date: Tue, 22 Apr 2014 16:18:38 -0700 (PDT) Subject: [Tutor] methods of sorting Message-ID: <1398208718.54563.YahooMailNeo@web161806.mail.bf1.yahoo.com> I'm practicing with lists.? I was looking for documentation on sorting with cmp() because it isn't immediately clear to me how comparing items two at a time can sort the entire list.? Identify max or min values, yes, but not sort the whole list.? So, the Sorting HOW TO (Dalke, Hettinger)? posted on python.org goes into detail on using a key parameter for sorted() and .sort(), and using operator module functions.? How obsolete are the cmp() and the decorate-sort-undecorate methods?? To be understood but probably not used in new code? Python Programming, Zelle;? Python 2.7.3,? PowerShell, Notepad ++ I tried several means of sorting for exercises, eg # sortgpa3.py # extended to let user print report out by name,GPA or credit hours from gpa import Student, makeStudent ??? def readStudents(filename): ??? infile = open(filename, 'r') ??? students = [] ??? for line in infile: ??? ??? students.append(makeStudent(line)) ??? infile.close() ??? return students ??? def writeStudents(students, filename): ??? outfile = open(filename, 'w') ??? for s in students: ??? ??? outfile.write("%0.2f\t%s\t%0.2f\t%0.2f\n" % (s.gpa(),s.getName(), s.getHours(), s.getQPoints())) ??? outfile.close() ??? def cmpGPA(s1, s2): ??? #function compares two students based on GPA ??? return cmp(s1.gpa(), s2.gpa()) def cmpHours(s1, s2): ??? #function compares two students based on credits ??? return cmp(s1.getHours(), s2.getHours()) ??? def cmpNames(s1, s2): ??? #function compares two students' names ??? return cmp(s1.getName(), s2.getName()) ??? def main(): ??? print "This program sorts student grade information by GPA." ??? order = raw_input("Do you want results printed by name, credits or GPA? ") ??? filename = raw_input("Enter the name of the data file: ") ??? data = readStudents(filename) ??? if order[0] == ('c' or 'C'): ??? ??? data.sort(cmpHours) ??? elif order[0] == ('g' or "G"): ??? ??? data.sort(cmpGPA) ??? else: ??? ??? data.sort(cmpNames) ??? filename = raw_input("Enter a name for the output file: ") ??? writeStudents(data, filename) ??? ??? print "The data has been written to file %s." % (filename) ??? ??? ??? main() or, def main(): ??? print "This program sorts students based on user request." ??? filename = raw_input("Enter name of the file containing student data: ") ??? data = readStudents(filename) ??? order = raw_input("Choose the field on which to sort students (name, GPA or credits): ") ??? #print order[0] ??? if order[0] == ('n' or "N"): ??? ??? tuples = [(student.getName(), student) for student in data] ??? ??? tuples.sort() ??? ??? data = [(tuples[i][1]) for i in range(len(tuples))] ??? ??? #data.sort() ??? ??? ??? elif order[0] == ('c' or "C"): ??? ??? tuples = [(student.getHours(), student) for student in data] ??? ??? tuples.sort() ??? ??? data = [(tuples[i][1]) for i in range(len(tuples))] ??? ??? ??? elif order[0] == ('g' or "G"): ??? ??? tuples = [(student.gpa(), student) for student in data] ??? ??? tuples.sort() ??? ??? data = [(tuples[i][1]) for i in range(len(tuples))] ??? ??? filename = raw_input("Enter a name for the output file: ") ??? writeStudents(data, filename) ??? print "The data has been written to %s ." % (filename) ??? if __name__=='__main__': ??? main() or, def main(): ??? print "This program sorts students based on user request." ??? filename = raw_input("Enter name of the file containing student data: ") ??? data = readStudents(filename) ??? order = raw_input("Choose the field on which to sort students (name, GPA or credits): ") ??? print order[0] ??? if order[0] == ('g' or "G"): ??? ??? data = sorted(data, key=lambda student: student.gpa()) ??? elif order[0] == ('c' or "C"): ??? ??? data = sorted(data, key=lambda student: student.getHours()) ??? elif order[0] == ('n' or "N"): ??? ??? data = sorted(data, key=lambda student: student.getName()) ??? filename = raw_input("Enter a name for the output file: ") ??? writeStudents(data, filename) ??? print "The data has been written to %s ." % (filename) ??? if __name__=='__main__': ??? main() -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Wed Apr 23 01:50:41 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 22 Apr 2014 16:50:41 -0700 Subject: [Tutor] methods of sorting In-Reply-To: <1398208718.54563.YahooMailNeo@web161806.mail.bf1.yahoo.com> References: <1398208718.54563.YahooMailNeo@web161806.mail.bf1.yahoo.com> Message-ID: On Tue, Apr 22, 2014 at 4:18 PM, Patti Scott wrote: > I'm practicing with lists. I was looking for documentation on sorting with > cmp() because it isn't immediately clear to me how comparing items two at a > time can sort the entire list. As you note, comparison itself doesn't sort a list. Comparing elements by itself is a query, not an action. But what it does is give Python enough tools to do the sort for you. Python uses a "comparison" based sorting routine which works by taking in a user-defined notion of when two elements are in order or not. http://en.wikipedia.org/wiki/Comparison_sort As a handwavy explanation of the idea: imagine a list that hasn't been sorted. Python can use the comparison function you give it to repeatedly compare elements in the list. Whenever if it sees disorder, it can swap elements and thereby reduce the disorder in the list. Assuming the comparison is a "good" one, then we can eventually sort the whole list by repeating this over and over. Note that Python will be calling the comparison function on your behalf: you won't be calling it directly yourself. (By "good", we mean a "total ordering" in the sense described in: http://en.wikipedia.org/wiki/Total_order) This is a handwavy explanation because Python does these comparisons and swapping in a fairly sophisticated way to avoid a lot of work. See: http://en.wikipedia.org/wiki/Timsort It maybe that you have not seen instances of functions that take functions as arguments. If so, consider two functions f and g: ######### def f(x): return x * x def g(x): return 2 * x ######### Toy functions, of course. Now they themselves don't do much but compute the square and the double of a number. But they can be passed as arguments to other functions to do something. For example, if we have some list of numbers: ######### numbers = [3, 1, 4, 1, 5, 9, 2, 6] ######### then we may apply f and g pointwise across those functions, using the map() function: ######### print(map(f, numbers)) print(map(g, numbers)) ######### and you'll see that we can compute bulk operations on lists. Again, we're leaving the map() function to call 'f' and 'g' for us. This is an example of a function that can take in other functions. The sorting routine you're looking at is conceptually doing a similar thing by taking in a comparison function, which it will use during its own work. Passing functions as values allows for a notion of "variable" that's really powerful: what is varying isn't just some static piece of plain data, but rather a behavior. From alan.gauld at btinternet.com Wed Apr 23 02:28:03 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 23 Apr 2014 01:28:03 +0100 Subject: [Tutor] loop couldn't work well and file couldn't close In-Reply-To: <4300c98a.e907.1458a367e37.Coremail.baidusandy@126.com> References: <4300c98a.e907.1458a367e37.Coremail.baidusandy@126.com> Message-ID: On 22/04/14 17:13, baidusandy wrote: > I'm a newbie to Python. I want to make python useful to my work, to > write a script for get some data out. But there are some setbacks. I > really needs your help. Please, help me. Its too late for me to study your code properly but here are a few immediate observations... > -------the first one-------------- > for x in range(1,a): > tem=open('results\\temp'+str(x)) > tem.seek(0) You shouldn'yt need to seek(0) immediately after opening to read since the file cursor will already be at the beginning. > i_line=tem.readline() > while i_line: > i_list=i_line.split('\t') > if float(i_list[1]) o=open('results\\'+str(x),'a') > o.write(i_line) > o.close > else: > tem02=open('results\\temp'+str(x+1),'a') > tem02.write(i_line) > tem02.close > i_line=tem.readline() This looks like it could be done more elegantly using a for loop over the file; for i_line in tem: ... > tem.close And if you use with you don;t need a close: with open(...) as tem: for i_line in tem: ... > for x in range(a,a+1): > close('results\\temp'+str(x)) You can't close a file by passing its filename in. You can only close it by calling close on the open file object - which you have already done for your files in the earlier loops. (Or if you use 'with' is done automatically) > os.rename('results\\temp'+str(x),'results\\'+str(x)) > ## for the last line, the code couldn't work. I run the script in > Windows XP, i don't know if there is something to do with the OS. Wjhat do you mean by 'wouldnt work'? Do you get an error? If the file not changed? Is it lost? We need more detail. > --------for another question:----------- > for x in range (a,a+1): > m=open(01) I assume the lack of quotes is a typo? Its better to paste real code rather than retype it. > m_line=m.readline() > b=open('02','w') > while m_line: > b.write(m_line) > m_line=m.readline() Again this would be better as a for loop for m_line in m: ... But better still would be to just copy the file using the shutil.copy() function. > b.close > m.close > ## for this one, file 02 and 01are not the same. to tell the truth, the > last line of 01 has not been written into file 02. What's wrong? Sorry, not sure, it looks OK from that point of view. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From jorge.a.leon.g at gmail.com Wed Apr 23 02:59:59 2014 From: jorge.a.leon.g at gmail.com (Jorge Leon) Date: Tue, 22 Apr 2014 20:59:59 -0400 Subject: [Tutor] inheritance and super() function in python In-Reply-To: References: Message-ID: Thank you Steve and Dave for the prompt response and advise, and sorry about the format. The version of Python I'm working under is 2.7.5. About the .super(): I'm going to try out the format you gave me for the files, and yes: that's exactly how I had it. Something that has stuck from all the C++ programming I'm doing, which also leads me to believe that it may be better for me to step away from using .super() if I don't get the program to work as intended when I apply your advise. Going to consult more tutorials from the page about inheritance and operator and function overloading. Regards, Jorge On Tue, Apr 22, 2014 at 3:54 PM, Dave Angel wrote: > Jorge Leon Wrote in message: >> > > I think Steven has nailed your main problem, but I have two other > suggestions: > > Use text mail, not html. This is a text list, and it can make a > difference in half a dozen ways. Any decent email program has a > way to select that. > > When showing an error, include the whole traceback. Steven would > not have had to guess if you had. The file names would show > him/us for sure. > > -- > DaveA > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From steve at pearwood.info Wed Apr 23 03:41:28 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 23 Apr 2014 11:41:28 +1000 Subject: [Tutor] inheritance and super() function in python In-Reply-To: References: Message-ID: <20140423014128.GD17388@ando> On Tue, Apr 22, 2014 at 08:59:59PM -0400, Jorge Leon wrote: > Thank you Steve and Dave for the prompt response and advise, and sorry > about the format. > > The version of Python I'm working under is 2.7.5. About the .super(): > I'm going to try out the format you gave me for the files, and yes: > that's exactly how I had it. Something that has stuck from all the C++ > programming I'm doing, which also leads me to believe that it may be > better for me to step away from using .super() if I don't get the > program to work as intended when I apply your advise. Using super() is fine. (Note that super is a function, not a method -- there is no dot at the front.) You just have to remember to inherit from object (or some other built-in type). You might like to read this to understand why there is a difference between inheriting from object and not: http://import-that.dreamwidth.org/3098.html -- Steven From steve at pearwood.info Wed Apr 23 03:36:51 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 23 Apr 2014 11:36:51 +1000 Subject: [Tutor] methods of sorting In-Reply-To: <1398208718.54563.YahooMailNeo@web161806.mail.bf1.yahoo.com> References: <1398208718.54563.YahooMailNeo@web161806.mail.bf1.yahoo.com> Message-ID: <20140423013650.GC17388@ando> Hi Patti, My answers below, interleaved between your questions. On Tue, Apr 22, 2014 at 04:18:38PM -0700, Patti Scott wrote: > I'm practicing with lists.? I was looking for documentation on sorting > with cmp() because it isn't immediately clear to me how comparing > items two at a time can sort the entire list.? Identify max or min > values, yes, but not sort the whole list.? So, the Sorting HOW TO > (Dalke, Hettinger)? posted on python.org goes into detail on using a > key parameter for sorted() and .sort(), and using operator module > functions.? Think about how you might sort four items 42, 23, 57, 30. There are many different ways to sort, and this is one of the least efficient, but easiest to understand. We start by putting the unsorted items on the left, and the sorted items on the right, as if we were sorting a handful of playing cards: [42, 23, 57, 30] [] Take the first item from the left, and find where it belongs on the right. Since the right is currently empty, that's easy: [23, 57, 30] [42] Now take the next item from the left, and find where it belongs on the right. How do you do that? By comparing it to each item already there. If it compares less than the item, insert it just before the item; otherwise keep going. In this case, we compare 23 < 42, which returns True, so we insert 23 to the left of 42. [57, 30] [23, 42] Now repeat with the next item. In this case, 57 < 23 returns False, so we continue. 57 < 42 also returns False, and there are no more numbers to check so we put 57 at the end: [30] [23, 42, 57] Finally we compare 30 < 23, which returns False, then 30 < 42, which returns True, so we insert 30 just to the left of 42: [] [23, 30, 42, 57] Now that we know how to sort using "less than" < as the comparison function, we can use some other comparison function that works similarly. Instead of using < we can use the built-in function cmp(a, b), which returns -1 if a < b, 0 if a == b, and +1 if a > b. Or instead of using the built-in cmp function, we can use any function that takes two arguments, the items to be compared, and returns one of -1, 0 or 1. Even though the Python list.sort() method is a lot faster and more clever than what I show above, it too allows you to provide a custom comparison function to decide which comes earlier or later when sorting. Here's an example with and without a comparison function: py> sorted(['dog', 'aardvark', 'chicken', 'horse']) ['aardvark', 'chicken', 'dog', 'horse'] py> sorted(['dog', 'aardvark', 'chicken', 'horse'], ... lambda a, b: cmp(len(a), len(b))) ['dog', 'horse', 'chicken', 'aardvark'] In the second case, we sort by the length of the words, not the content of the word. So "dog" (three letters) compares less than "aardvark" (eight letters). A couple of other notes: - Rather than define a comparison function using def, I use lambda as a shortcut. lambda creates a function, but limited only to a single expression. So "lambda a, b: cmp(len(a), len(b))" is equivalent to: def function(a, b): return cmp(len(a), len(b)) - Notice that I use the built-in cmp function inside my comparison function. That's just for convenience, you don't have to do that. > How obsolete are the cmp() and the decorate-sort-undecorate methods?? > To be understood but probably not used in new code? Both are very obsolute, but for different reasons. The problem with using a comparison function is that it is very inefficient and it can really slow down sorting of large lists by a lot. It is better to use the DSU idiom rather than call a comparison function. In fact, that is so much better, that recent versions of Python make the DSU idiom built-in: that's what the "key" argument to the sort() and sorted() functions is for. When you supply a key function to sort, it internally uses the DSU idiom. You almost never need to use it yourself. Using the key function is so much better than using a comparison function that in Python 3 the comparison function was dropped altogether and using key is the only way to customize sorting. > Python Programming, Zelle;? Python 2.7.3,? PowerShell, Notepad ++ > > I tried several means of sorting for exercises, eg [lots of code shown] I'm sorry, did you have a question about the sorting code or were you just sharing it with us? If you're asking which should be preferred, I would prefer the version using the key=... argument to sort. -- Steven From davea at davea.name Wed Apr 23 04:06:57 2014 From: davea at davea.name (Dave Angel) Date: Tue, 22 Apr 2014 22:06:57 -0400 (EDT) Subject: [Tutor] loop couldn't work well and file couldn't close References: <4300c98a.e907.1458a367e37.Coremail.baidusandy@126.com> Message-ID: baidusandy Wrote in message: [invisible message not copied here] By posting in html, you managed to pick black on black text. I literally could see none of your message except the boilerplate. Please tell your email program to use text mode. -- DaveA From davea at davea.name Wed Apr 23 04:18:11 2014 From: davea at davea.name (Dave Angel) Date: Tue, 22 Apr 2014 22:18:11 -0400 (EDT) Subject: [Tutor] inheritance and super() function in python References: Message-ID: Jorge Leon Wrote in message: > Thank you Steve and Dave for the prompt response and advise, and sorry > about the format. > > The version of Python I'm working under is 2.7.5. About the .super(): > I'm going to try out the format you gave me for the files, and yes: > that's exactly how I had it. Something that has stuck from all the C++ > programming I'm doing, which also leads me to believe that it may be > better for me to step away from using .super() if I don't get the > program to work as intended when I apply your advise. > > Going to consult more tutorials from the page about inheritance and > operator and function overloading. > Please don't top-post. Put your comments after the part you're quoting, and delete anything you're not responding to, which would certainly be anything following your message. There's another flaw in your call to super. You had : class Cylinder(Obstacle): ? ?def __init__(self,position, height, radius): ? ? ? ?super(Obstacle,self).__init__(position) But it looks to me like the last line should be super(Cylinder, self).__init__(position) -- DaveA From Hobie.Audet at comcast.net Wed Apr 23 05:35:18 2014 From: Hobie.Audet at comcast.net (Hobie Audet) Date: Tue, 22 Apr 2014 23:35:18 -0400 Subject: [Tutor] SMTPLIB Exception Object Message-ID: <3gD6hb2cmQz7Ljd@mail.python.org> I am using Python 3.3 and smtplib to generate and send some E-mail. I am trying to figure out how to handle some exceptions, but some of the documentation has me confused. Specifically, the documentation on the SMTPRecipientsRefused exception says: exception smtplib.SMTPRecipientsRefused All recipient addresses refused. The errors for each recipient are accessible through the attribute recipients, which is a dictionary of exactly the same sort as SMTP.sendmail() returns. But where is the "recipients" attribute? More specifically, what is it an attribute of? My code looks something like this: server = smtplib.SMTP(name,port) server.login(userid, password) try: server.sendmail(fromline, tolist, msg) except smtplib.SMTPRecipientsRefused: (here's where I need to access the recipients attribute) I have tried server.recipients, but get an attribute error (no such attribute). I've also tried: smtplib.recipients smtplib.SMTPRecipientsRefused.recipients and all these indicate that there is no attribute "recipients". So where is that "recipients" attribute? Thanks. Hobie Audet -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Wed Apr 23 11:22:11 2014 From: davea at davea.name (Dave Angel) Date: Wed, 23 Apr 2014 05:22:11 -0400 (EDT) Subject: [Tutor] SMTPLIB Exception Object References: <3gD6hb2cmQz7Ljd@mail.python.org> Message-ID: Hobie Audet Wrote in message: > It would be much better if you used text emails to post on this text list, rather than html. For one thing, your indentation might not be messed up. For another, I might be able to do proper quoting. > (you wrote): My code looks something like this: ????????server = smtplib.SMTP(name,port) ???????? server.login(userid, password) ????????try: ???????????? server.sendmail(fromline, tolist, msg) ????????? except smtplib.SMTPRecipientsRefused: ????????????? (here's where I need to access the recipients attribute) _____________ Use copypaste, as when you retype, it's likely you'll mess up somewhere. You don't save the exception object. Try except smtplib.SMTPRecipientsRefused as excep: If that doesn't get you going, then please tell us what Python version. -- DaveA From alan.gauld at btinternet.com Wed Apr 23 11:23:50 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 23 Apr 2014 10:23:50 +0100 Subject: [Tutor] SMTPLIB Exception Object In-Reply-To: <3gD6hb2cmQz7Ljd@mail.python.org> References: <3gD6hb2cmQz7Ljd@mail.python.org> Message-ID: On 23/04/14 04:35, Hobie Audet wrote: > documentation has me confused. Specifically, the documentation on the > SMTPRecipientsRefused exception says: > > All recipient addresses refused. The errors for each recipient are > accessible through the attribute recipients,... > > But where is the "recipients" attribute? More specifically, what is it > an attribute of? Since its talking about the exception I'd assume its an attribute of the exception object. > try: > server.sendmail(fromline, tolist, msg) > except smtplib.SMTPRecipientsRefused: But you are not fetching the exception object so you need to change your except line to be (assuming Python v3): except smtplib.SMTPRecipientsRefused as err: Now you can access err.recipients HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From jorge.a.leon.g at gmail.com Wed Apr 23 15:56:59 2014 From: jorge.a.leon.g at gmail.com (Jorge Leon) Date: Wed, 23 Apr 2014 09:56:59 -0400 Subject: [Tutor] inheritance and super() function in python In-Reply-To: References: Message-ID: > class Cylinder(Obstacle): > def __init__(self,position, height, radius): > super(Obstacle,self).__init__(position) > > But it looks to me like the last line should be > super(Cylinder, self).__init__(position) > Hey, thanks again for the help and sorry about all the format errors I was able to successfully use super(). The classes were made with the old style (thanks Steve for the blog post). Using the last bit of corrections the program successfully sourced the constructor from the parent class. Again, thank you for the prompt and professional response. Jorge From sunil.techspk at gmail.com Wed Apr 23 16:44:43 2014 From: sunil.techspk at gmail.com (Sunil Tech) Date: Wed, 23 Apr 2014 20:14:43 +0530 Subject: [Tutor] Fwd: Puzzle - Next Step to our interviewing process - Pramati Technologies! In-Reply-To: References: <06a601cf0b86$aefe5360$0cfafa20$@pramati.com> <03d301cf0c34$e3796450$aa6c2cf0$@pramati.com> <01e801cf0cee$98e359e0$caaa0da0$@pramati.com> <011f01cf0db5$5fc59660$1f50c320$@pramati.com> <076401cf0df4$8f481180$add83480$@pramati.com> <053c01cf1032$11b7c860$35275920$@pramati.com> <067401cf1044$a88717c0$f9954740$@pramati.com> <044601cf11b1$03387930$09a96b90$@pramati.com> <05f001cf11bf$619b09d0$24d11d70$@pramati.com> <07a901cf11d7$d2927c50$77b774f0$@pramati.com> <019801cf1276$6583a180$308ae480$@pramati.com> <07bd01cf12a0$4dd621a0$e98264e0$@pramati.com> <01e201cf133b$30ce7a30$926b6e90$@pramati.com> <025801cf1596$6bc7fd20$4357f760$@pramati.com> <027d01cf1665$40045f10$c00d1d30$@pramati.com> <01ac01cf1728$22d68ad0$6883a070$@pramati.com> <019001cf18c9$305629c0$91027d40$@pramati.com> <047c01cf1b20$d946e840$8bd4b8c0$@pramati.com> <076b01cf1b3e$b0ece070$12c6a150$@pramati.com> <0bf601cf1b5e$aa067760$fe136620$@pramati.com> <085701cf1c0c$bbda1290$338e37b0$@pramati.com> <074701cf1cde$77f89150$67e9b3f0$@pramati.com> <027401cf1d76$6c6f65a0$454e30e0$@pramati.com> <088501cf1d9f$128a41d0$379ec570$@pramati.com> <09af01cf1dab$7e68e350$7b3aa9f0$@pramati.com> <01e801cf1e36$e6fea8b0$b4fbfa10$@pramati.com> <042501cf20b1$c00a1460$401e3d20$@pramati.com> <000301cf20ca$da170ca0$8e4525e0$@pramati.com> <023b01cf22fd$0a2bd9f0$1e838dd0$@pramati.com> <06ce01cf2645$48559180$d900b480$@pramati.com> <084301cf2654$f74757f0$e5d607d0$@pramati.com> <00eb01cf26db$fa213f90$ee63beb0$@pramati.com> <04ea01cf26f0$ccc4a680$664df380$@pramati.com> <076901cf2705$54e187e0$fea497a0$@pramati.com> <023c01cf27ae$0652b4b0$12f81e10$@pramati.com> <024801cf27ae$24ea4780$6ebed680$@pramati.com> <084301cf27e2$2b2d8790$818896b0$@pramati.com> <04bc01cf288d$a72b0d30$f5812790$@pramati.com> <040301cf2bb5$8d57cc10$a8076430$@pramati.com> <058c01cf2bc6$2ef3ead0$8cdbc070$@pramati.com> <04ba01cf345e$114ec9a0$33ec5ce0$@pramati.com> <051601cf3463$6f4c9410$4de5bc30$@pramati.com> <04b601cf3c24$5c48ae30$14da0a90$@pramati.com> <08c901cf3ea1$e512e030$af38a090$@pramati.com> <01dd01cf434a$e0869130$a193b390$@pramati.com> <02d001cf44c9$a27e3890$e77aa9b0$@pramati.com> <02a701cf4715$2d165f80$87431e80$@pramati.com> <04a701cf47f9$32bf7270$983e5750$@pramati.com> <033401cf48bd$ce0b2750$6a2175f0$@pramati.com> <03fb01cf48c4$66f86b20$34e94160$@pramati.com> <022101cf4978$ba8475e0$2f8d61a0$@pramati.com> <014201cf4ef1$3aeba3c0$b0c2eb40$@pramati.com> <015c01cf4ef1$b0978a80$11c69f80$@pramati.com> <01bd01cf4fbc$c256fb40$4704f1c0$@pramati.com> <036601cf52f9$aaf76cb0$00e64610$@pramati.com> <052d01cf5311$10d9c390$328d4ab0$@pramati.com> <02b301cf5478$b1d21e60$15765b20$@pramati.com> <03e101cf57a9$227caf80$67760e80$@pramati.com> Message-ID: i have {'extreme_fajita': [*{5: 4.0}*, *{6: 6.0}*], 'fancy_european_water': [*{5: 8.0}*, *{6: 5.0}*]} if the keys of the dictionaries(bold & italic) are equal. I want to add bold dict values, & italic dict values. result should some thing like this [{5:12.0},{6:11.5}] i tried to do... but need your help. Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Wed Apr 23 17:28:52 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 23 Apr 2014 16:28:52 +0100 Subject: [Tutor] inheritance and super() function in python In-Reply-To: References: Message-ID: On 23/04/2014 14:56, Jorge Leon wrote: >> class Cylinder(Obstacle): >> def __init__(self,position, height, radius): >> super(Obstacle,self).__init__(position) >> >> But it looks to me like the last line should be >> super(Cylinder, self).__init__(position) >> > > Hey, thanks again for the help and sorry about all the format errors I > was able to successfully use super(). The classes were made with the > old style (thanks Steve for the blog post). Using the last bit of > corrections the program successfully sourced the constructor from the > parent class. > > Again, thank you for the prompt and professional response. > > Jorge Excellent article here on super for anybody who's interested http://rhettinger.wordpress.com/2011/05/26/super-considered-super/ -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From dyoo at hashcollision.org Wed Apr 23 19:12:42 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 23 Apr 2014 10:12:42 -0700 Subject: [Tutor] Fwd: Puzzle - Next Step to our interviewing process - Pramati Technologies! In-Reply-To: References: <06a601cf0b86$aefe5360$0cfafa20$@pramati.com> <03d301cf0c34$e3796450$aa6c2cf0$@pramati.com> <01e801cf0cee$98e359e0$caaa0da0$@pramati.com> <011f01cf0db5$5fc59660$1f50c320$@pramati.com> <076401cf0df4$8f481180$add83480$@pramati.com> <053c01cf1032$11b7c860$35275920$@pramati.com> <067401cf1044$a88717c0$f9954740$@pramati.com> <044601cf11b1$03387930$09a96b90$@pramati.com> <05f001cf11bf$619b09d0$24d11d70$@pramati.com> <07a901cf11d7$d2927c50$77b774f0$@pramati.com> <019801cf1276$6583a180$308ae480$@pramati.com> <07bd01cf12a0$4dd621a0$e98264e0$@pramati.com> <01e201cf133b$30ce7a30$926b6e90$@pramati.com> <025801cf1596$6bc7fd20$4357f760$@pramati.com> <027d01cf1665$40045f10$c00d1d30$@pramati.com> <01ac01cf1728$22d68ad0$6883a070$@pramati.com> <019001cf18c9$305629c0$91027d40$@pramati.com> <047c01cf1b20$d946e840$8bd4b8c0$@pramati.com> <076b01cf1b3e$b0ece070$12c6a150$@pramati.com> <0bf601cf1b5e$aa067760$fe136620$@pramati.com> <085701cf1c0c$bbda1290$338e37b0$@pramati.com> <074701cf1cde$77f89150$67e9b3f0$@pramati.com> <027401cf1d76$6c6f65a0$454e30e0$@pramati.com> <088501cf1d9f$128a41d0$379ec570$@pramati.com> <09af01cf1dab$7e68e350$7b3aa9f0$@pramati.com> <01e801cf1e36$e6fea8b0$b4fbfa10$@pramati.com> <042501cf20b1$c00a1460$401e3d20$@pramati.com> <000301cf20ca$da170ca0$8e4525e0$@pramati.com> <023b01cf22fd$0a2bd9f0$1e838dd0$@pramati.com> <06ce01cf2645$48559180$d900b480$@pramati.com> <084301cf2654$f74757f0$e5d607d0$@pramati.com> <00eb01cf26db$fa213f90$ee63beb0$@pramati.com> <04ea01cf26f0$ccc4a680$664df380$@pramati.com> <076901cf2705$54e187e0$fea497a0$@pramati.com> <023c01cf27ae$0652b4b0$12f81e10$@pramati.com> <024801cf27ae$24ea4780$6ebed680$@pramati.com> <084301cf27e2$2b2d8790$818896b0$@pramati.com> <04bc01cf288d$a72b0d30$f5812790$@pramati.com> <040301cf2bb5$8d57cc10$a8076430$@pramati.com> <058c01cf2bc6$2ef3ead0$8cdbc070$@pramati.com> <04ba01cf345e$114ec9a0$33ec5ce0$@pramati.com> <051601cf3463$6f4c9410$4de5bc30$@pramati.com> <04b601cf3c24$5c48ae30$14da0a90$@pramati.com> <08c901cf3ea1$e512e030$af38a090$@pramati.com> <01dd01cf434a$e0869130$a193b390$@pramati.com> <02d001cf44c9$a27e3890$e77aa9b0$@pramati.com> <02a701cf4715$2d165f80$87431e80$@pramati.com> <04a701cf47f9$32bf7270$983e5750$@pramati.com> <033401cf48bd$ce0b2750$6a2175f0$@pramati.com> <03fb01cf48c4$66f86b20$34e94160$@pramati.com> <022101cf4978$ba8475e0$2f8d61a0$@pramati.com> <014201cf4ef1$3aeba3c0$b0c2eb40$@pramati.com> <015c01cf4ef1$b0978a80$11c69f80$@pramati.com> <01bd01cf4fbc$c256fb40$4704f1c0$@pramati.com> <036601cf52f9$aaf76cb0$00e64610$@pramati.com> <052d01cf5311$10d9c390$328d4ab0$@pramati.com> <02b301cf5478$b1d21e60$15765b20$@pramati.com> <03e101cf57a9$227caf80$67760e80$@pramati.com> Message-ID: Hi Sunil, Try a simpler but related problem first. Say that you have two lists of numbers, like: ###### nums1 = [3, 1, 4] nums2 = [2, 7, 1] ###### Can you design a function addLists() that takes two lists of numbers of equal length, and adds them together? For example, addLists(nums1, nums2) == [5, 8, 5] should be true, as well as: addLists([4, 6], [8, 5]) == [12, 11] Would you be able to write the addLists() function? Would you be able to write a few test cases to check that the implementation works on those examples? From brianjamesarb at gmail.com Wed Apr 23 19:47:12 2014 From: brianjamesarb at gmail.com (brian arb) Date: Wed, 23 Apr 2014 13:47:12 -0400 Subject: [Tutor] Fwd: Puzzle - Next Step to our interviewing process - Pramati Technologies! In-Reply-To: References: <06a601cf0b86$aefe5360$0cfafa20$@pramati.com> <03d301cf0c34$e3796450$aa6c2cf0$@pramati.com> <01e801cf0cee$98e359e0$caaa0da0$@pramati.com> <011f01cf0db5$5fc59660$1f50c320$@pramati.com> <076401cf0df4$8f481180$add83480$@pramati.com> <053c01cf1032$11b7c860$35275920$@pramati.com> <067401cf1044$a88717c0$f9954740$@pramati.com> <044601cf11b1$03387930$09a96b90$@pramati.com> <05f001cf11bf$619b09d0$24d11d70$@pramati.com> <07a901cf11d7$d2927c50$77b774f0$@pramati.com> <019801cf1276$6583a180$308ae480$@pramati.com> <07bd01cf12a0$4dd621a0$e98264e0$@pramati.com> <01e201cf133b$30ce7a30$926b6e90$@pramati.com> <025801cf1596$6bc7fd20$4357f760$@pramati.com> <027d01cf1665$40045f10$c00d1d30$@pramati.com> <01ac01cf1728$22d68ad0$6883a070$@pramati.com> <019001cf18c9$305629c0$91027d40$@pramati.com> <047c01cf1b20$d946e840$8bd4b8c0$@pramati.com> <076b01cf1b3e$b0ece070$12c6a150$@pramati.com> <0bf601cf1b5e$aa067760$fe136620$@pramati.com> <085701cf1c0c$bbda1290$338e37b0$@pramati.com> <074701cf1cde$77f89150$67e9b3f0$@pramati.com> <027401cf1d76$6c6f65a0$454e30e0$@pramati.com> <088501cf1d9f$128a41d0$379ec570$@pramati.com> <09af01cf1dab$7e68e350$7b3aa9f0$@pramati.com> <01e801cf1e36$e6fea8b0$b4fbfa10$@pramati.com> <042501cf20b1$c00a1460$401e3d20$@pramati.com> <000301cf20ca$da170ca0$8e4525e0$@pramati.com> <023b01cf22fd$0a2bd9f0$1e838dd0$@pramati.com> <06ce01cf2645$48559180$d900b480$@pramati.com> <084301cf2654$f74757f0$e5d607d0$@pramati.com> <00eb01cf26db$fa213f90$ee63beb0$@pramati.com> <04ea01cf26f0$ccc4a680$664df380$@pramati.com> <076901cf2705$54e187e0$fea497a0$@pramati.com> <023c01cf27ae$0652b4b0$12f81e10$@pramati.com> <024801cf27ae$24ea4780$6ebed680$@pramati.com> <084301cf27e2$2b2d8790$818896b0$@pramati.com> <04bc01cf288d$a72b0d30$f5812790$@pramati.com> <040301cf2bb5$8d57cc10$a8076430$@pramati.com> <058c01cf2bc6$2ef3ead0$8cdbc070$@pramati.com> <04ba01cf345e$114ec9a0$33ec5ce0$@pramati.com> <051601cf3463$6f4c9410$4de5bc30$@pramati.com> <04b601cf3c24$5c48ae30$14da0a90$@pramati.com> <08c901cf3ea1$e512e030$af38a090$@pramati.com> <01dd01cf434a$e0869130$a193b390$@pramati.com> <02d001cf44c9$a27e3890$e77aa9b0$@pramati.com> <02a701cf4715$2d165f80$87431e80$@pramati.com> <04a701cf47f9$32bf7270$983e5750$@pramati.com> <033401cf48bd$ce0b2750$6a2175f0$@pramati.com> <03fb01cf48c4$66f86b20$34e94160$@pramati.com> <022101cf4978$ba8475e0$2f8d61a0$@pramati.com> <014201cf4ef1$3aeba3c0$b0c2eb40$@pramati.com> <015c01cf4ef1$b0978a80$11c69f80$@pramati.com> <01bd01cf4fbc$c256fb40$4704f1c0$@pramati.com> <036601cf52f9$aaf76cb0$00e64610$@pramati.com> <052d01cf5311$10d9c390$328d4ab0$@pramati.com> <02b301cf5478$b1d21e60$15765b20$@pramati.com> <03e101cf57a9$227caf80$67760e80$@pramati.com> Message-ID: >>> nums1 = [3, 1, 4] >>> nums2 = [2, 7, 1] >>> [ sum(i) for i in zip(nums1, nums2)] [5, 8, 5] On Wed, Apr 23, 2014 at 1:12 PM, Danny Yoo wrote: > Hi Sunil, > > > Try a simpler but related problem first. > > Say that you have two lists of numbers, like: > > ###### > nums1 = [3, 1, 4] > nums2 = [2, 7, 1] > ###### > > Can you design a function addLists() that takes two lists of numbers > of equal length, and adds them together? For example, > > addLists(nums1, nums2) == [5, 8, 5] > > should be true, as well as: > > addLists([4, 6], [8, 5]) == [12, 11] > > > Would you be able to write the addLists() function? Would you be able > to write a few test cases to check that the implementation works on > those examples? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Wed Apr 23 19:53:06 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 23 Apr 2014 10:53:06 -0700 Subject: [Tutor] Fwd: Puzzle - Next Step to our interviewing process - Pramati Technologies! In-Reply-To: References: <06a601cf0b86$aefe5360$0cfafa20$@pramati.com> <03d301cf0c34$e3796450$aa6c2cf0$@pramati.com> <01e801cf0cee$98e359e0$caaa0da0$@pramati.com> <011f01cf0db5$5fc59660$1f50c320$@pramati.com> <076401cf0df4$8f481180$add83480$@pramati.com> <053c01cf1032$11b7c860$35275920$@pramati.com> <067401cf1044$a88717c0$f9954740$@pramati.com> <044601cf11b1$03387930$09a96b90$@pramati.com> <05f001cf11bf$619b09d0$24d11d70$@pramati.com> <07a901cf11d7$d2927c50$77b774f0$@pramati.com> <019801cf1276$6583a180$308ae480$@pramati.com> <07bd01cf12a0$4dd621a0$e98264e0$@pramati.com> <01e201cf133b$30ce7a30$926b6e90$@pramati.com> <025801cf1596$6bc7fd20$4357f760$@pramati.com> <027d01cf1665$40045f10$c00d1d30$@pramati.com> <01ac01cf1728$22d68ad0$6883a070$@pramati.com> <019001cf18c9$305629c0$91027d40$@pramati.com> <047c01cf1b20$d946e840$8bd4b8c0$@pramati.com> <076b01cf1b3e$b0ece070$12c6a150$@pramati.com> <0bf601cf1b5e$aa067760$fe136620$@pramati.com> <085701cf1c0c$bbda1290$338e37b0$@pramati.com> <074701cf1cde$77f89150$67e9b3f0$@pramati.com> <027401cf1d76$6c6f65a0$454e30e0$@pramati.com> <088501cf1d9f$128a41d0$379ec570$@pramati.com> <09af01cf1dab$7e68e350$7b3aa9f0$@pramati.com> <01e801cf1e36$e6fea8b0$b4fbfa10$@pramati.com> <042501cf20b1$c00a1460$401e3d20$@pramati.com> <000301cf20ca$da170ca0$8e4525e0$@pramati.com> <023b01cf22fd$0a2bd9f0$1e838dd0$@pramati.com> <06ce01cf2645$48559180$d900b480$@pramati.com> <084301cf2654$f74757f0$e5d607d0$@pramati.com> <00eb01cf26db$fa213f90$ee63beb0$@pramati.com> <04ea01cf26f0$ccc4a680$664df380$@pramati.com> <076901cf2705$54e187e0$fea497a0$@pramati.com> <023c01cf27ae$0652b4b0$12f81e10$@pramati.com> <024801cf27ae$24ea4780$6ebed680$@pramati.com> <084301cf27e2$2b2d8790$818896b0$@pramati.com> <04bc01cf288d$a72b0d30$f5812790$@pramati.com> <040301cf2bb5$8d57cc10$a8076430$@pramati.com> <058c01cf2bc6$2ef3ead0$8cdbc070$@pramati.com> <04ba01cf345e$114ec9a0$33ec5ce0$@pramati.com> <051601cf3463$6f4c9410$4de5bc30$@pramati.com> <04b601cf3c24$5c48ae30$14da0a90$@pramati.com> <08c901cf3ea1$e512e030$af38a090$@pramati.com> <01dd01cf434a$e0869130$a193b390$@pramati.com> <02d001cf44c9$a27e3890$e77aa9b0$@pramati.com> <02a701cf4715$2d165f80$87431e80$@pramati.com> <04a701cf47f9$32bf7270$983e5750$@pramati.com> <033401cf48bd$ce0b2750$6a2175f0$@pramati.com> <03fb01cf48c4$66f86b20$34e94160$@pramati.com> <022101cf4978$ba8475e0$2f8d61a0$@pramati.com> <014201cf4ef1$3aeba3c0$b0c2eb40$@pramati.com> <015c01cf4ef1$b0978a80$11c69f80$@pramati.com> <01bd01cf4fbc$c256fb40$4704f1c0$@pramati.com> <036601cf52f9$aaf76cb0$00e64610$@pramati.com> <052d01cf5311$10d9c390$328d4ab0$@pramati.com> <02b301cf5478$b1d21e60$15765b20$@pramati.com> <03e101cf57a9$227caf80$67760e80$@pramati.com> Message-ID: Hi Brian, I would suggest not providing homework solutions. Look at the beginning of this thread to see why just giving homework solutions is not helpful for the questioner. From brianjamesarb at gmail.com Wed Apr 23 19:54:49 2014 From: brianjamesarb at gmail.com (brian arb) Date: Wed, 23 Apr 2014 13:54:49 -0400 Subject: [Tutor] Fwd: Puzzle - Next Step to our interviewing process - Pramati Technologies! In-Reply-To: References: <06a601cf0b86$aefe5360$0cfafa20$@pramati.com> <03d301cf0c34$e3796450$aa6c2cf0$@pramati.com> <01e801cf0cee$98e359e0$caaa0da0$@pramati.com> <011f01cf0db5$5fc59660$1f50c320$@pramati.com> <076401cf0df4$8f481180$add83480$@pramati.com> <053c01cf1032$11b7c860$35275920$@pramati.com> <067401cf1044$a88717c0$f9954740$@pramati.com> <044601cf11b1$03387930$09a96b90$@pramati.com> <05f001cf11bf$619b09d0$24d11d70$@pramati.com> <07a901cf11d7$d2927c50$77b774f0$@pramati.com> <019801cf1276$6583a180$308ae480$@pramati.com> <07bd01cf12a0$4dd621a0$e98264e0$@pramati.com> <01e201cf133b$30ce7a30$926b6e90$@pramati.com> <025801cf1596$6bc7fd20$4357f760$@pramati.com> <027d01cf1665$40045f10$c00d1d30$@pramati.com> <01ac01cf1728$22d68ad0$6883a070$@pramati.com> <019001cf18c9$305629c0$91027d40$@pramati.com> <047c01cf1b20$d946e840$8bd4b8c0$@pramati.com> <076b01cf1b3e$b0ece070$12c6a150$@pramati.com> <0bf601cf1b5e$aa067760$fe136620$@pramati.com> <085701cf1c0c$bbda1290$338e37b0$@pramati.com> <074701cf1cde$77f89150$67e9b3f0$@pramati.com> <027401cf1d76$6c6f65a0$454e30e0$@pramati.com> <088501cf1d9f$128a41d0$379ec570$@pramati.com> <09af01cf1dab$7e68e350$7b3aa9f0$@pramati.com> <01e801cf1e36$e6fea8b0$b4fbfa10$@pramati.com> <042501cf20b1$c00a1460$401e3d20$@pramati.com> <000301cf20ca$da170ca0$8e4525e0$@pramati.com> <023b01cf22fd$0a2bd9f0$1e838dd0$@pramati.com> <06ce01cf2645$48559180$d900b480$@pramati.com> <084301cf2654$f74757f0$e5d607d0$@pramati.com> <00eb01cf26db$fa213f90$ee63beb0$@pramati.com> <04ea01cf26f0$ccc4a680$664df380$@pramati.com> <076901cf2705$54e187e0$fea497a0$@pramati.com> <023c01cf27ae$0652b4b0$12f81e10$@pramati.com> <024801cf27ae$24ea4780$6ebed680$@pramati.com> <084301cf27e2$2b2d8790$818896b0$@pramati.com> <04bc01cf288d$a72b0d30$f5812790$@pramati.com> <040301cf2bb5$8d57cc10$a8076430$@pramati.com> <058c01cf2bc6$2ef3ead0$8cdbc070$@pramati.com> <04ba01cf345e$114ec9a0$33ec5ce0$@pramati.com> <051601cf3463$6f4c9410$4de5bc30$@pramati.com> <04b601cf3c24$5c48ae30$14da0a90$@pramati.com> <08c901cf3ea1$e512e030$af38a090$@pramati.com> <01dd01cf434a$e0869130$a193b390$@pramati.com> <02d001cf44c9$a27e3890$e77aa9b0$@pramati.com> <02a701cf4715$2d165f80$87431e80$@pramati.com> <04a701cf47f9$32bf7270$983e5750$@pramati.com> <033401cf48bd$ce0b2750$6a2175f0$@pramati.com> <03fb01cf48c4$66f86b20$34e94160$@pramati.com> <022101cf4978$ba8475e0$2f8d61a0$@pramati.com> <014201cf4ef1$3aeba3c0$b0c2eb40$@pramati.com> <015c01cf4ef1$b0978a80$11c69f80$@pramati.com> <01bd01cf4fbc$c256fb40$4704f1c0$@pramati.com> <036601cf52f9$aaf76cb0$00e64610$@pramati.com> <052d01cf5311$10d9c390$328d4ab0$@pramati.com> <02b301cf5478$b1d21e60$15765b20$@pramati.com> <03e101cf57a9$227caf80$67760e80$@pramati.com> Message-ID: My Bad On Wed, Apr 23, 2014 at 1:53 PM, Danny Yoo wrote: > Hi Brian, > > I would suggest not providing homework solutions. > > Look at the beginning of this thread to see why just giving homework > solutions is not helpful for the questioner. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Wed Apr 23 19:59:48 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 23 Apr 2014 10:59:48 -0700 Subject: [Tutor] Fwd: Puzzle - Next Step to our interviewing process - Pramati Technologies! In-Reply-To: References: <06a601cf0b86$aefe5360$0cfafa20$@pramati.com> <03d301cf0c34$e3796450$aa6c2cf0$@pramati.com> <01e801cf0cee$98e359e0$caaa0da0$@pramati.com> <011f01cf0db5$5fc59660$1f50c320$@pramati.com> <076401cf0df4$8f481180$add83480$@pramati.com> <053c01cf1032$11b7c860$35275920$@pramati.com> <067401cf1044$a88717c0$f9954740$@pramati.com> <044601cf11b1$03387930$09a96b90$@pramati.com> <05f001cf11bf$619b09d0$24d11d70$@pramati.com> <07a901cf11d7$d2927c50$77b774f0$@pramati.com> <019801cf1276$6583a180$308ae480$@pramati.com> <07bd01cf12a0$4dd621a0$e98264e0$@pramati.com> <01e201cf133b$30ce7a30$926b6e90$@pramati.com> <025801cf1596$6bc7fd20$4357f760$@pramati.com> <027d01cf1665$40045f10$c00d1d30$@pramati.com> <01ac01cf1728$22d68ad0$6883a070$@pramati.com> <019001cf18c9$305629c0$91027d40$@pramati.com> <047c01cf1b20$d946e840$8bd4b8c0$@pramati.com> <076b01cf1b3e$b0ece070$12c6a150$@pramati.com> <0bf601cf1b5e$aa067760$fe136620$@pramati.com> <085701cf1c0c$bbda1290$338e37b0$@pramati.com> <074701cf1cde$77f89150$67e9b3f0$@pramati.com> <027401cf1d76$6c6f65a0$454e30e0$@pramati.com> <088501cf1d9f$128a41d0$379ec570$@pramati.com> <09af01cf1dab$7e68e350$7b3aa9f0$@pramati.com> <01e801cf1e36$e6fea8b0$b4fbfa10$@pramati.com> <042501cf20b1$c00a1460$401e3d20$@pramati.com> <000301cf20ca$da170ca0$8e4525e0$@pramati.com> <023b01cf22fd$0a2bd9f0$1e838dd0$@pramati.com> <06ce01cf2645$48559180$d900b480$@pramati.com> <084301cf2654$f74757f0$e5d607d0$@pramati.com> <00eb01cf26db$fa213f90$ee63beb0$@pramati.com> <04ea01cf26f0$ccc4a680$664df380$@pramati.com> <076901cf2705$54e187e0$fea497a0$@pramati.com> <023c01cf27ae$0652b4b0$12f81e10$@pramati.com> <024801cf27ae$24ea4780$6ebed680$@pramati.com> <084301cf27e2$2b2d8790$818896b0$@pramati.com> <04bc01cf288d$a72b0d30$f5812790$@pramati.com> <040301cf2bb5$8d57cc10$a8076430$@pramati.com> <058c01cf2bc6$2ef3ead0$8cdbc070$@pramati.com> <04ba01cf345e$114ec9a0$33ec5ce0$@pramati.com> <051601cf3463$6f4c9410$4de5bc30$@pramati.com> <04b601cf3c24$5c48ae30$14da0a90$@pramati.com> <08c901cf3ea1$e512e030$af38a090$@pramati.com> <01dd01cf434a$e0869130$a193b390$@pramati.com> <02d001cf44c9$a27e3890$e77aa9b0$@pramati.com> <02a701cf4715$2d165f80$87431e80$@pramati.com> <04a701cf47f9$32bf7270$983e5750$@pramati.com> <033401cf48bd$ce0b2750$6a2175f0$@pramati.com> <03fb01cf48c4$66f86b20$34e94160$@pramati.com> <022101cf4978$ba8475e0$2f8d61a0$@pramati.com> <014201cf4ef1$3aeba3c0$b0c2eb40$@pramati.com> <015c01cf4ef1$b0978a80$11c69f80$@pramati.com> <01bd01cf4fbc$c256fb40$4704f1c0$@pramati.com> <036601cf52f9$aaf76cb0$00e64610$@pramati.com> <052d01cf5311$10d9c390$328d4ab0$@pramati.com> <02b301cf5478$b1d21e60$15765b20$@pramati.com> <03e101cf57a9$227caf80$67760e80$@pramati.com> Message-ID: Hi Brian, No problem. Just be more careful next time. In particular, look at the context. The homework question I'm posing to Sunil is fairly basic, intentionally so, but is designed so that if he solves it with basic, standard tools (fresh list construction, list iteration, list appending), he should be able to transfer that solution almost directly to his original question, because it's essentially the same problem in a different guise. On the other hand, your one-liner solution uses zip and list comprehensions. It's clever, but too clever: not only is it using advanced tools that he shouldn't touch yet, but that approach will not easily generalize to his original question without contortions. From denis.heidtmann at gmail.com Thu Apr 24 01:46:49 2014 From: denis.heidtmann at gmail.com (Denis Heidtmann) Date: Wed, 23 Apr 2014 16:46:49 -0700 Subject: [Tutor] global list Message-ID: In a coursera python course video the following code was presented: a = [4,5,6] def mutate_part(x): a[1] = x mutate_part(200) The presenter said something like "a is a global variable, so a becomes [4,200,6] after running mutate_part(200)." Indeed it does, but why does this work without specifying a as global within mutate()? My thinking was that an "undefined" error should have been raised. Help me understand. Thanks, -Denis From pscott_74 at yahoo.com Thu Apr 24 02:17:57 2014 From: pscott_74 at yahoo.com (Patti Scott) Date: Wed, 23 Apr 2014 17:17:57 -0700 (PDT) Subject: [Tutor] methods of sorting In-Reply-To: <20140423013650.GC17388@ando> References: <1398208718.54563.YahooMailNeo@web161806.mail.bf1.yahoo.com> <20140423013650.GC17388@ando> Message-ID: <1398298677.49361.YahooMailNeo@web161805.mail.bf1.yahoo.com> This makes sense.? Thanks.? No question on the specific code, I was just thinking I should show I'd done any experimenting with the methods Hi Patti, My answers below, interleaved between your questions. On Tue, Apr 22, 2014 at 04:18:38PM -0700, Patti Scott wrote: > I'm practicing with lists.? I was looking for documentation on sorting > with cmp() because it isn't immediately clear to me how comparing > items two at a time can sort the entire list.? Identify max or min > values, yes, but not sort the whole list.? So, the Sorting HOW TO > (Dalke, Hettinger)? posted on python.org goes into detail on using a > key parameter for sorted() and .sort(), and using operator module > functions.? Think about how you might sort four items 42, 23, 57, 30. There are many different ways to sort, and this is one of the least efficient, but easiest to understand. We start by putting the unsorted items on the left, and the sorted items on the right, as if we were sorting a handful of playing cards: [42, 23, 57, 30] [] Take the first item from the left, and find where it belongs on the right. Since the right is currently empty, that's easy: [23, 57, 30] [42] Now take the next item from the left, and find where it belongs on the right. How do you do that? By comparing it to each item already there. If it compares less than the item, insert it just before the item; otherwise keep going. In this case, we compare 23 < 42, which returns True, so we insert 23 to the left of 42. [57, 30] [23, 42] Now repeat with the next item. In this case, 57 < 23 returns False, so we continue. 57 < 42 also returns False, and there are no more numbers to check so we put 57 at the end: [30] [23, 42, 57] Finally we compare 30 < 23, which returns False, then 30 < 42, which returns True, so we insert 30 just to the left of 42: [] [23, 30, 42, 57] Now that we know how to sort using "less than" < as the comparison function, we can use some other comparison function that works similarly. Instead of using < we can use the built-in function cmp(a, b), which returns -1 if a < b, 0 if a == b, and +1 if a > b. Or instead of using the built-in cmp function, we can use any function that takes two arguments, the items to be compared, and returns one of -1, 0 or 1. Even though the Python list.sort() method is a lot faster and more clever than what I show above, it too allows you to provide a custom comparison function to decide which comes earlier or later when sorting. Here's an example with and without a comparison function: py> sorted(['dog', 'aardvark', 'chicken', 'horse']) ['aardvark', 'chicken', 'dog', 'horse'] py> sorted(['dog', 'aardvark', 'chicken', 'horse'], ...? lambda a, b: cmp(len(a), len(b))) ['dog', 'horse', 'chicken', 'aardvark'] In the second case, we sort by the length of the words, not the content of the word. So "dog" (three letters) compares less than "aardvark" (eight letters). A couple of other notes: - Rather than define a comparison function using def, I use lambda as ? a shortcut. lambda creates a function, but limited only to a single ? expression. So "lambda a, b: cmp(len(a), len(b))" is equivalent to: ? def function(a, b): ? ? ? return cmp(len(a), len(b)) - Notice that I use the built-in cmp function inside my comparison ? function. That's just for convenience, you don't have to do that. > How obsolete are the cmp() and the decorate-sort-undecorate methods?? > To be understood but probably not used in new code? Both are very obsolute, but for different reasons. The problem with using a comparison function is that it is very inefficient and it can really slow down sorting of large lists by a lot. It is better to use the DSU idiom rather than call a comparison function. In fact, that is so much better, that recent versions of Python make the DSU idiom built-in: that's what the "key" argument to the sort() and sorted() functions is for. When you supply a key function to sort, it internally uses the DSU idiom. You almost never need to use it yourself. Using the key function is so much better than using a comparison function that in Python 3 the comparison function was dropped altogether and using key is the only way to customize sorting. > Python Programming, Zelle;? Python 2.7.3,? PowerShell, Notepad ++ > > I tried several means of sorting for exercises, eg [lots of code shown] I'm sorry, did you have a question about the sorting code or were you just sharing it with us? If you're asking which should be preferred, I would prefer the version using the key=... argument to sort. -- Steven -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Thu Apr 24 03:00:01 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 24 Apr 2014 11:00:01 +1000 Subject: [Tutor] global list In-Reply-To: References: Message-ID: <20140424005956.GE17388@ando> On Wed, Apr 23, 2014 at 04:46:49PM -0700, Denis Heidtmann wrote: > In a coursera python course video the following code was presented: > > a = [4,5,6] > > def mutate_part(x): > a[1] = x > > mutate_part(200) > > The presenter said something like "a is a global variable, so a becomes > > [4,200,6] after running mutate_part(200)." > > Indeed it does, but why does this work without specifying a as global > within mutate()? > My thinking was that an "undefined" error should have been raised. You only need to define variables as global if you assign to them: def function(x): global a a = [1, 2, 3, x] # assignment to variable "a" The preferred name for this sort of assignment is "name binding", where you bind a value (in this case, the list [1, 2, 3, x]) to the name "a". You only need to declare variables as global when you perform a name binding on that variable. Merely retrieving the existing value of a variable doesn't count as a binding, and doesn't need to be declared: # This is okay def function(): n = len(a) # This is not needed def function(x): global len # Yes, built-in functions are variables too! global a n = len(a) and that would be too painful for words.[1] Now, let's go back to your function: def mutate_part(x): a[1] = x It looks like a name binding (an assignment) to a, but look more closely: you're not actually binding to the name "a", you are binding to the item *inside* a. After calling "a[1] = x", a remains bound to the same list as before, it is just that the list has been modified in-place. So this does not count as a name binding operation, and no global declaration is needed. The same applies to mutating dicts in place, or setting attributes. These are all examples of in-place modifications: some_dict.clear() some_list.sort() obj.attribute = 23 some_dict[key] = value del some_list[0] Only actual assignments to the bare name, including deleting the name, need a global declaration: some_dict = {} some_list = [1, 2, 3] obj = something() del some_list [1] For advanced users: built-ins aren't technically "global", they actually live in a separate namespace called "builtins" or "__builtin__" depending on the version of Python you use. But the principle is the same. -- Steven From dyoo at hashcollision.org Thu Apr 24 10:16:29 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 24 Apr 2014 01:16:29 -0700 Subject: [Tutor] Fwd: Re: Fwd: Puzzle - Next Step to our interviewing process - Pramati Technologies! In-Reply-To: References: <06a601cf0b86$aefe5360$0cfafa20$@pramati.com> <03d301cf0c34$e3796450$aa6c2cf0$@pramati.com> <01e801cf0cee$98e359e0$caaa0da0$@pramati.com> <011f01cf0db5$5fc59660$1f50c320$@pramati.com> <076401cf0df4$8f481180$add83480$@pramati.com> <053c01cf1032$11b7c860$35275920$@pramati.com> <067401cf1044$a88717c0$f9954740$@pramati.com> <044601cf11b1$03387930$09a96b90$@pramati.com> <05f001cf11bf$619b09d0$24d11d70$@pramati.com> <07a901cf11d7$d2927c50$77b774f0$@pramati.com> <019801cf1276$6583a180$308ae480$@pramati.com> <07bd01cf12a0$4dd621a0$e98264e0$@pramati.com> <01e201cf133b$30ce7a30$926b6e90$@pramati.com> <025801cf1596$6bc7fd20$4357f760$@pramati.com> <027d01cf1665$40045f10$c00d1d30$@pramati.com> <01ac01cf1728$22d68ad0$6883a070$@pramati.com> <019001cf18c9$305629c0$91027d40$@pramati.com> <047c01cf1b20$d946e840$8bd4b8c0$@pramati.com> <076b01cf1b3e$b0ece070$12c6a150$@pramati.com> <0bf601cf1b5e$aa067760$fe136620$@pramati.com> <085701cf1c0c$bbda1290$338e37b0$@pramati.com> <074701cf1cde$77f89150$67e9b3f0$@pramati.com> <027401cf1d76$6c6f65a0$454e30e0$@pramati.com> <088501cf1d9f$128a41d0$379ec570$@pramati.com> <09af01cf1dab$7e68e350$7b3aa9f0$@pramati.com> <01e801cf1e36$e6fea8b0$b4fbfa10$@pramati.com> <042501cf20b1$c00a1460$401e3d20$@pramati.com> <000301cf20ca$da170ca0$8e4525e0$@pramati.com> <023b01cf22fd$0a2bd9f0$1e838dd0$@pramati.com> <06ce01cf2645$48559180$d900b480$@pramati.com> <084301cf2654$f74757f0$e5d607d0$@pramati.com> <00eb01cf26db$fa213f90$ee63beb0$@pramati.com> <04ea01cf26f0$ccc4a680$664df380$@pramati.com> <076901cf2705$54e187e0$fea497a0$@pramati.com> <023c01cf27ae$0652b4b0$12f81e10$@pramati.com> <024801cf27ae$24ea4780$6ebed680$@pramati.com> <084301cf27e2$2b2d8790$818896b0$@pramati.com> <04bc01cf288d$a72b0d30$f5812790$@pramati.com> <040301cf2bb5$8d57cc10$a8076430$@pramati.com> <058c01cf2bc6$2ef3ead0$8cdbc070$@pramati.com> <04ba01cf345e$114ec9a0$33ec5ce0$@pramati.com> <051601cf3463$6f4c9410$4de5bc30$@pramati.com> <04b601cf3c24$5c48ae30$14da0a90$@pramati.com> <08c901cf3ea1$e512e030$af38a090$@pramati.com> <01dd01cf434a$e0869130$a193b390$@pramati.com> <02d001cf44c9$a27e3890$e77aa9b0$@pramati.com> <02a701cf4715$2d165f80$87431e80$@pramati.com> <04a701cf47f9$32bf7270$983e5750$@pramati.com> <033401cf48bd$ce0b2750$6a2175f0$@pramati.com> <03fb01cf48c4$66f86b20$34e94160$@pramati.com> <022101cf4978$ba8475e0$2f8d61a0$@pramati.com> <014201cf4ef1$3aeba3c0$b0c2eb40$@pramati.com> <015c01cf4ef1$b0978a80$11c69f80$@pramati.com> <01bd01cf4fbc$c256fb40$4704f1c0$@pramati.com> <036601cf52f9$aaf76cb0$00e64610$@pramati.com> <052d01cf5311$10d9c390$328d4ab0$@pramati.com> <02b301cf5478$b1d21e60$15765b20$@pramati.com> <03e101cf57a9$227caf80$67760e80$@pramati.com> Message-ID: ---------- Forwarded message ---------- From: "Danny Yoo" Date: Apr 24, 2014 1:14 AM Subject: Re: [Tutor] Fwd: Puzzle - Next Step to our interviewing process - Pramati Technologies! To: "Sunil Tech" Cc: On Apr 24, 2014 12:50 AM, "Sunil Tech" wrote: > > Hi danny, > > i want to delete this email chain. > > can you please help in this? > No, unfortunately not. I am not an administrative of this mailing list anymore. Even if I had the power to change one of the public archives, it is not feasible to try to wipe a public record from all archives, as most of the archives are not under central control. -------------- next part -------------- An HTML attachment was scrubbed... URL: From fomcl at yahoo.com Thu Apr 24 10:14:33 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 24 Apr 2014 01:14:33 -0700 (PDT) Subject: [Tutor] global list In-Reply-To: <20140424005956.GE17388@ando> References: <20140424005956.GE17388@ando> Message-ID: <1398327273.10772.YahooMailNeo@web163804.mail.gq1.yahoo.com> ----- Original Message ----- > From: Steven D'Aprano > To: tutor at python.org > Cc: > Sent: Thursday, April 24, 2014 3:00 AM > Subject: Re: [Tutor] global list > > You only need to define variables as global if you assign to them: > > def function(x): > ? ? global a > ? ? a = [1, 2, 3, x]? # assignment to variable "a" ah, thanks, I always wondered about that. But doesn't it make the function (slightly) faster if you use 'global' when you only refer to that global variable? You tell the interpreter that it is not needed to search for that variable locally, so no time wasted on that. The code below indicates that it makes NO difference (well, a whopping 2ns), but maybe for larger functions it does? albertjan at debian:~$ ipython Python 2.7.3 (default, Mar 13 2014, 11:03:55) In [1]: a = True In [2]: def function(): ?? ...:???? x = True if a else False ?? ...:???? In [3]: %timeit function() 10000000 loops, best of 3: 122 ns per loop In [4]: def function(): ?? ...:???? global a ?? ...:???? x = True if a else False ?? ...:???? In [5]: %timeit function() 10000000 loops, best of 3: 120 ns per loop From dyoo at hashcollision.org Thu Apr 24 10:23:09 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 24 Apr 2014 01:23:09 -0700 Subject: [Tutor] global list In-Reply-To: <1398327273.10772.YahooMailNeo@web163804.mail.gq1.yahoo.com> References: <20140424005956.GE17388@ando> <1398327273.10772.YahooMailNeo@web163804.mail.gq1.yahoo.com> Message-ID: Should have no runtime performance difference. For similar reasons, using a very long variable name does not change a program's runtime performance. -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Thu Apr 24 10:28:30 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 24 Apr 2014 09:28:30 +0100 Subject: [Tutor] global list In-Reply-To: References: <20140424005956.GE17388@ando> <1398327273.10772.YahooMailNeo@web163804.mail.gq1.yahoo.com> Message-ID: On 24/04/2014 09:23, Danny Yoo wrote: > Should have no runtime performance difference. > > For similar reasons, using a very long variable name does not change a > program's runtime performance. > Please quote some context, this is meaningless without it, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From gerardojuarez at buyteknet.info Thu Apr 24 02:26:02 2014 From: gerardojuarez at buyteknet.info (=?ISO-8859-1?Q?Gerardo_Ju=E1rez?=) Date: Wed, 23 Apr 2014 19:26:02 -0500 Subject: [Tutor] global list In-Reply-To: References: Message-ID: <53585A1A.9020606@buyteknet.info> On 04/23/2014 06:46 PM, Denis Heidtmann wrote: > In a coursera python course video the following code was presented: > > a = [4,5,6] > > def mutate_part(x): > a[1] = x > > mutate_part(200) > > The presenter said something like "a is a global variable, so a becomes > > [4,200,6] after running mutate_part(200)." > > Indeed it does, but why does this work without specifying a as global > within mutate()? > My thinking was that an "undefined" error should have been raised. > > Help me understand. Thanks, > > -Denis > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Hi, Try out this modified version of the example you provide: def mutate(x): global y y = x + 1 a[1] = x a = [1, 2, 3] y = 0 mutate(200) print a print y Both, 'a' and 'y' will be modified, but if you comment out global, 'y' will not change. In a few words, assigning a[k] = value is technically a method call, and it behaves differently (spans across scopes). A more detailed answer can be found here: http://stackoverflow.com/questions/4630543/defining-lists-as-global-variable-in-python Of course, I would limit this practice to *very* small programs, since anything beyond 50 lines should have more structure and you can use either modular programming or OOP, which is the preferred practice today to structure complex applications. Gerardo From j.m.rice at talktalk.net Thu Apr 24 01:14:35 2014 From: j.m.rice at talktalk.net (Martin) Date: Thu, 24 Apr 2014 00:14:35 +0100 Subject: [Tutor] some things work in IDLE but not a command prompt and vice versa Message-ID: <5358495B.6070407@talktalk.net> Experimenting with pickling... import pickle file1 = open('first.txt','r') contents = file1.read() file1.close() print(contents) file2 = open('pickle.dat','wb') pickle.dump(contents,file2,True) file2.close() contents = '' file3 = open('pickle.dat','rb') contents = pickle.load(file3) print(contents) input('\nPress Enter to finish') This works as expected when run under the IDLE. first.txt is just a small text file. If I run from a command prompt, however, I get C:\Users\Martin\Documents\College\python>python pickle.py Hello! How are you? 123 Traceback (most recent call last): File "pickle.py", line 1, in import pickle File "C:\Users\Martin\Documents\College\python\pickle.py", line 11, in pickle.dump(contents,file2,True) AttributeError: 'module' object has no attribute 'dump' C:\Users\Martin\Documents\College\python> Get similar problem on College computers as well as at home. Python 3.3.3 Windows 7 Professional SP1 I get the opposite problem with themsvcrt.getch() function. It works OK when run from a command prompt, but under IDLE it returns immediately without waiting for a key-press, with value b'\xff'. Is this just a feature of the IDLE? Martin --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Apr 24 11:41:23 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 24 Apr 2014 10:41:23 +0100 Subject: [Tutor] some things work in IDLE but not a command prompt and vice versa In-Reply-To: <5358495B.6070407@talktalk.net> References: <5358495B.6070407@talktalk.net> Message-ID: On 24/04/14 00:14, Martin wrote: > If I run from a command prompt, however, I get > > C:\Users\Martin\Documents\College\python>python pickle.py You have called your file pickle.py. So when you try to import pickle the interpreter sees your file first and imports that not the library pickle. Never name your files the same as modules in the standard library, or at least not the same as something you import. > pickle.dump(contents,file2,True) > AttributeError: 'module' object has no attribute 'dump' > I get the opposite problem with themsvcrt.getch() function. It works OK > when run from a command prompt, but under IDLE it returns immediately > without waiting for a key-press, with value b'\xff'. Is this just a > feature of the IDLE? I'd never noticed that particular quirk before but you are correct. I get the same behaviour on win8.1 with Python 3.3. I also get the same behaviour using Pythonwin (which is generally a much better IDE if you are on Windows BTW!) I'm not sure why they behave differently to the command line but most IDEs have slight oddities like this. If in doubt use the command line version as your reference. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Thu Apr 24 13:49:39 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 24 Apr 2014 13:49:39 +0200 Subject: [Tutor] global list References: <20140424005956.GE17388@ando> <1398327273.10772.YahooMailNeo@web163804.mail.gq1.yahoo.com> Message-ID: Albert-Jan Roskam wrote: > > > > > > ----- Original Message ----- >> From: Steven D'Aprano >> To: tutor at python.org >> Cc: >> Sent: Thursday, April 24, 2014 3:00 AM >> Subject: Re: [Tutor] global list >> > > > >> You only need to define variables as global if you assign to them: >> >> def function(x): >> global a >> a = [1, 2, 3, x] # assignment to variable "a" > > ah, thanks, I always wondered about that. But doesn't it make the function > (slightly) faster if you use 'global' when you only refer to that global > variable? You tell the interpreter that it is not needed to search for > that variable locally, so no time wasted on that. The code below indicates > that it makes NO difference (well, a whopping 2ns), but maybe for larger > functions it does? > > albertjan at debian:~$ ipython > Python 2.7.3 (default, Mar 13 2014, 11:03:55) > > In [1]: a = True > > In [2]: def function(): > ...: x = True if a else False > ...: > > In [3]: %timeit function() > 10000000 loops, best of 3: 122 ns per loop > > In [4]: def function(): > ...: global a > ...: x = True if a else False > ...: > > In [5]: %timeit function() > > 10000000 loops, best of 3: 120 ns per loop For functions whether a is global or not is determined at compile-time. Have a look at the byte code for your functions: >>> def f(): ... x = True if a else False ... >>> def g(): ... global a ... x = True if a else False ... >>> dis.dis(f) 2 0 LOAD_GLOBAL 0 (a) 3 POP_JUMP_IF_FALSE 12 6 LOAD_CONST 1 (True) 9 JUMP_FORWARD 3 (to 15) >> 12 LOAD_CONST 2 (False) >> 15 STORE_FAST 0 (x) 18 LOAD_CONST 0 (None) 21 RETURN_VALUE >>> dis.dis(g) 3 0 LOAD_GLOBAL 0 (a) 3 POP_JUMP_IF_FALSE 12 6 LOAD_CONST 1 (True) 9 JUMP_FORWARD 3 (to 15) >> 12 LOAD_CONST 2 (False) >> 15 STORE_FAST 0 (x) 18 LOAD_CONST 0 (None) 21 RETURN_VALUE It is identical. Both functions "know" that a is a global name. A name can refer to a global or a local name, not both. One consequence is this error: >>> a = 42 >>> def h(): ... print(a) ... a = "foo" ... >>> h() Traceback (most recent call last): File "", line 1, in File "", line 2, in h UnboundLocalError: local variable 'a' referenced before assignment For class bodies there is an exception to allow for assignments of a global to a local variable, e. g.: >>> a = 42 >>> class A: ... print(a) ... a = "foo" ... 42 >>> A.a 'foo' >>> a 42 From steve at pearwood.info Thu Apr 24 14:34:43 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 24 Apr 2014 22:34:43 +1000 Subject: [Tutor] Groups of mutually exclusive options In-Reply-To: References: <8f1c5af55b9058b847e019fa63f61262@sonic.net> <20140422023544.GQ28400@ando> Message-ID: <20140424123442.GA4273@ando> On Tue, Apr 22, 2014 at 10:47:09AM -0700, Alex Kleider wrote: > On 2014-04-21 19:35, Steven D'Aprano wrote: > > >Does docopt solve the Original Poster's question? If not, that advice > >is not terribly helpful. > > I don't pretend to fully understand the Original Poster's requirement > but I believe mutual exclusivity is supported. Here's a short excerpt. > """ > Example uses brackets "[ ]", parens "( )", pipes "|" and ellipsis "..." > to describe optional, required, mutually exclusive, and repeating > elements. > """ Ah, good to know! Thanks for the update. -- Steven From alan.gauld at btinternet.com Thu Apr 24 17:42:40 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 24 Apr 2014 16:42:40 +0100 Subject: [Tutor] Where does logging put its files? Message-ID: I've been playing with the logging module - long overdue! I started with the basic tutorial but fell at the first hurdle. It says to specify a file in the logging.basicConfig() function then asks you to open the file after logging some events. But I can't find the file anywhere... Here's my code: >>> import logging >>> logging.basicConfig(file='./log.txt', level=logging.DEBUG) >>> logging.info('some stuff') INFO:root:some stuff >>> logging.error('Somethings burning') ERROR:root:Somethings burning >>> logging.critical('I warned you!') CRITICAL:root:I warned you! >>> But if I exit Python there is no file called log.txt that I can find either in the current folder or in my home folder. The tutorial seems to suggest it should be obvious... Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Thu Apr 24 18:08:45 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 24 Apr 2014 18:08:45 +0200 Subject: [Tutor] Where does logging put its files? References: Message-ID: Alan Gauld wrote: > I've been playing with the logging module - long overdue! > > I started with the basic tutorial but fell at the first hurdle. > It says to specify a file in the logging.basicConfig() function then > asks you to open the file after logging some events. > > But I can't find the file anywhere... > > Here's my code: > > >>> import logging > >>> logging.basicConfig(file='./log.txt', level=logging.DEBUG) > >>> logging.info('some stuff') > INFO:root:some stuff > >>> logging.error('Somethings burning') > ERROR:root:Somethings burning > >>> logging.critical('I warned you!') > CRITICAL:root:I warned you! > >>> > > But if I exit Python there is no file called log.txt > that I can find either in the current folder or in my > home folder. The tutorial seems to suggest it > should be obvious... The printed logging messages might give a hint that you were not successful in your attempt to specify a log-file. Try "filename" instead of "file": $ ls log.txt $ python3 Python 3.3.2+ (default, Feb 28 2014, 00:52:16) [GCC 4.8.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import logging >>> logging.basicConfig(filename="./log.txt", level=logging.DEBUG) >>> logging.info("There you are") >>> $ cat log.txt INFO:root:There you are From alan.gauld at btinternet.com Thu Apr 24 18:30:10 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 24 Apr 2014 17:30:10 +0100 Subject: [Tutor] Where does logging put its files? In-Reply-To: References: Message-ID: On 24/04/14 17:08, Peter Otten wrote: >> >>> logging.basicConfig(file='./log.txt', level=logging.DEBUG) >> >>> logging.info('some stuff') >> INFO:root:some stuff > > The printed logging messages might give a hint that you were not successful > in your attempt to specify a log-file. Try "filename" instead of "file": Doh! That's embarrassing. I must get these spectacles cleaned :-) Thanks. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From fomcl at yahoo.com Thu Apr 24 22:30:18 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 24 Apr 2014 13:30:18 -0700 (PDT) Subject: [Tutor] global list In-Reply-To: References: <20140424005956.GE17388@ando> <1398327273.10772.YahooMailNeo@web163804.mail.gq1.yahoo.com> Message-ID: <1398371418.67018.YahooMailNeo@web163806.mail.gq1.yahoo.com> ________________________________ > From: Peter Otten <__peter__ at web.de> >To: tutor at python.org >Sent: Thursday, April 24, 2014 1:49 PM >Subject: Re: [Tutor] global list > > >Albert-Jan Roskam wrote: >> ----- Original Message ----- >>> From: Steven D'Aprano >>> To: tutor at python.org >>> Cc: >>> Sent: Thursday, April 24, 2014 3:00 AM >>> Subject: Re: [Tutor] global list >>> >> >> >> >>> You only need to define variables as global if you assign to them: >>> >>> def function(x): >>> global a >>> a = [1, 2, 3, x]? # assignment to variable "a" >> >> ah, thanks, I always wondered about that. But doesn't it make the function >> (slightly) faster if you use 'global' when you only refer to that global >> variable? You tell the interpreter that it is not needed to search for >> that variable locally, so no time wasted on that. The code below indicates >> that it makes NO difference (well, a whopping 2ns), but maybe for larger >> functions it does? >> >> albertjan at debian:~$ ipython >> Python 2.7.3 (default, Mar 13 2014, 11:03:55) >> >> In [1]: a = True >> >> In [2]: def function(): >> ...:? ???x = True if a else False >> ...: >> >> In [3]: %timeit function() >> 10000000 loops, best of 3: 122 ns per loop >> >> In [4]: def function(): >> ...:? ???global a >> ...:? ???x = True if a else False >> ...: >> >> In [5]: %timeit function() >> >> 10000000 loops, best of 3: 120 ns per loop > >For functions whether a is global or not is determined at compile-time. Have >a look at the byte code for your functions: > >>>> def f(): >...? ???x = True if a else False >... >>>> def g(): >...? ???global a >...? ???x = True if a else False >... >>>> dis.dis(f) >? 2? ? ? ? ???0 LOAD_GLOBAL? ? ? ? ? ? ? 0 (a) >? ? ? ? ? ? ? 3 POP_JUMP_IF_FALSE? ? ???12 >? ? ? ? ? ? ? 6 LOAD_CONST? ? ? ? ? ? ???1 (True) >? ? ? ? ? ? ? 9 JUMP_FORWARD? ? ? ? ? ???3 (to 15) >? ? ? ? >>???12 LOAD_CONST? ? ? ? ? ? ???2 (False) >? ? ? ? >>???15 STORE_FAST? ? ? ? ? ? ???0 (x) >? ? ? ? ? ???18 LOAD_CONST? ? ? ? ? ? ???0 (None) >? ? ? ? ? ???21 RETURN_VALUE? ? ? ? >>>> dis.dis(g) >? 3? ? ? ? ???0 LOAD_GLOBAL? ? ? ? ? ? ? 0 (a) >? ? ? ? ? ? ? 3 POP_JUMP_IF_FALSE? ? ???12 >? ? ? ? ? ? ? 6 LOAD_CONST? ? ? ? ? ? ???1 (True) >? ? ? ? ? ? ? 9 JUMP_FORWARD? ? ? ? ? ???3 (to 15) >? ? ? ? >>???12 LOAD_CONST? ? ? ? ? ? ???2 (False) >? ? ? ? >>???15 STORE_FAST? ? ? ? ? ? ???0 (x) >? ? ? ? ? ???18 LOAD_CONST? ? ? ? ? ? ???0 (None) >? ? ? ? ? ???21 RETURN_VALUE? ? ? ? > >It is identical. Both functions "know" that a is a global name. >A name can refer to a global or a local name, not both. One consequence is >this error: Thanks! 'dis' is very useful. I don't use it often enough. Another reason why I am sometimes inclined to use 'global': to explicitly mention that I am doing the one thing that all textbooks warn about, using an Evil Global Variable. But that's probably silly. As a side not, I find that variables (attributes) defined in __init__ are also much like globals, but that's probably a different discussion. From breamoreboy at yahoo.co.uk Thu Apr 24 22:48:35 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 24 Apr 2014 21:48:35 +0100 Subject: [Tutor] attributes vs globals (was Re: global list) In-Reply-To: <1398371418.67018.YahooMailNeo@web163806.mail.gq1.yahoo.com> References: <20140424005956.GE17388@ando> <1398327273.10772.YahooMailNeo@web163804.mail.gq1.yahoo.com> <1398371418.67018.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: On 24/04/2014 21:30, Albert-Jan Roskam wrote: > > As a side not, I find that variables (attributes) defined in __init__ are also much like globals, but that's probably a different discussion. > Would you please be kind enough to explain your logic. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From alan.gauld at btinternet.com Fri Apr 25 00:15:16 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 24 Apr 2014 23:15:16 +0100 Subject: [Tutor] attributes vs globals (was Re: global list) In-Reply-To: References: <20140424005956.GE17388@ando> <1398327273.10772.YahooMailNeo@web163804.mail.gq1.yahoo.com> <1398371418.67018.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: On 24/04/14 21:48, Mark Lawrence wrote: > On 24/04/2014 21:30, Albert-Jan Roskam wrote: >> >> As a side not, I find that variables (attributes) defined in __init__ >> are also much like globals, but that's probably a different discussion. > > Would you please be kind enough to explain your logic. I can't speak for Albert but I do tend to agree that class (or instance) variables are a lot like globals. They are slightly better controlled because they are unique to an instance but they do result in code(methods) that are dependant on side effects. You are changing data that is neither passed into the method or returned by it. So its not obvious which instance attributes are being modified by which methods. Now OOP theory says that with information hiding you shouldn't care, so long as the external behaviour is the correct, but in Python its common to directly access attributes. And if you inherit a class you often do care about its internal state. So instance variables carry many of the same negative characteristics that globals do, albeit confined to a single entity and a single, constrained, set of functions.. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Fri Apr 25 06:46:33 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 25 Apr 2014 14:46:33 +1000 Subject: [Tutor] global list In-Reply-To: <1398327273.10772.YahooMailNeo@web163804.mail.gq1.yahoo.com> References: <20140424005956.GE17388@ando> <1398327273.10772.YahooMailNeo@web163804.mail.gq1.yahoo.com> Message-ID: <20140425044631.GC4273@ando> On Thu, Apr 24, 2014 at 01:14:33AM -0700, Albert-Jan Roskam wrote: > ----- Original Message ----- > > From: Steven D'Aprano > > To: tutor at python.org > > Cc: > > Sent: Thursday, April 24, 2014 3:00 AM > > Subject: Re: [Tutor] global list > > > > > > > You only need to define variables as global if you assign to them: > > > > def function(x): > > ? ? global a > > ? ? a = [1, 2, 3, x]? # assignment to variable "a" > > ah, thanks, I always wondered about that. But doesn't it make the > function (slightly) faster if you use 'global' when you only refer to > that global variable? Theoretically, but not in practice. In Python 2.7, for example, you get exactly the same byte-code whether you declare it global or not: py> from dis import dis py> def test(): ... global a ... return a + b ... py> dis(test) 3 0 LOAD_GLOBAL 0 (a) 3 LOAD_GLOBAL 1 (b) 6 BINARY_ADD 7 RETURN_VALUE CPython, at least (I'm not sure about Jython and IronPython) doesn't bother checking locals for variables it already knows must be global, so there's no slowdown there. In principle an optimizing Python might be able to recognise when you're referring to a built-in, and avoid needlessly checking for a global of the same name, but that's actually quite a hard thing to get right. > You tell the interpreter that it is not needed > to search for that variable locally, so no time wasted on that. It's actually the other way around: you tell Python that a variable is local by assigning to it, in which case it is *only* looked for in the locals. Otherwise locals are always skipped. (The handling of locals() in Python 2 is a bit tricky, and there are differences between CPython, Jython and IronPython when you use exec or import * inside a function. Python 3 simplifies the odd corner cases by disallowing those troublesome cases.) > The > code below indicates that it makes NO difference (well, a whopping > 2ns), but maybe for larger functions it does? I wouldn't expect that 2ns is meaningful. I would treat it as mere noise in the measurement. -- Steven From ch2009 at arcor.de Thu Apr 24 21:09:39 2014 From: ch2009 at arcor.de (Chris) Date: Thu, 24 Apr 2014 21:09:39 +0200 Subject: [Tutor] Remove last newline only in print / open / read function Message-ID: <53596173.5020601@arcor.de> Hi, I'm a Python Newbie. Probably, this is a simple question. I've a function that offers a ZIP-file for download: def download_cert(form, config): cn = form['cn'].value serial = pki.util.serial_from_cn(config, cn) files = pki.util.cert_files(config, cn, serial, True) try: name = path.join(tempfile.mkdtemp(), cn + '.zip') zip = zipfile.ZipFile(name, 'w') for file in files: zip.write(file, path.basename(file)) zip.close() (1) except: pki.cgi.start_html('Internal error', True) pki.cgi.show_error(str(sys.exc_info()[1])) pki.cgi.show_link('Go Back') pki.cgi.end_html() else: print 'Content-Type: application/zip' print 'Content-Disposition: attachment;filename=' + cn + '.zip' print print open(name).read() (2) os.remove(name) os.rmdir(path.dirname(name)) The ZIP file on the server is okay (1), but when it's sent to the client (2), there's a linefeed in the last line. This makes the zip invalid. So how can I remove the last linefeed only? -- Chris From j.m.rice at talktalk.net Thu Apr 24 20:08:55 2014 From: j.m.rice at talktalk.net (Martin) Date: Thu, 24 Apr 2014 19:08:55 +0100 Subject: [Tutor] some things work in IDLE but not a command prompt and vice versa In-Reply-To: References: <5358495B.6070407@talktalk.net> Message-ID: <53595337.6000407@talktalk.net> On 24/04/2014 10:41, Alan Gauld wrote: > On 24/04/14 00:14, Martin wrote: > >> If I run from a command prompt, however, I get >> >> C:\Users\Martin\Documents\College\python>python pickle.py > > You have called your file pickle.py. > So when you try to import pickle the interpreter sees your file first > and imports that not the library pickle. > > Never name your files the same as modules in the standard > library, or at least not the same as something you import. > >> pickle.dump(contents,file2,True) >> AttributeError: 'module' object has no attribute 'dump' > > >> I get the opposite problem with themsvcrt.getch() function. It works OK >> when run from a command prompt, but under IDLE it returns immediately >> without waiting for a key-press, with value b'\xff'. Is this just a >> feature of the IDLE? > > I'd never noticed that particular quirk before but you are correct. I > get the same behaviour on win8.1 with Python 3.3. I also get the same > behaviour using Pythonwin (which is generally a much better IDE if you > are on Windows BTW!) > > I'm not sure why they behave differently to the command line but most > IDEs have slight oddities like this. If in doubt use the command line > version as your reference. > > HTH Many thanks for the advice about file names. I was having great fun (or Python was having fun with me) trying to run a very simple program with the file name dictionary.py but sanity returned when I renamed it dictionery.py --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From alan.gauld at btinternet.com Fri Apr 25 10:36:46 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 25 Apr 2014 09:36:46 +0100 Subject: [Tutor] Remove last newline only in print / open / read function In-Reply-To: <53596173.5020601@arcor.de> References: <53596173.5020601@arcor.de> Message-ID: On 24/04/14 20:09, Chris wrote: > for file in files: > zip.write(file, path.basename(file)) > zip.close() (1) > print open(name).read() (2) > > The ZIP file on the server is okay (1), but when it's sent to the client > (2), there's a linefeed in the last line. This makes the zip invalid. So > how can I remove the last linefeed only? Can I first ask what makes you think there is an extra linefeed at the end? Is it because of the print output? You do remember that print adds a newline? If you do print open(name).read(), # trailing comma suppresses newline Does it look OK? Or were already taking that into account? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From davea at davea.name Fri Apr 25 12:14:31 2014 From: davea at davea.name (Dave Angel) Date: Fri, 25 Apr 2014 06:14:31 -0400 (EDT) Subject: [Tutor] Remove last newline only in print / open / read function References: <53596173.5020601@arcor.de> Message-ID: Chris Wrote in message: > Hi, > > I'm a Python Newbie. Probably, this is a simple question. But what is your context? Your Python version (apparently 2.x, since you're using print as a statement, rather than a function)? What os, and what is stdout pointing to, since you're pretending that you can write binary data to it? > > I've a function that offers a ZIP-file for download: > > def download_cert(form, config): > cn = form['cn'].value > serial = pki.util.serial_from_cn(config, cn) > files = pki.util.cert_files(config, cn, serial, True) > try: > name = path.join(tempfile.mkdtemp(), cn + '.zip') > zip = zipfile.ZipFile(name, 'w') > for file in files: > zip.write(file, path.basename(file)) > zip.close() (1) > except: > pki.cgi.start_html('Internal error', True) > pki.cgi.show_error(str(sys.exc_info()[1])) > pki.cgi.show_link('Go Back') > pki.cgi.end_html() > else: > print 'Content-Type: application/zip' > print 'Content-Disposition: attachment;filename=' + cn + '.zip' > print > print open(name).read() (2) print adds a newline. Use write () instead. sys.stdout.write (open (name).read ()) This assumes stdout is opened in binary mode, so that bytes that happen to look like a newline are not corrupted. > os.remove(name) > os.rmdir(path.dirname(name)) > > The ZIP file on the server is okay (1), but when it's sent to the client > (2), there's a linefeed in the last line. This makes the zip invalid. So > how can I remove the last linefeed only? > Minimum change is to add a comma at the end of the print statement. But I'd prefer write method, to be parallel with the read (). Do you know that memory is not a problem, since you're reading the entire temp file in before writing any of it to stdout? If you're not sure, replace this with a loop. -- DaveA From matbioinfo at gmail.com Fri Apr 25 13:13:26 2014 From: matbioinfo at gmail.com (rahmad akbar) Date: Fri, 25 Apr 2014 13:13:26 +0200 Subject: [Tutor] trying to understand pattern matching code Message-ID: hey guys, i am trying to understand this code pasted bellow, 1. what is line means? mask[c] |= bit 2. then the line bit *=2, this increment the bit to 2 for each character? 3. what is this line means? accept_state = bit //2 4. lastly, what is this scary line means? D = (( D << 1) + 1) & masks [ c def ShiftAnd (P , T ): m = len ( P ) masks = dict () # empty dictionary bit = 1 for c in P : if c not in masks : masks [ c ] = 0 masks [ c ] |= bit bit *= 2 accept_state = bit // 2 D = 0 # bit - mask of active states i = 0 for c in T : D = (( D << 1) + 1) & masks [ c ] if ( D & accept_state ) != 0: yield i i += 1 -- many thanks mat -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Fri Apr 25 14:16:57 2014 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 25 Apr 2014 08:16:57 -0400 Subject: [Tutor] trying to understand pattern matching code In-Reply-To: References: Message-ID: On Apr 25, 2014 7:14 AM, "rahmad akbar" wrote: > > hey guys, > i am trying to understand this code pasted bellow, > 1. what is line means? mask[c] |= bit This bitwise or. It sets the rightmost bit in masks[c] > 2. then the line bit *=2, this increment the bit to 2 for each characters This doubles bit. > 3. what is this line means? accept_state = bit //2 Integer division > 4. lastly, what is this scary line means? D = (( D << 1) + 1) & masks [ c The << is bitwise shift left > def ShiftAnd (P , T ): > m = len ( P ) > masks = dict () # empty dictionary > bit = 1 > for c in P : > if c not in masks : masks [ c ] = 0 > masks [ c ] |= bit > bit *= 2 > accept_state = bit // 2 > D = 0 # bit - mask of active states > i = 0 > for c in T : > D = (( D << 1) + 1) & masks [ c ] > if ( D & accept_state ) != 0: > yield i > i += 1 > > You should sprinkle some print statements in and run with various arguments > > > > -- > many thanks > mat > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Fri Apr 25 14:51:05 2014 From: davea at davea.name (Dave Angel) Date: Fri, 25 Apr 2014 08:51:05 -0400 (EDT) Subject: [Tutor] trying to understand pattern matching code References: Message-ID: You both posted in html, rather than text mode. Joel Goldstick Wrote in message: > > > > 1. what is line means? mask[c] |= bit > This bitwise or. It sets the rightmost bit in masks[c] Only the first time through the loop, when bit == 1 -- DaveA From matbioinfo at gmail.com Fri Apr 25 15:33:54 2014 From: matbioinfo at gmail.com (rahmad akbar) Date: Fri, 25 Apr 2014 15:33:54 +0200 Subject: [Tutor] trying to understand pattern matching code In-Reply-To: References: Message-ID: Thanks joel Could you elaborate more on bitwise, and why do we need to double bit. I experimented with some input ShiftAnd('pattern', 'textcontainingpatternandpatern') print out the (masks, accept_state), looks like bellow, the rest make sense couse the numbers are doubled, but the 't' goes 4+8 = 12. why is this? ({'a': 2, 'e': 16, 'n': 64, 'p': 1, 'r': 32, 't': 12}, 64) and i could not make sense of this line yet : D = (( D << 1) + 1) & masks [ c ] to Dave, i do i do the text mode? i had no idea this was on html Am 25.04.2014 14:16 schrieb "Joel Goldstick" : > > On Apr 25, 2014 7:14 AM, "rahmad akbar" wrote: > > > > hey guys, > > i am trying to understand this code pasted bellow, > > 1. what is line means? mask[c] |= bit > This bitwise or. It sets the rightmost bit in masks[c] > > 2. then the line bit *=2, this increment the bit to 2 for each characters > This doubles bit. > > 3. what is this line means? accept_state = bit //2 > Integer division > > 4. lastly, what is this scary line means? D = (( D << 1) + 1) & masks [ > c > > The << is bitwise shift left > > def ShiftAnd (P , T ): > > m = len ( P ) > > masks = dict () # empty dictionary > > bit = 1 > > for c in P : > > if c not in masks : masks [ c ] = 0 > > masks [ c ] |= bit > > bit *= 2 > > accept_state = bit // 2 > > D = 0 # bit - mask of active states > > i = 0 > > for c in T : > > D = (( D << 1) + 1) & masks [ c ] > > if ( D & accept_state ) != 0: > > yield i > > i += 1 > > > > > You should sprinkle some print statements in and run with various arguments > > > > > > > > -- > > many thanks > > mat > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From waterfallroad at gmail.com Fri Apr 25 13:00:16 2014 From: waterfallroad at gmail.com (Leo Nardo) Date: Fri, 25 Apr 2014 07:00:16 -0400 Subject: [Tutor] Learning Message-ID: I want to program. I enjoy programming and am willing to put in time to do this.When I try to get started with programming I realize how difficult it is to find the information worthy of learning. Programming is broad and the possibilities are endless, as well as the ammount of learning material. It is hard to find quality material that is understandable. What i think would be helpful is an outline of all tactics and stuff I need to learn to complete a certain task that i am interested in. This task is to write a very basic botting program for a pc game. Tons of people bot on this game and i would rather make my own program as opposed to using theirs. The game is called Tibia(Tibia.com). The program i want to write would start out being one simple feature. I want my program to look at my game and see when my health is below a certain ammount and heal my character by typing in a certain spell in the console of the game. After i can write this program from start to finish i believe i can work my way out to include other features and increase my programming skills. Can anyone provide such a list of start to finish knowledge that i could teach myself that would make this possible? I would like to write the program in python, and I do not need a graphical interface. There is something called a tibia api and I am not sure if its relevant. Tibia is written in c++. Thanks :) From alan.gauld at btinternet.com Fri Apr 25 18:59:11 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 25 Apr 2014 17:59:11 +0100 Subject: [Tutor] Learning In-Reply-To: References: Message-ID: On 25/04/14 12:00, Leo Nardo wrote: > What i think would be helpful is an outline of all > tactics and stuff I need to learn to complete a certain task that i am > interested in. That's true provided the task is not too complicated to start with... > This task is to write a very basic botting program for > a pc game. That will only be possible if somebody on the list knows anything about this specific game. That's by no means certain. > Tibia(Tibia.com). The program i want to write would start out being > one simple feature. I want my program to look at my game and see when > my health is below a certain ammount and heal my character by typing > in a certain spell in the console of the game. Thats not necessarily simple. It involves interrogating another program and interacting with it. That may be quite easy if there is an exposed and documented programming interface but if not its quite an advanced programming task. > Can anyone provide such a list of start to finish knowledge Unless you are lucky and find somebody here that knows this particular game then I doubt it. We focus on teaching the fundamentals of Python and its standard library. > something called a tibia api and I am not sure if its relevant. > Tibia is written in c++. Thanks :) It depends a lot on what that interface looks like. There is no trivial way to connect Python to C++ code unless the interface has been designed to operate that way, which is unlikely. It may be possible but its not likely to be a simple job for a beginner. What is your experience level in programming? You say you enjoy it which implies you have some experience? What languages have you used other than python? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From jsmallwood82 at yahoo.com Fri Apr 25 21:52:52 2014 From: jsmallwood82 at yahoo.com (jordan smallwood) Date: Fri, 25 Apr 2014 12:52:52 -0700 (PDT) Subject: [Tutor] Help With an Assignment Message-ID: <1398455572.10920.YahooMailNeo@web162705.mail.bf1.yahoo.com> Hello, I am new to Python. I mean completely new and we're working on this problem set where they give us specs and we have to build something based off these specs. I have no idea what they're asking. Could someone help get me started on the path to figuring this out? Below is the question: 1. Write a program module with at least two functions. Follow this specifi- cation exactly for these two functions: 1. (a) ?One function, CalculateCentimeters, receives a value in inches and returns the equivalent value in centimeters. centimeters =2.54?inches 2. (b) ?The other function, CalculateInches receives a value in centime- ters and returns the equivalent value in inches. inches =centimeters/2.54 ... but you don?t 2.54 in your code 2 times. It?s a good candidate to be a module-level constant. Specified instructions about the internals of code, i.e., the names of your functions and how they behave, is called the internal specification. It tells you, the author of the function, as well as programmers who call your function, how to call it and what to expect when it is called. You must following them exactly or call a meeting of your programming team because your choices here affect the others. For this exercise, you design the rest of the functions for your program, but be careful to keep all the code in functions. Invent and arrange functions as you wish to ask the user for: (a) a value (b) a unit of measure and call the appropriate function to print out the value in the other unit of measure. ?Marilyn Davis, 2007-2014 64 HOMEWORK Specified instructions about the user?s view of your code (like just given) is called the external specification. Often the paying customer gives these directions so you must follow them exactly, doing things in the order given; but in this case, the internal design is up to you.? -------------- next part -------------- An HTML attachment was scrubbed... URL: From luky.romero at gmail.com Fri Apr 25 19:09:40 2014 From: luky.romero at gmail.com (Luky Romero) Date: Fri, 25 Apr 2014 13:09:40 -0400 Subject: [Tutor] Opening a new script Message-ID: Is there a way to open a new file script using only the python shell? I have both updated versions of python (2.7, 3.1) -Luky -------------- next part -------------- An HTML attachment was scrubbed... URL: From mik.stephen at yahoo.com Fri Apr 25 22:34:12 2014 From: mik.stephen at yahoo.com (Stephen Mik) Date: Fri, 25 Apr 2014 13:34:12 -0700 (PDT) Subject: [Tutor] Difficulty in getting logged on to python.org; want to resubscribe at the beginner level; finding "While" Loops in Python 3.4.0 to be extremely picky Message-ID: <1398458052.68527.YahooMailNeo@web124702.mail.ne1.yahoo.com> Dear Sir(s): ??? My name is Stephen W. Mik,my email address is "mik.stephen at yahoo.com"; and I am having trouble logging on to the "Python Tutor Site". I desperately need HELP with a Python 3.4.0 "Guess A Number" Homework Assignment 4 which is due VERY SOON.. I recognize,and acknowledge,that I am a Python Programming amateur and some of my questions may seem trivial or naive;but a guy has to start somewhere.I was briefly on the mailing list for a few days;unsubscribed Apr. 24,2014,and now I want to get back in,but am having trouble doing so. I have to do my programming in a shared computer lab(on Windows machines) Mondays,Tuesdays,Thursdays and limited hours on Friday because my home computer is a 2007 MacIntosh which I? can't configure to run Python 3.4.0. Anyway,people,I need help with Python as soon as I can get it.Thanks. SINCERELY,Stephen W. Mik -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Apr 26 00:04:17 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 25 Apr 2014 23:04:17 +0100 Subject: [Tutor] Help With an Assignment In-Reply-To: <1398455572.10920.YahooMailNeo@web162705.mail.bf1.yahoo.com> References: <1398455572.10920.YahooMailNeo@web162705.mail.bf1.yahoo.com> Message-ID: On 25/04/14 20:52, jordan smallwood wrote: > Hello, > > I am new to Python. I mean completely new and we're working on this > problem set where they give us specs and we have to build something > based off these specs. I have no idea what they're asking. Its pretty clear. They want you to build a module containing two functions. Now how much of that do you not understand? Also do you know which version of python you are using - it can make a difference when you get to the details. And which operating system - less important for a task like this. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From illusiontechniques at gmail.com Sat Apr 26 00:09:26 2014 From: illusiontechniques at gmail.com (C Smith) Date: Fri, 25 Apr 2014 18:09:26 -0400 Subject: [Tutor] Difficulty in getting logged on to python.org; want to resubscribe at the beginner level; finding "While" Loops in Python 3.4.0 to be extremely picky In-Reply-To: <1398458052.68527.YahooMailNeo@web124702.mail.ne1.yahoo.com> References: <1398458052.68527.YahooMailNeo@web124702.mail.ne1.yahoo.com> Message-ID: You can get python 3.4 to work on your mac, but it has 2.5 or 2.4 which the OS uses and things can get very messed up if you don't know what you are doing. You should use virtualbox to run virtual OS's on your mac without messing up your main computer. You should probably describe what kind of error you are getting when trying to log in to the website. On Fri, Apr 25, 2014 at 4:34 PM, Stephen Mik < mik.stephen at yahoo.com.dmarc.invalid> wrote: > Dear Sir(s): > My name is Stephen W. Mik,my email address is "mik.stephen at yahoo.com"; > and I am having trouble logging on to the "Python Tutor Site". I > desperately need HELP with a Python 3.4.0 "Guess A Number" Homework > Assignment 4 which is due VERY SOON.. I recognize,and acknowledge,that I am > a Python Programming amateur and some of my questions may seem trivial or > naive;but a guy has to start somewhere.I was briefly on the mailing list > for a few days;unsubscribed Apr. 24,2014,and now I want to get back in,but > am having trouble doing so. I have to do my programming in a shared > computer lab(on Windows machines) Mondays,Tuesdays,Thursdays and limited > hours on Friday because my home computer is a 2007 MacIntosh which I can't > configure to run Python 3.4.0. Anyway,people,I need help with Python as > soon as I can get it.Thanks. > SINCERELY,Stephen W. Mik > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Apr 26 00:06:25 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 25 Apr 2014 23:06:25 +0100 Subject: [Tutor] Opening a new script In-Reply-To: References: Message-ID: On 25/04/14 18:09, Luky Romero wrote: > Is there a way to open a new file script using only the python shell? > I have both updated versions of python (2.7, 3.1) What do you mean by open a new file script? What do you mean by script? What do you mean by the python shell? There are several possible answers to each of those questions. We need specific details to help. What exactly are you hoping to do? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Sat Apr 26 00:15:37 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 25 Apr 2014 23:15:37 +0100 Subject: [Tutor] Difficulty in getting logged on to python.org; want to resubscribe at the beginner level; finding "While" Loops in Python 3.4.0 to be extremely picky In-Reply-To: <1398458052.68527.YahooMailNeo@web124702.mail.ne1.yahoo.com> References: <1398458052.68527.YahooMailNeo@web124702.mail.ne1.yahoo.com> Message-ID: On 25/04/14 21:34, Stephen Mik wrote: > Dear Sir(s): > My name is Stephen W. Mik,my email address is > "mik.stephen at yahoo.com"; and I am having trouble logging on to the > "Python Tutor Site". Do you mean the mailing list admin site? You seem to have succeeded because your email address is registered and this mail got through... > I desperately need HELP with a Python 3.4.0 "Guess > A Number" Homework Assignment 4 which is due VERY SOON.. That's fine we can offer help, we just don't do solutions. > acknowledge,that I am a Python Programming amateur and some of my > questions may seem trivial or naive Thats what this list is here for. > computer is a 2007 MacIntosh which I can't configure to run Python > 3.4.0. You should be able to get a version 3 Python running on it and there really isn't that much difference between Python 3.1 and 3.4. In fact even version 2 isn't so differnt you couldn't do useful wprk, you just need to translate it in the class lab that's all. > Anyway,people,I need help with Python as soon as I can get it. You need to tell us what kind of help. The more specific the question the better the answer is likely to be. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From davea at davea.name Sat Apr 26 00:26:36 2014 From: davea at davea.name (Dave Angel) Date: Fri, 25 Apr 2014 18:26:36 -0400 (EDT) Subject: [Tutor] Learning References: Message-ID: Leo Nardo Wrote in message: > I want to program. I enjoy programming and am willing to put in time > to do this.When I try to get started with programming I realize how > difficult it is to find the information worthy of learning. > I would like to write the > program in python, and I do not need a graphical interface. There is > something called a tibia api and I am not sure if its relevant. Tibia > is written in c++. Thanks :) I spent a while hunting through tibia.com, and saw no reference to the api. Perhaps it is only available to members, and I have no interest in joining. I think you need to go find the spec, perhaps starting by asking on the forum at tibia.com. I found a link that shows promise, but I see no indication that it is sponsored or even condoned by tibia.com (cipsoft). https://code.google.com/p/tibiaapi/ Also note that the changelog hasn't been updated for 4 years. Perhaps it doesn?t even work for more recent versions of tibia. -- DaveA From davea at davea.name Sat Apr 26 00:37:07 2014 From: davea at davea.name (Dave Angel) Date: Fri, 25 Apr 2014 18:37:07 -0400 (EDT) Subject: [Tutor] trying to understand pattern matching code References: Message-ID: rahmad akbar Wrote in message: > > to Dave, > i do i do the text mode? i had no idea this was on html Whatever mail program you're using is apparently defaulting to html. Find a menu item that specifies 'text' or 'plain text' or 'text only'. Or tell us what email program you're using, what os, and versions for each. Maybe someone will have experience with it. Back to your problem, do you understand what binary is, and how to convert (mentally) to and from decimal? Do you know that the bits have values of 1, 2, 4, 8, 16, etc? And that 24 is created by turning on the 16 bit and the 8 bit, commonly referred to as bits 4 and 3, respectively. -- DaveA From davea at davea.name Sat Apr 26 00:51:05 2014 From: davea at davea.name (Dave Angel) Date: Fri, 25 Apr 2014 18:51:05 -0400 (EDT) Subject: [Tutor] Help With an Assignment References: <1398455572.10920.YahooMailNeo@web162705.mail.bf1.yahoo.com> Message-ID: jordan smallwood Wrote in message: Do you know what a module is? Can you use a text editor to create one? Do you know what a function looks like? Try writing the first one they asked. Post it here, along with some test code showing it works, or describe what goes wrong. And while you're at it, switch your emails to plain text, and tell us at least what version of Python you're trying to use. -- DaveA From alan.gauld at btinternet.com Sat Apr 26 02:43:52 2014 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Sat, 26 Apr 2014 01:43:52 +0100 (BST) Subject: [Tutor] Opening a new script In-Reply-To: References: Message-ID: <1398473032.31737.YahooMailNeo@web186004.mail.ir2.yahoo.com> From: Luky Romero >To: Alan Gauld >Sent: Saturday, 26 April 2014, 0:28 >Subject: Re: [Tutor] Opening a new script > > > >By script I mean the text editor that allows you to modify code,? >and by shell, I mean the portion of the IDLE that allows you to? >use the interpreter and the interactive part of Python simultaneously.?No, you cannot open a new file for editing from the IDLE shell. You have to use the File->New menu. But there are shortcuts? (Ctrl-N on Linux) to do that from the keyboard if that helps. You can also import the file into the interpreter for testing but? that can lead to problems if you change it and have to try to? reload it. Personally I prefer to just run the file separately using? Run Module (F5) and see the output in the shell window. HTH Alan G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From suhanavidyarthi at gmail.com Sat Apr 26 02:46:17 2014 From: suhanavidyarthi at gmail.com (Suhana Vidyarthi) Date: Fri, 25 Apr 2014 17:46:17 -0700 Subject: [Tutor] Help needed Message-ID: Hi, I need help with coding for the problem below. I am new in this area, so needed help. If anyone can help me program the below scenario, I will be thankful. I have this file: 1,3,5,0.03 2,3,5,5,4,0.11 3,3,5,5,4,5,8,0.04 2,5,8,7,8,0.04 3,14,10,14,13,17,13,0.04 1,14,18,0.06 4,10,13,14,13,17,13,12,13,0.04 4,11,6,11,9,11,12,11,19,0.08 3,19,20,15,20,21,20,0.24 1,21,20,0.05 3,20,21,21,16,21,22,0.27 And each line is interpreted as: - 1,3,5,0.03 -> This line means 1 link can be down i.e. between 3?5 with a probability of failure *0.03* - 2,3,5,5,4,0.11 -> This line means 2 links can be down i.e. between 3--5 and 5--4 with a probability of failure *0.11* (for each link) - 3,3,5,5,4,5,8,0.04 -> Similarly this line means 3 links can be down i.e. between 3--5 , 5?4 and 5?8 with a probability of failure *0.04*(for each link) - 2,5,8,7,8,0.04 -> This line means 2 links can be down i.e. between 5?8 and 7?8 with probability of failure *0.04* (for each link) - 3,14,10,14,13,17,13,0.04 -> Means 3 links can be down i.e. between 14?10, 14?13 and 17?13 with a probability of failure *0.04* (for each link) - 1,14,18,0.06 -> Means 1link can be down i.e. between 14?18 with a probability of failure as* 0.06* - 4,10,13,14,13,17,13,12,13,0.04 -> Means 4 links can go down i.e. between 10?13, 14?13, 17?13 and 12?13 with a probability of failure *0.04* (for each link) - 4,11,6,11,9,11,12,11,19,0.08 -> Means 4 links can go down i.e. between 11?6, 11?9, 11?12 and 11?19 with a probability of failure *0.08* (for each link) - 3,19,20,15,20,21,20,0.24 -> Means 3 links can go down i.e. between 19?20, 15?20 and 21?20 with a probability of failure *0.24* (for each link) - 1,21,20,0.05 -> Means 1 link is down i.e. between 21?20 with a probability of failure *0.05* - 3,20,21,21,16,21,22,0.27 -> Means 3 links are down i.e. between 20?21, 21?16 and 21?22 with a probability of failure as* 0.27* (for each link) I want to create two arrays using the above file (Links array and Prob array) that should give following output: *Links *= { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] [10,13] [14,13] [17,13] [12,13] [11,6] [11,9] [11,12] [11,19] [19,20] [15,20] [21,20] [20,21] [21,16] [21,22] } *Prob *= {[0.28] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] [0.08] [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} *IMP Note*: The probabilities get added if a link is mentioned twice or thrice. For example: link 3?5 is repeated 3 times: in line one, it has a probability of failure as *0.03*, in line two it is *0.11* and in line three it is *0.04*. So the probability of failure for link 3?5 is 0.03+0.11+0.04 =* 0.28* So the first element in Links array is [3,5] and its probability of failure is the first element in Prob array i.e. 0.28 Can anyone help me with this please? -------------- next part -------------- An HTML attachment was scrubbed... URL: From sunil.techspk at gmail.com Sat Apr 26 06:48:13 2014 From: sunil.techspk at gmail.com (Sunil Tech) Date: Sat, 26 Apr 2014 10:18:13 +0530 Subject: [Tutor] xlrd package Message-ID: Hi, I want to know how many sheets can be created in Excel using xlrd package. Thank you -------------- next part -------------- An HTML attachment was scrubbed... URL: From matbioinfo at gmail.com Sat Apr 26 10:36:08 2014 From: matbioinfo at gmail.com (rahmad akbar) Date: Sat, 26 Apr 2014 10:36:08 +0200 Subject: [Tutor] trying to understand pattern matching code In-Reply-To: References: Message-ID: Danny and Dave, super thanks on the plain text advice. i'm sending this mail on plain text, please let me know if it turned out to be otherwise back to the problem just googled binary and i now have some idea on it. i now understand turning on 16 and 8 gives 24 and thus 4+8 = 12. why is this? i now understand this bit. but i still couldnt get this line though D = (( D << 1) + 1) & masks [ c ] On Sat, Apr 26, 2014 at 12:37 AM, Dave Angel wrote: > rahmad akbar Wrote in message: >> >> > to Dave, >> i do i do the text mode? i had no idea this was on html > > Whatever mail program you're using is apparently defaulting to > html. Find a menu item that specifies 'text' or 'plain text' or > 'text only'. > > Or tell us what email program you're using, what os, and versions > for each. Maybe someone will have experience with > it. > > Back to your problem, do you understand what binary is, and how > to convert (mentally) to and from decimal? Do you know that the > bits have values of 1, 2, 4, 8, 16, etc? And that 24 is created > by turning on the 16 bit and the 8 bit, commonly referred to as > bits 4 and 3, respectively. > > > -- > DaveA > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- many thanks mat From alan.gauld at btinternet.com Sat Apr 26 10:41:33 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 26 Apr 2014 09:41:33 +0100 Subject: [Tutor] Help needed In-Reply-To: References: Message-ID: On 26/04/14 01:46, Suhana Vidyarthi wrote: > I have this file: > > 1,3,5,0.03 > > 2,3,5,5,4,0.11 > > 3,3,5,5,4,5,8,0.04 .... > And each line is interpreted as: > > * 1,3,5,0.03-> This line means 1 link can be down i.e. between 3?5 > with a probability of failure *0.03* > * 2,3,5,5,4,0.11 -> This line means 2 links can be down i.e. between > 3--5 and 5--4 with a probability of failure *0.11* (for each link) > * 3,3,5,5,4,5,8,0.04 -> Similarly this line means 3 links can be down > i.e. between 3--5 , 5?4 and 5?8 with a probability of failure *0.04* > (for each link) ... > I want to create two arrays using the above file (Links array and Prob > array) that should give following output: > > *Links *= { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] > [10,13] [14,13] [17,13] [12,13] [11,6] [11,9][11,12] [11,19] [19,20] > [15,20] [21,20] [20,21] [21,16] [21,22] } > > *Prob *= {[0.28] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] [0.08] > [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} I don't understand how you develop this? The first list has 22 items the second 17. I would have expected them to be the same? Also why do you want these two lists? What do you plan on doing with them? I would have thought a mapping of link to probability would be much more useful? (mapping => dictionary) > So the first element in Links array is [3,5] and its probability of > failure is the first element in Prob array i.e. 0.28 > > Can anyone help me with this please? Do you know how to open and read a file line by line? Do you know how to extract the elements from a line? Do you know how to define a list and add elements to it? Do you know how to find an element in a list? Do you know how to modify an element in a list? If you know the above you have all the pieces you need to complete the task. If not tell us which bits you are stuck with. And show us some code, we will not do your work for you but are happy to help. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Sat Apr 26 10:43:35 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 26 Apr 2014 09:43:35 +0100 Subject: [Tutor] xlrd package In-Reply-To: References: Message-ID: On 26/04/14 05:48, Sunil Tech wrote: > I want to know how many sheets can be created in Excel using xlrd package. > This list if for people learning the Python language and standard library. xlrd is not part of that so you may have more success asking on an xlrd forum. Or failing that a Windows forum? Or you may just get lucky and find somebody here who has used xlrd... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Sat Apr 26 10:58:56 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 26 Apr 2014 09:58:56 +0100 Subject: [Tutor] trying to understand pattern matching code In-Reply-To: References: Message-ID: On 26/04/14 09:36, rahmad akbar wrote: > but i still couldnt get this line though > > D = (( D << 1) + 1) & masks [ c ] Do you understand the concept of bit shifting? ie 000110 shifted left gives 001100 and 000110 shifted right gives 000011 In other words the bit pattern moves left or right and the missing bits are replaced with zeros. The effect of shift left is to multiply the number by two. + 1 just adds 1 to the resulting number So the first bit of your line is the same as D = ((D*2)+1) The second part uses bitwise and with a mask chosen from a list of masks, A bitwise and has the effect of zeroing any bit that is zero in the mask and keeping any bit that is one in the mask. So 11100011 -> My data 00001111 -> my mask, designed to return the right hand 4 bits 00000011 -> data & mask Similarly 11100011 -> data 11110000 -> mask for leftmost 4 bits 11100000 -> data & mask So which bits are preserved in your D after the math and masking depends on what the masks[c] mask looks like. You might want to use some print statements using the bin() function to see the actual bit patterns. (If you do you might find that long bit patterns don;t show what you expect, if that happens try masking the result to say 16 places like this: print bin(mydata & 0xFFFF) # FFFF => 1111111111111111 HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From kliateni at gmail.com Sat Apr 26 11:26:49 2014 From: kliateni at gmail.com (Karim) Date: Sat, 26 Apr 2014 11:26:49 +0200 Subject: [Tutor] xlrd package In-Reply-To: References: Message-ID: <535B7BD9.4080400@gmail.com> On 26/04/2014 10:43, Alan Gauld wrote: > On 26/04/14 05:48, Sunil Tech wrote: > >> I want to know how many sheets can be created in Excel using xlrd >> package. >> > > This list if for people learning the Python language and standard > library. xlrd is not part of that so you may have more success asking > on an xlrd forum. > > Or failing that a Windows forum? > > Or you may just get lucky and find somebody here who has used xlrd... > Hello, xlrd is for reading xl sheets not for writing. xlwt is for writing Excel sheets. I think there is "no human limit" to create hundred. I use only xlrd. for reading to create csv (ascii) documents per existing sheet. Regards From breamoreboy at yahoo.co.uk Sat Apr 26 14:58:32 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 26 Apr 2014 13:58:32 +0100 Subject: [Tutor] xlrd package In-Reply-To: References: Message-ID: On 26/04/2014 05:48, Sunil Tech wrote: > Hi, > > I want to know how many sheets can be created in Excel using xlrd package. > > Thank you > By using your favourite search engine you could have found this http://www.python-excel.org/ and hence this https://groups.google.com/forum/#!forum/python-excel -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From dyoo at hashcollision.org Sat Apr 26 18:05:47 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sat, 26 Apr 2014 09:05:47 -0700 Subject: [Tutor] Help needed In-Reply-To: References: Message-ID: Hi Suhana, Also note that you asked this question just a few days ago. https://mail.python.org/pipermail/tutor/2014-April/101019.html We're not robots. We don't like repetition unless there's a reason for it, and in this case, you got responses to the earlier question. For example: https://mail.python.org/pipermail/tutor/2014-April/101022.html https://mail.python.org/pipermail/tutor/2014-April/101029.html Did you see these responses? If not, please check your mail settings. If you want to continue working on this problem, I'd recommend continuing that thread rather than start a fresh one. Reason is, if I were to look at your question fresh, I'd answer the exact same way to it. I'm trying to lightly probe what you've done and what you understand already. If you're repeating the question in the hopes that repetition will wear down the people you're trying to get answers from, please change your learning strategy: it won't be effective here. From matbioinfo at gmail.com Sat Apr 26 18:08:38 2014 From: matbioinfo at gmail.com (rahmad akbar) Date: Sat, 26 Apr 2014 18:08:38 +0200 Subject: [Tutor] trying to understand pattern matching code In-Reply-To: References: Message-ID: hi Alan, your explanation clears most things, super thanks!! On Sat, Apr 26, 2014 at 10:58 AM, Alan Gauld wrote: > On 26/04/14 09:36, rahmad akbar wrote: > >> but i still couldnt get this line though >> >> D = (( D << 1) + 1) & masks [ c ] > > > Do you understand the concept of bit shifting? > ie > 000110 shifted left gives > 001100 > > and > 000110 shifted right gives > 000011 > > In other words the bit pattern moves left or > right and the missing bits are replaced with > zeros. > > The effect of shift left is to multiply the > number by two. > > + 1 just adds 1 to the resulting number > > So the first bit of your line is the same as > > D = ((D*2)+1) > > The second part uses bitwise and with a mask chosen from a list of masks, > A bitwise and has the effect of zeroing any bit that is zero in the mask and > keeping any bit that is one in the mask. > > So > > 11100011 -> My data > 00001111 -> my mask, designed to return the right hand 4 bits > 00000011 -> data & mask > > Similarly > 11100011 -> data > 11110000 -> mask for leftmost 4 bits > 11100000 -> data & mask > > So which bits are preserved in your D after the math and masking depends on > what the masks[c] mask looks like. > > You might want to use some print statements using the bin() > function to see the actual bit patterns. (If you do you might > find that long bit patterns don;t show what you expect, if > that happens try masking the result to say 16 places > like this: > > print bin(mydata & 0xFFFF) # FFFF => 1111111111111111 > > HTH > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- many thanks mat From suhanavidyarthi at gmail.com Sat Apr 26 19:00:41 2014 From: suhanavidyarthi at gmail.com (Suhana Vidyarthi) Date: Sat, 26 Apr 2014 10:00:41 -0700 Subject: [Tutor] Help needed In-Reply-To: References: Message-ID: Hi, The reason I opened a link is because there are changes in the code. Does it make sense? Else I can definitely go back to the thread. On Sat, Apr 26, 2014 at 9:05 AM, Danny Yoo wrote: > Hi Suhana, > > Also note that you asked this question just a few days ago. > > https://mail.python.org/pipermail/tutor/2014-April/101019.html > > We're not robots. We don't like repetition unless there's a reason > for it, and in this case, you got responses to the earlier question. > For example: > > https://mail.python.org/pipermail/tutor/2014-April/101022.html > https://mail.python.org/pipermail/tutor/2014-April/101029.html > > Did you see these responses? If not, please check your mail settings. > > > If you want to continue working on this problem, I'd recommend > continuing that thread rather than start a fresh one. Reason is, if I > were to look at your question fresh, I'd answer the exact same way to > it. I'm trying to lightly probe what you've done and what you > understand already. > > If you're repeating the question in the hopes that repetition will > wear down the people you're trying to get answers from, please change > your learning strategy: it won't be effective here. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From suhanavidyarthi at gmail.com Sat Apr 26 19:10:37 2014 From: suhanavidyarthi at gmail.com (Suhana Vidyarthi) Date: Sat, 26 Apr 2014 10:10:37 -0700 Subject: [Tutor] Help needed In-Reply-To: References: Message-ID: Thanks for the response Alan. my clarifications are below: On Sat, Apr 26, 2014 at 1:41 AM, Alan Gauld wrote: > On 26/04/14 01:46, Suhana Vidyarthi wrote: > > I have this file: >> >> 1,3,5,0.03 >> >> 2,3,5,5,4,0.11 >> >> 3,3,5,5,4,5,8,0.04 >> > .... > >> And each line is interpreted as: >> >> * 1,3,5,0.03-> This line means 1 link can be down i.e. between 3?5 >> with a probability of failure *0.03* >> * 2,3,5,5,4,0.11 -> This line means 2 links can be down i.e. between >> 3--5 and 5--4 with a probability of failure *0.11* (for each link) >> * 3,3,5,5,4,5,8,0.04 -> Similarly this line means 3 links can be down >> i.e. between 3--5 , 5?4 and 5?8 with a probability of failure *0.04* >> (for each link) >> > ... > >> I want to create two arrays using the above file (Links array and Prob >> array) that should give following output: >> >> *Links *= { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] >> [10,13] [14,13] [17,13] [12,13] [11,6] [11,9][11,12] [11,19] [19,20] >> [15,20] [21,20] [20,21] [21,16] [21,22] } >> >> *Prob *= {[0.28] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] [0.08] >> [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} > > > I don't understand how you develop this? The first list has 22 items the > second 17. I would have expected them to be the same? > In the "Prob" array the elements are less because if you read the note below: I said the links that are repeating for example [3,5] their probabilities get added and stored as a single value in the "Prob" array. > > Also why do you want these two lists? What do you plan on doing with them? > I would have thought a mapping of link to probability would be much more > useful? (mapping => dictionary) I want these two lists because using the links and probs array, I will check which link has the lowest probability. Here lowest probability means the risk of failure for that link. So based on which link has least probability of failure, I will use it to setup a connection (I have a source and destination and the links mentioned above are the paths between them) I want to select the path which has least failure probability. Did it make sense? > > So the first element in Links array is [3,5] and its probability of >> failure is the first element in Prob array i.e. 0.28 >> >> Can anyone help me with this please? >> > > Do you know how to open and read a file line by line? > Do you know how to extract the elements from a line? > Do you know how to define a list and add elements to it? > Do you know how to find an element in a list? > Do you know how to modify an element in a list? > > If you know the above you have all the pieces you need to complete the > task. If not tell us which bits you are stuck with. > > I have been studying about it and have come up with my code. > And show us some code, we will not do your work for you but are happy to > help. > > I actually used the map and dictionary function to write the code, please see the attachment. The only problem I am having is that when I try to display the links and probabilities, they are not displayed in the order of the file content. This is how it should display: Links = { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] [10,13] [14,13] [17,13] [12,13] [11,6] [11,9][11,12] [11,19] [19,20] [15,20] [21,20] [20,21] [21,16] [21,22] } Prob = {[0.18] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] [0.08] [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} However when I run my code, this is how the arrays are displayed: Links -> [('10', '13'), ('14', '18'), ('7', '8'), ('15', '20'), ('5', '8'), ('5', '4'), ('11', '9'), ('21', '22'), ('12', '13'), ('21', '20'), ('17', '13'), ('20', '21'), ('21', '16'), ('14', '10'), ('11', '12'), ('11', '19'), ('14', '13'), ('3', '5'), ('11', '6'), ('19', '20')] Probability -> [0.04, 0.06, 0.04, 0.24, 0.08, 0.15, 0.08, 0.27, 0.04, 0.29, 0.08, 0.27, 0.27, 0.04, 0.08, 0.08, 0.08, 0.18000000000000002, 0.08, 0.24] Can you please see my code and help me find out what is wrong? > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: pyth-new2.py Type: text/x-python-script Size: 1811 bytes Desc: not available URL: From dyoo at hashcollision.org Sat Apr 26 20:41:56 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sat, 26 Apr 2014 11:41:56 -0700 Subject: [Tutor] Help needed In-Reply-To: References: Message-ID: >>> I want to create two arrays using the above file (Links array and Prob >>> array) that should give following output: >>> >>> *Links *= { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] >>> [10,13] [14,13] [17,13] [12,13] [11,6] [11,9][11,12] [11,19] [19,20] >>> [15,20] [21,20] [20,21] [21,16] [21,22] } >>> >>> *Prob *= {[0.28] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] [0.08] >>> [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} >> >> >> I don't understand how you develop this? The first list has 22 items the >> second 17. I would have expected them to be the same? > > > In the "Prob" array the elements are less because if you read the note > below: I said the links that are repeating for example [3,5] their > probabilities get added and stored as a single value in the "Prob" array. But what will you plan to do with these values afterwards? I think Alan's point here is that if there's no direct relationship between the elements in Links and the elements in Probs, those values aren't going to be very useful to solve the rest of the problem. One way to look at this problem is to simplify or normalize the input; the original structure in the file is slightly weird to process, since a single line of the input represents several link/failure pairs. One concrete example is: 4,10,13,14,13,17,13,12,13,0.04 where all these numbers are uninterpreted. You can imagine something that takes the line above, and breaks it down into a series of LinkFailure items. ###### class LinkFailure(object): """Represents a link and the probability of failure.""" def __init__(self, start, end, failure): self.start = start self.end = end self.failure = failure ###### which represent a link and failure structure. If we have a structure like this, then it explicitly represents a relationship between a link and its failure, and the string line: 4,10,13,14,13,17,13,12,13,0.04 can be distilled and represented as a collection of LinkFailure instances: [LinkFailure(10, 13, 0.04), LinkFailure(14, 13, 0.04), LinkFailure(17, 13, 0.04), LinkFailure(12, 13, 0.04)] Then the relationship is explicit. >> Also why do you want these two lists? What do you plan on doing with them? >> I would have thought a mapping of link to probability would be much more >> useful? (mapping => dictionary) > > > I want these two lists because using the links and probs array, I will check > which link has the lowest probability. Here lowest probability means the > risk of failure for that link. So based on which link has least probability > of failure, I will use it to setup a connection (I have a source and > destination and the links mentioned above are the paths between them) I want > to select the path which has least failure probability. > > Did it make sense? Unfortunately, I'm still confused. If you just have the Links and the Probs lists of unequal length, unless there's some additional information that you're represented, then I don't see the necessary connection between the two lists that lets you go any further in the problem. There's no one-to-one-ness: given a link in Links, which Probs do you want to look at? If you don't represent that linkage in some way, I don't understand yet where you go next. >>> So the first element in Links array is [3,5] and its probability of >>> failure is the first element in Prob array i.e. 0.28 But if the two lists are different lengths, what probability of failure is associates with the last element in the Prob array? The representation of data is important: if you choose an awkward representation, it makes solving this problem more difficult than it needs be. That is, if you're already compressing multiple elements in Prob that correspond to the same link, you also need some way to figure out what link that a compressed probability refer to. Otherwise, you don't have enough information to solve the problem anymore. From suhanavidyarthi at gmail.com Sat Apr 26 21:16:27 2014 From: suhanavidyarthi at gmail.com (Suhana Vidyarthi) Date: Sat, 26 Apr 2014 12:16:27 -0700 Subject: [Tutor] Help needed In-Reply-To: References: Message-ID: Hi Danny, Let me give you a high level brief of what I am doing: I am working on doing "disaster aware routing" considering the 24-node US network where I will be setting up connection between two any two nodes (I will select the source and destination nodes randomly). Also I have some links whose "probability of failure" is mentioned in the attached file. Other links, which are not mentioned in the file - we suppose their "probability of failure" is zero. So between the source-destination nodes, there will be multiple paths and I will select the one which has "least probability of failure". Now to setup the connection between two nodes, I have to select a path whose "probability of failure" is least. To do that first I will calculate the risk of each path from the attached file and then select the path with least risk value. Did you get this part? I know it can be a bit confusing. Now I break the problem into parts: 1. I have to topology of the 24-node map 2. I have the link values of each link - where risk values are the "probability of failure" 3. I calculate the total "probability of failure" of each path (a path may have multiple links): Suppose my source node is "a" and destination node is "b". I can setup a path between a to b via c or via d (a-c-b or a-d-c): Here I will check the risk values of a-c and c-b; also risk values of a-d and d-c. If the total risk valure of a-c-b is lower that risk value of a-d-c, then I select the path a-c-d to setup the connection. (again risk value = probability of failure) Now, I will first calculate the "total probability of failure" of each link (using the file.txt) and since some links are repeated their values will be added. The probabilities get added if a link is mentioned twice or thrice. For example: link 3?5 is repeated 3 times: in line one, it has a probability of failure as 0.03, in line two it is 0.11 and in line three it is 0.04. So the probability of failure for link 3?5 is 0.03+0.11+0.04 = 0.18 The length of each array will be same. You see the code I wrote: here is the output for it: Links -> [('10', '13'), ('14', '18'), ('7', '8'), ('15', '20'), ('5', '8'), ('5', '4'), ('11', '9'), ('21', '22'), ('12', '13'), ('21', '20'), ('17', '13'), ('20', '21'), ('21', '16'), ('14', '10'), ('11', '12'), ('11', '19'), ('14', '13'), ('3', '5'), ('11', '6'), ('19', '20')] Probability -> [0.04, 0.06, 0.04, 0.24, 0.08, 0.15, 0.08, 0.27, 0.04, 0.29, 0.08, 0.27, 0.27, 0.04, 0.08, 0.08, 0.08, 0.18000000000000002, 0.08, 0.24] It means that link [10,13] has a "probability of failure" as [0.04] and since the link [3-5] is repeated thrice with probability of 0.03, 0.11 and 0.04, its "probability of failure" is [0.18] (third last element in the Probability array). For some reason instead of 0.18 it is showing 0.180000000002, which I cannot figure to why. Please see the attached code. If you see the file.txt and my output: the output is not displayed in sequence and that is what I need help with. I want this to display : Links = { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] [10,13] [14,13] [17,13] [12,13] [11,6] [11,9] [11,12] [11,19] [19,20] [15,20] [21,20] [20,21] [21,16] [21,22] } Prob = {[0.18] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] [0.08] [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} If you can figure why the output is not generated in same sequence as in the file.txt for me, it will be very helpful. let me know if I explained correctly, and if you have any questions or doubts? On Sat, Apr 26, 2014 at 11:41 AM, Danny Yoo wrote: > >>> I want to create two arrays using the above file (Links array and Prob > >>> array) that should give following output: > >>> > >>> *Links *= { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] > >>> [10,13] [14,13] [17,13] [12,13] [11,6] [11,9][11,12] [11,19] [19,20] > >>> [15,20] [21,20] [20,21] [21,16] [21,22] } > >>> > >>> *Prob *= {[0.28] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] > [0.08] > >>> [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} > >> > >> > >> I don't understand how you develop this? The first list has 22 items the > >> second 17. I would have expected them to be the same? > > > > > > In the "Prob" array the elements are less because if you read the note > > below: I said the links that are repeating for example [3,5] their > > probabilities get added and stored as a single value in the "Prob" > array. > > > But what will you plan to do with these values afterwards? I think > Alan's point here is that if there's no direct relationship between > the elements in Links and the elements in Probs, those values aren't > going to be very useful to solve the rest of the problem. > > > One way to look at this problem is to simplify or normalize the input; > the original structure in the file is slightly weird to process, since > a single line of the input represents several link/failure pairs. > > One concrete example is: > > 4,10,13,14,13,17,13,12,13,0.04 > > where all these numbers are uninterpreted. > > You can imagine something that takes the line above, and breaks it > down into a series of LinkFailure items. > > ###### > class LinkFailure(object): > """Represents a link and the probability of failure.""" > def __init__(self, start, end, failure): > self.start = start > self.end = end > self.failure = failure > ###### > > which represent a link and failure structure. If we have a structure > like this, then it explicitly represents a relationship between a link > and its failure, and the string line: > > 4,10,13,14,13,17,13,12,13,0.04 > > can be distilled and represented as a collection of LinkFailure instances: > > [LinkFailure(10, 13, 0.04), LinkFailure(14, 13, 0.04), > LinkFailure(17, 13, 0.04), LinkFailure(12, 13, 0.04)] > > Then the relationship is explicit. > > > >> Also why do you want these two lists? What do you plan on doing with > them? > >> I would have thought a mapping of link to probability would be much more > >> useful? (mapping => dictionary) > > > > > > I want these two lists because using the links and probs array, I will > check > > which link has the lowest probability. Here lowest probability means the > > risk of failure for that link. So based on which link has least > probability > > of failure, I will use it to setup a connection (I have a source and > > destination and the links mentioned above are the paths between them) I > want > > to select the path which has least failure probability. > > > > Did it make sense? > > > Unfortunately, I'm still confused. If you just have the Links and the > Probs lists of unequal length, unless there's some additional > information that you're represented, then I don't see the necessary > connection between the two lists that lets you go any further in the > problem. There's no one-to-one-ness: given a link in Links, which > Probs do you want to look at? If you don't represent that linkage in > some way, I don't understand yet where you go next. > > > >>> So the first element in Links array is [3,5] and its probability of > >>> failure is the first element in Prob array i.e. 0.28 > > But if the two lists are different lengths, what probability of > failure is associates with the last element in the Prob array? > > > The representation of data is important: if you choose an awkward > representation, it makes solving this problem more difficult than it > needs be. That is, if you're already compressing multiple elements in > Prob that correspond to the same link, you also need some way to > figure out what link that a compressed probability refer to. > Otherwise, you don't have enough information to solve the problem > anymore. > -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- 1,3,5,0.03 2,3,5,5,4,0.11 3,3,5,5,4,5,8,0.04 2,5,8,7,8,0.04 3,14,10,14,13,17,13,0.04 1,14,18,0.06 4,10,13,14,13,17,13,12,13,0.04 4,11,6,11,9,11,12,11,19,0.08 3,19,20,15,20,21,20,0.24 1,21,20,0.05 3,20,21,21,16,21,22,0.27 -------------- next part -------------- A non-text attachment was scrubbed... Name: Code.py Type: text/x-python-script Size: 1957 bytes Desc: not available URL: From illusiontechniques at gmail.com Sat Apr 26 21:20:31 2014 From: illusiontechniques at gmail.com (C Smith) Date: Sat, 26 Apr 2014 15:20:31 -0400 Subject: [Tutor] Help needed In-Reply-To: References: Message-ID: Just glancing at your work, I see you have curly braces around what looks like it should be a list. If you are concerned with the order of your output, dictionaries do not have a concept of order. On Sat, Apr 26, 2014 at 3:16 PM, Suhana Vidyarthi wrote: > Hi Danny, > > Let me give you a high level brief of what I am doing: > I am working on doing "disaster aware routing" considering the 24-node US > network where I will be setting up connection between two any two nodes (I > will select the source and destination nodes randomly). Also I have some > links whose "probability of failure" is mentioned in the attached file. > Other links, which are not mentioned in the file - we suppose their > "probability of failure" is zero. So between the source-destination nodes, > there will be multiple paths and I will select the one which has "least > probability of failure". > > Now to setup the connection between two nodes, I have to select a path > whose "probability of failure" is least. To do that first I will calculate > the risk of each path from the attached file and then select the path with > least risk value. Did you get this part? I know it can be a bit confusing. > > Now I break the problem into parts: > > 1. I have to topology of the 24-node map > 2. I have the link values of each link - where risk values are the > "probability of failure" > 3. I calculate the total "probability of failure" of each path (a path may > have multiple links): Suppose my source node is "a" and destination node is > "b". I can setup a path between a to b via c or via d (a-c-b or a-d-c): > Here I will check the risk values of a-c and c-b; also risk values of a-d > and d-c. If the total risk valure of a-c-b is lower that risk value of > a-d-c, then I select the path a-c-d to setup the connection. (again risk > value = probability of failure) > > Now, I will first calculate the "total probability of failure" of each > link (using the file.txt) and since some links are repeated their values > will be added. The probabilities get added if a link is mentioned twice > or thrice. For example: link 3?5 is repeated 3 times: in line one, it > has a probability of failure as 0.03, in line two it is 0.11 and in line > three it is 0.04. So the probability of failure for link 3?5 is > 0.03+0.11+0.04 = 0.18 > > The length of each array will be same. You see the code I wrote: here is > the output for it: > > Links -> > > [('10', '13'), ('14', '18'), ('7', '8'), ('15', '20'), ('5', '8'), ('5', > '4'), ('11', '9'), ('21', '22'), ('12', '13'), ('21', '20'), ('17', '13'), > ('20', '21'), ('21', '16'), ('14', '10'), ('11', '12'), ('11', '19'), > ('14', '13'), ('3', '5'), ('11', '6'), ('19', '20')] > > > Probability -> > > [0.04, 0.06, 0.04, 0.24, 0.08, 0.15, 0.08, 0.27, 0.04, 0.29, 0.08, 0.27, > 0.27, 0.04, 0.08, 0.08, 0.08, 0.18000000000000002, 0.08, 0.24] > > > It means that link [10,13] has a "probability of failure" as [0.04] and > since the link [3-5] is repeated thrice with probability of 0.03, 0.11 and > 0.04, its "probability of failure" is [0.18] (third last element in the > Probability array). For some reason instead of 0.18 it is showing > 0.180000000002, which I cannot figure to why. > > > Please see the attached code. If you see the file.txt and my output: the > output is not displayed in sequence and that is what I need help with. I > want this to display : > > > Links = { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] [10,13] > [14,13] [17,13] [12,13] [11,6] [11,9] [11,12] [11,19] [19,20] [15,20] > [21,20] [20,21] [21,16] [21,22] } > > > Prob = {[0.18] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] [0.08] > [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} > > > If you can figure why the output is not generated in same sequence as in > the file.txt for me, it will be very helpful. > > > let me know if I explained correctly, and if you have any questions or > doubts? > > > On Sat, Apr 26, 2014 at 11:41 AM, Danny Yoo wrote: > >> >>> I want to create two arrays using the above file (Links array and Prob >> >>> array) that should give following output: >> >>> >> >>> *Links *= { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] >> >>> [10,13] [14,13] [17,13] [12,13] [11,6] [11,9][11,12] [11,19] [19,20] >> >>> [15,20] [21,20] [20,21] [21,16] [21,22] } >> >>> >> >>> *Prob *= {[0.28] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] >> [0.08] >> >>> [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} >> >> >> >> >> >> I don't understand how you develop this? The first list has 22 items >> the >> >> second 17. I would have expected them to be the same? >> > >> > >> > In the "Prob" array the elements are less because if you read the note >> > below: I said the links that are repeating for example [3,5] their >> > probabilities get added and stored as a single value in the "Prob" >> array. >> >> >> But what will you plan to do with these values afterwards? I think >> Alan's point here is that if there's no direct relationship between >> the elements in Links and the elements in Probs, those values aren't >> going to be very useful to solve the rest of the problem. >> >> >> One way to look at this problem is to simplify or normalize the input; >> the original structure in the file is slightly weird to process, since >> a single line of the input represents several link/failure pairs. >> >> One concrete example is: >> >> 4,10,13,14,13,17,13,12,13,0.04 >> >> where all these numbers are uninterpreted. >> >> You can imagine something that takes the line above, and breaks it >> down into a series of LinkFailure items. >> >> ###### >> class LinkFailure(object): >> """Represents a link and the probability of failure.""" >> def __init__(self, start, end, failure): >> self.start = start >> self.end = end >> self.failure = failure >> ###### >> >> which represent a link and failure structure. If we have a structure >> like this, then it explicitly represents a relationship between a link >> and its failure, and the string line: >> >> 4,10,13,14,13,17,13,12,13,0.04 >> >> can be distilled and represented as a collection of LinkFailure instances: >> >> [LinkFailure(10, 13, 0.04), LinkFailure(14, 13, 0.04), >> LinkFailure(17, 13, 0.04), LinkFailure(12, 13, 0.04)] >> >> Then the relationship is explicit. >> >> >> >> Also why do you want these two lists? What do you plan on doing with >> them? >> >> I would have thought a mapping of link to probability would be much >> more >> >> useful? (mapping => dictionary) >> > >> > >> > I want these two lists because using the links and probs array, I will >> check >> > which link has the lowest probability. Here lowest probability means the >> > risk of failure for that link. So based on which link has least >> probability >> > of failure, I will use it to setup a connection (I have a source and >> > destination and the links mentioned above are the paths between them) I >> want >> > to select the path which has least failure probability. >> > >> > Did it make sense? >> >> >> Unfortunately, I'm still confused. If you just have the Links and the >> Probs lists of unequal length, unless there's some additional >> information that you're represented, then I don't see the necessary >> connection between the two lists that lets you go any further in the >> problem. There's no one-to-one-ness: given a link in Links, which >> Probs do you want to look at? If you don't represent that linkage in >> some way, I don't understand yet where you go next. >> >> >> >>> So the first element in Links array is [3,5] and its probability of >> >>> failure is the first element in Prob array i.e. 0.28 >> >> But if the two lists are different lengths, what probability of >> failure is associates with the last element in the Prob array? >> >> >> The representation of data is important: if you choose an awkward >> representation, it makes solving this problem more difficult than it >> needs be. That is, if you're already compressing multiple elements in >> Prob that correspond to the same link, you also need some way to >> figure out what link that a compressed probability refer to. >> Otherwise, you don't have enough information to solve the problem >> anymore. >> > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From illusiontechniques at gmail.com Sat Apr 26 21:36:52 2014 From: illusiontechniques at gmail.com (C Smith) Date: Sat, 26 Apr 2014 15:36:52 -0400 Subject: [Tutor] Help needed In-Reply-To: References: Message-ID: err, set also is unordered. I can see you are using set for a reason, but has no concept of order. On Sat, Apr 26, 2014 at 3:20 PM, C Smith wrote: > Just glancing at your work, I see you have curly braces around what looks > like it should be a list. If you are concerned with the order of your > output, dictionaries do not have a concept of order. > > > On Sat, Apr 26, 2014 at 3:16 PM, Suhana Vidyarthi < > suhanavidyarthi at gmail.com> wrote: > >> Hi Danny, >> >> Let me give you a high level brief of what I am doing: >> I am working on doing "disaster aware routing" considering the 24-node US >> network where I will be setting up connection between two any two nodes (I >> will select the source and destination nodes randomly). Also I have some >> links whose "probability of failure" is mentioned in the attached file. >> Other links, which are not mentioned in the file - we suppose their >> "probability of failure" is zero. So between the source-destination nodes, >> there will be multiple paths and I will select the one which has "least >> probability of failure". >> >> Now to setup the connection between two nodes, I have to select a path >> whose "probability of failure" is least. To do that first I will calculate >> the risk of each path from the attached file and then select the path with >> least risk value. Did you get this part? I know it can be a bit confusing. >> >> Now I break the problem into parts: >> >> 1. I have to topology of the 24-node map >> 2. I have the link values of each link - where risk values are the >> "probability of failure" >> 3. I calculate the total "probability of failure" of each path (a path >> may have multiple links): Suppose my source node is "a" and destination >> node is "b". I can setup a path between a to b via c or via d (a-c-b or >> a-d-c): Here I will check the risk values of a-c and c-b; also risk values >> of a-d and d-c. If the total risk valure of a-c-b is lower that risk value >> of a-d-c, then I select the path a-c-d to setup the connection. (again risk >> value = probability of failure) >> >> Now, I will first calculate the "total probability of failure" of each >> link (using the file.txt) and since some links are repeated their values >> will be added. The probabilities get added if a link is mentioned twice >> or thrice. For example: link 3?5 is repeated 3 times: in line one, it >> has a probability of failure as 0.03, in line two it is 0.11 and in line >> three it is 0.04. So the probability of failure for link 3?5 is >> 0.03+0.11+0.04 = 0.18 >> >> The length of each array will be same. You see the code I wrote: here is >> the output for it: >> >> Links -> >> >> [('10', '13'), ('14', '18'), ('7', '8'), ('15', '20'), ('5', '8'), ('5', >> '4'), ('11', '9'), ('21', '22'), ('12', '13'), ('21', '20'), ('17', '13'), >> ('20', '21'), ('21', '16'), ('14', '10'), ('11', '12'), ('11', '19'), >> ('14', '13'), ('3', '5'), ('11', '6'), ('19', '20')] >> >> >> Probability -> >> >> [0.04, 0.06, 0.04, 0.24, 0.08, 0.15, 0.08, 0.27, 0.04, 0.29, 0.08, 0.27, >> 0.27, 0.04, 0.08, 0.08, 0.08, 0.18000000000000002, 0.08, 0.24] >> >> >> It means that link [10,13] has a "probability of failure" as [0.04] and >> since the link [3-5] is repeated thrice with probability of 0.03, 0.11 and >> 0.04, its "probability of failure" is [0.18] (third last element in the >> Probability array). For some reason instead of 0.18 it is showing >> 0.180000000002, which I cannot figure to why. >> >> >> Please see the attached code. If you see the file.txt and my output: the >> output is not displayed in sequence and that is what I need help with. I >> want this to display : >> >> >> Links = { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] [10,13] >> [14,13] [17,13] [12,13] [11,6] [11,9] [11,12] [11,19] [19,20] [15,20] >> [21,20] [20,21] [21,16] [21,22] } >> >> >> Prob = {[0.18] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] [0.08] >> [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} >> >> >> If you can figure why the output is not generated in same sequence as in >> the file.txt for me, it will be very helpful. >> >> >> let me know if I explained correctly, and if you have any questions or >> doubts? >> >> >> On Sat, Apr 26, 2014 at 11:41 AM, Danny Yoo wrote: >> >>> >>> I want to create two arrays using the above file (Links array and >>> Prob >>> >>> array) that should give following output: >>> >>> >>> >>> *Links *= { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] >>> >>> [10,13] [14,13] [17,13] [12,13] [11,6] [11,9][11,12] [11,19] [19,20] >>> >>> [15,20] [21,20] [20,21] [21,16] [21,22] } >>> >>> >>> >>> *Prob *= {[0.28] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] >>> [0.08] >>> >>> [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} >>> >> >>> >> >>> >> I don't understand how you develop this? The first list has 22 items >>> the >>> >> second 17. I would have expected them to be the same? >>> > >>> > >>> > In the "Prob" array the elements are less because if you read the note >>> > below: I said the links that are repeating for example [3,5] their >>> > probabilities get added and stored as a single value in the "Prob" >>> array. >>> >>> >>> But what will you plan to do with these values afterwards? I think >>> Alan's point here is that if there's no direct relationship between >>> the elements in Links and the elements in Probs, those values aren't >>> going to be very useful to solve the rest of the problem. >>> >>> >>> One way to look at this problem is to simplify or normalize the input; >>> the original structure in the file is slightly weird to process, since >>> a single line of the input represents several link/failure pairs. >>> >>> One concrete example is: >>> >>> 4,10,13,14,13,17,13,12,13,0.04 >>> >>> where all these numbers are uninterpreted. >>> >>> You can imagine something that takes the line above, and breaks it >>> down into a series of LinkFailure items. >>> >>> ###### >>> class LinkFailure(object): >>> """Represents a link and the probability of failure.""" >>> def __init__(self, start, end, failure): >>> self.start = start >>> self.end = end >>> self.failure = failure >>> ###### >>> >>> which represent a link and failure structure. If we have a structure >>> like this, then it explicitly represents a relationship between a link >>> and its failure, and the string line: >>> >>> 4,10,13,14,13,17,13,12,13,0.04 >>> >>> can be distilled and represented as a collection of LinkFailure >>> instances: >>> >>> [LinkFailure(10, 13, 0.04), LinkFailure(14, 13, 0.04), >>> LinkFailure(17, 13, 0.04), LinkFailure(12, 13, 0.04)] >>> >>> Then the relationship is explicit. >>> >>> >>> >> Also why do you want these two lists? What do you plan on doing with >>> them? >>> >> I would have thought a mapping of link to probability would be much >>> more >>> >> useful? (mapping => dictionary) >>> > >>> > >>> > I want these two lists because using the links and probs array, I will >>> check >>> > which link has the lowest probability. Here lowest probability means >>> the >>> > risk of failure for that link. So based on which link has least >>> probability >>> > of failure, I will use it to setup a connection (I have a source and >>> > destination and the links mentioned above are the paths between them) >>> I want >>> > to select the path which has least failure probability. >>> > >>> > Did it make sense? >>> >>> >>> Unfortunately, I'm still confused. If you just have the Links and the >>> Probs lists of unequal length, unless there's some additional >>> information that you're represented, then I don't see the necessary >>> connection between the two lists that lets you go any further in the >>> problem. There's no one-to-one-ness: given a link in Links, which >>> Probs do you want to look at? If you don't represent that linkage in >>> some way, I don't understand yet where you go next. >>> >>> >>> >>> So the first element in Links array is [3,5] and its probability of >>> >>> failure is the first element in Prob array i.e. 0.28 >>> >>> But if the two lists are different lengths, what probability of >>> failure is associates with the last element in the Prob array? >>> >>> >>> The representation of data is important: if you choose an awkward >>> representation, it makes solving this problem more difficult than it >>> needs be. That is, if you're already compressing multiple elements in >>> Prob that correspond to the same link, you also need some way to >>> figure out what link that a compressed probability refer to. >>> Otherwise, you don't have enough information to solve the problem >>> anymore. >>> >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From suhanavidyarthi at gmail.com Sat Apr 26 21:48:13 2014 From: suhanavidyarthi at gmail.com (Suhana Vidyarthi) Date: Sat, 26 Apr 2014 12:48:13 -0700 Subject: [Tutor] Help needed In-Reply-To: References: Message-ID: Thanks for the response Smith, I was thinking make be I have done something incorrect and if there is some other function that can be used to display the output in desired order but don't see it possible thats why was wondering if any of you Python gurus have any inputs for me :-) On Sat, Apr 26, 2014 at 12:36 PM, C Smith wrote: > err, set also is unordered. I can see you are using set for a reason, but > has no concept of order. > > > On Sat, Apr 26, 2014 at 3:20 PM, C Smith wrote: > >> Just glancing at your work, I see you have curly braces around what looks >> like it should be a list. If you are concerned with the order of your >> output, dictionaries do not have a concept of order. >> >> >> On Sat, Apr 26, 2014 at 3:16 PM, Suhana Vidyarthi < >> suhanavidyarthi at gmail.com> wrote: >> >>> Hi Danny, >>> >>> Let me give you a high level brief of what I am doing: >>> I am working on doing "disaster aware routing" considering the 24-node >>> US network where I will be setting up connection between two any two nodes >>> (I will select the source and destination nodes randomly). Also I have some >>> links whose "probability of failure" is mentioned in the attached file. >>> Other links, which are not mentioned in the file - we suppose their >>> "probability of failure" is zero. So between the source-destination nodes, >>> there will be multiple paths and I will select the one which has "least >>> probability of failure". >>> >>> Now to setup the connection between two nodes, I have to select a path >>> whose "probability of failure" is least. To do that first I will calculate >>> the risk of each path from the attached file and then select the path with >>> least risk value. Did you get this part? I know it can be a bit confusing. >>> >>> Now I break the problem into parts: >>> >>> 1. I have to topology of the 24-node map >>> 2. I have the link values of each link - where risk values are the >>> "probability of failure" >>> 3. I calculate the total "probability of failure" of each path (a path >>> may have multiple links): Suppose my source node is "a" and destination >>> node is "b". I can setup a path between a to b via c or via d (a-c-b or >>> a-d-c): Here I will check the risk values of a-c and c-b; also risk values >>> of a-d and d-c. If the total risk valure of a-c-b is lower that risk value >>> of a-d-c, then I select the path a-c-d to setup the connection. (again risk >>> value = probability of failure) >>> >>> Now, I will first calculate the "total probability of failure" of each >>> link (using the file.txt) and since some links are repeated their values >>> will be added. The probabilities get added if a link is mentioned twice >>> or thrice. For example: link 3?5 is repeated 3 times: in line one, it >>> has a probability of failure as 0.03, in line two it is 0.11 and in line >>> three it is 0.04. So the probability of failure for link 3?5 is >>> 0.03+0.11+0.04 = 0.18 >>> >>> The length of each array will be same. You see the code I wrote: here >>> is the output for it: >>> >>> Links -> >>> >>> [('10', '13'), ('14', '18'), ('7', '8'), ('15', '20'), ('5', '8'), ('5', >>> '4'), ('11', '9'), ('21', '22'), ('12', '13'), ('21', '20'), ('17', '13'), >>> ('20', '21'), ('21', '16'), ('14', '10'), ('11', '12'), ('11', '19'), >>> ('14', '13'), ('3', '5'), ('11', '6'), ('19', '20')] >>> >>> >>> Probability -> >>> >>> [0.04, 0.06, 0.04, 0.24, 0.08, 0.15, 0.08, 0.27, 0.04, 0.29, 0.08, 0.27, >>> 0.27, 0.04, 0.08, 0.08, 0.08, 0.18000000000000002, 0.08, 0.24] >>> >>> >>> It means that link [10,13] has a "probability of failure" as [0.04] and >>> since the link [3-5] is repeated thrice with probability of 0.03, 0.11 and >>> 0.04, its "probability of failure" is [0.18] (third last element in the >>> Probability array). For some reason instead of 0.18 it is showing >>> 0.180000000002, which I cannot figure to why. >>> >>> >>> Please see the attached code. If you see the file.txt and my output: the >>> output is not displayed in sequence and that is what I need help with. I >>> want this to display : >>> >>> >>> Links = { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] >>> [10,13] [14,13] [17,13] [12,13] [11,6] [11,9] [11,12] [11,19] [19,20] >>> [15,20] [21,20] [20,21] [21,16] [21,22] } >>> >>> >>> Prob = {[0.18] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] [0.08] >>> [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} >>> >>> >>> If you can figure why the output is not generated in same sequence as in >>> the file.txt for me, it will be very helpful. >>> >>> >>> let me know if I explained correctly, and if you have any questions or >>> doubts? >>> >>> >>> On Sat, Apr 26, 2014 at 11:41 AM, Danny Yoo wrote: >>> >>>> >>> I want to create two arrays using the above file (Links array and >>>> Prob >>>> >>> array) that should give following output: >>>> >>> >>>> >>> *Links *= { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] >>>> >>> [10,13] [14,13] [17,13] [12,13] [11,6] [11,9][11,12] [11,19] [19,20] >>>> >>> [15,20] [21,20] [20,21] [21,16] [21,22] } >>>> >>> >>>> >>> *Prob *= {[0.28] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] >>>> [0.08] >>>> >>> [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} >>>> >> >>>> >> >>>> >> I don't understand how you develop this? The first list has 22 items >>>> the >>>> >> second 17. I would have expected them to be the same? >>>> > >>>> > >>>> > In the "Prob" array the elements are less because if you read the note >>>> > below: I said the links that are repeating for example [3,5] their >>>> > probabilities get added and stored as a single value in the "Prob" >>>> array. >>>> >>>> >>>> But what will you plan to do with these values afterwards? I think >>>> Alan's point here is that if there's no direct relationship between >>>> the elements in Links and the elements in Probs, those values aren't >>>> going to be very useful to solve the rest of the problem. >>>> >>>> >>>> One way to look at this problem is to simplify or normalize the input; >>>> the original structure in the file is slightly weird to process, since >>>> a single line of the input represents several link/failure pairs. >>>> >>>> One concrete example is: >>>> >>>> 4,10,13,14,13,17,13,12,13,0.04 >>>> >>>> where all these numbers are uninterpreted. >>>> >>>> You can imagine something that takes the line above, and breaks it >>>> down into a series of LinkFailure items. >>>> >>>> ###### >>>> class LinkFailure(object): >>>> """Represents a link and the probability of failure.""" >>>> def __init__(self, start, end, failure): >>>> self.start = start >>>> self.end = end >>>> self.failure = failure >>>> ###### >>>> >>>> which represent a link and failure structure. If we have a structure >>>> like this, then it explicitly represents a relationship between a link >>>> and its failure, and the string line: >>>> >>>> 4,10,13,14,13,17,13,12,13,0.04 >>>> >>>> can be distilled and represented as a collection of LinkFailure >>>> instances: >>>> >>>> [LinkFailure(10, 13, 0.04), LinkFailure(14, 13, 0.04), >>>> LinkFailure(17, 13, 0.04), LinkFailure(12, 13, 0.04)] >>>> >>>> Then the relationship is explicit. >>>> >>>> >>>> >> Also why do you want these two lists? What do you plan on doing with >>>> them? >>>> >> I would have thought a mapping of link to probability would be much >>>> more >>>> >> useful? (mapping => dictionary) >>>> > >>>> > >>>> > I want these two lists because using the links and probs array, I >>>> will check >>>> > which link has the lowest probability. Here lowest probability means >>>> the >>>> > risk of failure for that link. So based on which link has least >>>> probability >>>> > of failure, I will use it to setup a connection (I have a source and >>>> > destination and the links mentioned above are the paths between them) >>>> I want >>>> > to select the path which has least failure probability. >>>> > >>>> > Did it make sense? >>>> >>>> >>>> Unfortunately, I'm still confused. If you just have the Links and the >>>> Probs lists of unequal length, unless there's some additional >>>> information that you're represented, then I don't see the necessary >>>> connection between the two lists that lets you go any further in the >>>> problem. There's no one-to-one-ness: given a link in Links, which >>>> Probs do you want to look at? If you don't represent that linkage in >>>> some way, I don't understand yet where you go next. >>>> >>>> >>>> >>> So the first element in Links array is [3,5] and its probability of >>>> >>> failure is the first element in Prob array i.e. 0.28 >>>> >>>> But if the two lists are different lengths, what probability of >>>> failure is associates with the last element in the Prob array? >>>> >>>> >>>> The representation of data is important: if you choose an awkward >>>> representation, it makes solving this problem more difficult than it >>>> needs be. That is, if you're already compressing multiple elements in >>>> Prob that correspond to the same link, you also need some way to >>>> figure out what link that a compressed probability refer to. >>>> Otherwise, you don't have enough information to solve the problem >>>> anymore. >>>> >>> >>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> https://mail.python.org/mailman/listinfo/tutor >>> >>> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From illusiontechniques at gmail.com Sat Apr 26 21:59:13 2014 From: illusiontechniques at gmail.com (C Smith) Date: Sat, 26 Apr 2014 15:59:13 -0400 Subject: [Tutor] Help needed In-Reply-To: References: Message-ID: As others have pointed out, a mapping/dictionary or just a list of lists seems like how you would want to organize the data for input. I think your problem is insistence on using sets. I am no Python guru, but I think this list is more for exploratory learning of Python. I think people are trying to get you to answer your own questions or coax more information from you rather than simply provide their own version of your code. On Sat, Apr 26, 2014 at 3:48 PM, Suhana Vidyarthi wrote: > Thanks for the response Smith, I was thinking make be I have done > something incorrect and if there is some other function that can be used to > display the output in desired order but don't see it possible thats why was > wondering if any of you Python gurus have any inputs for me :-) > > > > On Sat, Apr 26, 2014 at 12:36 PM, C Smith wrote: > >> err, set also is unordered. I can see you are using set for a reason, but >> has no concept of order. >> >> >> On Sat, Apr 26, 2014 at 3:20 PM, C Smith wrote: >> >>> Just glancing at your work, I see you have curly braces around what >>> looks like it should be a list. If you are concerned with the order of your >>> output, dictionaries do not have a concept of order. >>> >>> >>> On Sat, Apr 26, 2014 at 3:16 PM, Suhana Vidyarthi < >>> suhanavidyarthi at gmail.com> wrote: >>> >>>> Hi Danny, >>>> >>>> Let me give you a high level brief of what I am doing: >>>> I am working on doing "disaster aware routing" considering the 24-node >>>> US network where I will be setting up connection between two any two nodes >>>> (I will select the source and destination nodes randomly). Also I have some >>>> links whose "probability of failure" is mentioned in the attached file. >>>> Other links, which are not mentioned in the file - we suppose their >>>> "probability of failure" is zero. So between the source-destination nodes, >>>> there will be multiple paths and I will select the one which has "least >>>> probability of failure". >>>> >>>> Now to setup the connection between two nodes, I have to select a path >>>> whose "probability of failure" is least. To do that first I will calculate >>>> the risk of each path from the attached file and then select the path with >>>> least risk value. Did you get this part? I know it can be a bit confusing. >>>> >>>> Now I break the problem into parts: >>>> >>>> 1. I have to topology of the 24-node map >>>> 2. I have the link values of each link - where risk values are the >>>> "probability of failure" >>>> 3. I calculate the total "probability of failure" of each path (a path >>>> may have multiple links): Suppose my source node is "a" and destination >>>> node is "b". I can setup a path between a to b via c or via d (a-c-b or >>>> a-d-c): Here I will check the risk values of a-c and c-b; also risk values >>>> of a-d and d-c. If the total risk valure of a-c-b is lower that risk value >>>> of a-d-c, then I select the path a-c-d to setup the connection. (again risk >>>> value = probability of failure) >>>> >>>> Now, I will first calculate the "total probability of failure" of each >>>> link (using the file.txt) and since some links are repeated their values >>>> will be added. The probabilities get added if a link is mentioned >>>> twice or thrice. For example: link 3?5 is repeated 3 times: in line >>>> one, it has a probability of failure as 0.03, in line two it is 0.11 and in >>>> line three it is 0.04. So the probability of failure for link 3?5 is >>>> 0.03+0.11+0.04 = 0.18 >>>> >>>> The length of each array will be same. You see the code I wrote: here >>>> is the output for it: >>>> >>>> Links -> >>>> >>>> [('10', '13'), ('14', '18'), ('7', '8'), ('15', '20'), ('5', '8'), >>>> ('5', '4'), ('11', '9'), ('21', '22'), ('12', '13'), ('21', '20'), ('17', >>>> '13'), ('20', '21'), ('21', '16'), ('14', '10'), ('11', '12'), ('11', >>>> '19'), ('14', '13'), ('3', '5'), ('11', '6'), ('19', '20')] >>>> >>>> >>>> Probability -> >>>> >>>> [0.04, 0.06, 0.04, 0.24, 0.08, 0.15, 0.08, 0.27, 0.04, 0.29, 0.08, >>>> 0.27, 0.27, 0.04, 0.08, 0.08, 0.08, 0.18000000000000002, 0.08, 0.24] >>>> >>>> >>>> It means that link [10,13] has a "probability of failure" as [0.04] and >>>> since the link [3-5] is repeated thrice with probability of 0.03, 0.11 and >>>> 0.04, its "probability of failure" is [0.18] (third last element in the >>>> Probability array). For some reason instead of 0.18 it is showing >>>> 0.180000000002, which I cannot figure to why. >>>> >>>> >>>> Please see the attached code. If you see the file.txt and my output: >>>> the output is not displayed in sequence and that is what I need help with. >>>> I want this to display : >>>> >>>> >>>> Links = { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] >>>> [10,13] [14,13] [17,13] [12,13] [11,6] [11,9] [11,12] [11,19] [19,20] >>>> [15,20] [21,20] [20,21] [21,16] [21,22] } >>>> >>>> >>>> Prob = {[0.18] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] [0.08] >>>> [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} >>>> >>>> >>>> If you can figure why the output is not generated in same sequence as >>>> in the file.txt for me, it will be very helpful. >>>> >>>> >>>> let me know if I explained correctly, and if you have any questions or >>>> doubts? >>>> >>>> >>>> On Sat, Apr 26, 2014 at 11:41 AM, Danny Yoo wrote: >>>> >>>>> >>> I want to create two arrays using the above file (Links array and >>>>> Prob >>>>> >>> array) that should give following output: >>>>> >>> >>>>> >>> *Links *= { [3,5] [5,4] [5,8] [7,8] [14,10] [14,13] [17,13] [14,18] >>>>> >>> [10,13] [14,13] [17,13] [12,13] [11,6] [11,9][11,12] [11,19] >>>>> [19,20] >>>>> >>> [15,20] [21,20] [20,21] [21,16] [21,22] } >>>>> >>> >>>>> >>> *Prob *= {[0.28] [0.15] [0.08] [0.04] [0.04] [0.04] [0.08] [0.04] >>>>> [0.08] >>>>> >>> [0.08] [0.08] [0.08] [0.24] [0.24] [0.34] [0.27] [0.27]} >>>>> >> >>>>> >> >>>>> >> I don't understand how you develop this? The first list has 22 >>>>> items the >>>>> >> second 17. I would have expected them to be the same? >>>>> > >>>>> > >>>>> > In the "Prob" array the elements are less because if you read the >>>>> note >>>>> > below: I said the links that are repeating for example [3,5] their >>>>> > probabilities get added and stored as a single value in the "Prob" >>>>> array. >>>>> >>>>> >>>>> But what will you plan to do with these values afterwards? I think >>>>> Alan's point here is that if there's no direct relationship between >>>>> the elements in Links and the elements in Probs, those values aren't >>>>> going to be very useful to solve the rest of the problem. >>>>> >>>>> >>>>> One way to look at this problem is to simplify or normalize the input; >>>>> the original structure in the file is slightly weird to process, since >>>>> a single line of the input represents several link/failure pairs. >>>>> >>>>> One concrete example is: >>>>> >>>>> 4,10,13,14,13,17,13,12,13,0.04 >>>>> >>>>> where all these numbers are uninterpreted. >>>>> >>>>> You can imagine something that takes the line above, and breaks it >>>>> down into a series of LinkFailure items. >>>>> >>>>> ###### >>>>> class LinkFailure(object): >>>>> """Represents a link and the probability of failure.""" >>>>> def __init__(self, start, end, failure): >>>>> self.start = start >>>>> self.end = end >>>>> self.failure = failure >>>>> ###### >>>>> >>>>> which represent a link and failure structure. If we have a structure >>>>> like this, then it explicitly represents a relationship between a link >>>>> and its failure, and the string line: >>>>> >>>>> 4,10,13,14,13,17,13,12,13,0.04 >>>>> >>>>> can be distilled and represented as a collection of LinkFailure >>>>> instances: >>>>> >>>>> [LinkFailure(10, 13, 0.04), LinkFailure(14, 13, 0.04), >>>>> LinkFailure(17, 13, 0.04), LinkFailure(12, 13, 0.04)] >>>>> >>>>> Then the relationship is explicit. >>>>> >>>>> >>>>> >> Also why do you want these two lists? What do you plan on doing >>>>> with them? >>>>> >> I would have thought a mapping of link to probability would be much >>>>> more >>>>> >> useful? (mapping => dictionary) >>>>> > >>>>> > >>>>> > I want these two lists because using the links and probs array, I >>>>> will check >>>>> > which link has the lowest probability. Here lowest probability means >>>>> the >>>>> > risk of failure for that link. So based on which link has least >>>>> probability >>>>> > of failure, I will use it to setup a connection (I have a source and >>>>> > destination and the links mentioned above are the paths between >>>>> them) I want >>>>> > to select the path which has least failure probability. >>>>> > >>>>> > Did it make sense? >>>>> >>>>> >>>>> Unfortunately, I'm still confused. If you just have the Links and the >>>>> Probs lists of unequal length, unless there's some additional >>>>> information that you're represented, then I don't see the necessary >>>>> connection between the two lists that lets you go any further in the >>>>> problem. There's no one-to-one-ness: given a link in Links, which >>>>> Probs do you want to look at? If you don't represent that linkage in >>>>> some way, I don't understand yet where you go next. >>>>> >>>>> >>>>> >>> So the first element in Links array is [3,5] and its probability of >>>>> >>> failure is the first element in Prob array i.e. 0.28 >>>>> >>>>> But if the two lists are different lengths, what probability of >>>>> failure is associates with the last element in the Prob array? >>>>> >>>>> >>>>> The representation of data is important: if you choose an awkward >>>>> representation, it makes solving this problem more difficult than it >>>>> needs be. That is, if you're already compressing multiple elements in >>>>> Prob that correspond to the same link, you also need some way to >>>>> figure out what link that a compressed probability refer to. >>>>> Otherwise, you don't have enough information to solve the problem >>>>> anymore. >>>>> >>>> >>>> >>>> _______________________________________________ >>>> Tutor maillist - Tutor at python.org >>>> To unsubscribe or change subscription options: >>>> https://mail.python.org/mailman/listinfo/tutor >>>> >>>> >>> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jsmallwood82 at yahoo.com Sun Apr 27 00:53:33 2014 From: jsmallwood82 at yahoo.com (jordan smallwood) Date: Sat, 26 Apr 2014 15:53:33 -0700 (PDT) Subject: [Tutor] New to Python Message-ID: <1398552813.94073.YahooMailNeo@web162703.mail.bf1.yahoo.com> Hello, I am new to Python. I mean completely new and we're working on this problem set in class where they give us specs and we have to build something based off these specs. I have no idea what they're asking. Could someone help get me started on the path to figuring this out? Below is the question: 1. Write a program module with at least two functions. Follow this specifi- cation exactly for these two functions: 1. (a) ?One function,?CalculateCentimeters, receives a value in inches and returns the equivalent value in centimeters. centimeters?=2.54?inches 2. (b) ?The other function,?CalculateInches?receives a value in centime- ters and returns the equivalent value in inches.?inches?=centimeters/2.54 ... but you don?t 2.54 in your code 2 times. It?s a good candidate to be a module-level constant. Specified instructions about the internals of code, i.e., the names of your functions and how they behave, is called theinternal specification. It tells you, the author of the function, as well as programmers who call your function, how to call it and what to expect when it is called. You must following them?exactly?or call a meeting of your programming team because your choices here affect the others. For this exercise, you design the rest of the functions for your program, but be careful to keep all the code in functions. Invent and arrange functions as you wish to ask the user for: (a) a value (b) a unit of measure and call the appropriate function to print out the value in the other unit of measure. Specified instructions about the user?s view of your code (like just given) is called the?external specification. Often the paying customer gives these directions so you must follow them exactly, doing things in the order given; but in this case, the internal design is up to you.? -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Mon Apr 28 10:57:20 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 28 Apr 2014 09:57:20 +0100 Subject: [Tutor] New to Python In-Reply-To: <1398552813.94073.YahooMailNeo@web162703.mail.bf1.yahoo.com> References: <1398552813.94073.YahooMailNeo@web162703.mail.bf1.yahoo.com> Message-ID: On 26/04/2014 23:53, jordan smallwood wrote: > Hello, > > I am new to Python. I mean completely new and we're working on this > problem set in class where they give us specs and we have to build > something based off these specs. I have no idea what they're asking. > Could someone help get me started on the path to figuring this out? > > Below is the question: > > 1. Write a program module with at least two functions. Follow this > specifi- cation exactly for these two functions: > > 1. > (a) One function, CalculateCentimeters, receives a value in inches > and returns the equivalent value in centimeters. > centimeters =2.54?inches > 2. > (b) The other function, CalculateInches receives a value in centime- > ters and returns the equivalent value in inches. inches > =centimeters/2.54 > > ... but you don?t 2.54 in your code 2 times. It?s a good candidate to be > a module-level constant. > Specified instructions about the internals of code, i.e., the names of > your functions and how they behave, is called theinternal specification. > It tells you, the author of the function, as well as programmers who > call your function, how to call it and what to expect when it is called. > You must following them exactly or call a meeting of your programming > team because your choices here affect the others. > For this exercise, you design the rest of the functions for your > program, but be careful to keep all the code in functions. > Invent and arrange functions as you wish to ask the user for: > (a) a value > (b) a unit of measure > and call the appropriate function to print out the value in the other > unit of measure. > > Specified instructions about the user?s view of your code (like just > given) is called the external specification. Often the paying customer > gives these directions so you must follow them exactly, doing things in > the order given; but in this case, the internal design is up to you. > What did you not understand about the answers you received to your original question "Help with an assignment"? Or if you could not be bothered to reply to either Alan Gauld or Dave Angel, why should we now waste our time attempting to help you? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From amonroe at columbus.rr.com Mon Apr 28 15:41:00 2014 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Mon, 28 Apr 2014 09:41:00 -0400 Subject: [Tutor] New to Python In-Reply-To: <1398552813.94073.YahooMailNeo@web162703.mail.bf1.yahoo.com> References: <1398552813.94073.YahooMailNeo@web162703.mail.bf1.yahoo.com> Message-ID: <1233780576.20140428094100@columbus.rr.com> > 1. Write a program module with at least two functions. Hint: "def" is the Python keyword used to define a function. You can read all about def in the docs on python.org. Alan From gb.gabrielebrambilla at gmail.com Mon Apr 28 17:13:10 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Mon, 28 Apr 2014 11:13:10 -0400 Subject: [Tutor] converting strings to float: strange case Message-ID: Hi, I'm trying to convert a string to a float. It seems a basic thing but I don't know why I'm getting this erroris Traceback (most recent call last): File "phresREADER.py", line 27, in tra = float(stri) ValueError: could not convert string to float: My file has this line 5.50000e+000 5.50000e+001 5.50000e+002 5.50000e+003 my code is: my_line = f.readline() avg_energySTR = [str(i) for i in my_line.split(' ')] for stri in avg_energySTR: tra = float(stri) do you have any idea? thanks Gabriele -------------- next part -------------- An HTML attachment was scrubbed... URL: From gb.gabrielebrambilla at gmail.com Mon Apr 28 17:22:35 2014 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Mon, 28 Apr 2014 11:22:35 -0400 Subject: [Tutor] converting strings to float: strange case In-Reply-To: References: Message-ID: solved, sorry for the disturb http://stackoverflow.com/questions/23344345/strings-not-converting-to-float-as-expected/23344830#23344830 bye Gabriele 2014-04-28 11:13 GMT-04:00 Gabriele Brambilla < gb.gabrielebrambilla at gmail.com>: > Hi, > > I'm trying to convert a string to a float. It seems a basic thing but I > don't know why I'm getting this erroris > > Traceback (most recent call last): > File "phresREADER.py", line 27, in > tra = float(stri) > ValueError: could not convert string to float: > > My file has this line > > 5.50000e+000 5.50000e+001 5.50000e+002 5.50000e+003 > > my code is: > > my_line = f.readline() > avg_energySTR = [str(i) for i in my_line.split(' ')] > for stri in avg_energySTR: > tra = float(stri) > > do you have any idea? > > thanks > > Gabriele > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Mon Apr 28 17:25:59 2014 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 28 Apr 2014 11:25:59 -0400 Subject: [Tutor] converting strings to float: strange case In-Reply-To: References: Message-ID: On Apr 28, 2014 11:14 AM, "Gabriele Brambilla" < gb.gabrielebrambilla at gmail.com> wrote: > > Hi, > > I'm trying to convert a string to a float. It seems a basic thing but I don't know why I'm getting this erroris > > Traceback (most recent call last): > File "phresREADER.py", line 27, in > tra = float(stri) > ValueError: could not convert string to float: > > My file has this line > > 5.50000e+000 5.50000e+001 5.50000e+002 5.50000e+003 > > my code is: > > my_line = f.readline() > avg_energySTR = [str(i) for i in my_line.split(' ')] > for stri in avg_energySTR: > tra = float(stri) Works for me. Try printing art in the loop > > do you have any idea? > > thanks > > Gabriele > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From illusiontechniques at gmail.com Mon Apr 28 17:26:41 2014 From: illusiontechniques at gmail.com (C Smith) Date: Mon, 28 Apr 2014 11:26:41 -0400 Subject: [Tutor] converting strings to float: strange case In-Reply-To: References: Message-ID: .split() will split things based on whitespace or newlines. Do you know that your file is only going to contain things that should convert to floats? If you post your entire code, the error you have included will be more helpful as it points to a certain line. The last line in your code has (stri) should be (str). On Mon, Apr 28, 2014 at 11:13 AM, Gabriele Brambilla < gb.gabrielebrambilla at gmail.com> wrote: > Hi, > > I'm trying to convert a string to a float. It seems a basic thing but I > don't know why I'm getting this erroris > > Traceback (most recent call last): > File "phresREADER.py", line 27, in > tra = float(stri) > ValueError: could not convert string to float: > > My file has this line > > 5.50000e+000 5.50000e+001 5.50000e+002 5.50000e+003 > > my code is: > > my_line = f.readline() > avg_energySTR = [str(i) for i in my_line.split(' ')] > for stri in avg_energySTR: > tra = float(stri) > > do you have any idea? > > thanks > > Gabriele > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mik.stephen at yahoo.com Mon Apr 28 19:32:06 2014 From: mik.stephen at yahoo.com (Stephen Mik) Date: Mon, 28 Apr 2014 10:32:06 -0700 (PDT) Subject: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected Message-ID: <1398706326.78128.YahooMailNeo@web124706.mail.ne1.yahoo.com> Dear Python Tutor: ??? Well,I am Stephen Mik,and I'm a beginning,rookie programmer who is just trying to get a class Assignment going. The instructor of my class does not accept email and she is not on Campus on Monday. So,my only recourse is to turn to Python Tutor for assistance. ??? My program, Assignment4,does run partially. You can see the results of the Python Shell attached to this email. I also have included part of my code for your perusal. ??? I must be doing something very wrong. The program is supposed to run a main loop ,for control of the program. The program DOES print out the prompts before the While Loop, but when it comes to a variable named"smv_guessNumber" the program DOES NOT prompt for the input for "smv_guessNumber" as it should. It is a mystery to me as to why the program will not get to the "smv_guessNumber=int(input("Think out a first guess:")". I am mystified why it doesn't reach that point in the program! Can anyone please help? I have attached the Python Traceback Error Output,which shows that at least part of the program IS working. I also have attached part of the code for the Assignment 4 which should help in the debugging.I need help ASAP,another program is due very soon and I have not even worked out the pseudocode for it yet! CONCERNED,Stephen W. Mik -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: assignment4-fragment1.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: PythAsst4Error.py URL: From illusiontechniques at gmail.com Mon Apr 28 19:49:31 2014 From: illusiontechniques at gmail.com (C Smith) Date: Mon, 28 Apr 2014 13:49:31 -0400 Subject: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected In-Reply-To: <1398706326.78128.YahooMailNeo@web124706.mail.ne1.yahoo.com> References: <1398706326.78128.YahooMailNeo@web124706.mail.ne1.yahoo.com> Message-ID: That is definitely more useful information in answering your questions. Whenever you see the error you are getting: NameError: name 'smv_guessNumber' is not defined That means you are using a variable, in this case 'smv_guessNumber', that has not been created yet. The reason this is happening here is you need to import sys. On Mon, Apr 28, 2014 at 1:32 PM, Stephen Mik < mik.stephen at yahoo.com.dmarc.invalid> wrote: > Dear Python Tutor: > Well,I am Stephen Mik,and I'm a beginning,rookie programmer who is > just trying to get a class Assignment going. The instructor of my class > does not accept email and she is not on Campus on Monday. So,my only > recourse is to turn to Python Tutor for assistance. > > My program, Assignment4,does run partially. You can see the results of > the Python Shell attached to this email. I also have included part of my > code for your perusal. > > I must be doing something very wrong. The program is supposed to run a > main loop ,for control of the program. The program DOES print out the > prompts before the While Loop, but when it comes to a variable > named"smv_guessNumber" the program DOES NOT prompt for the input for > "smv_guessNumber" as it should. It is a mystery to me as to why the program > will not get to the "smv_guessNumber=int(input("Think out a first > guess:")". I am mystified why it doesn't reach that point in the program! > Can anyone please help? I have attached the Python Traceback Error > Output,which shows that at least part of the program IS working. I also > have attached part of the code for the Assignment 4 which should help in > the debugging.I need help ASAP,another program is due very soon and I have > not even worked out the pseudocode for it yet! > CONCERNED,Stephen W. Mik > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From illusiontechniques at gmail.com Mon Apr 28 19:50:56 2014 From: illusiontechniques at gmail.com (C Smith) Date: Mon, 28 Apr 2014 13:50:56 -0400 Subject: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected In-Reply-To: References: <1398706326.78128.YahooMailNeo@web124706.mail.ne1.yahoo.com> Message-ID: I should probably clarify that this list is mainly for python2.7, correct me if I am wrong. On Mon, Apr 28, 2014 at 1:49 PM, C Smith wrote: > That is definitely more useful information in answering your questions. > Whenever you see the error you are getting: > > NameError: name 'smv_guessNumber' is not defined > > That means you are using a variable, in this case 'smv_guessNumber', that > has not been created yet. > > The reason this is happening here is you need to import sys. > > > On Mon, Apr 28, 2014 at 1:32 PM, Stephen Mik < > mik.stephen at yahoo.com.dmarc.invalid> wrote: > >> Dear Python Tutor: >> Well,I am Stephen Mik,and I'm a beginning,rookie programmer who is >> just trying to get a class Assignment going. The instructor of my class >> does not accept email and she is not on Campus on Monday. So,my only >> recourse is to turn to Python Tutor for assistance. >> >> My program, Assignment4,does run partially. You can see the results >> of the Python Shell attached to this email. I also have included part of my >> code for your perusal. >> >> I must be doing something very wrong. The program is supposed to run >> a main loop ,for control of the program. The program DOES print out the >> prompts before the While Loop, but when it comes to a variable >> named"smv_guessNumber" the program DOES NOT prompt for the input for >> "smv_guessNumber" as it should. It is a mystery to me as to why the program >> will not get to the "smv_guessNumber=int(input("Think out a first >> guess:")". I am mystified why it doesn't reach that point in the program! >> Can anyone please help? I have attached the Python Traceback Error >> Output,which shows that at least part of the program IS working. I also >> have attached part of the code for the Assignment 4 which should help in >> the debugging.I need help ASAP,another program is due very soon and I have >> not even worked out the pseudocode for it yet! >> CONCERNED,Stephen W. Mik >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Mon Apr 28 19:52:17 2014 From: __peter__ at web.de (Peter Otten) Date: Mon, 28 Apr 2014 19:52:17 +0200 Subject: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected References: <1398706326.78128.YahooMailNeo@web124706.mail.ne1.yahoo.com> Message-ID: Stephen Mik wrote: > My program, Assignment4,does run partially. You can see the results of the > Python Shell attached to this email. I also have included part of my code > for your perusal. > > I must be doing something very wrong. The program is supposed to run a > main loop ,for control of the program. The program DOES print out the > prompts before the While Loop, but when it comes to a variable > named"smv_guessNumber" the program DOES NOT prompt for the input for > "smv_guessNumber" as it should. It is a mystery to me as to why the > program will not get to the "smv_guessNumber=int(input("Think out a first > guess:")". > smv_grandCounter=int(input("Enter a 1 to play or 0 to exit: ")) > > while(smv_grandCounter=="1"): Hint: >>> 1 == "1" False From illusiontechniques at gmail.com Mon Apr 28 19:59:34 2014 From: illusiontechniques at gmail.com (C Smith) Date: Mon, 28 Apr 2014 13:59:34 -0400 Subject: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected In-Reply-To: References: <1398706326.78128.YahooMailNeo@web124706.mail.ne1.yahoo.com> Message-ID: The reason this is happening here is you need to import sys. > I don't know why you would think importing sys would fix this. docs say it accepts from sys.stdin On Mon, Apr 28, 2014 at 1:55 PM, Philip Dexter wrote: > > > On Mon, 28 Apr 2014, C Smith wrote: > > I should probably clarify that this list is mainly for python2.7, correct >> me if I am wrong. >> > > I don't think that is true. > > > On Mon, Apr 28, 2014 at 1:49 PM, C Smith >> wrote: >> > > > > The reason this is happening here is you need to import sys. >> > > I don't know why you would think importing sys would fix this. > > On Mon, Apr 28, 2014 at 1:32 PM, Stephen Mik >> wrote: >> I must be doing something very wrong. The program is supposed to run >> a main loop ,for control of the program. The program DOES print out the >> prompts >> before the While Loop, but when it comes to a variable >> named"smv_guessNumber" the program DOES NOT prompt for the input for >> "smv_guessNumber" as it should. It >> is a mystery to me as to why the program will not get to the >> "smv_guessNumber=int(input("Think out a first guess:")". I am mystified >> why it doesn't reach that >> point in the program! Can anyone please help? I have attached the Python >> Traceback Error Output,which shows that at least part of the program IS >> working. I >> also have attached part of the code for the Assignment 4 which should >> help in the debugging.I need help ASAP,another program is due very soon and >> I have not >> even worked out the pseudocode for it yet! >> CONCERNED,Stephen W. Mik >> > > Your first while loop is not running. You convert smv_grandCounter to > an int but compare it with a string. -------------- next part -------------- An HTML attachment was scrubbed... URL: From illusiontechniques at gmail.com Mon Apr 28 20:04:41 2014 From: illusiontechniques at gmail.com (C Smith) Date: Mon, 28 Apr 2014 14:04:41 -0400 Subject: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected In-Reply-To: References: <1398706326.78128.YahooMailNeo@web124706.mail.ne1.yahoo.com> Message-ID: > > The reason this is happening here is you need to import sys. > I don't know why you would think importing sys would fix this. docs say it accepts from sys.stdin I now see that it is not necessary to import sys, although I am not sure why. On Mon, Apr 28, 2014 at 1:59 PM, C Smith wrote: > > > The reason this is happening here is you need to import sys. >> > > I don't know why you would think importing sys would fix this. > > docs say it accepts from sys.stdin > > > On Mon, Apr 28, 2014 at 1:55 PM, Philip Dexter wrote: > >> >> >> On Mon, 28 Apr 2014, C Smith wrote: >> >> I should probably clarify that this list is mainly for python2.7, >>> correct me if I am wrong. >>> >> >> I don't think that is true. >> >> >> On Mon, Apr 28, 2014 at 1:49 PM, C Smith >>> wrote: >>> >> >> >> >> The reason this is happening here is you need to import sys. >>> >> >> I don't know why you would think importing sys would fix this. >> >> On Mon, Apr 28, 2014 at 1:32 PM, Stephen Mik >>> wrote: >>> I must be doing something very wrong. The program is supposed to run >>> a main loop ,for control of the program. The program DOES print out the >>> prompts >>> before the While Loop, but when it comes to a variable >>> named"smv_guessNumber" the program DOES NOT prompt for the input for >>> "smv_guessNumber" as it should. It >>> is a mystery to me as to why the program will not get to the >>> "smv_guessNumber=int(input("Think out a first guess:")". I am mystified >>> why it doesn't reach that >>> point in the program! Can anyone please help? I have attached the Python >>> Traceback Error Output,which shows that at least part of the program IS >>> working. I >>> also have attached part of the code for the Assignment 4 which should >>> help in the debugging.I need help ASAP,another program is due very soon and >>> I have not >>> even worked out the pseudocode for it yet! >>> CONCERNED,Stephen W. Mik >>> >> >> Your first while loop is not running. You convert smv_grandCounter to >> an int but compare it with a string. > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon Apr 28 20:20:30 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 29 Apr 2014 04:20:30 +1000 Subject: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected In-Reply-To: <1398706326.78128.YahooMailNeo@web124706.mail.ne1.yahoo.com> References: <1398706326.78128.YahooMailNeo@web124706.mail.ne1.yahoo.com> Message-ID: <20140428182029.GK4273@ando> On Mon, Apr 28, 2014 at 10:32:06AM -0700, Stephen Mik wrote: > ??? I must be doing something very wrong. The program is supposed to > run a main loop ,for control of the program. The program DOES print > out the prompts before the While Loop, but when it comes to a variable > named"smv_guessNumber" the program DOES NOT prompt for the input for > "smv_guessNumber" as it should. It is a mystery to me as to why the > program will not get to the "smv_guessNumber=int(input("Think out a > first guess:")". I am mystified why it doesn't reach that point in the > program! That was a tricky one! It took me a while to see it, but the problem comes from three factors. Firstly, these two lines: smv_grandCounter=int(input("Enter a 1 to play or 0 to exit: ")) while(smv_grandCounter=="1"): In the first line, you get input from the user, either "1" or "0". User input is a string, then you convert to an int. But in the second line, you compare it to the string "1". So regardless of whether the user types "1" or "0", the while loop is ALWAYS skipped: int 1 == "1" returns False int 0 == "1" returns False and the while loop never runs at all. I recommend you either remove the call to int(), and keep smv_grandCounter as a string, or you change the while condition to while smv_grandCounter == 1: (But don't do both!) Secondly, since the while loop is skipped, the line which initialises the smv_guessNumber variable: smv_guessNumber= int(input("Take a guess!")) also gets skipped. So smv_guessNumber doesn't get a value. Thirdly, you haven't indented the "Number Identification Loop". The game logic *should* be: # Game loop while you want to play a game: set up the next game # Number Identification Loop while your guess is not equal to the number: ... The number identification loop only happens while you want to play a game. But you have it like this by mistake: # Game loop while you want to play a game: set up the next game # At this point, the game loop has finished, # so you no longer wish to play # Now you start the Number Identification Loop while your guess is not equal to the number: ... You need to take this line: while(smv_guessNumber!=smv_pickNumber): and all the code which belongs to it, and indent it one extra block, so it is considered *inside* the "do you want to play a game?" loop. Once you have fixed those issues, you can then continue your testing. By the way, what is the meaning of the mysterious "smv_" prefixes on all your variables? Please don't tell me that stands for "Stephen Mik Variable." It seems to me that *every* variable has the same smv_ prefix, and so that prefix doesn't have any meaning. It would be like me deciding to add "blah" to the beginning of every word: blahit blahwould blahbe blahlike blahme blahdeciding blahto blahadd "blahblah" blahto blahthe blahbeginning blahof blahevery blahword The "blah"s are just meaningless noise. When programming, your code should all carry its weight. Programming is hard enough without sticking "blah" at the beginning of every word! Variable names should describe what the variable stands for, or at least follow some common convention like "x" for mathematical quantities. If "smv_" doesn't carry it's weight in helping your code be more easily understood, you should remove it. -- Steven From steve at pearwood.info Mon Apr 28 20:26:30 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 29 Apr 2014 04:26:30 +1000 Subject: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected In-Reply-To: References: <1398706326.78128.YahooMailNeo@web124706.mail.ne1.yahoo.com> Message-ID: <20140428182630.GL4273@ando> On Mon, Apr 28, 2014 at 01:50:56PM -0400, C Smith wrote: > I should probably clarify that this list is mainly for python2.7, correct > me if I am wrong. Nope, any version of Python. If anyone is silly enough to be using Python 0.9 (which is over 20 years old!) I can try to answer their questions. The answer will nearly always be "don't use that version, it is ancient!!!" but still it will be an answer :-) More seriously, anything from Python 2.4 to 3.4 is still under commercial or free support. Many questions, especially beginner questions, don't usually depend on the version, also sometimes they do. Most programming errors are errors regardless of the version: x = 1 + "2" # is an error in every version of Python -- Steven From steve at pearwood.info Mon Apr 28 20:29:38 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 29 Apr 2014 04:29:38 +1000 Subject: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected In-Reply-To: References: <1398706326.78128.YahooMailNeo@web124706.mail.ne1.yahoo.com> Message-ID: <20140428182938.GM4273@ando> On Mon, Apr 28, 2014 at 01:49:31PM -0400, C Smith wrote: > That is definitely more useful information in answering your questions. > Whenever you see the error you are getting: > > NameError: name 'smv_guessNumber' is not defined > > That means you are using a variable, in this case 'smv_guessNumber', that > has not been created yet. So far so good. But your next comment: > The reason this is happening here is you need to import sys. Not so much. This has nothing to do with importing sys. If it were, the error would likely have been: NameError: name 'sys' is not defined -- Steven From dyoo at hashcollision.org Mon Apr 28 20:35:19 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 28 Apr 2014 11:35:19 -0700 Subject: [Tutor] New to Python In-Reply-To: <1398552813.94073.YahooMailNeo@web162703.mail.bf1.yahoo.com> References: <1398552813.94073.YahooMailNeo@web162703.mail.bf1.yahoo.com> Message-ID: Hi Jordan, You probably want to read up to chapter 3 (including the "Functions" chapter) in "How to Think Like a Computer Scientist": http://www.greenteapress.com/thinkpython/html/index.html or some equivalent tutorial, so that you at least know what the terms in the problem statement means. In particular, you'll want a very concrete idea of what a _function_ is, because this assignment is all how to write and use functions. That's the core takeaway from the problem, so learn about functions. Are you using a particular book or material in your learning? If so, mention that. Maybe one of us here has also read the same book and can point out things for you to look at. Also are there particular terms in the problem statement that are confusing? If so, point them out, and one of us here on Tutor can probably help. Personally, I would actually treat this problem in at least two steps. You don't have to get your whole program perfect the first time. The first paragraph which says "Write a program module..." up to the description talking about CalculateInches(). But I would not initially follow any of the paragraph material after "... but you don't...". Get the two functions working first. Learn how to test and run those functions first. If you have questions on how to do so, ask. Ignore the advice about module level constants _until_ you've got the functions working ok. You'll be a better position to improve your solution to fit the final approach. Good luck to you! From taserian at gmail.com Mon Apr 28 20:45:47 2014 From: taserian at gmail.com (taserian) Date: Mon, 28 Apr 2014 14:45:47 -0400 Subject: [Tutor] Keeping change-in-place vs. copy methods straight Message-ID: I can't claim to be new to programming, but I've dabbled in Python over and over again to get small problems and puzzles resolved. One thing that I find I can't keep straight are the methods that change a list in place, vs. those that return a copy (sometimes transformed) of the list. Call me old-fashioned, but my programming experience mostly comes from languages where you assigned the output of a function to another variable, so you always had a copy of whatever you were working on. var array; sorted = array.sort(); If you didn't care to keep both copies, you could always re-assign the returned value to the original variable. array = array.sort(); If I try to do the same in Python: sorted = arrayList.sort() sorted comes back as None, while arrayList has changed its order. Is there some sort of rule-of-thumb to determine if a function is in-place or returns a value? Antonio Rodriguez -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon Apr 28 20:52:08 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 29 Apr 2014 04:52:08 +1000 Subject: [Tutor] New to Python In-Reply-To: <1398552813.94073.YahooMailNeo@web162703.mail.bf1.yahoo.com> References: <1398552813.94073.YahooMailNeo@web162703.mail.bf1.yahoo.com> Message-ID: <20140428185207.GN4273@ando> On Sat, Apr 26, 2014 at 03:53:33PM -0700, jordan smallwood wrote: > Hello, > > I am new to Python. I mean completely new and we're working on this > problem set in class where they give us specs and we have to build > something based off these specs. I have no idea what they're asking. > Could someone help get me started on the path to figuring this out? Yes. You need to write two functions, one to convert from inches to centimetres and one from centimetres to inches. You've learned about functions in maths class, I expect. This is similar. Here's how you write functions in Python. def double(x): """Return double x.""" return 2*x def half_plus_one(x): """Return half of x, plus 1.""" return x/2.0 + 1 The keyword "def" starts the definition of the function. It is followed by the name of the function, then inside round brackets (parentheses) is a list of the arguments that the function requires. In my examples, the function only takes one argument, x, which you can assume is a number. If possible, you should use a more descriptive name than "x". The next line, a string starting and ending with THREE quotation marks, is called a "doc string". It's just a short comment explaining what the function does. (It's also optional, but recommended.) Inside the function, all your code needs to be indented by one level. You should indent by either: Four spaces (this is recommended) One Tab (if you must) although any number of spaces is allowed, so long as it is consistent. Whatever you use, pick one, and use it for all indentation. Python will complain, or worse, do the wrong thing, if you have inconsistent indentation. (Say, four spaces on one line, then three on the next.) Both my functions are simple enough that I only have a single line. The keyword "return" tells Python what value should be returned. It should, I hope, be obvious that 2*x gives two times x. Is that enough to get you started? -- Steven From dyoo at hashcollision.org Mon Apr 28 22:55:18 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 28 Apr 2014 13:55:18 -0700 Subject: [Tutor] Keeping change-in-place vs. copy methods straight In-Reply-To: References: Message-ID: Hi Antonio, Unfortunately, I don't think it's apparent whether or not a function applies mutations or is a pure computation. In Python, those are by convention or documentation rather than part of the language. There are other programming languages can control the effects and scope of mutation, but Python is not one of those languages. In particular, I know from experience that list.sort() mutates the list, and so it doesn't return a useful return value. On the other hand, there's a separate built-in function called "sorted()" that can sort lists, and it does not mutate the original list. ################################### >>> lst = [3, 1, 4, 1, 5, 9, 2, 6] >>> lst2 = sorted(lst) >>> lst [3, 1, 4, 1, 5, 9, 2, 6] >>> lst2 [1, 1, 2, 3, 4, 5, 6, 9] ################################### See: https://docs.python.org/2/library/functions.html#sorted From emile at fenx.com Tue Apr 29 00:15:39 2014 From: emile at fenx.com (Emile van Sebille) Date: Mon, 28 Apr 2014 15:15:39 -0700 Subject: [Tutor] Keeping change-in-place vs. copy methods straight In-Reply-To: References: Message-ID: On 4/28/2014 11:45 AM, taserian wrote: > Is there some sort of rule-of-thumb to determine if a function is > in-place or returns a value? my rule of thumb is to ask: Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> help([].sort) Help on built-in function sort: sort(...) L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1 >>> help([].reverse) Help on built-in function reverse: reverse(...) L.reverse() -- reverse *IN PLACE* >>> The *IN PLACE* tells me. HTH, Emile From mik.stephen at yahoo.com Tue Apr 29 20:38:21 2014 From: mik.stephen at yahoo.com (Stephen Mik) Date: Tue, 29 Apr 2014 11:38:21 -0700 (PDT) Subject: [Tutor] "Guess My Number" Python 3.4.0 Program partially fixed but now has Logic Errors-by Stephen Mik-novice programmer-getting desperate Message-ID: <1398796701.19427.YahooMailNeo@web124704.mail.ne1.yahoo.com> Dear Sir(s): ??? I am new to Python programming,and I have a "Guess My Number" program which partially works. The main while control works,the guessing of an integer between 1 and 60 seems to give the "too high" or "too low" elif branches effectively. However,when the correct number is guessed the "elif" for the Congratulatory Message does not print out,and the number of attempts at guessing the mystery number does not print out. Instead, the program apparently goes into the main while control loop again and queries the User if they want to run the program again. I have attached a sample Python Shell run;along with code fragments of the relevant areas. Anybody,please help me work out this code and get "Guess My Number" correctly running. CONCERNED,Stephen W. Mik -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: assignment4-fragment2.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: PythShellrunerror.py URL: From __peter__ at web.de Wed Apr 30 00:38:43 2014 From: __peter__ at web.de (Peter Otten) Date: Wed, 30 Apr 2014 00:38:43 +0200 Subject: [Tutor] "Guess My Number" Python 3.4.0 Program partially fixed but now has Logic Errors References: <1398796701.19427.YahooMailNeo@web124704.mail.ne1.yahoo.com> Message-ID: Stephen Mik wrote: > Stephen Mik-novice programmer-getting desperate Don't despair just yet! As a programmer you will be constantly producing and fixing errors. That is business as usual. What will change is that you will produce trickier bugs as your knowledge level increases... > Dear Sir(s): > I am new to Python programming,and I have a "Guess My Number" program > which partially works. The main while control works,the guessing of an > integer between 1 and 60 seems to give the "too high" or "too low" elif > branches effectively. However,when the correct number is guessed the > "elif" for the Congratulatory Message does not print out,and the number of > attempts at guessing the mystery number does not print out. Instead, the > program apparently goes into the main while control loop again and queries > the User if they want to run the program again. I have attached a sample > Python Shell run;along with code fragments of the relevant areas. > Anybody,please help me work out this code and get "Guess My Number" > correctly running. CONCERNED,Stephen W. Mik Look at that loop once more: > while(smv_guessNumber!=smv_pickNumber): > if (smv_guessNumber > smv_pickNumber): > print("Guess of mystery number Too high,enter a lesser number: \n") > smv_attemptCounter+=1 > elif (smv_guessNumber < smv_pickNumber): > print("Guess of mystery number Too Low.Enter a greater number \n") > smv_attemptCounter+=1 > elif (smv_guessNumber == smv_pickNumber): > #Print Congratulatory Message,the mystery number,the number of attempts > print("Congratulations! You have guessed the mystery number") [...] > smv_guessNumber=int(input("Take a new guess!")) Here's a simplified version: while x != y: if ... elif ... elif x == y: print("congratulations") x = int(input()) Can you see now why the print statement cannot be reached? If x == y were true x != y would be false, and the loop would already have been terminated. The easiest fix is to move the congratulations out of the loop: while x != y: if x > y: ... elif x < y: ... # see note 1 x = int(input()) print("congratulations") # at this point you can be sure that # x == y. Otherwise the loop would still # be running. Note 1: you do not actually need the test here as you know that x != y and (not x > y) so that there's no other option than x < y. From jsmallwood82 at yahoo.com Mon Apr 28 14:56:35 2014 From: jsmallwood82 at yahoo.com (Jordan Smallwood) Date: Mon, 28 Apr 2014 05:56:35 -0700 Subject: [Tutor] New to Python In-Reply-To: References: <1398552813.94073.YahooMailNeo@web162703.mail.bf1.yahoo.com> Message-ID: I never got a response. Should I check my spam? Sent from my iPhone > On Apr 28, 2014, at 1:57 AM, Mark Lawrence wrote: > >> On 26/04/2014 23:53, jordan smallwood wrote: >> Hello, >> >> I am new to Python. I mean completely new and we're working on this >> problem set in class where they give us specs and we have to build >> something based off these specs. I have no idea what they're asking. >> Could someone help get me started on the path to figuring this out? >> >> Below is the question: >> >> 1. Write a program module with at least two functions. Follow this >> specifi- cation exactly for these two functions: >> >> 1. >> (a) One function, CalculateCentimeters, receives a value in inches >> and returns the equivalent value in centimeters. >> centimeters =2.54?inches >> 2. >> (b) The other function, CalculateInches receives a value in centime- >> ters and returns the equivalent value in inches. inches >> =centimeters/2.54 >> >> ... but you don?t 2.54 in your code 2 times. It?s a good candidate to be >> a module-level constant. >> Specified instructions about the internals of code, i.e., the names of >> your functions and how they behave, is called theinternal specification. >> It tells you, the author of the function, as well as programmers who >> call your function, how to call it and what to expect when it is called. >> You must following them exactly or call a meeting of your programming >> team because your choices here affect the others. >> For this exercise, you design the rest of the functions for your >> program, but be careful to keep all the code in functions. >> Invent and arrange functions as you wish to ask the user for: >> (a) a value >> (b) a unit of measure >> and call the appropriate function to print out the value in the other >> unit of measure. >> >> Specified instructions about the user?s view of your code (like just >> given) is called the external specification. Often the paying customer >> gives these directions so you must follow them exactly, doing things in >> the order given; but in this case, the internal design is up to you. > > What did you not understand about the answers you received to your original question "Help with an assignment"? Or if you could not be bothered to reply to either Alan Gauld or Dave Angel, why should we now waste our time attempting to help you? > > -- > My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. > > Mark Lawrence > > --- > This email is free from viruses and malware because avast! Antivirus protection is active. > http://www.avast.com > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From jsmallwood82 at yahoo.com Mon Apr 28 14:56:35 2014 From: jsmallwood82 at yahoo.com (Jordan Smallwood) Date: Mon, 28 Apr 2014 05:56:35 -0700 Subject: [Tutor] New to Python In-Reply-To: References: <1398552813.94073.YahooMailNeo@web162703.mail.bf1.yahoo.com> Message-ID: I never got a response. Should I check my spam? Sent from my iPhone > On Apr 28, 2014, at 1:57 AM, Mark Lawrence wrote: > >> On 26/04/2014 23:53, jordan smallwood wrote: >> Hello, >> >> I am new to Python. I mean completely new and we're working on this >> problem set in class where they give us specs and we have to build >> something based off these specs. I have no idea what they're asking. >> Could someone help get me started on the path to figuring this out? >> >> Below is the question: >> >> 1. Write a program module with at least two functions. Follow this >> specifi- cation exactly for these two functions: >> >> 1. >> (a) One function, CalculateCentimeters, receives a value in inches >> and returns the equivalent value in centimeters. >> centimeters =2.54?inches >> 2. >> (b) The other function, CalculateInches receives a value in centime- >> ters and returns the equivalent value in inches. inches >> =centimeters/2.54 >> >> ... but you don?t 2.54 in your code 2 times. It?s a good candidate to be >> a module-level constant. >> Specified instructions about the internals of code, i.e., the names of >> your functions and how they behave, is called theinternal specification. >> It tells you, the author of the function, as well as programmers who >> call your function, how to call it and what to expect when it is called. >> You must following them exactly or call a meeting of your programming >> team because your choices here affect the others. >> For this exercise, you design the rest of the functions for your >> program, but be careful to keep all the code in functions. >> Invent and arrange functions as you wish to ask the user for: >> (a) a value >> (b) a unit of measure >> and call the appropriate function to print out the value in the other >> unit of measure. >> >> Specified instructions about the user?s view of your code (like just >> given) is called the external specification. Often the paying customer >> gives these directions so you must follow them exactly, doing things in >> the order given; but in this case, the internal design is up to you. > > What did you not understand about the answers you received to your original question "Help with an assignment"? Or if you could not be bothered to reply to either Alan Gauld or Dave Angel, why should we now waste our time attempting to help you? > > -- > My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. > > Mark Lawrence > > --- > This email is free from viruses and malware because avast! Antivirus protection is active. > http://www.avast.com > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From philip.dexter at gmail.com Mon Apr 28 19:55:46 2014 From: philip.dexter at gmail.com (Philip Dexter) Date: Mon, 28 Apr 2014 13:55:46 -0400 (EDT) Subject: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected In-Reply-To: References: <1398706326.78128.YahooMailNeo@web124706.mail.ne1.yahoo.com> Message-ID: On Mon, 28 Apr 2014, C Smith wrote: > I should probably clarify that this list is mainly for python2.7, correct me if I am wrong. I don't think that is true. > On Mon, Apr 28, 2014 at 1:49 PM, C Smith wrote: > The reason this is happening here is you need to import sys. I don't know why you would think importing sys would fix this. > On Mon, Apr 28, 2014 at 1:32 PM, Stephen Mik wrote: > ??? I must be doing something very wrong. The program is supposed to run a main loop ,for control of the program. The program DOES print out the prompts > before the While Loop, but when it comes to a variable named"smv_guessNumber" the program DOES NOT prompt for the input for "smv_guessNumber" as it should. It > is a mystery to me as to why the program will not get to the "smv_guessNumber=int(input("Think out a first guess:")". I am mystified why it doesn't reach that > point in the program! Can anyone please help? I have attached the Python Traceback Error Output,which shows that at least part of the program IS working. I > also have attached part of the code for the Assignment 4 which should help in the debugging.I need help ASAP,another program is due very soon and I have not > even worked out the pseudocode for it yet! > CONCERNED,Stephen W. Mik Your first while loop is not running. You convert smv_grandCounter to an int but compare it with a string. From ch2009 at arcor.de Mon Apr 28 19:28:32 2014 From: ch2009 at arcor.de (Chris) Date: Mon, 28 Apr 2014 19:28:32 +0200 Subject: [Tutor] Remove last newline only in print / open / read function In-Reply-To: References: <53596173.5020601@arcor.de> Message-ID: <535E8FC0.7000708@arcor.de> Dear Dave, On 04/25/2014 12:14 PM, Dave Angel wrote: > But what is your context? Your Python version (apparently 2.x, > since you're using print as a statement, rather than a function)? > What os, and what is stdout pointing to, since you're pretending > that you can write binary data to it? it's Python 2.7 on Debian Wheezy. The method is from http://code.google.com/p/vinty/ Thank you for your reply! -- Chris From ch2009 at arcor.de Mon Apr 28 19:29:56 2014 From: ch2009 at arcor.de (Chris) Date: Mon, 28 Apr 2014 19:29:56 +0200 Subject: [Tutor] Remove last newline only in print / open / read function In-Reply-To: References: <53596173.5020601@arcor.de> Message-ID: <535E9014.7090203@arcor.de> On 04/25/2014 12:14 PM, Dave Angel wrote: > print adds a newline. Use write () instead. > sys.stdout.write (open (name).read ()) The comma at the end of the line didn't change anything, but the write method worked! Thank you! -- Chris From ch2009 at arcor.de Mon Apr 28 19:30:49 2014 From: ch2009 at arcor.de (Chris) Date: Mon, 28 Apr 2014 19:30:49 +0200 Subject: [Tutor] Remove last newline only in print / open / read function In-Reply-To: References: <53596173.5020601@arcor.de> Message-ID: <535E9049.8020806@arcor.de> On 04/25/2014 10:36 AM, Alan Gauld wrote: > Can I first ask what makes you think there is an extra > linefeed at the end? Is it because of the print output? > You do remember that print adds a newline? I opened the zip file with an editor. My zip program said the file was invalid, until I removed the last LF. -- Chris From cs at zip.com.au Tue Apr 29 00:27:11 2014 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 29 Apr 2014 08:27:11 +1000 Subject: [Tutor] Keeping change-in-place vs. copy methods straight In-Reply-To: References: Message-ID: <20140428222711.GA33068@cskk.homeip.net> On 28Apr2014 14:45, taserian wrote: >I can't claim to be new to programming, but I've dabbled in Python over and >over again to get small problems and puzzles resolved. One thing that I >find I can't keep straight are the methods that change a list in place, vs. >those that return a copy (sometimes transformed) of the list. > >Call me old-fashioned, but my programming experience mostly comes from >languages where you assigned the output of a function to another variable, >so you always had a copy of whatever you were working on. [...] >Is there some sort of rule-of-thumb to determine if a function is in-place >or returns a value? In python, the convention is that a function that changes in place returns None. This avois people accidentally thinking they have a copy when in fact they have the original. Of course, it is a work practice, so you need to consult the doco of any particular function. But it is a good practice to adopt in one's own code. As you suggest, this keeps the two notions separate by making their use in the code distinct. Cheers, Cameron Simpson Too young to rest on the weekend, too old to rest during the week. - Mark Randol