Skip to content

Commit 1209578

Browse files
committed
Updated python 3 side of the house. Added link to @hebbertja's about_asserts video
1 parent d11989b commit 1209578

File tree

12 files changed

+133
-89
lines changed

12 files changed

+133
-89
lines changed

python2/koans/about_asserts.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ def test_assert_truth(self):
1010
"""
1111
We shall contemplate truth by testing reality, via asserts.
1212
"""
13+
14+
# Confused? This video should help:
15+
#
16+
# http://bit.ly/about_asserts
17+
1318
self.assertTrue(False) # This should be true
1419

1520
def test_assert_with_message(self):
@@ -71,4 +76,4 @@ def test_that_sometimes_we_need_to_know_the_class_type(self):
7176

7277
# Need an illustration? More reading can be found here:
7378
#
74-
# https://github.com/gregmalcolm/python_koans/wiki/Class-Attribute
79+
# http://bit.ly/__class__

python2/runner/path_to_enlightenment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from koans.about_none import AboutNone
1111
from koans.about_lists import AboutLists
1212
from koans.about_list_assignments import AboutListAssignments
13-
from koans.about_string_manipulation import AboutStringManipulation
1413
from koans.about_dictionaries import AboutDictionaries
14+
from koans.about_string_manipulation import AboutStringManipulation
1515
from koans.about_tuples import AboutTuples
1616
from koans.about_methods import AboutMethods
1717
from koans.about_control_statements import AboutControlStatements
@@ -53,8 +53,8 @@ def koans():
5353
suite.addTests(loader.loadTestsFromTestCase(AboutNone))
5454
suite.addTests(loader.loadTestsFromTestCase(AboutLists))
5555
suite.addTests(loader.loadTestsFromTestCase(AboutListAssignments))
56-
suite.addTests(loader.loadTestsFromTestCase(AboutStringManipulation))
5756
suite.addTests(loader.loadTestsFromTestCase(AboutDictionaries))
57+
suite.addTests(loader.loadTestsFromTestCase(AboutStringManipulation))
5858
suite.addTests(loader.loadTestsFromTestCase(AboutTuples))
5959
suite.addTests(loader.loadTestsFromTestCase(AboutMethods))
6060
suite.addTests(loader.loadTestsFromTestCase(AboutControlStatements))

python3/koans/about_asserts.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ def test_assert_truth(self):
99
"""
1010
We shall contemplate truth by testing reality, via asserts.
1111
"""
12+
13+
# Confused? This video should help:
14+
#
15+
# http://bit.ly/about_asserts
16+
1217
self.assertTrue(False) # This should be true
1318

1419
def test_assert_with_message(self):
@@ -42,9 +47,32 @@ def test_a_better_way_of_asserting_equality(self):
4247

4348
def test_that_unittest_asserts_work_the_same_way_as_python_asserts(self):
4449
"""
45-
Knowing how things really work is half the battle
50+
Understand what lies within.
4651
"""
4752

4853
# This throws an AssertionError exception
4954
assert False
5055

56+
def test_that_sometimes_we_need_to_know_the_class_type(self):
57+
"""
58+
What is in a class name?
59+
"""
60+
61+
# Sometimes we will ask you what the class type of an object is.
62+
#
63+
# For example, contemplate the text string "naval". What is it's class type?
64+
# The koans runner will include this feedback for this koan:
65+
#
66+
# AssertionError: '-=> FILL ME IN! <=-' != <type 'str'>
67+
#
68+
# So "naval".__class__ is equal to <type 'str'>? No not quite. This
69+
# is just what it displays. The answer is simply str.
70+
#
71+
# See for yourself:
72+
73+
self.assertEqual(__, "naval".__class__) # It's str, not <type 'str'>
74+
75+
# Need an illustration? More reading can be found here:
76+
#
77+
# http://bit.ly/__class__
78+

python3/koans/about_classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Dog:
1010

1111
def test_instances_of_classes_can_be_created_adding_parentheses(self):
1212
fido = self.Dog()
13-
self.assertEqual(__, type(fido).__name__)
13+
self.assertEqual(__, fido.__class__)
1414

1515
def test_classes_have_docstrings(self):
1616
self.assertRegexpMatches(self.Dog.__doc__, __)

python3/koans/about_iteration.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def test_map_transforms_elements_of_a_list(self):
3939

4040
mapping = map(self.add_ten, seq)
4141

42-
self.assertNotEqual(list, type(mapping).__name__)
43-
self.assertEqual(__, type(mapping).__name__)
42+
self.assertNotEqual(list, mapping.__class__)
43+
self.assertEqual(__, mapping.__class__)
4444
# In Python 3 built in iterator funcs return iteratable view objects
4545
# instead of lists
4646

@@ -94,7 +94,7 @@ def test_reduce_will_blow_your_mind(self):
9494
# to the functools module.
9595

9696
result = functools.reduce(self.add, [2, 3, 4])
97-
self.assertEqual(__, type(result).__name__)
97+
self.assertEqual(__, result.__class__)
9898
# Reduce() syntax is same as Python 2
9999

100100
self.assertEqual(__, result)

python3/koans/about_method_bindings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def test_get_descriptor_resolves_attribute_binding(self):
6868
# binding_owner = obj
6969
# owner_type = cls
7070

71-
self.assertEqual(__, type(bound_obj).__name__)
72-
self.assertEqual(__, type(binding_owner).__name__)
71+
self.assertEqual(__, bound_obj.__class__)
72+
self.assertEqual(__, binding_owner.__class__)
7373
self.assertEqual(AboutMethodBindings, owner_type)
7474

7575
# ------------------------------------------------------------------

python3/koans/about_none.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_none_is_universal(self):
1717
"There is only one None"
1818
self.assertEqual(____, None is None)
1919

20-
def test_what_exception_do_you_get_when_calling_nonexistent_methods_on_None(self):
20+
def test_what_exception_do_you_get_when_calling_nonexistent_methods(self):
2121
"""
2222
What is the Exception that is thrown when you call a method that does
2323
not exist?
@@ -32,7 +32,12 @@ def test_what_exception_do_you_get_when_calling_nonexistent_methods_on_None(self
3232
ex2 = ex
3333

3434
# What exception has been caught?
35-
self.assertEqual(__, ex2.__class__.__name__)
35+
#
36+
# Need a recap on how to evaluate __class__ attributes?
37+
#
38+
# http://bit.ly/__class__
39+
40+
self.assertEqual(__, ex2.__class__)
3641

3742
# What message was attached to the exception?
3843
# (HINT: replace __ with part of the error message.)
@@ -44,5 +49,3 @@ def test_none_is_distinct(self):
4449
"""
4550
self.assertEqual(__, None is not 0)
4651
self.assertEqual(__, None is not False)
47-
48-

python3/koans/about_sets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ def test_empty_sets_have_different_syntax_to_populated_sets(self):
1818
def test_dictionaries_and_sets_use_same_curly_braces(self):
1919
# Note: Sets have only started using braces since Python 3
2020

21-
self.assertEqual(__, type({1, 2, 3}).__name__)
22-
self.assertEqual(__, type({'one': 1, 'two': 2}).__name__)
21+
self.assertEqual(__, {1, 2, 3}.__class__)
22+
self.assertEqual(__, {'one': 1, 'two': 2}.__class__)
2323

24-
self.assertEqual(__, type({}).__name__)
24+
self.assertEqual(__, {}.__class__)
2525

2626
def test_creating_sets_using_strings(self):
2727
self.assertEqual(__, {'12345'})
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from runner.koan import *
5+
6+
class AboutStringManipulation(Koan):
7+
8+
def test_use_format_to_interpolate_variables(self):
9+
value1 = 'one'
10+
value2 = 2
11+
string = "The values are {0} and {1}".format(value1, value2)
12+
self.assertEqual(__, string)
13+
14+
def test_formatted_values_con_be_shown_in_any_order_or_be_repeated(self):
15+
value1 = 'doh'
16+
value2 = 'DOH'
17+
string = "The values are {1}, {0}, {0} and {1}!".format(value1, value2)
18+
self.assertEqual(__, string)
19+
20+
def test_any_python_expression_may_be_interpolated(self):
21+
import math # import a standard python module with math functions
22+
23+
decimal_places = 4
24+
string = "The square root of 5 is {0:.{1}f}".format(math.sqrt(5), \
25+
decimal_places)
26+
self.assertEqual(__, string)
27+
28+
def test_you_can_get_a_substring_from_a_string(self):
29+
string = "Bacon, lettuce and tomato"
30+
self.assertEqual(__, string[7:10])
31+
32+
def test_you_can_get_a_single_character_from_a_string(self):
33+
string = "Bacon, lettuce and tomato"
34+
self.assertEqual(__, string[1])
35+
36+
def test_single_characters_can_be_represented_by_integers(self):
37+
self.assertEqual(__, ord('a'))
38+
self.assertEqual(__, ord('b') == (ord('a') + 1))
39+
40+
def test_strings_can_be_split(self):
41+
string = "Sausage Egg Cheese"
42+
words = string.split()
43+
self.assertListEqual([__, __, __], words)
44+
45+
def test_strings_can_be_split_with_different_patterns(self):
46+
import re #import python regular expression library
47+
48+
string = "the,rain;in,spain"
49+
pattern = re.compile(',|;')
50+
51+
words = pattern.split(string)
52+
53+
self.assertListEqual([__, __, __, __], words)
54+
55+
# Pattern is a Python regular expression pattern which matches ',' or ';'
56+
57+
def test_raw_strings_do_not_interpret_escape_characters(self):
58+
string = r'\n'
59+
self.assertNotEqual('\n', string)
60+
self.assertEqual(__, string)
61+
self.assertEqual(__, len(string))
62+
63+
# Useful in regular expressions, file paths, URLs, etc.
64+
65+
def test_strings_can_be_joined(self):
66+
words = ["Now", "is", "the", "time"]
67+
self.assertEqual(__, ' '.join(words))
68+
69+
def test_strings_can_change_case(self):
70+
self.assertEqual(__, 'guido'.capitalize())
71+
self.assertEqual(__, 'guido'.upper())
72+
self.assertEqual(__, 'TimBot'.lower())
73+
self.assertEqual(__, 'guido van rossum'.title())
74+
self.assertEqual(__, 'ToTaLlY aWeSoMe'.swapcase())

python3/koans/about_strings.py

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -91,71 +91,3 @@ def test_most_strings_interpret_escape_characters(self):
9191
self.assertEqual('\n', string)
9292
self.assertEqual("""\n""", string)
9393
self.assertEqual(__, len(string))
94-
95-
def test_use_format_to_interpolate_variables(self):
96-
value1 = 'one'
97-
value2 = 2
98-
string = "The values are {0} and {1}".format(value1, value2)
99-
self.assertEqual(__, string)
100-
101-
def test_formatted_values_con_be_shown_in_any_order_or_be_repeated(self):
102-
value1 = 'doh'
103-
value2 = 'DOH'
104-
string = "The values are {1}, {0}, {0} and {1}!".format(value1, value2)
105-
self.assertEqual(__, string)
106-
107-
def test_any_python_expression_may_be_interpolated(self):
108-
import math # import a standard python module with math functions
109-
110-
decimal_places = 4
111-
string = "The square root of 5 is {0:.{1}f}".format(math.sqrt(5), \
112-
decimal_places)
113-
self.assertEqual(__, string)
114-
115-
def test_you_can_get_a_substring_from_a_string(self):
116-
string = "Bacon, lettuce and tomato"
117-
self.assertEqual(__, string[7:10])
118-
119-
def test_you_can_get_a_single_character_from_a_string(self):
120-
string = "Bacon, lettuce and tomato"
121-
self.assertEqual(__, string[1])
122-
123-
def test_single_characters_can_be_represented_by_integers(self):
124-
self.assertEqual(__, ord('a'))
125-
self.assertEqual(__, ord('b') == (ord('a') + 1))
126-
127-
def test_strings_can_be_split(self):
128-
string = "Sausage Egg Cheese"
129-
words = string.split()
130-
self.assertListEqual([__, __, __], words)
131-
132-
def test_strings_can_be_split_with_different_patterns(self):
133-
import re #import python regular expression library
134-
135-
string = "the,rain;in,spain"
136-
pattern = re.compile(',|;')
137-
138-
words = pattern.split(string)
139-
140-
self.assertListEqual([__, __, __, __], words)
141-
142-
# Pattern is a Python regular expression pattern which matches ',' or ';'
143-
144-
def test_raw_strings_do_not_interpret_escape_characters(self):
145-
string = r'\n'
146-
self.assertNotEqual('\n', string)
147-
self.assertEqual(__, string)
148-
self.assertEqual(__, len(string))
149-
150-
# Useful in regular expressions, file paths, URLs, etc.
151-
152-
def test_strings_can_be_joined(self):
153-
words = ["Now", "is", "the", "time"]
154-
self.assertEqual(__, ' '.join(words))
155-
156-
def test_strings_can_change_case(self):
157-
self.assertEqual(__, 'guido'.capitalize())
158-
self.assertEqual(__, 'guido'.upper())
159-
self.assertEqual(__, 'TimBot'.lower())
160-
self.assertEqual(__, 'guido van rossum'.title())
161-
self.assertEqual(__, 'ToTaLlY aWeSoMe'.swapcase())

0 commit comments

Comments
 (0)