From 3e787720482c01b56640b286baf7681889741666 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 11 May 2013 18:06:01 +0200 Subject: [PATCH 01/35] WIP about asserts --- python2/koans/about_asserts.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/python2/koans/about_asserts.py b/python2/koans/about_asserts.py index 61b53ada1..8905cb3b2 100644 --- a/python2/koans/about_asserts.py +++ b/python2/koans/about_asserts.py @@ -15,26 +15,26 @@ def test_assert_truth(self): # # http://bit.ly/about_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) @@ -42,7 +42,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) @@ -53,7 +53,7 @@ def test_that_unittest_asserts_work_the_same_way_as_python_asserts(self): """ # This throws an AssertionError exception - assert False + assert True def test_that_sometimes_we_need_to_know_the_class_type(self): """ @@ -72,7 +72,7 @@ def test_that_sometimes_we_need_to_know_the_class_type(self): # # See for yourself: - self.assertEqual(__, "naval".__class__) # It's str, not + self.assertEqual(str, "naval".__class__) # It's str, not # Need an illustration? More reading can be found here: # From 29829c70de861d093788f19cbfccf3affb88823e Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 11 May 2013 20:13:59 +0200 Subject: [PATCH 02/35] Finish about none and strings koans --- python2/koans/about_none.py | 12 ++++++------ python2/koans/about_strings.py | 36 +++++++++++++++++----------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/python2/koans/about_none.py b/python2/koans/about_none.py index e64164dbd..498da5cee 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): """ @@ -37,15 +37,15 @@ def test_what_exception_do_you_get_when_calling_nonexistent_methods(self): # Need a recap on how to evaluate __class__ attributes? # https://github.com/gregmalcolm/python_koans/wiki/Class-Attribute - self.assertEqual(__, ex.__class__) + self.assertEqual(AttributeError, ex.__class__) # 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_strings.py b/python2/koans/about_strings.py index 241e4ec67..446500562 100644 --- a/python2/koans/about_strings.py +++ b/python2/koans/about_strings.py @@ -8,87 +8,87 @@ class AboutStrings(Koan): def test_double_quoted_strings_are_strings(self): string = "Hello, world." - self.assertEqual(__, isinstance(string, basestring)) + self.assertEqual(True, isinstance(string, basestring)) def test_single_quoted_strings_are_also_strings(self): string = 'Goodbye, world.' - self.assertEqual(__, isinstance(string, basestring)) + self.assertEqual(True, isinstance(string, basestring)) def test_triple_quote_strings_are_also_strings(self): string = """Howdy, world!""" - self.assertEqual(__, isinstance(string, basestring)) + self.assertEqual(True, isinstance(string, basestring)) def test_triple_single_quotes_work_too(self): string = '''Bonjour tout le monde!''' - self.assertEqual(__, isinstance(string, basestring)) + self.assertEqual(True, isinstance(string, basestring)) def test_raw_strings_are_also_strings(self): string = r"Konnichi wa, world!" - self.assertEqual(__, isinstance(string, basestring)) + self.assertEqual(True, isinstance(string, basestring)) def test_use_single_quotes_to_create_string_with_double_quotes(self): string = 'He said, "Go Away."' - self.assertEqual(__, string) + self.assertEqual('He said, "Go Away."', string) def test_use_double_quotes_to_create_strings_with_single_quotes(self): string = "Don't" - self.assertEqual(__, string) + self.assertEqual("Don't", string) def test_use_backslash_for_escaping_quotes_in_strings(self): a = "He said, \"Don't\"" b = 'He said, "Don\'t"' - self.assertEqual(__, (a == b)) + self.assertEqual(True, (a == b)) def test_use_backslash_at_the_end_of_a_line_to_continue_onto_the_next_line(self): string = "It was the best of times,\n\ It was the worst of times." - self.assertEqual(__, len(string)) + self.assertEqual(52, len(string)) def test_triple_quoted_strings_can_span_lines(self): string = """ Howdy, world! """ - self.assertEqual(__, len(string)) + self.assertEqual(15, len(string)) def test_triple_quoted_strings_need_less_escaping(self): a = "Hello \"world\"." b = """Hello "world".""" - self.assertEqual(__, (a == b)) + self.assertEqual(True, (a == b)) def but_quotes_at_the_end_of_a_triple_quoted_string_are_still_tricky(self): string = """Hello "world\"""" def test_plus_concatenates_strings(self): string = "Hello, " + "world" - self.assertEqual(__, string) + self.assertEqual("Hello, world", string) def test_adjacent_strings_are_concatenated_automatically(self): string = "Hello" ", " "world" - self.assertEqual(__, string) + self.assertEqual("Hello, world", string) def test_plus_will_not_modify_original_strings(self): hi = "Hello, " there = "world" string = hi + there - self.assertEqual(__, hi) - self.assertEqual(__, there) + self.assertEqual("Hello, ", hi) + self.assertEqual("world", there) def test_plus_equals_will_append_to_end_of_string(self): hi = "Hello, " there = "world" hi += there - self.assertEqual(__, hi) + self.assertEqual("Hello, world", hi) def test_plus_equals_also_leaves_original_string_unmodified(self): original = "Hello, " hi = original there = "world" hi += there - self.assertEqual(__, original) + self.assertEqual("Hello, ", original) def test_most_strings_interpret_escape_characters(self): string = "\n" self.assertEqual('\n', string) self.assertEqual("""\n""", string) - self.assertEqual(__, len(string)) + self.assertEqual(1, len(string)) From 42c0ef98f02c58ea4400c128b8624f4c5d6a1bb9 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sun, 12 May 2013 20:21:51 +0200 Subject: [PATCH 03/35] Complete about lists koans --- python2/koans/about_lists.py | 64 ++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/python2/koans/about_lists.py b/python2/koans/about_lists.py index 4d2588272..55175f76b 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? @@ -99,8 +99,8 @@ def test_use_deques_for_making_queues(self): 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)) From e7b2392dcf572086aa3a6a3dd169fe236135c059 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sun, 12 May 2013 20:23:59 +0200 Subject: [PATCH 04/35] Complete list assignments koans --- python2/koans/about_list_assignments.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/python2/koans/about_list_assignments.py b/python2/koans/about_list_assignments.py index 2c0267805..a48e4e6d6 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) From 8799a6ac8bdf692af75f5f0a7bf33bf4335a5cab Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sun, 12 May 2013 20:27:34 +0200 Subject: [PATCH 05/35] Complete dictionaries koans --- python2/koans/about_dictionaries.py | 30 ++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/python2/koans/about_dictionaries.py b/python2/koans/about_dictionaries.py index 8591b5439..8458665db 100644 --- a/python2/koans/about_dictionaries.py +++ b/python2/koans/about_dictionaries.py @@ -13,40 +13,40 @@ def test_creating_dictionaries(self): empty_dict = dict() self.assertEqual(dict, type(empty_dict)) self.assertEqual(dict(), empty_dict) - self.assertEqual(__, len(empty_dict)) + self.assertEqual(0, len(empty_dict)) def test_dictionary_literals(self): empty_dict = {} self.assertEqual(dict, type(empty_dict)) babel_fish = {'one': 'uno', 'two': 'dos'} - self.assertEqual(__, len(babel_fish)) + self.assertEqual(2, len(babel_fish)) def test_accessing_dictionaries(self): babel_fish = {'one': 'uno', 'two': 'dos'} - self.assertEqual(__, babel_fish['one']) - self.assertEqual(__, babel_fish['two']) + self.assertEqual('uno', babel_fish['one']) + self.assertEqual('dos', babel_fish['two']) def test_changing_dictionaries(self): babel_fish = {'one': 'uno', 'two': 'dos'} babel_fish['one'] = 'eins' - expected = {'two': 'dos', 'one': __} + expected = {'two': 'dos', 'one': 'eins'} self.assertEqual(expected, babel_fish) def test_dictionary_is_unordered(self): dict1 = {'one': 'uno', 'two': 'dos'} dict2 = {'two': 'dos', 'one': 'uno'} - self.assertEqual(____, dict1 == dict2) + self.assertEqual(True, dict1 == dict2) def test_dictionary_keys_and_values(self): babel_fish = {'one': 'uno', 'two': 'dos'} - self.assertEqual(__, len(babel_fish.keys())) - self.assertEqual(__, len(babel_fish.values())) - self.assertEqual(__, 'one' in babel_fish.keys()) - self.assertEqual(__, 'two' in babel_fish.values()) - self.assertEqual(__, 'uno' in babel_fish.keys()) - self.assertEqual(__, 'dos' in babel_fish.values()) + self.assertEqual(2, len(babel_fish.keys())) + self.assertEqual(2, len(babel_fish.values())) + self.assertEqual(True, 'one' in babel_fish.keys()) + self.assertEqual(False, 'two' in babel_fish.values()) + self.assertEqual(False, 'uno' in babel_fish.keys()) + self.assertEqual(True, 'dos' in babel_fish.values()) def test_making_a_dictionary_from_a_sequence_of_keys(self): cards = {}.fromkeys( @@ -54,6 +54,6 @@ def test_making_a_dictionary_from_a_sequence_of_keys(self): 'confused looking zebra'), 42) - self.assertEqual(__, len(cards)) - self.assertEqual(__, cards['green elf']) - self.assertEqual(__, cards['yellow dwarf']) + self.assertEqual(5, len(cards)) + self.assertEqual(42, cards['green elf']) + self.assertEqual(42, cards['yellow dwarf']) From bd2a79ae71f879338df2b11cc15b5dc3f4764a7d Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sun, 12 May 2013 20:33:02 +0200 Subject: [PATCH 06/35] Complete string manipulation koans --- python2/koans/about_string_manipulation.py | 34 +++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/python2/koans/about_string_manipulation.py b/python2/koans/about_string_manipulation.py index c395309cc..15c5cd246 100644 --- a/python2/koans/about_string_manipulation.py +++ b/python2/koans/about_string_manipulation.py @@ -10,13 +10,13 @@ def test_use_format_to_interpolate_variables(self): value1 = 'one' value2 = 2 string = "The values are {0} and {1}".format(value1, value2) - self.assertEqual(__, string) + self.assertEqual("The values are one and 2", string) def test_formatted_values_can_be_shown_in_any_order_or_be_repeated(self): value1 = 'doh' value2 = 'DOH' string = "The values are {1}, {0}, {0} and {1}!".format(value1, value2) - self.assertEqual(__, string) + self.assertEqual("The values are DOH, doh, doh and DOH!", string) def test_any_python_expression_may_be_interpolated(self): import math # import a standard python module with math functions @@ -24,24 +24,24 @@ def test_any_python_expression_may_be_interpolated(self): decimal_places = 4 string = "The square root of 5 is {0:.{1}f}".format(math.sqrt(5), \ decimal_places) - self.assertEqual(__, string) + self.assertEqual("The square root of 5 is 2.2361", string) def test_you_can_get_a_substring_from_a_string(self): string = "Bacon, lettuce and tomato" - self.assertEqual(__, string[7:10]) + self.assertEqual("let", string[7:10]) def test_you_can_get_a_single_character_from_a_string(self): string = "Bacon, lettuce and tomato" - self.assertEqual(__, string[1]) + self.assertEqual("a", string[1]) def test_single_characters_can_be_represented_by_integers(self): - self.assertEqual(__, ord('a')) - self.assertEqual(__, ord('b') == (ord('a') + 1)) + self.assertEqual(97, ord('a')) + self.assertEqual(True, ord('b') == (ord('a') + 1)) def test_strings_can_be_split(self): string = "Sausage Egg Cheese" words = string.split() - self.assertEqual([__, __, __], words) + self.assertEqual(["Sausage", "Egg", "Cheese"], words) def test_strings_can_be_split_with_different_patterns(self): import re # import python regular expression library @@ -51,7 +51,7 @@ def test_strings_can_be_split_with_different_patterns(self): words = pattern.split(string) - self.assertEqual([__, __, __, __], words) + self.assertEqual(["the", "rain", "in", "spain"], words) # `pattern` is a Python regular expression pattern which matches # ',' or ';' @@ -59,18 +59,18 @@ def test_strings_can_be_split_with_different_patterns(self): def test_raw_strings_do_not_interpret_escape_characters(self): string = r'\n' self.assertNotEqual('\n', string) - self.assertEqual(__, string) - self.assertEqual(__, len(string)) + self.assertEqual("\\n", string) + self.assertEqual(2, len(string)) # Useful in regular expressions, file paths, URLs, etc. def test_strings_can_be_joined(self): words = ["Now", "is", "the", "time"] - self.assertEqual(__, ' '.join(words)) + self.assertEqual("Now is the time", ' '.join(words)) def test_strings_can_change_case(self): - self.assertEqual(__, 'guido'.capitalize()) - self.assertEqual(__, 'guido'.upper()) - self.assertEqual(__, 'TimBot'.lower()) - self.assertEqual(__, 'guido van rossum'.title()) - self.assertEqual(__, 'ToTaLlY aWeSoMe'.swapcase()) + self.assertEqual("Guido", 'guido'.capitalize()) + self.assertEqual("GUIDO", 'guido'.upper()) + self.assertEqual("timbot", 'TimBot'.lower()) + self.assertEqual("Guido Van Rossum", 'guido van rossum'.title()) + self.assertEqual("tOtAlLy AwEsOmE", 'ToTaLlY aWeSoMe'.swapcase()) From 000eacf79c92b8659c250496dca4404dfaf4a8d5 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sun, 12 May 2013 20:42:07 +0200 Subject: [PATCH 07/35] Complete tuple koans --- python2/koans/about_tuples.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/python2/koans/about_tuples.py b/python2/koans/about_tuples.py index 84ba5d552..27590375c 100644 --- a/python2/koans/about_tuples.py +++ b/python2/koans/about_tuples.py @@ -7,14 +7,14 @@ class AboutTuples(Koan): def test_creating_a_tuple(self): count_of_three = (1, 2, 5) - self.assertEqual(__, count_of_three[2]) + self.assertEqual(5, count_of_three[2]) def test_tuples_are_immutable_so_item_assignment_is_not_possible(self): count_of_three = (1, 2, 5) try: count_of_three[2] = "three" except TypeError as ex: - self.assertMatch(__, ex[0]) + self.assertMatch("'tuple' object does not support item assignment", ex[0]) def test_tuples_are_immutable_so_appending_is_not_possible(self): count_of_three = (1, 2, 5) @@ -25,7 +25,7 @@ def test_tuples_are_immutable_so_appending_is_not_possible(self): # Note, assertMatch() uses regular expression pattern matching, # so you don't have to copy the whole message. - self.assertMatch(__, ex[0]) + self.assertMatch("has no attribute 'append'", ex[0]) # Tuples are less flexible than lists, but faster. @@ -36,25 +36,25 @@ def test_tuples_can_only_be_changed_through_replacement(self): list_count.append("boom") count_of_three = tuple(list_count) - self.assertEqual(__, count_of_three) + self.assertEqual((1, 2, 5, "boom"), count_of_three) def test_tuples_of_one_look_peculiar(self): - self.assertEqual(__, (1).__class__) - self.assertEqual(__, (1,).__class__) - self.assertEqual(__, ("Hello comma!", )) + self.assertEqual(int, (1).__class__) + self.assertEqual(tuple, (1,).__class__) + self.assertEqual(('Hello comma!',), ("Hello comma!", )) def test_tuple_constructor_can_be_surprising(self): - self.assertEqual(__, tuple("Surprise!")) + self.assertEqual(("S", "u", "r", "p", "r", "i", "s", "e", "!"), tuple("Surprise!")) def test_creating_empty_tuples(self): - self.assertEqual(__, ()) - self.assertEqual(__, tuple()) # Sometimes less confusing + self.assertEqual((), ()) + self.assertEqual((), tuple()) # Sometimes less confusing def test_tuples_can_be_embedded(self): lat = (37, 14, 6, 'N') lon = (115, 48, 40, 'W') place = ('Area 51', lat, lon) - self.assertEqual(__, place) + self.assertEqual(('Area 51', (37, 14, 6, 'N'), (115, 48, 40, 'W')), place) def test_tuples_are_good_for_representing_records(self): locations = [ @@ -66,5 +66,5 @@ def test_tuples_are_good_for_representing_records(self): ("Cthulhu", (26, 40, 1, 'N'), (70, 45, 7, 'W')) ) - self.assertEqual(__, locations[2][0]) - self.assertEqual(__, locations[0][1][2]) + self.assertEqual("Cthulhu", locations[2][0]) + self.assertEqual(15.56, locations[0][1][2]) From 2033af86e6c283f6e9a78b201596ce725ce16ca0 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sun, 12 May 2013 20:58:34 +0200 Subject: [PATCH 08/35] Complete methods koans --- python2/koans/about_methods.py | 40 +++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/python2/koans/about_methods.py b/python2/koans/about_methods.py index 9584e33e3..b5c78057c 100644 --- a/python2/koans/about_methods.py +++ b/python2/koans/about_methods.py @@ -14,7 +14,7 @@ def my_global_function(a, b): class AboutMethods(Koan): def test_calling_a_global_function(self): - self.assertEqual(__, my_global_function(2, 3)) + self.assertEqual(5, my_global_function(2, 3)) # NOTE: Wrong number of arguments is not a SYNTAX error, but a # runtime error. @@ -22,7 +22,7 @@ def test_calling_functions_with_wrong_number_of_arguments(self): try: my_global_function() except Exception as exception: - self.assertEqual(__, exception.__class__) + self.assertEqual(TypeError, exception.__class__) self.assertMatch( r'my_global_function\(\) takes exactly 2 arguments \(0 given\)', exception[0]) @@ -32,7 +32,7 @@ def test_calling_functions_with_wrong_number_of_arguments(self): except Exception as e: # Note, watch out for parenthesis. They need slashes in front! - self.assertMatch(__, e[0]) + self.assertMatch("takes exactly 2", e[0]) # ------------------------------------------------------------------ @@ -40,7 +40,7 @@ def pointless_method(self, a, b): sum = a + b def test_which_does_not_return_anything(self): - self.assertEqual(__, self.pointless_method(1, 2)) + self.assertEqual(None, self.pointless_method(1, 2)) # Notice that methods accessed from class scope do not require # you to pass the first "self" argument? @@ -50,8 +50,8 @@ def method_with_defaults(self, a, b='default_value'): return [a, b] def test_calling_with_default_values(self): - self.assertEqual(__, self.method_with_defaults(1)) - self.assertEqual(__, self.method_with_defaults(1, 2)) + self.assertEqual([1, 'default_value'], self.method_with_defaults(1)) + self.assertEqual([1, 2], self.method_with_defaults(1, 2)) # ------------------------------------------------------------------ @@ -59,9 +59,9 @@ def method_with_var_args(self, *args): return args def test_calling_with_variable_arguments(self): - self.assertEqual(__, self.method_with_var_args()) + self.assertEqual((), self.method_with_var_args()) self.assertEqual(('one', ), self.method_with_var_args('one')) - self.assertEqual(__, self.method_with_var_args('one', 'two')) + self.assertEqual(('one', 'two'), self.method_with_var_args('one', 'two')) # ------------------------------------------------------------------ @@ -72,13 +72,13 @@ def test_functions_without_self_arg_are_global_functions(self): def function_with_the_same_name(a, b): return a * b - self.assertEqual(__, function_with_the_same_name(3, 4)) + self.assertEqual(12, function_with_the_same_name(3, 4)) def test_calling_methods_in_same_class_with_explicit_receiver(self): def function_with_the_same_name(a, b): return a * b - self.assertEqual(__, self.function_with_the_same_name(3, 4)) + self.assertEqual(7, self.function_with_the_same_name(3, 4)) # ------------------------------------------------------------------ @@ -91,10 +91,10 @@ def another_method_with_the_same_name(self): return 42 def test_that_old_methods_are_hidden_by_redefinitions(self): - self.assertEqual(__, self.another_method_with_the_same_name()) + self.assertEqual(42, self.another_method_with_the_same_name()) def test_that_overlapped_method_is_still_there(self): - self.assertEqual(__, self.link_to_overlapped_method()) + self.assertEqual(10, self.link_to_overlapped_method()) # ------------------------------------------------------------------ @@ -102,21 +102,21 @@ def empty_method(self): pass def test_methods_that_do_nothing_need_to_use_pass_as_a_filler(self): - self.assertEqual(__, self.empty_method()) + self.assertEqual(None, self.empty_method()) def test_pass_does_nothing_at_all(self): "You" "shall" "not" pass - self.assertEqual(____, "Still got to this line" != None) + self.assertEqual(True, "Still got to this line" != None) # ------------------------------------------------------------------ def one_line_method(self): return 'Madagascar' def test_no_indentation_required_for_one_line_statement_bodies(self): - self.assertEqual(__, self.one_line_method()) + self.assertEqual("Madagascar", self.one_line_method()) # ------------------------------------------------------------------ @@ -125,7 +125,7 @@ def method_with_documentation(self): return "ok" def test_the_documentation_can_be_viewed_with_the_doc_method(self): - self.assertMatch(__, self.method_with_documentation.__doc__) + self.assertMatch("string placed", self.method_with_documentation.__doc__) # ------------------------------------------------------------------ @@ -142,13 +142,13 @@ def __password(self): def test_calling_methods_in_other_objects(self): rover = self.Dog() - self.assertEqual(__, rover.name()) + self.assertEqual("Fido", rover.name()) def test_private_access_is_implied_but_not_enforced(self): rover = self.Dog() # This is a little rude, but legal - self.assertEqual(__, rover._tail()) + self.assertEqual("wagging", rover._tail()) def test_double_underscore_attribute_prefixes_cause_name_mangling(self): """Attributes names that start with a double underscore get @@ -158,10 +158,10 @@ def test_double_underscore_attribute_prefixes_cause_name_mangling(self): #This may not be possible... password = rover.__password() except Exception as ex: - self.assertEqual(__, ex.__class__) + self.assertEqual(AttributeError, ex.__class__) # But this still is! - self.assertEqual(__, rover._Dog__password()) + self.assertEqual("password", rover._Dog__password()) # Name mangling exists to avoid name clash issues when subclassing. # It is not for providing effective access protection From 8ccf726cc6a93ac6f4d7fb147539da4e71bf420d Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sun, 12 May 2013 21:05:37 +0200 Subject: [PATCH 09/35] Complete control statements koans --- python2/koans/about_control_statements.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/python2/koans/about_control_statements.py b/python2/koans/about_control_statements.py index 921f0bf7f..24064b9fb 100644 --- a/python2/koans/about_control_statements.py +++ b/python2/koans/about_control_statements.py @@ -11,13 +11,13 @@ def test_if_then_else_statements(self): result = 'true value' else: result = 'false value' - self.assertEqual(__, result) + self.assertEqual("true value", result) def test_if_then_statements(self): result = 'default value' if True: result = 'true value' - self.assertEqual(__, result) + self.assertEqual("true value", result) def test_while_statement(self): i = 1 @@ -25,7 +25,7 @@ def test_while_statement(self): while i <= 10: result = result * i i += 1 - self.assertEqual(__, result) + self.assertEqual(3628800, result) def test_break_statement(self): i = 1 @@ -34,7 +34,7 @@ def test_break_statement(self): if i > 10: break result = result * i i += 1 - self.assertEqual(__, result) + self.assertEqual(3628800, result) def test_continue_statement(self): i = 0 @@ -43,14 +43,14 @@ def test_continue_statement(self): i += 1 if (i % 2) == 0: continue result.append(i) - self.assertEqual(__, result) + self.assertEqual([1, 3, 5, 7, 9], result) def test_for_statement(self): phrase = ["fish", "and", "chips"] result = [] for item in phrase: result.append(item.upper()) - self.assertEqual([__, __, __], result) + self.assertEqual(["FISH", "AND", "CHIPS"], result) def test_for_statement_with_tuples(self): round_table = [ @@ -64,7 +64,7 @@ def test_for_statement_with_tuples(self): result.append("Contestant: '" + knight + \ "' Answer: '" + answer + "'") - text = __ + text = "Contestant: 'Robin' Answer: 'Blue! I mean Green!" self.assertMatch(text, result[2]) From d611c71d28114cfa00abb587d9cd9a35580d27e4 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sun, 12 May 2013 21:08:59 +0200 Subject: [PATCH 10/35] Complete true and false koans --- python2/koans/about_true_and_false.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/python2/koans/about_true_and_false.py b/python2/koans/about_true_and_false.py index 89250353c..4d1928ddc 100644 --- a/python2/koans/about_true_and_false.py +++ b/python2/koans/about_true_and_false.py @@ -12,30 +12,30 @@ def truth_value(self, condition): return 'false stuff' def test_true_is_treated_as_true(self): - self.assertEqual(__, self.truth_value(True)) + self.assertEqual("true stuff", self.truth_value(True)) def test_false_is_treated_as_false(self): - self.assertEqual(__, self.truth_value(False)) + self.assertEqual("false stuff", self.truth_value(False)) def test_none_is_treated_as_false(self): - self.assertEqual(__, self.truth_value(None)) + self.assertEqual("false stuff", self.truth_value(None)) def test_zero_is_treated_as_false(self): - self.assertEqual(__, self.truth_value(0)) + self.assertEqual("false stuff", self.truth_value(0)) def test_empty_collections_are_treated_as_false(self): - self.assertEqual(__, self.truth_value([])) - self.assertEqual(__, self.truth_value(())) - self.assertEqual(__, self.truth_value({})) - self.assertEqual(__, self.truth_value(set())) + self.assertEqual("false stuff", self.truth_value([])) + self.assertEqual("false stuff", self.truth_value(())) + self.assertEqual("false stuff", self.truth_value({})) + self.assertEqual("false stuff", self.truth_value(set())) def test_blank_strings_are_treated_as_false(self): - self.assertEqual(__, self.truth_value("")) + self.assertEqual("false stuff", self.truth_value("")) def test_everything_else_is_treated_as_true(self): - self.assertEqual(__, self.truth_value(1)) - self.assertEqual(__, self.truth_value(1,)) + self.assertEqual("true stuff", self.truth_value(1)) + self.assertEqual("true stuff", self.truth_value(1,)) self.assertEqual( - __, + "true stuff", self.truth_value("Python is named after Monty Python")) - self.assertEqual(__, self.truth_value(' ')) + self.assertEqual("true stuff", self.truth_value(' ')) From 48bcb4438290bad8be065b992b82d35756136e34 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sun, 12 May 2013 21:16:48 +0200 Subject: [PATCH 11/35] Complete sets koans --- python2/koans/about_sets.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/python2/koans/about_sets.py b/python2/koans/about_sets.py index 86403d5d5..d127ed504 100644 --- a/python2/koans/about_sets.py +++ b/python2/koans/about_sets.py @@ -11,13 +11,13 @@ def test_sets_make_keep_lists_unique(self): there_can_only_be_only_one = set(highlanders) - self.assertEqual(__, there_can_only_be_only_one) + self.assertEqual(set(["MacLeod", "Ramirez", "Matunas", "Malcolm"]), there_can_only_be_only_one) def test_sets_are_unordered(self): - self.assertEqual(set([__, __, __, __, __]), set('12345')) + self.assertEqual(set(['1', '3', '2', '5', '4']), set('12345')) def test_convert_the_set_into_a_list_to_sort_it(self): - self.assertEqual(__, sorted(set('13245'))) + self.assertEqual(['1', '2', '3', '4', '5'], sorted(set('13245'))) # ------------------------------------------------------------------ @@ -25,19 +25,19 @@ def test_set_have_arithmetic_operators(self): scotsmen = set(['MacLeod', 'Wallace', 'Willie']) warriors = set(['MacLeod', 'Wallace', 'Leonidas']) - self.assertEqual(__, scotsmen - warriors) - self.assertEqual(__, scotsmen | warriors) - self.assertEqual(__, scotsmen & warriors) - self.assertEqual(__, scotsmen ^ warriors) + self.assertEqual(set(['Willie']), scotsmen - warriors) + self.assertEqual(set(['Wallace', 'MacLeod', 'Willie', 'Leonidas']), scotsmen | warriors) + self.assertEqual(set(['MacLeod', 'Wallace']), scotsmen & warriors) + self.assertEqual(set(['Willie', 'Leonidas']), scotsmen ^ warriors) # ------------------------------------------------------------------ def test_we_can_query_set_membership(self): - self.assertEqual(__, 127 in set([127, 0, 0, 1])) - self.assertEqual(__, 'cow' not in set('apocalypse now')) + self.assertEqual(True, 127 in set([127, 0, 0, 1])) + self.assertEqual(True, 'cow' not in set('apocalypse now')) def test_we_can_compare_subsets(self): - self.assertEqual(__, set('cake') <= set('cherry cake')) - self.assertEqual(__, set('cake').issubset(set('cherry cake'))) + self.assertEqual(True, set('cake') <= set('cherry cake')) + self.assertEqual(True, set('cake').issubset(set('cherry cake'))) - self.assertEqual(__, set('cake') > set('pie')) + self.assertEqual(False, set('cake') > set('pie')) From 008adcbe2f2be903f6c575010124c0cac3ec3d28 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sun, 12 May 2013 22:18:59 +0200 Subject: [PATCH 12/35] Complete triangle koans --- python2/koans/triangle.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/python2/koans/triangle.py b/python2/koans/triangle.py index 8f3faeed3..fe5744714 100644 --- a/python2/koans/triangle.py +++ b/python2/koans/triangle.py @@ -16,12 +16,16 @@ # about_triangle_project.py # and # about_triangle_project_2.py -# + def triangle(a, b, c): - # DELETE 'PASS' AND WRITE THIS CODE - pass + if (a == b and b == c): + return 'equilateral' + if (a == b or b == c or a == c): + return 'isosceles' + return 'scalene' + # Error class used in part 2. No need to change this code. class TriangleError(StandardError): pass From f922acf22ba260333ce165d3b330975397862c16 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sun, 12 May 2013 22:41:36 +0200 Subject: [PATCH 13/35] Complete exceptions koans --- python2/koans/about_exceptions.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/python2/koans/about_exceptions.py b/python2/koans/about_exceptions.py index dd2b3823f..3acb588a7 100644 --- a/python2/koans/about_exceptions.py +++ b/python2/koans/about_exceptions.py @@ -11,10 +11,10 @@ class MySpecialError(RuntimeError): def test_exceptions_inherit_from_exception(self): mro = self.MySpecialError.__mro__ - self.assertEqual(__, mro[1].__name__) - self.assertEqual(__, mro[2].__name__) - self.assertEqual(__, mro[3].__name__) - self.assertEqual(__, mro[4].__name__) + self.assertEqual('RuntimeError', mro[1].__name__) + self.assertEqual('StandardError', mro[2].__name__) + self.assertEqual('Exception', mro[3].__name__) + self.assertEqual('BaseException', mro[4].__name__) def test_try_clause(self): result = None @@ -23,15 +23,15 @@ def test_try_clause(self): except StandardError as ex: result = 'exception handled' - self.assertEqual(__, result) + self.assertEqual('exception handled', result) - self.assertEqual(____, isinstance(ex, StandardError)) - self.assertEqual(____, isinstance(ex, RuntimeError)) + self.assertEqual(True, isinstance(ex, StandardError)) + self.assertEqual(False, isinstance(ex, RuntimeError)) self.assertTrue(issubclass(RuntimeError, StandardError), \ "RuntimeError is a subclass of StandardError") - self.assertEqual(__, ex[0]) + self.assertEqual("Oops", ex[0]) def test_raising_a_specific_error(self): result = None @@ -40,8 +40,8 @@ def test_raising_a_specific_error(self): except self.MySpecialError as ex: result = 'exception handled' - self.assertEqual(__, result) - self.assertEqual(__, ex[0]) + self.assertEqual('exception handled', result) + self.assertEqual('My Message', ex[0]) def test_else_clause(self): result = None @@ -53,7 +53,7 @@ def test_else_clause(self): else: result = 'no damage done' - self.assertEqual(__, result) + self.assertEqual('no damage done', result) def test_finally_clause(self): result = None @@ -65,4 +65,4 @@ def test_finally_clause(self): finally: result = 'always run' - self.assertEqual(__, result) + self.assertEqual('always run', result) From 437ea5a0bf88b21ce666f7188c9855c5a40a7619 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sun, 12 May 2013 23:10:57 +0200 Subject: [PATCH 14/35] Complete second triangle koans --- python2/koans/triangle.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/python2/koans/triangle.py b/python2/koans/triangle.py index fe5744714..6cdff9fb3 100644 --- a/python2/koans/triangle.py +++ b/python2/koans/triangle.py @@ -18,6 +18,12 @@ # about_triangle_project_2.py def triangle(a, b, c): + if (a <= 0 or b <= 0 or c <= 0): + raise TriangleError + + if ((a + b) <= c or (a + c) <= b or (b + c) <= a): + raise TriangleError + if (a == b and b == c): return 'equilateral' From cd9fedf8fd78ab4b08f1a0131a6e723d43e9f90d Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Mon, 13 May 2013 00:04:16 +0200 Subject: [PATCH 15/35] Complete iteration koans --- python2/koans/about_iteration.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/python2/koans/about_iteration.py b/python2/koans/about_iteration.py index 2bf446f41..ee3c90a68 100644 --- a/python2/koans/about_iteration.py +++ b/python2/koans/about_iteration.py @@ -14,20 +14,20 @@ def test_iterators_are_a_type(self): for num in it: fib += num - self.assertEqual(__, fib) + self.assertEqual(15, fib) def test_iterating_with_next(self): stages = iter(['alpha', 'beta', 'gamma']) try: - self.assertEqual(__, next(stages)) + self.assertEqual('alpha', next(stages)) next(stages) - self.assertEqual(__, next(stages)) + self.assertEqual('gamma', next(stages)) next(stages) except StopIteration as ex: err_msg = 'Ran out of iterations' - self.assertMatch(__, err_msg) + self.assertMatch('out of', err_msg) # ------------------------------------------------------------------ @@ -38,7 +38,7 @@ def test_map_transforms_elements_of_a_list(self): seq = [1, 2, 3] mapped_seq = map(self.add_ten, seq) - self.assertEqual(__, mapped_seq) + self.assertEqual([11, 12, 13], mapped_seq) def test_filter_selects_certain_items_from_a_list(self): def is_even(item): @@ -47,7 +47,7 @@ def is_even(item): seq = [1, 2, 3, 4, 5, 6] even_numbers = filter(is_even, seq) - self.assertEqual(__, even_numbers) + self.assertEqual([2, 4, 6], even_numbers) def test_just_return_first_item_found(self): def is_big_name(item): @@ -57,12 +57,12 @@ def is_big_name(item): # NOTE This still iterates through the whole names, so not particularly # efficient - self.assertEqual([__], filter(is_big_name, names)[:1]) + self.assertEqual(['Clarence'], filter(is_big_name, names)[:1]) # Boring but effective for item in names: if is_big_name(item): - self.assertEqual(__, item) + self.assertEqual('Clarence', item) break # ------------------------------------------------------------------ @@ -75,10 +75,10 @@ def multiply(self, accum, item): def test_reduce_will_blow_your_mind(self): result = reduce(self.add, [2, 3, 4]) - self.assertEqual(__, result) + self.assertEqual(9, result) result2 = reduce(self.multiply, [2, 3, 4], 1) - self.assertEqual(__, result2) + self.assertEqual(24, result2) # Extra Credit: # Describe in your own words what reduce does. @@ -91,21 +91,21 @@ def test_creating_lists_with_list_comprehensions(self): comprehension = [delicacy.capitalize() for delicacy in feast] - self.assertEqual(__, comprehension[0]) - self.assertEqual(__, comprehension[2]) + self.assertEqual('Lambs', comprehension[0]) + self.assertEqual('Orangutans', comprehension[2]) def test_use_pass_for_iterations_with_no_body(self): for num in range(1, 5): pass - self.assertEqual(__, num) + self.assertEqual(4, num) # ------------------------------------------------------------------ def test_all_iteration_methods_work_on_any_sequence_not_just_lists(self): # Ranges are an iteratable sequence result = map(self.add_ten, range(1, 4)) - self.assertEqual(__, list(result)) + self.assertEqual([11, 12, 13], list(result)) try: f = open("example_file.txt") @@ -114,7 +114,7 @@ def test_all_iteration_methods_work_on_any_sequence_not_just_lists(self): def make_upcase(line): return line.strip().upper() upcase_lines = map(make_upcase, f.readlines()) - self.assertEqual(__, list(upcase_lines)) + self.assertEqual(["THIS", "IS", "A", "TEST"], list(upcase_lines)) finally: # Arg, this is ugly. # We will figure out how to fix this later. From f91bf7f22835df1477522bcf2d6a58cab9aedd36 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Tue, 14 May 2013 21:36:59 +0200 Subject: [PATCH 16/35] Complete generators koans --- python2/koans/about_generators.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/python2/koans/about_generators.py b/python2/koans/about_generators.py index 5fc239cae..303c0defc 100644 --- a/python2/koans/about_generators.py +++ b/python2/koans/about_generators.py @@ -19,7 +19,7 @@ def test_generating_values_on_the_fly(self): n in ['crunchy', 'veggie', 'danish']) for bacon in bacon_generator: result.append(bacon) - self.assertEqual(__, result) + self.assertEqual(['crunchy bacon', 'veggie bacon', 'danish bacon'], result) def test_generators_are_different_to_list_comprehensions(self): num_list = [x * 2 for x in range(1, 3)] @@ -28,7 +28,7 @@ def test_generators_are_different_to_list_comprehensions(self): self.assertEqual(2, num_list[0]) # A generator has to be iterated through. - self.assertEqual(__, list(num_generator)[0]) + self.assertEqual(2, list(num_generator)[0]) # Both list comprehensions and generators can be iterated # though. However, a generator function is only called on the @@ -43,8 +43,8 @@ def test_generator_expressions_are_a_one_shot_deal(self): attempt1 = list(dynamite) attempt2 = list(dynamite) - self.assertEqual(__, list(attempt1)) - self.assertEqual(__, list(attempt2)) + self.assertEqual(['Boom!', 'Boom!', 'Boom!'], list(attempt1)) + self.assertEqual([], list(attempt2)) # ------------------------------------------------------------------ @@ -58,12 +58,12 @@ def test_generator_method_will_yield_values_during_iteration(self): result = list() for item in self.simple_generator_method(): result.append(item) - self.assertEqual(__, result) + self.assertEqual(['peanut', 'butter', 'and', 'jelly'], result) def test_coroutines_can_take_arguments(self): result = self.simple_generator_method() - self.assertEqual(__, next(result)) - self.assertEqual(__, next(result)) + self.assertEqual('peanut', next(result)) + self.assertEqual('butter', next(result)) result.close() # ------------------------------------------------------------------ @@ -74,7 +74,7 @@ def square_me(self, seq): def test_generator_method_with_parameter(self): result = self.square_me(range(2, 5)) - self.assertEqual(__, list(result)) + self.assertEqual([4, 9, 16], list(result)) # ------------------------------------------------------------------ @@ -87,7 +87,7 @@ def sum_it(self, seq): def test_generator_keeps_track_of_local_variables(self): result = self.sum_it(range(2, 5)) - self.assertEqual(__, list(result)) + self.assertEqual([2, 5, 9], list(result)) # ------------------------------------------------------------------ @@ -105,7 +105,7 @@ def test_generators_can_take_coroutines(self): # section of http://www.python.org/dev/peps/pep-0342/ next(generator) - self.assertEqual(__, generator.send(1 + 2)) + self.assertEqual(3, generator.send(1 + 2)) def test_before_sending_a_value_to_a_generator_next_must_be_called(self): generator = self.generator_with_coroutine() @@ -113,7 +113,7 @@ def test_before_sending_a_value_to_a_generator_next_must_be_called(self): try: generator.send(1 + 2) except TypeError as ex: - self.assertMatch(__, ex[0]) + self.assertMatch('can\'t send', ex[0]) # ------------------------------------------------------------------ @@ -131,11 +131,11 @@ def test_generators_can_see_if_they_have_been_called_with_a_value(self): generator2 = self.yield_tester() next(generator2) - self.assertEqual(__, next(generator2)) + self.assertEqual('no value', next(generator2)) def test_send_none_is_equivalent_to_next(self): generator = self.yield_tester() next(generator) # 'next(generator)' is exactly equivalent to 'generator.send(None)' - self.assertEqual(__, generator.send(None)) + self.assertEqual('no value', generator.send(None)) From 890169d93b59b68563185ce72d57f5e8dd732630 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Tue, 14 May 2013 22:07:08 +0200 Subject: [PATCH 17/35] Complete lambdas koans --- python2/koans/about_lambdas.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python2/koans/about_lambdas.py b/python2/koans/about_lambdas.py index dfc72f9e6..fb8352139 100644 --- a/python2/koans/about_lambdas.py +++ b/python2/koans/about_lambdas.py @@ -11,7 +11,7 @@ class AboutLambdas(Koan): def test_lambdas_can_be_assigned_to_variables_and_called_explicitly(self): add_one = lambda n: n + 1 - self.assertEqual(__, add_one(10)) + self.assertEqual(11, add_one(10)) # ------------------------------------------------------------------ @@ -22,8 +22,8 @@ def test_accessing_lambda_via_assignment(self): sausages = self.make_order('sausage') eggs = self.make_order('egg') - self.assertEqual(__, sausages(3)) - self.assertEqual(__, eggs(2)) + self.assertEqual('3 sausages', sausages(3)) + self.assertEqual('2 eggs', eggs(2)) def test_accessing_lambda_without_assignment(self): - self.assertEqual(__, self.make_order('spam')(39823)) + self.assertEqual('39823 spams', self.make_order('spam')(39823)) From 9f67804128a00a60d37d91b1ee21af0ad336e037 Mon Sep 17 00:00:00 2001 From: david Date: Thu, 23 May 2013 22:01:54 +0200 Subject: [PATCH 18/35] Complete scoring project koans --- python2/koans/about_scoring_project.py | 22 ++++++++++++++++++++-- python3/koans/about_scoring_project.py | 1 - 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/python2/koans/about_scoring_project.py b/python2/koans/about_scoring_project.py index da51563b4..7901a555c 100644 --- a/python2/koans/about_scoring_project.py +++ b/python2/koans/about_scoring_project.py @@ -34,8 +34,26 @@ # Your goal is to write the score method. def score(dice): - # You need to write this method - pass + points = 0 + result = {} + combo3 = { 1: 10, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6 } + combo1 = { 1: 100, 2: 0, 3: 0, 4: 0, 5: 50, 6: 0 } + + for val in dice: + if not(val in result): + result[val] = 0 + result[val] += 1 + + for val in result: + if(result[val] >= 3): + result[val] -= 3 + points += combo3[val] * 100 + if(result[val] > 0): + points += result[val] * combo1[val] + + return points + + class AboutScoringProject(Koan): diff --git a/python3/koans/about_scoring_project.py b/python3/koans/about_scoring_project.py index 2bd06c418..eea35f434 100644 --- a/python3/koans/about_scoring_project.py +++ b/python3/koans/about_scoring_project.py @@ -33,7 +33,6 @@ # Your goal is to write the score method. def score(dice): - # You need to write this method pass class AboutScoringProject(Koan): From 2e147e7c245935c94cd437a02bd6a2fe63f583da Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 10:50:26 +0200 Subject: [PATCH 19/35] Complete about classes koans --- python2/koans/about_classes.py | 40 +++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/python2/koans/about_classes.py b/python2/koans/about_classes.py index 570b03cb0..97adf694d 100644 --- a/python2/koans/about_classes.py +++ b/python2/koans/about_classes.py @@ -10,10 +10,10 @@ class Dog(object): def test_instances_of_classes_can_be_created_adding_parentheses(self): fido = self.Dog() - self.assertEqual(__, fido.__class__) + self.assertEqual(self.Dog, fido.__class__) def test_classes_have_docstrings(self): - self.assertMatch(__, self.Dog.__doc__) + self.assertMatch("Dogs need regular walkies. Never, ever let them drive.", self.Dog.__doc__) # ------------------------------------------------------------------ @@ -26,12 +26,12 @@ def set_name(self, a_name): def test_init_method_is_the_constructor(self): dog = self.Dog2() - self.assertEqual(__, dog._name) + self.assertEqual('Paul', dog._name) def test_private_attributes_are_not_really_private(self): dog = self.Dog2() dog.set_name("Fido") - self.assertEqual(__, dog._name) + self.assertEqual('Fido', dog._name) # The _ prefix in _name implies private ownership, but nothing is truly # private in Python. @@ -39,11 +39,11 @@ def test_you_can_also_access_the_value_out_using_getattr_and_dict(self): fido = self.Dog2() fido.set_name("Fido") - self.assertEqual(__, getattr(fido, "_name")) + self.assertEqual('Fido', getattr(fido, "_name")) # getattr(), setattr() and delattr() are a way of accessing attributes # by method rather than through assignment operators - self.assertEqual(__, fido.__dict__["_name"]) + self.assertEqual('Fido', fido.__dict__["_name"]) # Yes, this works here, but don't rely on the __dict__ object! Some # class implementations use optimization which result in __dict__ not # showing everything. @@ -66,8 +66,8 @@ def test_that_name_can_be_read_as_a_property(self): fido = self.Dog3() fido.set_name("Fido") - self.assertEqual(__, fido.get_name()) # access as method - self.assertEqual(__, fido.name) # access as property + self.assertEqual('Fido', fido.get_name()) # access as method + self.assertEqual('Fido', fido.name) # access as property # ------------------------------------------------------------------ @@ -87,7 +87,7 @@ def test_creating_properties_with_decorators_is_slightly_easier(self): fido = self.Dog4() fido.name = "Fido" - self.assertEqual(__, fido.name) + self.assertEqual("Fido", fido.name) # ------------------------------------------------------------------ @@ -101,10 +101,10 @@ def name(self): def test_init_provides_initial_values_for_instance_variables(self): fido = self.Dog5("Fido") - self.assertEqual(__, fido.name) + self.assertEqual("Fido", fido.name) def test_args_must_match_init(self): - self.assertRaises(___, self.Dog5) # Evaluates self.Dog5() + self.assertRaises(TypeError, self.Dog5) # Evaluates self.Dog5() # THINK ABOUT IT: # Why is this so? @@ -113,7 +113,7 @@ def test_different_objects_have_difference_instance_variables(self): fido = self.Dog5("Fido") rover = self.Dog5("Rover") - self.assertEqual(____, rover.name == fido.name) + self.assertEqual(False, rover.name == fido.name) # ------------------------------------------------------------------ @@ -125,7 +125,7 @@ def get_self(self): return self def __str__(self): - return __ + return self._name def __repr__(self): return "" @@ -133,7 +133,7 @@ def __repr__(self): def test_inside_a_method_self_refers_to_the_containing_object(self): fido = self.Dog6("Fido") - self.assertEqual(__, fido.get_self()) # Not a string! + self.assertEqual(fido, fido.get_self()) # Not a string! def test_str_provides_a_string_version_of_the_object(self): fido = self.Dog6("Fido") @@ -141,17 +141,17 @@ def test_str_provides_a_string_version_of_the_object(self): def test_str_is_used_explicitly_in_string_interpolation(self): fido = self.Dog6("Fido") - self.assertEqual(__, "My dog is " + str(fido)) + self.assertEqual("My dog is Fido", "My dog is " + str(fido)) def test_repr_provides_a_more_complete_string_version(self): fido = self.Dog6("Fido") - self.assertEqual(__, repr(fido)) + self.assertEqual("", repr(fido)) def test_all_objects_support_str_and_repr(self): seq = [1, 2, 3] - self.assertEqual(__, str(seq)) - self.assertEqual(__, repr(seq)) + self.assertEqual('[1, 2, 3]', str(seq)) + self.assertEqual('[1, 2, 3]', repr(seq)) - self.assertEqual(__, str("STRING")) - self.assertEqual(__, repr("STRING")) + self.assertEqual("STRING", str("STRING")) + self.assertEqual("'STRING'", repr("STRING")) From eee3fa07fff56ab3a503ef419c6f98f1516a33c0 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 13:15:46 +0200 Subject: [PATCH 20/35] Complete about new style classes koans --- python2/koans/about_new_style_classes.py | 27 ++++++++++++------------ 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/python2/koans/about_new_style_classes.py b/python2/koans/about_new_style_classes.py index 34b3957b9..c360ff9d4 100644 --- a/python2/koans/about_new_style_classes.py +++ b/python2/koans/about_new_style_classes.py @@ -18,15 +18,15 @@ class NewStyleClass(object): pass def test_new_style_classes_inherit_from_object_base_class(self): - self.assertEqual(____, issubclass(self.NewStyleClass, object)) - self.assertEqual(____, issubclass(self.OldStyleClass, object)) + self.assertEqual(True, issubclass(self.NewStyleClass, object)) + self.assertEqual(False, issubclass(self.OldStyleClass, object)) def test_new_style_classes_have_more_attributes(self): - self.assertEqual(__, len(dir(self.OldStyleClass))) - self.assertEqual(__, self.OldStyleClass.__doc__) - self.assertEqual(__, self.OldStyleClass.__module__) + self.assertEqual(2, len(dir(self.OldStyleClass))) + self.assertEqual("An old style class", self.OldStyleClass.__doc__) + self.assertEqual('koans.about_new_style_classes', self.OldStyleClass.__module__) - self.assertEqual(__, len(dir(self.NewStyleClass))) + self.assertEqual(18, len(dir(self.NewStyleClass))) # To examine the available attributes, run # 'dir()' # from a python console @@ -34,29 +34,30 @@ def test_new_style_classes_have_more_attributes(self): # ------------------------------------------------------------------ def test_old_style_classes_have_type_but_no_class_attribute(self): - self.assertEqual(__, self.OldStyleClass.__class__) + import new + self.assertEqual(new.classobj, type(self.OldStyleClass)) try: cls = self.OldStyleClass.__class__ except Exception as ex: pass - self.assertMatch(__, ex[0]) + self.assertMatch('has no attribute', ex[0]) def test_new_style_classes_have_same_class_as_type(self): new_style = self.NewStyleClass() - self.assertEqual(__, self.NewStyleClass.__class__) + self.assertEqual(type, self.NewStyleClass.__class__) self.assertEqual( - __, + True, type(self.NewStyleClass) == self.NewStyleClass.__class__) # ------------------------------------------------------------------ def test_in_old_style_instances_class_is_different_to_type(self): old_style = self.OldStyleClass() - self.assertEqual(__, old_style.__class__) + self.assertEqual(self.OldStyleClass, old_style.__class__) def test_new_style_instances_have_same_class_as_type(self): new_style = self.NewStyleClass() - self.assertEqual(__, new_style.__class__) - self.assertEqual(__, type(new_style) == new_style.__class__) + self.assertEqual(self.NewStyleClass, new_style.__class__) + self.assertEqual(True, type(new_style) == new_style.__class__) From 80581d1c65479c070495db8eee22be647bbf1f75 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 13:19:52 +0200 Subject: [PATCH 21/35] complete with statements koans --- python2/koans/about_with_statements.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/python2/koans/about_with_statements.py b/python2/koans/about_with_statements.py index 15cfbaf69..142c89a76 100644 --- a/python2/koans/about_with_statements.py +++ b/python2/koans/about_with_statements.py @@ -26,7 +26,7 @@ def count_lines(self, file_name): self.fail() def test_counting_lines(self): - self.assertEqual(__, self.count_lines("example_file.txt")) + self.assertEqual(4, self.count_lines("example_file.txt")) # ------------------------------------------------------------------ @@ -45,7 +45,7 @@ def find_line(self, file_name): self.fail() def test_finding_lines(self): - self.assertEqual(__, self.find_line("example_file.txt")) + self.assertEqual("test\n", self.find_line("example_file.txt")) ## ------------------------------------------------------------------ ## THINK ABOUT IT: @@ -92,16 +92,21 @@ def count_lines2(self, file_name): return count def test_counting_lines2(self): - self.assertEqual(__, self.count_lines2("example_file.txt")) + self.assertEqual(4, self.count_lines2("example_file.txt")) # ------------------------------------------------------------------ def find_line2(self, file_name): # Rewrite find_line using the Context Manager. - pass + with self.FileContextManager(file_name) as f: + for line in f.readlines(): + match = re.search('e', line) + if match: + return line + def test_finding_lines2(self): - self.assertEqual(__, self.find_line2("example_file.txt")) + self.assertEqual('test\n', self.find_line2("example_file.txt")) self.assertNotEqual(None, self.find_line2("example_file.txt")) # ------------------------------------------------------------------ @@ -114,4 +119,4 @@ def count_lines3(self, file_name): return count def test_open_already_has_its_own_built_in_context_manager(self): - self.assertEqual(__, self.count_lines3("example_file.txt")) + self.assertEqual(4, self.count_lines3("example_file.txt")) From 5dcf38b3b3452f72a877dc267ec8dcb2c3804f5e Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 13:21:32 +0200 Subject: [PATCH 22/35] Complete about monkey patching --- python2/koans/about_monkey_patching.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/python2/koans/about_monkey_patching.py b/python2/koans/about_monkey_patching.py index 65bd461d9..8f4a7cad4 100644 --- a/python2/koans/about_monkey_patching.py +++ b/python2/koans/about_monkey_patching.py @@ -15,7 +15,7 @@ def bark(self): def test_as_defined_dogs_do_bark(self): fido = self.Dog() - self.assertEqual(__, fido.bark()) + self.assertEqual('WOOF', fido.bark()) # ------------------------------------------------------------------ @@ -27,8 +27,8 @@ def wag(self): self.Dog.wag = wag fido = self.Dog() - self.assertEqual(__, fido.wag()) - self.assertEqual(__, fido.bark()) + self.assertEqual('HAPPY', fido.wag()) + self.assertEqual('WOOF', fido.bark()) # ------------------------------------------------------------------ @@ -36,7 +36,7 @@ def test_most_built_in_classes_cannot_be_monkey_patched(self): try: int.is_even = lambda self: (self % 2) == 0 except StandardError as ex: - self.assertMatch(__, ex[0]) + self.assertMatch("can't set attributes", ex[0]) # ------------------------------------------------------------------ @@ -46,5 +46,5 @@ class MyInt(int): def test_subclasses_of_built_in_classes_can_be_be_monkey_patched(self): self.MyInt.is_even = lambda self: (self % 2) == 0 - self.assertEqual(____, self.MyInt(1).is_even()) - self.assertEqual(____, self.MyInt(2).is_even()) + self.assertEqual(False, self.MyInt(1).is_even()) + self.assertEqual(True, self.MyInt(2).is_even()) From 9e97564e08f1e4bbe8b3aa4390bd045748ae154c Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 13:25:18 +0200 Subject: [PATCH 23/35] Complete about Dice project koans --- python2/koans/about_dice_project.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python2/koans/about_dice_project.py b/python2/koans/about_dice_project.py index c2710075f..d3f4683e0 100644 --- a/python2/koans/about_dice_project.py +++ b/python2/koans/about_dice_project.py @@ -17,7 +17,7 @@ def values(self): def roll(self, n): # Needs implementing! # Tip: random.randint(min, max) can be used to generate random numbers - pass + self._values = [random.randint(1, 6) for k in range(1, n + 1)] class AboutDiceProject(Koan): From ec0d3e963e3e787f8c041b01175a8581f52aeee2 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 13:34:40 +0200 Subject: [PATCH 24/35] complete about method bindings koans --- python2/koans/about_method_bindings.py | 28 +++++++++++++------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/python2/koans/about_method_bindings.py b/python2/koans/about_method_bindings.py index d41263dab..2c8d0edb3 100644 --- a/python2/koans/about_method_bindings.py +++ b/python2/koans/about_method_bindings.py @@ -20,47 +20,47 @@ def method(self): class AboutMethodBindings(Koan): def test_methods_are_bound_to_an_object(self): obj = Class() - self.assertEqual(__, obj.method.im_self == obj) + self.assertEqual(True, obj.method.im_self == obj) def test_methods_are_also_bound_to_a_function(self): obj = Class() - self.assertEqual(__, obj.method()) - self.assertEqual(__, obj.method.im_func(obj)) + self.assertEqual('parrot', obj.method()) + self.assertEqual('parrot', obj.method.im_func(obj)) def test_functions_have_attributes(self): - self.assertEqual(__, len(dir(function))) - self.assertEqual(__, dir(function) == dir(Class.method.im_func)) + self.assertEqual(31, len(dir(function))) + self.assertEqual(True, dir(function) == dir(Class.method.im_func)) def test_bound_methods_have_different_attributes(self): obj = Class() - self.assertEqual(__, len(dir(obj.method))) + self.assertEqual(23, len(dir(obj.method))) def test_setting_attributes_on_an_unbound_function(self): function.cherries = 3 - self.assertEqual(__, function.cherries) + self.assertEqual(3, function.cherries) def test_setting_attributes_on_a_bound_method_directly(self): obj = Class() try: obj.method.cherries = 3 except AttributeError as ex: - self.assertMatch(__, ex[0]) + self.assertMatch('object has no attribute', ex[0]) def test_setting_attributes_on_methods_by_accessing_the_inner_function(self): obj = Class() obj.method.im_func.cherries = 3 - self.assertEqual(__, obj.method.cherries) + self.assertEqual(3, obj.method.cherries) def test_functions_can_have_inner_functions(self): function2.get_fruit = function - self.assertEqual(__, function2.get_fruit()) + self.assertEqual('pineapple', function2.get_fruit()) def test_inner_functions_are_unbound(self): function2.get_fruit = function try: cls = function2.get_fruit.im_self except AttributeError as ex: - self.assertMatch(__, ex[0]) + self.assertMatch('object has no attribute', ex[0]) # ------------------------------------------------------------------ @@ -77,8 +77,8 @@ def test_get_descriptor_resolves_attribute_binding(self): # binding_owner = obj # owner_type = cls - self.assertEqual(__, bound_obj.__class__) - self.assertEqual(__, binding_owner.__class__) + self.assertEqual(self.BoundClass, bound_obj.__class__) + self.assertEqual(AboutMethodBindings, binding_owner.__class__) self.assertEqual(AboutMethodBindings, owner_type) # ------------------------------------------------------------------ @@ -95,4 +95,4 @@ def __set__(self, obj, val): def test_set_descriptor_changes_behavior_of_attribute_assignment(self): self.assertEqual(None, self.color.choice) self.color = 'purple' - self.assertEqual(__, self.color.choice) + self.assertEqual('purple', self.color.choice) From 36a443cacd13dedf1506544df78e73b61969521c Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 13:37:02 +0200 Subject: [PATCH 25/35] Complete about decorating with functions koans --- python2/koans/about_decorating_with_functions.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python2/koans/about_decorating_with_functions.py b/python2/koans/about_decorating_with_functions.py index c2bfe28f4..f44a008e9 100644 --- a/python2/koans/about_decorating_with_functions.py +++ b/python2/koans/about_decorating_with_functions.py @@ -14,8 +14,8 @@ def mediocre_song(self): return "o/~ We all live in a broken submarine o/~" def test_decorators_can_modify_a_function(self): - self.assertMatch(__, self.mediocre_song()) - self.assertEqual(__, self.mediocre_song.wow_factor) + self.assertMatch("o/~ We all live in a broken submarine o/~", self.mediocre_song()) + self.assertEqual("COWBELL BABY!", self.mediocre_song.wow_factor) # ------------------------------------------------------------------ @@ -29,4 +29,4 @@ def render_tag(self, name): return name def test_decorators_can_change_a_function_output(self): - self.assertEqual(__, self.render_tag('llama')) + self.assertEqual('', self.render_tag('llama')) From f6bbb2bc2b487eb484fcda5e0647de5ec4b58265 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 13:44:17 +0200 Subject: [PATCH 26/35] Complete about decorating with classes koans --- .../koans/about_decorating_with_classes.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/python2/koans/about_decorating_with_classes.py b/python2/koans/about_decorating_with_classes.py index b8f67a573..cba01f8d1 100644 --- a/python2/koans/about_decorating_with_classes.py +++ b/python2/koans/about_decorating_with_classes.py @@ -20,21 +20,21 @@ def test_partial_that_wrappers_no_args(self): """ max = functools.partial(self.maximum) - self.assertEqual(__, max(7, 23)) - self.assertEqual(__, max(10, -10)) + self.assertEqual(23, max(7, 23)) + self.assertEqual(10, max(10, -10)) def test_partial_that_wrappers_first_arg(self): max0 = functools.partial(self.maximum, 0) - self.assertEqual(__, max0(-4)) - self.assertEqual(__, max0(5)) + self.assertEqual(0, max0(-4)) + self.assertEqual(5, max0(5)) def test_partial_that_wrappers_all_args(self): always99 = functools.partial(self.maximum, 99, 20) always20 = functools.partial(self.maximum, 9, 20) - self.assertEqual(__, always99()) - self.assertEqual(__, always20()) + self.assertEqual(99, always99()) + self.assertEqual(20, always20()) # ------------------------------------------------------------------ @@ -65,8 +65,8 @@ def test_decorator_with_no_arguments(self): # To clarify: the decorator above the function has no arguments, even # if the decorated function does - self.assertEqual(__, self.foo()) - self.assertEqual(__, self.parrot('pieces of eight')) + self.assertEqual('foo, foo', self.foo()) + self.assertEqual('PIECES OF EIGHT, PIECES OF EIGHT', self.parrot('pieces of eight')) # ------------------------------------------------------------------ @@ -78,7 +78,7 @@ def test_what_a_decorator_is_doing_to_a_function(self): #wrap the function with the decorator self.sound_check = self.doubleit(self.sound_check) - self.assertEqual(__, self.sound_check()) + self.assertEqual('Testing..., Testing...', self.sound_check()) # ------------------------------------------------------------------ @@ -110,11 +110,11 @@ def idler(self, num): pass def test_decorator_with_an_argument(self): - self.assertEqual(__, self.count_badly(2)) - self.assertEqual(__, self.count_badly.__doc__) + self.assertEqual(5, self.count_badly(2)) + self.assertEqual("Increments a value by one. Kind of.", self.count_badly.__doc__) def test_documentor_which_already_has_a_docstring(self): - self.assertEqual(__, self.idler.__doc__) + self.assertEqual('Idler: Does nothing', self.idler.__doc__) # ------------------------------------------------------------------ @@ -125,5 +125,5 @@ def homer(self): return "D'oh" def test_we_can_chain_decorators(self): - self.assertEqual(__, self.homer()) - self.assertEqual(__, self.homer.__doc__) + self.assertEqual("D'oh, D'oh, D'oh, D'oh", self.homer()) + self.assertEqual("DOH!", self.homer.__doc__) From b11f497a73fe431e02617b73766df5658828d1e0 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 13:49:31 +0200 Subject: [PATCH 27/35] Complete about inheritance koans --- python2/koans/about_inheritance.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/python2/koans/about_inheritance.py b/python2/koans/about_inheritance.py index e4310ad9a..3eb29b19b 100644 --- a/python2/koans/about_inheritance.py +++ b/python2/koans/about_inheritance.py @@ -24,31 +24,31 @@ def bark(self): return "yip" def test_subclasses_have_the_parent_as_an_ancestor(self): - self.assertEqual(____, issubclass(self.Chihuahua, self.Dog)) + self.assertEqual(True, issubclass(self.Chihuahua, self.Dog)) def test_this_subclass_ultimately_inherits_from_object_class(self): - self.assertEqual(____, issubclass(self.Chihuahua, object)) + self.assertEqual(True, issubclass(self.Chihuahua, object)) def test_instances_inherit_behavior_from_parent_class(self): chico = self.Chihuahua("Chico") - self.assertEqual(__, chico.name) + self.assertEqual('Chico', chico.name) def test_subclasses_add_new_behavior(self): chico = self.Chihuahua("Chico") - self.assertEqual(__, chico.wag()) + self.assertEqual('happy', chico.wag()) try: fido = self.Dog("Fido") fido.wag() except StandardError as ex: - self.assertMatch(__, ex[0]) + self.assertMatch('has no attribute', ex[0]) def test_subclasses_can_modify_existing_behavior(self): chico = self.Chihuahua("Chico") - self.assertEqual(__, chico.bark()) + self.assertEqual('yip', chico.bark()) fido = self.Dog("Fido") - self.assertEqual(__, fido.bark()) + self.assertEqual('WOOF', fido.bark()) # ------------------------------------------------------------------ @@ -58,7 +58,7 @@ def bark(self): def test_subclasses_can_invoke_parent_behavior_via_super(self): ralph = self.BullDog("Ralph") - self.assertEqual(__, ralph.bark()) + self.assertEqual('WOOF, GRR', ralph.bark()) # ------------------------------------------------------------------ @@ -68,7 +68,7 @@ def growl(self): def test_super_works_across_methods(self): george = self.GreatDane("George") - self.assertEqual(__, george.growl()) + self.assertEqual('WOOF, GROWL', george.growl()) # --------------------------------------------------------- @@ -85,8 +85,8 @@ def test_base_init_does_not_get_called_automatically(self): try: name = snoopy.name except Exception as ex: - self.assertMatch(__, ex[0]) + self.assertMatch('has no attribute', ex[0]) def test_base_init_has_to_be_called_explicitly(self): boxer = self.Greyhound("Boxer") - self.assertEqual(__, boxer.name) + self.assertEqual('Boxer', boxer.name) From f18389ba81351f0152b099f5b74b860e32897c71 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 13:56:29 +0200 Subject: [PATCH 28/35] Complete about multiple inheritance --- python2/koans/about_multiple_inheritance.py | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/python2/koans/about_multiple_inheritance.py b/python2/koans/about_multiple_inheritance.py index b204c85c5..446a93e6c 100644 --- a/python2/koans/about_multiple_inheritance.py +++ b/python2/koans/about_multiple_inheritance.py @@ -88,7 +88,7 @@ def here(self): def test_normal_methods_are_available_in_the_object(self): jeff = self.Spiderpig() - self.assertMatch(__, jeff.speak()) + self.assertMatch('This looks like a job for Spiderpig!', jeff.speak()) def test_base_class_methods_are_also_available_in_the_object(self): jeff = self.Spiderpig() @@ -96,22 +96,22 @@ def test_base_class_methods_are_also_available_in_the_object(self): jeff.set_name("Rover") except: self.fail("This should not happen") - self.assertEqual(____, jeff.can_climb_walls()) + self.assertEqual(True, jeff.can_climb_walls()) def test_base_class_methods_can_affect_instance_variables_in_the_object(self): jeff = self.Spiderpig() - self.assertEqual(__, jeff.name) + self.assertEqual('Jeff', jeff.name) jeff.set_name("Rover") - self.assertEqual(__, jeff.name) + self.assertEqual('Rover', jeff.name) def test_left_hand_side_inheritance_tends_to_be_higher_priority(self): jeff = self.Spiderpig() - self.assertEqual(__, jeff.color()) + self.assertEqual('pink', jeff.color()) def test_super_class_methods_are_higher_priority_than_super_super_classes(self): jeff = self.Spiderpig() - self.assertEqual(__, jeff.legs()) + self.assertEqual(8, jeff.legs()) def test_we_can_inspect_the_method_resolution_order(self): # @@ -120,10 +120,10 @@ def test_we_can_inspect_the_method_resolution_order(self): mro = type(self.Spiderpig()).__mro__ self.assertEqual('Spiderpig', mro[0].__name__) self.assertEqual('Pig', mro[1].__name__) - self.assertEqual(__, mro[2].__name__) - self.assertEqual(__, mro[3].__name__) - self.assertEqual(__, mro[4].__name__) - self.assertEqual(__, mro[5].__name__) + self.assertEqual('Spider', mro[2].__name__) + self.assertEqual('Animal', mro[3].__name__) + self.assertEqual('Nameable', mro[4].__name__) + self.assertEqual('object', mro[5].__name__) def test_confirm_the_mro_controls_the_calling_order(self): jeff = self.Spiderpig() @@ -133,7 +133,7 @@ def test_confirm_the_mro_controls_the_calling_order(self): self.assertMatch('Pig', next.here()) next = super(AboutMultipleInheritance.Pig, jeff) - self.assertMatch(__, next.here()) + self.assertMatch('Spider', next.here()) # Hang on a minute?!? That last class name might be a super class of # the 'jeff' object, but its hardly a superclass of Pig, is it? From 90d1c12635502fd56e80d39769b279042e3be6af Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 13:59:55 +0200 Subject: [PATCH 29/35] Complete about scope koans --- python2/koans/about_scope.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/python2/koans/about_scope.py b/python2/koans/about_scope.py index 0e7666124..03aab3bed 100644 --- a/python2/koans/about_scope.py +++ b/python2/koans/about_scope.py @@ -20,18 +20,18 @@ def test_dog_is_not_available_in_the_current_scope(self): try: fido = Dog() except Exception as ex: - self.assertMatch(__, ex[0]) + self.assertMatch('global name', ex[0]) def test_you_can_reference_nested_classes_using_the_scope_operator(self): fido = jims.Dog() # name 'jims' module name is taken from jim.py filename rover = joes.Dog() - self.assertEqual(__, fido.identify()) - self.assertEqual(__, rover.identify()) + self.assertEqual('jims dog', fido.identify()) + self.assertEqual('joes dog', rover.identify()) - self.assertEqual(____, type(fido) == type(rover)) - self.assertEqual(____, jims.Dog == joes.Dog) + self.assertEqual(False, type(fido) == type(rover)) + self.assertEqual(False, jims.Dog == joes.Dog) # ------------------------------------------------------------------ @@ -39,26 +39,26 @@ class str(object): pass def test_bare_bones_class_names_do_not_assume_the_current_scope(self): - self.assertEqual(____, AboutScope.str == str) + self.assertEqual(False, AboutScope.str == str) def test_nested_string_is_not_the_same_as_the_system_string(self): - self.assertEqual(____, self.str == type("HI")) + self.assertEqual(False, self.str == type("HI")) def test_str_without_self_prefix_stays_in_the_global_scope(self): - self.assertEqual(____, str == type("HI")) + self.assertEqual(True, str == type("HI")) # ------------------------------------------------------------------ PI = 3.1416 def test_constants_are_defined_with_an_initial_uppercase_letter(self): - self.assertAlmostEqual(_____, self.PI) + self.assertAlmostEqual(3.1416, self.PI) # Note, floating point numbers in python are not precise. # assertAlmostEqual will check that it is 'close enough' def test_constants_are_assumed_by_convention_only(self): self.PI = "rhubarb" - self.assertEqual(_____, self.PI) + self.assertEqual('rhubarb', self.PI) # There aren't any real constants in python. Its up to the developer # to keep to the convention and not modify them. @@ -75,13 +75,13 @@ def test_incrementing_with_local_counter(self): global counter start = counter self.increment_using_local_counter(start) - self.assertEqual(____, counter == start + 1) + self.assertEqual(False, counter == start + 1) def test_incrementing_with_global_counter(self): global counter start = counter self.increment_using_global_counter() - self.assertEqual(____, counter == start + 1) + self.assertEqual(True, counter == start + 1) # ------------------------------------------------------------------ @@ -89,4 +89,4 @@ def test_incrementing_with_global_counter(self): deadly_bingo = [4, 8, 15, 16, 23, 42] def test_global_attributes_can_be_created_in_the_middle_of_a_class(self): - self.assertEqual(__, deadly_bingo[5]) + self.assertEqual(42, deadly_bingo[5]) From db4e3924cb66e444fe942bfc2364b902d6cc394d Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 14:03:22 +0200 Subject: [PATCH 30/35] Complete about modules koans --- python2/koans/about_modules.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/python2/koans/about_modules.py b/python2/koans/about_modules.py index ddd1be9ff..88a42d584 100644 --- a/python2/koans/about_modules.py +++ b/python2/koans/about_modules.py @@ -17,21 +17,21 @@ def test_importing_other_python_scripts_as_modules(self): import local_module # local_module.py duck = local_module.Duck() - self.assertEqual(__, duck.name) + self.assertEqual('Daffy', duck.name) def test_importing_attributes_from_classes_using_from_keyword(self): from local_module import Duck duck = Duck() # no module qualifier needed this time - self.assertEqual(__, duck.name) + self.assertEqual('Daffy', duck.name) def test_we_can_import_multiple_items_at_once(self): import jims, joes jims_dog = jims.Dog() joes_dog = joes.Dog() - self.assertEqual(__, jims_dog.identify()) - self.assertEqual(__, joes_dog.identify()) + self.assertEqual('jims dog', jims_dog.identify()) + self.assertEqual('joes dog', joes_dog.identify()) def test_importing_all_module_attributes_at_once(self): """ @@ -43,20 +43,20 @@ def test_importing_all_module_attributes_at_once(self): goose = Goose() hamster = Hamster() - self.assertEqual(__, goose.name) - self.assertEqual(__, hamster.name) + self.assertEqual('Mr Stabby', goose.name) + self.assertEqual('Phil', hamster.name) def test_modules_hide_attributes_prefixed_by_underscores(self): try: private_squirrel = _SecretSquirrel() except NameError as ex: - self.assertMatch(__, ex[0]) + self.assertMatch('global name', ex[0]) def test_private_attributes_are_still_accessible_in_modules(self): from local_module import Duck # local_module.py duck = Duck() - self.assertEqual(__, duck._password) + self.assertEqual('password', duck._password) # module level attribute hiding doesn't affect class attributes # (unless the class itself is hidden). @@ -65,14 +65,14 @@ def test_a_modules_XallX_statement_limits_what_wildcards_will_match(self): # 'Goat' is on the __all__ list goat = Goat() - self.assertEqual(__, goat.name) + self.assertEqual('George', goat.name) # How about velociraptors? lizard = _Velociraptor() - self.assertEqual(__, lizard.name) + self.assertEqual('Cuddles', lizard.name) # SecretDuck? Never heard of her! try: duck = SecretDuck() except NameError as ex: - self.assertMatch(__, ex[0]) + self.assertMatch('is not defined', ex[0]) From 6a2df20ecdc2106fd91df38ce6ed81cb9652cf63 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 14:06:36 +0200 Subject: [PATCH 31/35] complete about packages koans --- python2/koans/about_packages.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python2/koans/about_packages.py b/python2/koans/about_packages.py index 99293ebd7..d40aba33a 100644 --- a/python2/koans/about_packages.py +++ b/python2/koans/about_packages.py @@ -30,20 +30,20 @@ def test_subfolders_can_form_part_of_a_module_package(self): from a_package_folder.a_module import Duck duck = Duck() - self.assertEqual(__, duck.name) + self.assertEqual('Donald', duck.name) def test_subfolders_become_modules_if_they_have_an_init_module(self): # Import ./a_package_folder/__init__.py from a_package_folder import an_attribute - self.assertEqual(__, an_attribute) + self.assertEqual(1984, an_attribute) def test_subfolders_without_an_init_module_are_not_part_of_the_package(self): # Import ./a_normal_folder/ try: import a_normal_folder except ImportError as ex: - self.assertMatch(__, ex[0]) + self.assertMatch('No module', ex[0]) # ------------------------------------------------------------------ @@ -51,7 +51,7 @@ def test_use_absolute_imports_to_import_upper_level_modules(self): # Import /contemplate_koans.py import contemplate_koans - self.assertEqual(__, contemplate_koans.__name__) + self.assertEqual('contemplate_koans', contemplate_koans.__name__) # contemplate_koans.py is the root module in this package because its # the first python module called in koans. @@ -65,4 +65,4 @@ def test_import_a_module_in_a_subfolder_using_an_absolute_path(self): # Import contemplate_koans.py/koans/a_package_folder/a_module.py from koans.a_package_folder.a_module import Duck - self.assertEqual(__, Duck.__module__) + self.assertEqual('koans.a_package_folder.a_module', Duck.__module__) From 8bba7af9ef8c358bd859a26d4ccb1bde1064c844 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 14:15:57 +0200 Subject: [PATCH 32/35] complete about class attributes koans --- python2/koans/about_class_attributes.py | 44 ++++++++++++------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/python2/koans/about_class_attributes.py b/python2/koans/about_class_attributes.py index d48211054..1695a64dd 100644 --- a/python2/koans/about_class_attributes.py +++ b/python2/koans/about_class_attributes.py @@ -17,36 +17,36 @@ def test_new_style_class_objects_are_objects(self): # phased out it Python 3. fido = self.Dog() - self.assertEqual(__, isinstance(fido, object)) + self.assertEqual(True, isinstance(fido, object)) def test_classes_are_types(self): - self.assertEqual(__, self.Dog.__class__ == type) + self.assertEqual(True, self.Dog.__class__ == type) def test_classes_are_objects_too(self): - self.assertEqual(__, issubclass(self.Dog, object)) + self.assertEqual(True, issubclass(self.Dog, object)) def test_objects_have_methods(self): fido = self.Dog() - self.assertEqual(__, len(dir(fido))) + self.assertEqual(18, len(dir(fido))) def test_classes_have_methods(self): - self.assertEqual(__, len(dir(self.Dog))) + self.assertEqual(18, len(dir(self.Dog))) def test_creating_objects_without_defining_a_class(self): singularity = object() - self.assertEqual(__, len(dir(singularity))) + self.assertEqual(15, len(dir(singularity))) def test_defining_attributes_on_individual_objects(self): fido = self.Dog() fido.legs = 4 - self.assertEqual(__, fido.legs) + self.assertEqual(4, fido.legs) def test_defining_functions_on_individual_objects(self): fido = self.Dog() fido.wag = lambda: 'fidos wag' - self.assertEqual(__, fido.wag()) + self.assertEqual('fidos wag', fido.wag()) def test_other_objects_are_not_affected_by_these_singleton_functions(self): fido = self.Dog() @@ -59,7 +59,7 @@ def wag(): try: rover.wag() except Exception as ex: - self.assertMatch(__, ex[0]) + self.assertMatch('has no attribute', ex[0]) # ------------------------------------------------------------------ @@ -82,19 +82,19 @@ def growl(cls): return "classmethod growl, arg: cls=" + cls.__name__ def test_like_all_objects_classes_can_have_singleton_methods(self): - self.assertMatch(__, self.Dog2.growl()) + self.assertMatch('classmethod growl, arg: cls=Dog2', self.Dog2.growl()) def test_classmethods_are_not_independent_of_instance_methods(self): fido = self.Dog2() - self.assertMatch(__, fido.growl()) - self.assertMatch(__, self.Dog2.growl()) + self.assertMatch('classmethod growl', fido.growl()) + self.assertMatch('classmethod growl', self.Dog2.growl()) def test_staticmethods_are_unbound_functions_housed_in_a_class(self): - self.assertMatch(__, self.Dog2.bark()) + self.assertMatch('staticmethod bark', self.Dog2.bark()) def test_staticmethods_also_overshadow_instance_methods(self): fido = self.Dog2() - self.assertMatch(__, fido.bark()) + self.assertMatch('staticmethod bark', fido.bark()) # ------------------------------------------------------------------ @@ -125,20 +125,20 @@ def test_classmethods_can_not_be_used_as_properties(self): try: fido.name = "Fido" except Exception as ex: - self.assertMatch(__, ex[0]) + self.assertMatch('is not callable', ex[0]) def test_classes_and_instances_do_not_share_instance_attributes(self): fido = self.Dog3() fido.set_name_from_instance("Fido") fido.set_name("Rover") - self.assertEqual(__, fido.get_name_from_instance()) - self.assertEqual(__, self.Dog3.get_name()) + self.assertEqual('Fido', fido.get_name_from_instance()) + self.assertEqual('Rover', self.Dog3.get_name()) def test_classes_and_instances_do_share_class_attributes(self): fido = self.Dog3() fido.set_name("Fido") - self.assertEqual(__, fido.get_name()) - self.assertEqual(__, self.Dog3.get_name()) + self.assertEqual('Fido', fido.get_name()) + self.assertEqual('Fido', self.Dog3.get_name()) # ------------------------------------------------------------------ @@ -153,13 +153,13 @@ def a_static_method(): a_static_method = staticmethod(a_static_method) def test_you_can_define_class_methods_without_using_a_decorator(self): - self.assertEqual(__, self.Dog4.a_class_method()) + self.assertEqual('dogs class method', self.Dog4.a_class_method()) def test_you_can_define_static_methods_without_using_a_decorator(self): - self.assertEqual(__, self.Dog4.a_static_method()) + self.assertEqual('dogs static method', self.Dog4.a_static_method()) # ------------------------------------------------------------------ def test_you_can_explicitly_call_class_methods_from_instance_methods(self): fido = self.Dog4() - self.assertEqual(__, fido.__class__.a_class_method()) + self.assertEqual('dogs class method', fido.__class__.a_class_method()) From 5fe7e40caf8c7cc681861aba5dcee659b2de5cc6 Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 15:17:52 +0200 Subject: [PATCH 33/35] Complete about attribute access koans --- python2/koans/about_attribute_access.py | 38 ++++++++++++------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/python2/koans/about_attribute_access.py b/python2/koans/about_attribute_access.py index fcd66a79d..2e32a52ae 100644 --- a/python2/koans/about_attribute_access.py +++ b/python2/koans/about_attribute_access.py @@ -19,8 +19,8 @@ def test_calling_undefined_functions_normally_results_in_errors(self): try: typical.foobar() except Exception as exception: - self.assertEqual(__, exception.__class__) - self.assertMatch(__, exception[0]) + self.assertEqual(AttributeError, exception.__class__) + self.assertMatch('has no attribute', exception[0]) def test_calling_getattribute_causes_an_attribute_error(self): typical = self.TypicalObject() @@ -28,7 +28,7 @@ def test_calling_getattribute_causes_an_attribute_error(self): try: typical.__getattribute__('foobar') except AttributeError as exception: - self.assertMatch(__, exception[0]) + self.assertMatch('has no attribute', exception[0]) # THINK ABOUT IT: # @@ -45,17 +45,17 @@ def __getattribute__(self, attr_name): def test_all_attribute_reads_are_caught(self): catcher = self.CatchAllAttributeReads() - self.assertMatch(__, catcher.foobar) + self.assertMatch('Someone called', catcher.foobar) def test_intercepting_return_values_can_disrupt_the_call_chain(self): catcher = self.CatchAllAttributeReads() - self.assertMatch(__, catcher.foobaz) # This is fine + self.assertMatch('Someone called', catcher.foobaz) # This is fine try: catcher.foobaz(1) except TypeError as ex: - self.assertMatch(__, ex[0]) + self.assertMatch('is not callable', ex[0]) # foobaz returns a string. What happens to the '(1)' part? # Try entering this into a python console to reproduce the issue: @@ -66,7 +66,7 @@ def test_intercepting_return_values_can_disrupt_the_call_chain(self): def test_changing_getattribute_will_affect__the_getattr_function(self): catcher = self.CatchAllAttributeReads() - self.assertMatch(__, getattr(catcher, 'any_attribute')) + self.assertMatch('Someone called', getattr(catcher, 'any_attribute')) # ------------------------------------------------------------------ @@ -82,8 +82,8 @@ def __getattribute__(self, attr_name): def test_foo_attributes_are_caught(self): catcher = self.WellBehavedFooCatcher() - self.assertEqual(__, catcher.foo_bar) - self.assertEqual(__, catcher.foo_baz) + self.assertEqual('Foo to you too', catcher.foo_bar) + self.assertEqual('Foo to you too', catcher.foo_baz) def test_non_foo_messages_are_treated_normally(self): catcher = self.WellBehavedFooCatcher() @@ -91,7 +91,7 @@ def test_non_foo_messages_are_treated_normally(self): try: catcher.normal_undefined_attribute except AttributeError as ex: - self.assertMatch(__, ex[0]) + self.assertMatch('has no attribute', ex[0]) # ------------------------------------------------------------------ @@ -130,7 +130,7 @@ def test_getattribute_is_a_bit_overzealous_sometimes(self): catcher = self.RecursiveCatcher() catcher.my_method() global stack_depth - self.assertEqual(__, stack_depth) + self.assertEqual(11, stack_depth) # ------------------------------------------------------------------ @@ -152,17 +152,17 @@ def test_getattr_ignores_known_attributes(self): catcher = self.MinimalCatcher() catcher.my_method() - self.assertEqual(__, catcher.no_of_getattr_calls) + self.assertEqual(0, catcher.no_of_getattr_calls) def test_getattr_only_catches_unknown_attributes(self): catcher = self.MinimalCatcher() catcher.purple_flamingos() catcher.free_pie() - self.assertEqual(__, + self.assertEqual(self.MinimalCatcher.DuffObject, catcher.give_me_duff_or_give_me_death().__class__) - self.assertEqual(__, catcher.no_of_getattr_calls) + self.assertEqual(3, catcher.no_of_getattr_calls) # ------------------------------------------------------------------ @@ -183,13 +183,13 @@ def test_setattr_intercepts_attribute_assignments(self): fanboy.comic = 'The Laminator, issue #1' fanboy.pie = 'blueberry' - self.assertEqual(__, fanboy.a_pie) + self.assertEqual('blueberry', fanboy.a_pie) # # NOTE: Change the prefix to make this next assert pass # - prefix = '__' + prefix = 'my' self.assertEqual( "The Laminator, issue #1", getattr(fanboy, prefix + '_comic')) @@ -213,7 +213,7 @@ def test_it_modifies_external_attribute_as_expected(self): setter = self.ScarySetter() setter.e = "mc hammer" - self.assertEqual(__, setter.altered_e) + self.assertEqual('mc hammer', setter.altered_e) def test_it_mangles_some_internal_attributes(self): setter = self.ScarySetter() @@ -221,9 +221,9 @@ def test_it_mangles_some_internal_attributes(self): try: coconuts = setter.num_of_coconuts except AttributeError: - self.assertEqual(__, setter.altered_num_of_coconuts) + self.assertEqual(9, setter.altered_num_of_coconuts) def test_in_this_case_private_attributes_remain_unmangled(self): setter = self.ScarySetter() - self.assertEqual(__, setter._num_of_private_coconuts) + self.assertEqual(2, setter._num_of_private_coconuts) From 7d758bc70125f4241903cb81fb0910a79879127f Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 15:21:03 +0200 Subject: [PATCH 34/35] complete about deleting objects --- python2/koans/about_deleting_objects.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/python2/koans/about_deleting_objects.py b/python2/koans/about_deleting_objects.py index d2ee8be05..9649453ab 100644 --- a/python2/koans/about_deleting_objects.py +++ b/python2/koans/about_deleting_objects.py @@ -10,7 +10,7 @@ def test_del_can_remove_slices(self): del lottery_nums[1] del lottery_nums[2:4] - self.assertEqual(__, lottery_nums) + self.assertEqual([4, 15, 42], lottery_nums) def test_del_can_remove_entire_lists(self): lottery_nums = [4, 8, 15, 16, 23, 42] @@ -19,7 +19,7 @@ def test_del_can_remove_entire_lists(self): win = lottery_nums except Exception as e: pass - self.assertMatch(__, e[0]) + self.assertMatch('referenced before', e[0]) # -------------------------------------------------------------------- @@ -52,8 +52,8 @@ def test_del_can_remove_attributes(self): except AttributeError as e: err_msg2 = e.args[0] - self.assertMatch(__, err_msg1) - self.assertMatch(__, err_msg2) + self.assertMatch('toilet_brushes', err_msg1) + self.assertMatch('hamsters', err_msg2) # -------------------------------------------------------------------- @@ -82,7 +82,7 @@ def test_del_works_with_properties(self): self.assertEqual('Senor Ninguno', cowboy.name) del cowboy.name - self.assertEqual(__, cowboy.name) + self.assertEqual('The man with no name', cowboy.name) # -------------------------------------------------------------------- @@ -108,7 +108,7 @@ def test_another_way_to_make_a_deletable_property(self): self.assertEqual('Patrick', citizen.name) del citizen.name - self.assertEqual(__, citizen.name) + self.assertEqual('Number Six', citizen.name) # -------------------------------------------------------------------- @@ -124,4 +124,4 @@ def tests_del_can_be_overriden(self): sale = self.MoreOrganisedClosingSale() self.assertEqual(5, sale.jellies()) del sale.jellies - self.assertEqual(__, sale.last_deletion) + self.assertEqual('jellies', sale.last_deletion) From 995aec506adcaea5659e6f46ac428277a4cedb0e Mon Sep 17 00:00:00 2001 From: ultrayoshi Date: Sat, 25 May 2013 15:48:42 +0200 Subject: [PATCH 35/35] Complete python koans --- python2/koans/about_proxy_object_project.py | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/python2/koans/about_proxy_object_project.py b/python2/koans/about_proxy_object_project.py index 50a949496..17745d7c2 100644 --- a/python2/koans/about_proxy_object_project.py +++ b/python2/koans/about_proxy_object_project.py @@ -22,11 +22,34 @@ class Proxy(object): def __init__(self, target_object): # WRITE CODE HERE + self._messages = list() #initialize '_obj' attribute last. Trust me on this! self._obj = target_object # WRITE CODE HERE + def messages(self): + return self._messages + + def was_called(self, attr_name): + return attr_name in self._messages + + def number_of_times_called(self, attr_name): + return self._messages.count(attr_name) + + def __setattr__(self, attr_name, value): + if attr_name != '_obj' and attr_name != '_messages': + self._messages.append(attr_name) + self._obj.__setattr__(attr_name, value) + + object.__setattr__(self, attr_name, value) + + def __getattribute__(self, attr_name): + try: + return object.__getattribute__(self, attr_name) + except AttributeError as ex: + self._messages.append(attr_name) + return self._obj.__getattribute__(attr_name) # The proxy object should pass the following Koan: