Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "edit the publish_subscribe.py"
This reverts commit fee0702.
- Loading branch information
Showing
3 changed files
with
57 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/ | ||
Implementation of the iterator pattern with a generator""" | ||
|
||
|
||
def count_to(count): | ||
"""Counts by word numbers, up to a maximum of five""" | ||
numbers = ["one", "two", "three", "four", "five"] | ||
# enumerate() returns a tuple containing a count (from start which | ||
# defaults to 0) and the values obtained from iterating over sequence | ||
for pos, number in zip(range(count), numbers): | ||
yield number | ||
|
||
# Test the generator | ||
count_to_two = lambda: count_to(2) | ||
count_to_five = lambda: count_to(5) | ||
|
||
print('Counting to two...') | ||
for number in count_to_two(): | ||
print(number, end=' ') | ||
|
||
print() | ||
|
||
print('Counting to five...') | ||
for number in count_to_five(): | ||
print(number, end=' ') | ||
|
||
print() | ||
|
||
### OUTPUT ### | ||
# Counting to two... | ||
# one two | ||
# Counting to five... | ||
# one two three four five |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters