-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Completed the about_generators koan #19
base: devel
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(__, attempt1) | ||
self.assertEqual(__, attempt2) | ||
self.assertEqual(['Boom!', 'Boom!', 'Boom!'], attempt1) | ||
self.assertEqual([], attempt2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this? |
||
|
||
# ------------------------------------------------------------------ | ||
|
||
|
@@ -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_generators_can_be_manually_iterated_and_closed(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)) | ||
|
||
# ------------------------------------------------------------------ | ||
|
||
|
@@ -103,17 +103,18 @@ def test_generators_can_act_as_coroutines(self): | |
# | ||
# Hint: Read the "Specification: Sending Values into Generators" | ||
# section of http://www.python.org/dev/peps/pep-0342/ | ||
#I think is because the generator has just been created so it's not prepared yet to receive a value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's right. |
||
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.coroutine() | ||
|
||
try: | ||
generator.send(1 + 2) | ||
except TypeError as ex: | ||
self.assertMatch(__, ex[0]) | ||
self.assertMatch("can't send non-None value to a just-started generator", ex[0]) | ||
|
||
# ------------------------------------------------------------------ | ||
|
||
|
@@ -131,11 +132,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)' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Key point! |
||
self.assertEqual(__, generator.send(None)) | ||
self.assertEqual('no value', generator.send(None)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: Research about tuple generators and tell me what you find.