-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.py
64 lines (50 loc) · 1.2 KB
/
gen.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
53
54
55
56
57
58
59
60
61
62
63
64
"""
Module for demonstration the use generator execution
"""
def take(count, iterable):
"""
Take items from the front of an iterable
:param count: The maximum number of items to retrieve
:param iterable: The source series
:yields: At most 'count' items for 'iterable'
"""
counter = 0
for item in iterable:
if counter == count:
return
counter += 1
yield item
def run_take():
"""
Test the take() function
"""
items = [2, 4, 6, 8, 10]
for item in take(3, items):
print(item)
def distinct(iterable):
"""
Return unique items by eliminating duplicates
:param 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_pipeline():
items = [3, 6, 6, 2, 1, 1]
for item in take(3, distinct(items)):
print(item)
def run_distinct():
items = [5, 7, 7, 6, 5, 5]
for item in distinct(items):
print(item)
def main():
# run_take()
# run_distinct()
run_pipeline()
if __name__ == '__main__':
main()
exit(0)