Skip to content

Commit cacbdae

Browse files
committed
Made a Python 2.5 friendly version of Koans
1 parent ad6e64c commit cacbdae

22 files changed

+57
-97
lines changed

python 2/contemplate_koans.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
"Did you accidentally use the wrong python script? \nTry:\n\n" +
2222
" python contemplate_koans.py\n")
2323
else:
24-
if sys.version_info < (2, 6):
24+
if sys.version_info < (2, 5):
2525
print("\n" +
2626
"********************************************************\n" +
2727
"WARNING:\n" +
2828
"This version of Python Koans was designed for " +
29-
"Python 2.6 or greater.\n" +
29+
"Python 2.5 or greater.\n" +
3030
"Your version of Python is older, so this is unlikely " +
3131
"to work!\n\n" +
3232
"But lets see how far we get...\n" +

python 2/koans/about_attribute_access.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_calling_undefined_functions_normally_results_in_errors(self):
1717

1818
try:
1919
typical.foobar()
20-
except Exception as exception:
20+
except Exception, exception:
2121
self.assertEqual(__, type(exception).__name__)
2222
self.assertMatch(__, exception[0])
2323

@@ -26,7 +26,7 @@ def test_calling_getattribute_causes_an_attribute_error(self):
2626

2727
try:
2828
typical.__getattribute__('foobar')
29-
except AttributeError as exception:
29+
except AttributeError, exception:
3030
self.assertMatch(__, exception[0])
3131

3232
# THINK ABOUT IT:
@@ -52,7 +52,7 @@ def test_intercepting_return_values_can_disrupt_the_call_chain(self):
5252

5353
try:
5454
catcher.foobaz(1)
55-
except TypeError as ex:
55+
except TypeError, ex:
5656
self.assertMatch(__, ex[0])
5757

5858
# foobaz returns a string. What happens to the '(1)' part?
@@ -87,7 +87,7 @@ def test_non_foo_messages_are_treated_normally(self):
8787

8888
try:
8989
catcher.normal_undefined_attribute
90-
except AttributeError as ex:
90+
except AttributeError, ex:
9191
self.assertMatch(__, ex[0])
9292

9393
# ------------------------------------------------------------------

python 2/koans/about_class_attributes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def wag():
5757

5858
try:
5959
rover.wag()
60-
except Exception as ex:
60+
except Exception, ex:
6161
self.assertMatch(__, ex[0])
6262

6363
# ------------------------------------------------------------------
@@ -122,7 +122,7 @@ def test_classmethods_can_not_be_used_as_properties(self):
122122
fido = self.Dog3()
123123
try:
124124
fido.name = "Fido"
125-
except Exception as ex:
125+
except Exception, ex:
126126
self.assertMatch(__, ex[0])
127127

128128
def test_classes_and_instances_do_not_share_instance_attributes(self):

python 2/koans/about_classes.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,13 @@ def __init__(self):
7979
def name(self):
8080
return self._name
8181

82-
@name.setter
83-
def name(self, a_name):
82+
def set_name(self, a_name):
8483
self._name = a_name
8584

86-
def test_creating_properties_with_decorators_is_slightly_easier(self):
85+
def test_accessing_properties_with_decorators(self):
8786
fido = self.Dog4()
8887

89-
fido.name = "Fido"
88+
fido.set_name("Fido")
9089
self.assertEqual(__, fido.name)
9190

9291
# ------------------------------------------------------------------

python 2/koans/about_deleting_objects.py

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_del_can_remove_entire_lists(self):
1616
del lottery_nums
1717
try:
1818
win = lottery_nums
19-
except Exception as e:
19+
except Exception, e:
2020
pass
2121
self.assertMatch(__, e[0])
2222

@@ -43,12 +43,12 @@ def test_del_can_remove_attributes(self):
4343

4444
try:
4545
still_available = crazy_discounts.toilet_brushes()
46-
except AttributeError as e:
46+
except AttributeError, e:
4747
err_msg1 = e.args[0]
4848

4949
try:
5050
still_available = crazy_discounts.hamsters
51-
except AttributeError as e:
51+
except AttributeError, e:
5252
err_msg2 = e.args[0]
5353

5454
self.assertMatch(__, err_msg1)
@@ -86,31 +86,6 @@ def test_del_works_with_properties(self):
8686

8787
# ====================================================================
8888

89-
class Prisoner(object):
90-
def __init__(self):
91-
self._name = None
92-
93-
@property
94-
def name(self):
95-
return self._name
96-
97-
@name.setter
98-
def name(self, name):
99-
self._name = name
100-
101-
@name.deleter
102-
def name(self):
103-
self._name = 'Number Six'
104-
105-
def test_another_way_to_make_a_deletable_property(self):
106-
citizen = self.Prisoner()
107-
citizen.name = "Patrick"
108-
self.assertEqual('Patrick', citizen.name)
109-
110-
del citizen.name
111-
self.assertEqual(__, citizen.name)
112-
113-
# ====================================================================
11489

11590
class MoreOrganisedClosingSale(ClosingSale):
11691
def __init__(self):

python 2/koans/about_exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_try_clause(self):
1919
result = None
2020
try:
2121
self.fail("Oops")
22-
except StandardError as ex:
22+
except StandardError, ex:
2323
result = 'exception handled'
2424

2525
self.assertEqual(__, result)
@@ -36,7 +36,7 @@ def test_raising_a_specific_error(self):
3636
result = None
3737
try:
3838
raise self.MySpecialError, "My Message"
39-
except self.MySpecialError as ex:
39+
except self.MySpecialError, ex:
4040
result = 'exception handled'
4141

4242
self.assertEqual(__, result)

python 2/koans/about_generators.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def generator_with_coroutine(self):
9595

9696
def test_generators_can_take_coroutines(self):
9797
generator = self.generator_with_coroutine()
98-
next(generator)
98+
generator.next()
9999
self.assertEqual(__, generator.send(1 + 2))
100100

101101
# ------------------------------------------------------------------
@@ -109,11 +109,11 @@ def yield_tester(self):
109109

110110
def test_generators_can_see_if_they_have_been_called_with_a_value(self):
111111
generator = self.yield_tester()
112-
next(generator)
112+
generator.next()
113113
self.assertEqual('with value', generator.send('with value'))
114114

115115
generator2 = self.yield_tester()
116-
next(generator2)
117-
self.assertEqual(__, next(generator2))
116+
generator2.next()
117+
self.assertEqual(__, generator2.next())
118118

119119

python 2/koans/about_inheritance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_subclasses_add_new_behavior(self):
3939
try:
4040
fido = self.Dog("Fido")
4141
fido.wag()
42-
except StandardError as ex:
42+
except StandardError, ex:
4343
self.assertMatch(__, ex[0])
4444

4545
def test_subclasses_can_modify_existing_behavior(self):
@@ -83,7 +83,7 @@ def test_base_init_does_not_get_called_automatically(self):
8383
snoopy = self.Pug("Snoopy")
8484
try:
8585
name = snoopy.name
86-
except Exception as ex:
86+
except Exception, ex:
8787
self.assertMatch(__, ex[0])
8888

8989
def test_base_init_has_to_be_called_explicitly(self):

python 2/koans/about_iteration.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ def test_iterating_with_next(self):
1919
stages = iter(['alpha','beta','gamma'])
2020

2121
try:
22-
self.assertEqual(__, next(stages))
23-
next(stages)
24-
self.assertEqual(__, next(stages))
25-
next(stages)
26-
except StopIteration as ex:
22+
self.assertEqual(__, stages.next())
23+
stages.next()
24+
self.assertEqual(__, stages.next())
25+
stages.next()
26+
except StopIteration, ex:
2727
err_msg = 'Ran out of iterations'
2828

2929
self.assertMatch(__, err_msg)

python 2/koans/about_method_bindings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_setting_attributes_on_a_bound_method_directly(self):
3939
obj = Class()
4040
try:
4141
obj.method.cherries = 3
42-
except AttributeError as ex:
42+
except AttributeError, ex:
4343
self.assertMatch(__, ex[0])
4444

4545
def test_setting_attributes_on_methods_by_accessing_the_inner_function(self):
@@ -55,7 +55,7 @@ def test_inner_functions_are_unbound(self):
5555
function2.get_fruit = function
5656
try:
5757
cls = function2.get_fruit.im_self
58-
except AttributeError as ex:
58+
except AttributeError, ex:
5959
self.assertMatch(__, ex[0])
6060

6161
# ------------------------------------------------------------------

python 2/koans/about_methods.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ def test_calling_an_global_function(self):
1919
def test_calling_functions_with_wrong_number_of_arguments(self):
2020
try:
2121
my_global_function()
22-
except Exception as exception:
22+
except Exception, exception:
2323
self.assertEqual(__, type(exception).__name__)
2424
self.assertMatch(
2525
r'my_global_function\(\) takes exactly 2 arguments \(0 given\)'
2626
, exception[0])
2727

2828
try:
2929
my_global_function(1, 2, 3)
30-
except Exception as e:
30+
except Exception, e:
3131

3232
# Note, watch out for parenthesis. They need slashes in front!
3333
self.assertMatch(__, e[0])
@@ -153,7 +153,7 @@ def test_attributes_with_double_underscore_prefixes_are_subject_to_name_mangling
153153
try:
154154
#This may not be possible...
155155
password = __password()
156-
except Exception as ex:
156+
except Exception, ex:
157157
self.assertEqual(__, type(ex).__name__)
158158

159159
# But this still is!

python 2/koans/about_modules.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_importing_all_module_attributes_at_once(self):
4747
def test_modules_hide_attributes_prefixed_by_underscores(self):
4848
try:
4949
private_squirrel = _SecretSquirrel()
50-
except NameError as ex:
50+
except NameError, ex:
5151
self.assertMatch(__, ex[0])
5252

5353
def test_private_attributes_are_still_accessible_in_modules(self):
@@ -73,5 +73,5 @@ def test_a_module_can_choose_which_attributes_are_available_to_wildcards(self):
7373
# SecretDuck? Never heard of her!
7474
try:
7575
duck = SecretDuck()
76-
except NameError as ex:
76+
except NameError, ex:
7777
self.assertMatch(__, ex[0])

python 2/koans/about_monkey_patching.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def wag(self): return "HAPPY"
3232
def test_most_built_in_classes_cannot_be_monkey_patched(self):
3333
try:
3434
int.is_even = lambda self: (self % 2) == 0
35-
except StandardError as ex:
35+
except StandardError, ex:
3636
self.assertMatch(__, ex[0])
3737

3838
# ------------------------------------------------------------------

python 2/koans/about_new_style_classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_old_style_classes_have_type_but_no_class_attribute(self):
3636

3737
try:
3838
cls = self.OldStyleClass.__class__
39-
except Exception as ex:
39+
except Exception, ex:
4040
pass
4141

4242
self.assertMatch(__, ex[0])

python 2/koans/about_none.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_what_exception_do_you_get_when_calling_nonexistent_methods_on_None(self
2828
"""
2929
try:
3030
None.some_method_none_does_not_know_about()
31-
except Exception as ex:
31+
except Exception, ex:
3232
# What exception has been caught?
3333
self.assertEqual(__, ex.__class__.__name__)
3434

python 2/koans/about_packages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_subfolders_without_an_init_module_are_not_part_of_the_package(self):
4242
# Import ./a_normal_folder/
4343
try:
4444
import a_normal_folder
45-
except ImportError as ex:
45+
except ImportError, ex:
4646
self.assertMatch(__, ex[0])
4747

4848
# ------------------------------------------------------------------

python 2/koans/about_proxy_object_project.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# In this assignment, create a proxy class (one is started for you
77
# below). You should be able to initialize the proxy object with any
88
# object. Any attributes called on the proxy object should be forwarded
9-
# to the target object. As each attribute call is sent, the proxy should
9+
# to the target object. , each attribute call is sent, the proxy should
1010
# record the name of the attribute sent.
1111
#
1212
# The proxy class is started for you. You will need to add a method
@@ -39,7 +39,7 @@ def test_proxy_method_returns_wrapped_object(self):
3939
def test_tv_methods_still_perform_their_function(self):
4040
tv = Proxy(Television())
4141

42-
tv.channel = 10
42+
tv.set_channel(10)
4343
tv.power()
4444

4545
self.assertEqual(10, tv.channel)
@@ -49,17 +49,17 @@ def test_proxy_records_messages_sent_to_tv(self):
4949
tv = Proxy(Television())
5050

5151
tv.power()
52-
tv.channel = 10
52+
tv.set_channel(10)
5353

54-
self.assertEqual(['power', 'channel='], tv.messages())
54+
self.assertEqual(['power', 'set_channel'], tv.messages())
5555

5656
def test_proxy_handles_invalid_messages(self):
5757
tv = Proxy(Television())
5858

5959
ex = None
6060
try:
6161
tv.no_such_method()
62-
except AttributeError as ex:
62+
except AttributeError, ex:
6363
pass
6464

6565
self.assertEqual(AttributeError, type(ex))
@@ -78,11 +78,11 @@ def test_proxy_counts_method_calls(self):
7878
tv = Proxy(Television())
7979

8080
tv.power()
81-
tv.channel = 48
81+
tv.set_channel(48)
8282
tv.power()
8383

8484
self.assertEqual(2, tv.number_of_times_called('power'))
85-
self.assertEqual(1, tv.number_of_times_called('channel='))
85+
self.assertEqual(1, tv.number_of_times_called('set_channel'))
8686
self.assertEqual(0, tv.number_of_times_called('is_on'))
8787

8888
def test_proxy_can_record_more_than_just_tv_objects(self):
@@ -111,8 +111,7 @@ def __init__(self):
111111
def channel(self):
112112
return self._channel
113113

114-
@channel.setter
115-
def channel(self, value):
114+
def set_channel(self, value):
116115
self._channel = value
117116

118117
def power(self):
@@ -156,5 +155,5 @@ def test_edge_case_on_off(self):
156155
def test_can_set_the_channel(self):
157156
tv = Television()
158157

159-
tv.channel = 11
158+
tv.set_channel(11)
160159
self.assertEqual(11, tv.channel)

python 2/koans/about_scope.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class AboutScope(Koan):
1818
def test_dog_is_not_available_in_the_current_scope(self):
1919
try:
2020
fido = Dog()
21-
except Exception as ex:
21+
except Exception, ex:
2222
self.assertMatch(__, ex[0])
2323

2424
def test_you_can_reference_nested_classes_using_the_scope_operator(self):

0 commit comments

Comments
 (0)