diff --git a/.gitignore b/.gitignore index 27b56e02d..4b3d788a9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.swp .DS_Store answers +idea .hg diff --git a/python2/koans/about_asserts.py b/python2/koans/about_asserts.py index 8c80671d7..cbe5276be 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 is True") 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(__, "navel".__class__) # It's str, not + self.assertEqual(str, "navel".__class__) # It's str, not # Need an illustration? More reading can be found here: # diff --git a/python2/koans/about_classes.py b/python2/koans/about_classes.py index 7b82e60d1..7c9ee57aa 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__.__name__) + self.assertEqual("Dog", fido.__class__.__name__) 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_different_instance_variables(self): fido = self.Dog5("Fido") rover = self.Dog5("Rover") - self.assertEqual(____, rover.name == fido.name) + self.assertEqual(False, rover.name == fido.name) # ------------------------------------------------------------------ @@ -128,7 +128,7 @@ def __str__(self): # # Implement this! # - return __ + return self._name def __repr__(self): return "" @@ -136,7 +136,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") @@ -144,17 +144,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")) diff --git a/python2/koans/about_comprehension.py b/python2/koans/about_comprehension.py index 7f19c0891..464727a67 100644 --- a/python2/koans/about_comprehension.py +++ b/python2/koans/about_comprehension.py @@ -5,57 +5,53 @@ class AboutComprehension(Koan): - - def test_creating_lists_with_list_comprehensions(self): feast = ['lambs', 'sloths', 'orangutans', 'breakfast cereals', - 'fruit bats'] + 'fruit bats'] 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_filtering_lists_with_list_comprehensions(self): feast = ['spam', 'sloths', 'orangutans', 'breakfast cereals', - 'fruit bats'] + 'fruit bats'] comprehension = [delicacy for delicacy in feast if len(delicacy) > 6] - self.assertEqual(__, len(feast)) - self.assertEqual(__, len(comprehension)) + self.assertEqual(5, len(feast)) + self.assertEqual(3, len(comprehension)) def test_unpacking_tuples_in_list_comprehensions(self): list_of_tuples = [(1, 'lumberjack'), (2, 'inquisition'), (4, 'spam')] - comprehension = [ skit * number for number, skit in list_of_tuples ] + comprehension = [skit * number for number, skit in list_of_tuples] - self.assertEqual(__, comprehension[0]) - self.assertEqual(__, len(comprehension[2])) + self.assertEqual('lumberjack', comprehension[0]) + self.assertEqual(16, len(comprehension[2])) def test_double_list_comprehension(self): list_of_eggs = ['poached egg', 'fried egg'] list_of_meats = ['lite spam', 'ham spam', 'fried spam'] + comprehension = ['{0} and {1}'.format(egg, meat) for egg in list_of_eggs for meat in list_of_meats] - comprehension = [ '{0} and {1}'.format(egg, meat) for egg in list_of_eggs for meat in list_of_meats] - - - self.assertEqual(__, len(comprehension)) - self.assertEqual(__, comprehension[0]) + self.assertEqual(6, len(comprehension)) + self.assertEqual('poached egg and lite spam', comprehension[0]) def test_creating_a_set_with_set_comprehension(self): - comprehension = { x for x in 'aabbbcccc'} + comprehension = {x for x in 'aabbbcccc'} - self.assertEqual(__, comprehension) # rememeber that set members are unique + self.assertEqual(set(['a', 'c', 'b']), comprehension) # rememeber that set members are unique def test_creating_a_dictionary_with_dictionary_comprehension(self): dict_of_weapons = {'first': 'fear', 'second': 'surprise', - 'third':'ruthless efficiency', 'fourth':'fanatical devotion', + 'third': 'ruthless efficiency', 'fourth': 'fanatical devotion', 'fifth': None} - dict_comprehension = { k.upper(): weapon for k, weapon in dict_of_weapons.iteritems() if weapon} + dict_comprehension = {k.upper(): weapon for k, weapon in dict_of_weapons.iteritems() if weapon} - self.assertEqual(__, 'first' in dict_comprehension) - self.assertEqual(__, 'FIRST' in dict_comprehension) - self.assertEqual(__, len(dict_of_weapons)) - self.assertEqual(__, len(dict_comprehension)) + self.assertEqual(False, 'first' in dict_comprehension) + self.assertEqual(True, 'FIRST' in dict_comprehension) + self.assertEqual(5, len(dict_of_weapons)) + self.assertEqual(4, len(dict_comprehension)) diff --git a/python2/koans/about_control_statements.py b/python2/koans/about_control_statements.py index 38665cb15..9daea4f8d 100644 --- a/python2/koans/about_control_statements.py +++ b/python2/koans/about_control_statements.py @@ -5,28 +5,27 @@ class AboutControlStatements(Koan): - def test_if_then_else_statements(self): if True: 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_if_then_elif_else_statements(self): if False: result = 'first value' - elif True: + elif True: result = 'true value' else: result = 'default value' - self.assertEqual(__, result) + self.assertEqual('true value', result) def test_while_statement(self): i = 1 @@ -34,7 +33,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 @@ -43,7 +42,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 @@ -52,14 +51,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 = [ @@ -71,9 +70,9 @@ def test_for_statement_with_tuples(self): result = [] for knight, answer in round_table: result.append("Contestant: '" + knight + \ - "' Answer: '" + answer + "'") + "' Answer: '" + answer + "'") - text = __ + text = "Contestant: 'Robin' Answer: 'Blue! I mean Green!'" self.assertMatch(text, result[2]) diff --git a/python2/koans/about_decorating_with_classes.py b/python2/koans/about_decorating_with_classes.py index b8f67a573..aa9894b2f 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,20 +65,20 @@ 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')) # ------------------------------------------------------------------ def sound_check(self): - #Note: no decorator + # Note: no decorator return "Testing..." def test_what_a_decorator_is_doing_to_a_function(self): - #wrap the function with the decorator + # 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__) diff --git a/python2/koans/about_decorating_with_functions.py b/python2/koans/about_decorating_with_functions.py index c2bfe28f4..5498eaf2e 100644 --- a/p