Re: [Tutor] Modulus Operator ?
On Thu, Nov 19, 2015 at 1:11 PM, Ken Hammer wrote: > y = 49%13 > print y > 10 Actually, let me pretend for a moment that I don't know what the modulus operator is. Why do we get 10 here? Can you verbalize the reason? Can you modify this example above to use the modulus operator with "10" on the right hand side? What do you expect it computes? What do you see? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modulus Operator ?
On 19/11/15 21:11, Ken Hammer wrote: ...The 2nd below mystifies me. How do I use "x % 10" and "x % 100" ... Also, you can extract the right-most digit or digits from a number. > For example, x % 10 yields the right-most digit of x (in base 10). I understand these: x = 49/13 print x 3 > > y = 49%13 > print y > 10 So now try it with 10 instead of 13 print 49%10 9 z = 0.0 + x + y/13.0 print z 3.76923076923 print 49.0/13 3.76923076923 Since mod doesn't come into these you can't really apply it. But here is another example, lets create a list of numbers: >>> nums = [123,234,345] Now we want the last two digits from each number, so we use mod 100: >>> for n in nums: ... print n % 100 23 34 45 As always in python the best way to see how something works is just to try it out at the >>> prompt. If something gives a different result to what you expect then come back here and ask. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unittest not working
On Thu, Nov 19, 2015 at 8:25 AM, Mike wrote: > I'm trying to unit test a self-built regular expression processor for an > assignment. I'm trying to set up unit tests for the package, but it's not > executing them. This is my first time trying to use the unittest module, so > I'm sure I'm missing something, I'm just not sure what. I even put a test > case in there I knew would fail just to try it. > > Unit Test code: > import unittest > from regex import regexp > class RegexTest(unittest.TestCase): > def fail_test(self): > self.assertEqual(1, 2) > > def basic_test(self): > self.assertEqual(regexp('Hello', 'Goodbye'), '') > self.assertEqual(regexp('hello', 'ello'), 'ello') > with self.assertRaises(SyntaxError): > regexp('hello', 'he)') > Your functions should start with the word 'test'. For example: def test_fail_test(self): According to the docs, "This naming convention informs the test runner about which methods represent tests." ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Modulus Operator ?
A text for Python 2.7 includes these two paragraphs. The first I understand. The 2nd below mystifies me. How do I use "x % 10" and "x % 100" in the context of the examples I've created following the paragraphs? "The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another if x % y is zero, then x is divisible by y. Also, you can extract the right-most digit or digits from a number. For example, x % 10 yields the right-most digit of x (in base 10). Similarly x % 100 yields the last two digits." I understand these: x = 49/13 print x 3 y = 49%13 print y 10 z = 0.0 + x + y/13.0 print z 3.76923076923 print 49.0/13 3.76923076923 Thanks, Ken ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unittest not working
On 19 November 2015 at 13:25, Mike wrote: > I'm trying to unit test a self-built regular expression processor for an > assignment. I'm trying to set up unit tests for the package, but it's not > executing them. This is my first time trying to use the unittest module, so > I'm sure I'm missing something, I'm just not sure what. I even put a test > case in there I knew would fail just to try it. > > Unit Test code: > import unittest > from regex import regexp > > class RegexTest(unittest.TestCase): > def fail_test(self): > self.assertEqual(1, 2) > > def basic_test(self): > self.assertEqual(regexp('Hello', 'Goodbye'), '') > self.assertEqual(regexp('hello', 'ello'), 'ello') > with self.assertRaises(SyntaxError): > regexp('hello', 'he)') > > if __name__ == '__main__': > unittest.main() > > Output: > > -- > Ran 0 tests in 0.000s > > OK > Exit code: False The unittest.main() call will attempt to find all of the TestCase subclasses and for each class, create an instance and call all of the methods that are named test_xxx. So if you rename your methods as test_fail and test_basic then I think it should work. The reason for this is that you might want to add other methods to your TestCase subclass that will not be directly called by the test runner but that you can use in your tests. -- Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unittest not working
Mike wrote: > I'm trying to unit test a self-built regular expression processor for an > assignment. I'm trying to set up unit tests for the package, but it's not > executing them. This is my first time trying to use the unittest module, > so I'm sure I'm missing something, I'm just not sure what. I even put a > test case in there I knew would fail just to try it. > > Unit Test code: > import unittest > from regex import regexp > > class RegexTest(unittest.TestCase): > def fail_test(self): > self.assertEqual(1, 2) > > def basic_test(self): > self.assertEqual(regexp('Hello', 'Goodbye'), '') > self.assertEqual(regexp('hello', 'ello'), 'ello') > with self.assertRaises(SyntaxError): > regexp('hello', 'he)') > > if __name__ == '__main__': > unittest.main() > > Output: > > -- > Ran 0 tests in 0.000s > > OK > Exit code: False Rename the mathods fail_test to test_fail, and basic_test to test_basic. Test methods are recognized by their prefix. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using sorted in Python 3.3.5
On 19/11/15 15:31, Greg Christian wrote: I’m trying to sort a list of tuples based on the second item in the tuple. When I run this in IDLE I get the correct output; however, when running inside of a program, and calling the counter() function, sorted does not seem to work? Any ideas on why this works in IDLE and not in program would be appreciated. Thank You. def getKey(item): return item[1] def counter(): L = [("baby", ), ("aeuron", 100), ("pablo", 1234)] sorted(L, key=getKey) print ("L = ", L) OUTPUTS THIS (when calling counter inside of program): L = [('baby', ), ('aeuron', 100), ('pablo', 1234)] sorted() returns the soirted collection, it does not sort it in place. If you want in-place use the sort method. OUTPUTS THIS (when running with IDLE – desired output): [('aeuron', 100), ('pablo', 1234), ('baby', )] I'm not sure why IDLE does that. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using sorted in Python 3.3.5
On 19/11/2015 15:31, Greg Christian wrote: I’m trying to sort a list of tuples based on the second item in the tuple. When I run this in IDLE I get the correct output; however, when running inside of a program, and calling the counter() function, sorted does not seem to work? Any ideas on why this works in IDLE and not in program would be appreciated. Thank You. def getKey(item): return item[1] def counter(): L = [("baby", ), ("aeuron", 100), ("pablo", 1234)] sorted(L, key=getKey) print ("L = ", L) OUTPUTS THIS (when calling counter inside of program): L = [('baby', ), ('aeuron', 100), ('pablo', 1234)] OUTPUTS THIS (when running with IDLE – desired output): [('aeuron', 100), ('pablo', 1234), ('baby', )] Your use of sorted achieves precisely nothing as you discard the return value. Either save the value or use the built-in sort which works in place. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Strange error
On 19/11/15 10:06, jarod_v6--- via Tutor wrote: I have this error In [381]: len(Span) --- TypeError Traceback (most recent call last) in () > 1 len(Span) TypeError: 'str' object is not callable To call something in Python you use parens - eg. to call f you use f() So the error is talking about whatever has parens after it. In this case that's len() And it's saying len is a string. That means somewhere in your code you have created a variable called len and assigned a string to it. You thus hide the built in function len() You need to delete the len variable. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] unittest not working
I'm trying to unit test a self-built regular expression processor for an assignment. I'm trying to set up unit tests for the package, but it's not executing them. This is my first time trying to use the unittest module, so I'm sure I'm missing something, I'm just not sure what. I even put a test case in there I knew would fail just to try it. Unit Test code: import unittest from regex import regexp class RegexTest(unittest.TestCase): def fail_test(self): self.assertEqual(1, 2) def basic_test(self): self.assertEqual(regexp('Hello', 'Goodbye'), '') self.assertEqual(regexp('hello', 'ello'), 'ello') with self.assertRaises(SyntaxError): regexp('hello', 'he)') if __name__ == '__main__': unittest.main() Output: >>> -- Ran 0 tests in 0.000s OK Exit code: False >>> ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Using sorted in Python 3.3.5
I’m trying to sort a list of tuples based on the second item in the tuple. When I run this in IDLE I get the correct output; however, when running inside of a program, and calling the counter() function, sorted does not seem to work? Any ideas on why this works in IDLE and not in program would be appreciated. Thank You. def getKey(item): return item[1] def counter(): L = [("baby", ), ("aeuron", 100), ("pablo", 1234)] sorted(L, key=getKey) print ("L = ", L) OUTPUTS THIS (when calling counter inside of program): L = [('baby', ), ('aeuron', 100), ('pablo', 1234)] OUTPUTS THIS (when running with IDLE – desired output): [('aeuron', 100), ('pablo', 1234), ('baby', )] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Strange error
jarod_v6--- via Tutor wrote: > HI!! > This is my list: > > In [378]: type(Span) > Out[378]: list > > In [379]: Span > Out[379]: > [['M02898:39:0-AH4BK:1:2107:17412:10850', > 'M02898:39:0-AH4BK:1:2117:15242:18766', > 'M02898:39:0-AH4BK:1:1112:21747:21214', > 'M02898:39:0-AH4BK:1:2112:5119:9813', > 'M02898:39:0-AH4BK:1:1102:26568:5630', > 'M02898:39:0-AH4BK:1:2118:19680:11792', > 'M02898:39:0-AH4BK:1:1103:5469:6578', > 'M02898:39:0-AH4BK:1:2101:13087:20965', > 'M02898:39:0-AH4BK:1:1103:28031:13653', > 'M02898:39:0-AH4BK:1:1103:8013:21346', > 'M02898:39:0-AH4BK:1:1107:9189:22557', > 'M02898:39:0-AH4BK:1:2118:21263:23091', > 'M02898:39:0-AH4BK:1:1115:12279:20054', > 'M02898:39:0-AH4BK:1:1102:19433:17489', > 'M02898:39:0-AH4BK:1:1110:14533:11792', > 'M02898:39:0-AH4BK:1:2106:18027:12878', > 'M02898:39:0-AH4BK:1:1104:4408:6824', > 'M02898:39:0-AH4BK:1:2101:5678:7400']] > > I have this error > > > In [381]: len(Span) > --- > TypeError Traceback (most recent call > last) in () > > 1 len(Span) > > TypeError: 'str' object is not callable > > > > Why??? IS a list!!! My crystal ball says that this is self-inflicted pain. You shaded the built- by typing In [81]: len = "whatever" and then forgot about it until after 300 more lines. To make the built-in len() function visible again delete the string in the global namespace of the __main__ module with In [382]: del len ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Strange error
HI!! This is my list: In [378]: type(Span) Out[378]: list In [379]: Span Out[379]: [['M02898:39:0-AH4BK:1:2107:17412:10850', 'M02898:39:0-AH4BK:1:2117:15242:18766', 'M02898:39:0-AH4BK:1:1112:21747:21214', 'M02898:39:0-AH4BK:1:2112:5119:9813', 'M02898:39:0-AH4BK:1:1102:26568:5630', 'M02898:39:0-AH4BK:1:2118:19680:11792', 'M02898:39:0-AH4BK:1:1103:5469:6578', 'M02898:39:0-AH4BK:1:2101:13087:20965', 'M02898:39:0-AH4BK:1:1103:28031:13653', 'M02898:39:0-AH4BK:1:1103:8013:21346', 'M02898:39:0-AH4BK:1:1107:9189:22557', 'M02898:39:0-AH4BK:1:2118:21263:23091', 'M02898:39:0-AH4BK:1:1115:12279:20054', 'M02898:39:0-AH4BK:1:1102:19433:17489', 'M02898:39:0-AH4BK:1:1110:14533:11792', 'M02898:39:0-AH4BK:1:2106:18027:12878', 'M02898:39:0-AH4BK:1:1104:4408:6824', 'M02898:39:0-AH4BK:1:2101:5678:7400']] I have this error In [381]: len(Span) --- TypeError Traceback (most recent call last) in () > 1 len(Span) TypeError: 'str' object is not callable Why??? IS a list!!! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor