-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gen1.py
52 lines (42 loc) · 1.08 KB
/
Gen1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Module for demonstrating generator execution."""
def take(count, iterable):
"""Take items from the front of an iterable.
Args:
count: The maximum number of items to retrieve.
iterable: The source series.
Yields:
At most 'count' items from 'iterable'.
"""
counter = 0
for item in iterable:
if counter == count:
return
counter += 1
yield item
def run_take():
items = [2,4,6,8,10]
for item in take(3,items):
print(item)
def distinct(iterable):
"""Return unique items by eliminating duplicates.
Args:
iterable: The source series.
Yields:
Unique elements in order from 'iterable'.
"""
seen = set()
for item in iterable:
if item in seen:
continue
yield item
seen.add(item)
def run_distinct():
items = [5,7,7,6,5,5]
for item in distinct(items):
print(item)
def run_pipeline():
items = [3,6,6,2,1,1]
for item in take(3,distinct(items)):
print(item)
if __name__ == '__main__':
run_pipeline()