From 3a6737bcdc83e46b2aafc7b8b85278bd339b87e0 Mon Sep 17 00:00:00 2001 From: Zac Gross Date: Sat, 27 Apr 2013 16:25:59 -0400 Subject: [PATCH 1/2] working konan --- python2/koans/about_asserts.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/python2/koans/about_asserts.py b/python2/koans/about_asserts.py index 1af8b4e8e..d3aec2d76 100644 --- a/python2/koans/about_asserts.py +++ b/python2/koans/about_asserts.py @@ -10,26 +10,26 @@ def test_assert_truth(self): """ We shall contemplate truth by testing reality, via asserts. """ - self.assertTrue(False) # This should be true + self.assertTrue(True) # This should be true def test_assert_with_message(self): """ Enlightenment may be more easily achieved with appropriate messages. """ - self.assertTrue(False, "This should be true -- Please fix this") + self.assertTrue(True, "This should be true -- Please fix this") def test_fill_in_values(self): """ Sometimes we will ask you to fill in the values """ - self.assertEqual(__, 1 + 1) + self.assertEqual(2, 1 + 1) def test_assert_equality(self): """ To understand reality, we must compare our expectations against reality. """ - expected_value = __ + expected_value = 2 actual_value = 1 + 1 self.assertTrue(expected_value == actual_value) @@ -37,7 +37,7 @@ def test_a_better_way_of_asserting_equality(self): """ Some ways of asserting equality are better than others. """ - expected_value = __ + expected_value = 2 actual_value = 1 + 1 self.assertEqual(expected_value, actual_value) @@ -48,4 +48,4 @@ def test_that_unittest_asserts_work_the_same_way_as_python_asserts(self): """ # This throws an AssertionError exception - assert False + assert True From 3b5debbb764c8aa2dab1a24a907fe7c0c4650c98 Mon Sep 17 00:00:00 2001 From: Zac Gross Date: Sat, 27 Apr 2013 17:29:30 -0400 Subject: [PATCH 2/2] 4 lessons complete --- python2/koans/about_asserts.py~ | 51 +++++++++++ python2/koans/about_list_assignments.py | 14 +-- python2/koans/about_list_assignments.py~ | 31 +++++++ python2/koans/about_lists.py | 70 +++++++-------- python2/koans/about_lists.py~ | 106 +++++++++++++++++++++++ python2/koans/about_none.py | 12 +-- python2/koans/about_none.py~ | 47 ++++++++++ 7 files changed, 283 insertions(+), 48 deletions(-) create mode 100644 python2/koans/about_asserts.py~ create mode 100644 python2/koans/about_list_assignments.py~ create mode 100644 python2/koans/about_lists.py~ create mode 100644 python2/koans/about_none.py~ diff --git a/python2/koans/about_asserts.py~ b/python2/koans/about_asserts.py~ new file mode 100644 index 000000000..1af8b4e8e --- /dev/null +++ b/python2/koans/about_asserts.py~ @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from runner.koan import * + + +class AboutAsserts(Koan): + + def test_assert_truth(self): + """ + We shall contemplate truth by testing reality, via asserts. + """ + self.assertTrue(False) # This should be true + + def test_assert_with_message(self): + """ + Enlightenment may be more easily achieved with appropriate messages. + """ + self.assertTrue(False, "This should be true -- Please fix this") + + def test_fill_in_values(self): + """ + Sometimes we will ask you to fill in the values + """ + self.assertEqual(__, 1 + 1) + + def test_assert_equality(self): + """ + To understand reality, we must compare our expectations against + reality. + """ + expected_value = __ + actual_value = 1 + 1 + self.assertTrue(expected_value == actual_value) + + def test_a_better_way_of_asserting_equality(self): + """ + Some ways of asserting equality are better than others. + """ + expected_value = __ + actual_value = 1 + 1 + + self.assertEqual(expected_value, actual_value) + + def test_that_unittest_asserts_work_the_same_way_as_python_asserts(self): + """ + Knowing how things really work is half the battle + """ + + # This throws an AssertionError exception + assert False diff --git a/python2/koans/about_list_assignments.py b/python2/koans/about_list_assignments.py index 2c0267805..956bf15e0 100644 --- a/python2/koans/about_list_assignments.py +++ b/python2/koans/about_list_assignments.py @@ -11,21 +11,21 @@ class AboutListAssignments(Koan): def test_non_parallel_assignment(self): names = ["John", "Smith"] - self.assertEqual(__, names) + self.assertEqual(["John","Smith"], names) def test_parallel_assignments(self): first_name, last_name = ["John", "Smith"] - self.assertEqual(__, first_name) - self.assertEqual(__, last_name) + self.assertEqual("John", first_name) + self.assertEqual("Smith", last_name) def test_parallel_assignments_with_sublists(self): first_name, last_name = [["Willie", "Rae"], "Johnson"] - self.assertEqual(__, first_name) - self.assertEqual(__, last_name) + self.assertEqual(["Willie","Rae"], first_name) + self.assertEqual("Johnson", last_name) def test_swapping_with_parallel_assignment(self): first_name = "Roy" last_name = "Rob" first_name, last_name = last_name, first_name - self.assertEqual(__, first_name) - self.assertEqual(__, last_name) + self.assertEqual("Rob", first_name) + self.assertEqual("Roy", last_name) diff --git a/python2/koans/about_list_assignments.py~ b/python2/koans/about_list_assignments.py~ new file mode 100644 index 000000000..78e0a6793 --- /dev/null +++ b/python2/koans/about_list_assignments.py~ @@ -0,0 +1,31 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Based on AboutArrayAssignments in the Ruby Koans +# + +from runner.koan import * + + +class AboutListAssignments(Koan): + def test_non_parallel_assignment(self): + names = ["John", "Smith"] + self.assertEqual(["John","Smith"], names) + + def test_parallel_assignments(self): + first_name, last_name = ["John", "Smith"] + self.assertEqual("John", first_name) + self.assertEqual("Smith", last_name) + + def test_parallel_assignments_with_sublists(self): + first_name, last_name = [["Willie", "Rae"], "Johnson"] + self.assertEqual(["Willie","Rae"], first_name) + self.assertEqual("Johnson", last_name) + + def test_swapping_with_parallel_assignment(self): + first_name = "Roy" + last_name = "Rob" + first_name, last_name = last_name, first_name + self.assertEqual("Roy", first_name) + self.assertEqual("Rob", last_name) diff --git a/python2/koans/about_lists.py b/python2/koans/about_lists.py index 4d2588272..bc990df3e 100644 --- a/python2/koans/about_lists.py +++ b/python2/koans/about_lists.py @@ -12,7 +12,7 @@ class AboutLists(Koan): def test_creating_lists(self): empty_list = list() self.assertEqual(list, type(empty_list)) - self.assertEqual(__, len(empty_list)) + self.assertEqual(0, len(empty_list)) def test_list_literals(self): nums = list() @@ -22,68 +22,68 @@ def test_list_literals(self): self.assertEqual([1], nums) nums[1:] = [2] - self.assertEqual([1, __], nums) + self.assertEqual([1, 2], nums) nums.append(333) - self.assertEqual([1, 2, __], nums) + self.assertEqual([1, 2, 333], nums) def test_accessing_list_elements(self): noms = ['peanut', 'butter', 'and', 'jelly'] - self.assertEqual(__, noms[0]) - self.assertEqual(__, noms[3]) - self.assertEqual(__, noms[-1]) - self.assertEqual(__, noms[-3]) + self.assertEqual('peanut', noms[0]) + self.assertEqual('jelly', noms[3]) + self.assertEqual('jelly', noms[-1]) + self.assertEqual('butter', noms[-3]) def test_slicing_lists(self): noms = ['peanut', 'butter', 'and', 'jelly'] - self.assertEqual(__, noms[0:1]) - self.assertEqual(__, noms[0:2]) - self.assertEqual(__, noms[2:2]) - self.assertEqual(__, noms[2:20]) - self.assertEqual(__, noms[4:0]) - self.assertEqual(__, noms[4:100]) - self.assertEqual(__, noms[5:0]) + self.assertEqual(['peanut'], noms[0:1]) + self.assertEqual(['peanut','butter'], noms[0:2]) + self.assertEqual([], noms[2:2]) + self.assertEqual(['and','jelly'], noms[2:20]) + self.assertEqual([], noms[4:0]) + self.assertEqual([], noms[4:100]) + self.assertEqual([], noms[5:0]) def test_slicing_to_the_edge(self): noms = ['peanut', 'butter', 'and', 'jelly'] - self.assertEqual(__, noms[2:]) - self.assertEqual(__, noms[:2]) + self.assertEqual(['and','jelly'], noms[2:]) + self.assertEqual(['peanut','butter'], noms[:2]) def test_lists_and_ranges(self): self.assertEqual(list, type(range(5))) - self.assertEqual(__, range(5)) - self.assertEqual(__, range(5, 9)) + self.assertEqual([0,1,2,3,4], range(5)) + self.assertEqual([5,6,7,8], range(5, 9)) def test_ranges_with_steps(self): - self.assertEqual(__, range(0, 8, 2)) - self.assertEqual(__, range(1, 8, 3)) - self.assertEqual(__, range(5, -7, -4)) - self.assertEqual(__, range(5, -8, -4)) + self.assertEqual([0,2,4,6], range(0, 8, 2)) + self.assertEqual([1,4,7], range(1, 8, 3)) + self.assertEqual([5,1,-3], range(5, -7, -4)) + self.assertEqual([5,1,-3,-7], range(5, -8, -4)) def test_insertions(self): knight = ['you', 'shall', 'pass'] knight.insert(2, 'not') - self.assertEqual(__, knight) + self.assertEqual(['you','shall','not','pass'], knight) knight.insert(0, 'Arthur') - self.assertEqual(__, knight) + self.assertEqual(['Arthur','you','shall','not','pass'], knight) def test_popping_lists(self): stack = [10, 20, 30, 40] stack.append('last') - self.assertEqual(__, stack) + self.assertEqual([10,20,30,40,'last'], stack) popped_value = stack.pop() - self.assertEqual(__, popped_value) - self.assertEqual(__, stack) + self.assertEqual('last', popped_value) + self.assertEqual([10, 20, 30, 40], stack) popped_value = stack.pop(1) - self.assertEqual(__, popped_value) - self.assertEqual(__, stack) + self.assertEqual(20, popped_value) + self.assertEqual([10, 30, 40], stack) # Notice that there is a "pop" but no "push" in python? @@ -95,12 +95,12 @@ def test_popping_lists(self): def test_use_deques_for_making_queues(self): from collections import deque - + queue = deque([1, 2]) queue.append('last') - - self.assertEqual(__, list(queue)) - + + self.assertEqual([1,2,'last'], list(queue)) + popped_value = queue.popleft() - self.assertEqual(__, popped_value) - self.assertEqual(__, list(queue)) + self.assertEqual(1, popped_value) + self.assertEqual([2,'last'], list(queue)) diff --git a/python2/koans/about_lists.py~ b/python2/koans/about_lists.py~ new file mode 100644 index 000000000..6bae74fdd --- /dev/null +++ b/python2/koans/about_lists.py~ @@ -0,0 +1,106 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Based on AboutArrays in the Ruby Koans +# + +from runner.koan import * + + +class AboutLists(Koan): + def test_creating_lists(self): + empty_list = list() + self.assertEqual(list, type(empty_list)) + self.assertEqual(0, len(empty_list)) + + def test_list_literals(self): + nums = list() + self.assertEqual([], nums) + + nums[0:] = [1] + self.assertEqual([1], nums) + + nums[1:] = [2] + self.assertEqual([1, 2], nums) + + nums.append(333) + self.assertEqual([1, 2, 333], nums) + + def test_accessing_list_elements(self): + noms = ['peanut', 'butter', 'and', 'jelly'] + + self.assertEqual('peanut', noms[0]) + self.assertEqual('jelly', noms[3]) + self.assertEqual('jelly', noms[-1]) + self.assertEqual('butter', noms[-3]) + + def test_slicing_lists(self): + noms = ['peanut', 'butter', 'and', 'jelly'] + + self.assertEqual(['peanut'], noms[0:1]) + self.assertEqual(['peanut','butter'], noms[0:2]) + self.assertEqual([], noms[2:2]) + self.assertEqual(['and','jelly'], noms[2:20]) + self.assertEqual([], noms[4:0]) + self.assertEqual([], noms[4:100]) + self.assertEqual([], noms[5:0]) + + def test_slicing_to_the_edge(self): + noms = ['peanut', 'butter', 'and', 'jelly'] + + self.assertEqual(['and','jelly'], noms[2:]) + self.assertEqual(['peanut','butter'], noms[:2]) + + def test_lists_and_ranges(self): + self.assertEqual(list, type(range(5))) + self.assertEqual([0,1,2,3,4], range(5)) + self.assertEqual([5,6,7,8], range(5, 9)) + + def test_ranges_with_steps(self): + self.assertEqual([0,2,4,6], range(0, 8, 2)) + self.assertEqual([1,4,7], range(1, 8, 3)) + self.assertEqual([5,1,-3], range(5, -7, -4)) + self.assertEqual([5,1,-3,-7], range(5, -8, -4)) + + def test_insertions(self): + knight = ['you', 'shall', 'pass'] + knight.insert(2, 'not') + self.assertEqual(['you','shall','not','pass'], knight) + + knight.insert(0, 'Arthur') + self.assertEqual(['Arthur','you','shall','not','pass'], knight) + + def test_popping_lists(self): + stack = [10, 20, 30, 40] + stack.append('last') + + self.assertEqual([10,20,30,40,'last'], stack) + + popped_value = stack.pop() + self.assertEqual('last', popped_value) + self.assertEqual([10, 20, 30, 40], stack) + + popped_value = stack.pop(1) + self.assertEqual(20, popped_value) + self.assertEqual([10, 30, 40], stack) + + # Notice that there is a "pop" but no "push" in python? + + # Part of the Python philosophy is that there ideally should be one and + # only one way of doing anything. A 'push' is the same as an 'append'. + + # To learn more about this try typing "import this" from the python + # console... ;) + + def test_use_deques_for_making_queues(self): + from collections import deque + + queue = deque([1, 2]) + queue.append('last') + + self.assertEqual(__, list(queue)) + + popped_value = queue.popleft() + self.assertEqual(__, popped_value) + self.assertEqual(__, list(queue)) diff --git a/python2/koans/about_none.py b/python2/koans/about_none.py index 556360eed..45fad155a 100644 --- a/python2/koans/about_none.py +++ b/python2/koans/about_none.py @@ -12,11 +12,11 @@ class AboutNone(Koan): def test_none_is_an_object(self): "Unlike NULL in a lot of languages" - self.assertEqual(__, isinstance(None, object)) + self.assertEqual(True, isinstance(None, object)) def test_none_is_universal(self): "There is only one None" - self.assertEqual(__, None is None) + self.assertEqual(True, None is None) def test_what_exception_do_you_get_when_calling_nonexistent_methods(self): """ @@ -33,15 +33,15 @@ def test_what_exception_do_you_get_when_calling_nonexistent_methods(self): None.some_method_none_does_not_know_about() except Exception as ex: # What exception has been caught? - self.assertEqual(__, ex.__class__.__name__) + self.assertEqual("AttributeError", ex.__class__.__name__) # What message was attached to the exception? # (HINT: replace __ with part of the error message.) - self.assertMatch(__, ex.args[0]) + self.assertMatch( "'NoneType' object has no attribute 'some_method_none_does_not_know_about'", ex.args[0]) def test_none_is_distinct(self): """ None is distinct from other things which are False. """ - self.assertEqual(____, None is not 0) - self.assertEqual(____, None is not False) + self.assertEqual(True, None is not 0) + self.assertEqual(True, None is not False) diff --git a/python2/koans/about_none.py~ b/python2/koans/about_none.py~ new file mode 100644 index 000000000..ffc4f18c2 --- /dev/null +++ b/python2/koans/about_none.py~ @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Based on AboutNil in the Ruby Koans +# + +from runner.koan import * + + +class AboutNone(Koan): + + def test_none_is_an_object(self): + "Unlike NULL in a lot of languages" + self.assertEqual(True, isinstance(None, object)) + + def test_none_is_universal(self): + "There is only one None" + self.assertEqual(True, None is None) + + def test_what_exception_do_you_get_when_calling_nonexistent_methods(self): + """ + What is the Exception that is thrown when you call a method that does + not exist? + + Hint: launch python command console and try the code in the + block below. + + Don't worry about what 'try' and 'except' do, we'll talk about + this later + """ + try: + None.some_method_none_does_not_know_about() + except Exception as ex: + # What exception has been caught? + self.assertEqual("AttributeError", ex.__class__.__name__) + + # What message was attached to the exception? + # (HINT: replace __ with part of the error message.) + self.assertMatch("'None Type' object has no attribute 'some_method_none_does_not_know_about'", ex.args[0]) + + def test_none_is_distinct(self): + """ + None is distinct from other things which are False. + """ + self.assertEqual(True, None is not 0) + self.assertEqual(True, None is not False)