-
Notifications
You must be signed in to change notification settings - Fork 59
/
12_generators_and_iterators.py
141 lines (101 loc) · 2.64 KB
/
12_generators_and_iterators.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
##########################################
# Generators in python
# ----------------------
#
#
#
# By Doug Purcell
# http://www.purcellconsult.com
#
##########################################
from random import choice
from string import ascii_letters, digits
symbols = '★☺⁂♬ϟ⌚'
# option 1
option_1 = []
for x in symbols:
option_1.append(ord(x))
# option 2
option_2 = [ord(x) for x in symbols]
# option 3 generator expression
genexpr = (ord(x) for x in symbols)
print(option_1)
print(option_2)
print(next(genexpr))
print(next(genexpr))
print(next(genexpr))
# Option 2 is still easy to understand plus only contains one line of code compared to 3 lines in option 1.
# Also, a list comprehensions intent is clear. It only builds a list. A for loop can do a ton of stuff, compute values, count items, etc.
# list comprehensions vs map and filter
# list comprehensions can emulate the logic of maps and filter
message = 'python is a fine language'
output_1 = [ord(x) for x in message if ord(x) > 110]
# maps and filter
output_2 = list(filter(lambda x: x > 110, map(ord, message)))
print(output_1)
print(output_2)
print()
symbols = '★☺⁂♬ϟ⌚'
list_comp = [ord(x) for x in symbols]
genexpr = (ord(x) for x in symbols if ord(x) > 110)
print(list_comp)
print(genexpr)
print(next(genexpr))
print(next(genexpr))
print(next(genexpr))
def forever(start=1, step=5.5):
while True:
start = (start * step)**3
yield start
forever()
# simple generator example
message = 'hello'
for char in message:
print(char)
# if we had to implement this with a while loop, it looks something like..
it = iter(message)
while True:
try:
print(next(it))
except StopIteration:
del it
break
def gen_12345():
for x in range(5):
yield x
print(next(gen_12345()))
print(next(gen_12345()))
class Password:
def __init__(self, length=8):
self.password = ''
i = 0
if length >= 8 and length < 21:
chars = ascii_letters + digits
while i < length:
self.password += choice(chars)
i += 1
def __iter__(self):
return self
def __next__(self):
for ch in self.password:
yield ch
secret = Password()
print(secret.password)
it = iter(secret)
print(it)
print(next(it))
print(next(it))
# iterators in python
class SimpleNumber:
def __init__(self, start=0):
self.start = start
def __iter__(self):
return self
def __next__(self):
for x in range(self.start):
yield x
simple = SimpleNumber()
it = iter(simple)
print(it.__next__())
print(next(it))
print(next(it))