Presentation given at Mumbai Technology Meetup by Anand Chitipothu on March 15, 2015.
The talk will be given via Google Hangouts on Air from 4PM to 5PM IST. Any one can join it by visiting the hangout page.
This webpage will be available at:
http://anandology.com/nb/2015/python3-generators-mtp/
It'll be updated along with the talk. You'll have to reload the web page to see latest contents.
for a in [1, 2, 3, 4]:
print(a)
for a in (1, 2, 3, 4):
print(a)
for c in "hello":
print(c)
for k in {"x": 1, "y": 2}:
print(k)
max([1, 2, 3, 4])
max("helloworld")
",".join("hello")
"-".join(["hello", "world"])
"-".join({"x": 1, "y": 2})
x = [1, 2, 3, 4]
it = iter(x)
it
next(it)
next(it)
next(it)
next(it)
next(it)
def numbers():
yield 1
yield 2
yield 3
n = numbers()
print(n)
next(n)
next(n)
next(n)
next(n)
def numbers():
print("begin numbers")
yield 1
print("after 1")
yield 2
print("after 2")
yield 3
print("end numbers")
n = numbers()
next(n)
next(n)
next(n)
next(n)
for i in numbers():
print(i)
sum(numbers())
max(numbers())
def squares(nums):
"""Takes a sequence of numbers and returns
a sequence of their squares.
"""
for n in nums:
yield n*n
nums = range(100)
numsq = squares(nums)
print(sum(numsq))
!seq 1000 > numbers.txt
%%file sumofsq1.py
# usual way of computing sum of squares
def main(filename):
result = 0
for line in open(filename):
n = int(line)
n_sq = n*n
result += n_sq
print(result)
if __name__ == "__main__":
import sys
main(sys.argv[1])
!python sumofsq1.py numbers.txt
%%file sumofsq2.py
# computing sum of squares using generators
def ints(strings):
for s in strings:
yield int(s)
def squares(nums):
for n in nums:
yield n*n
def main(filename):
# lines = open(filename)
# numbers = ints(lines)
# sq = squares(numbers)
# print(sum(sq))
print(sum(squares(ints(open(filename)))))
if __name__ == "__main__":
import sys
main(sys.argv[1])
!python sumofsq2.py numbers.txt
def squares(nums):
return (n*n for n in nums)
print(list(squares(range(10))))
print(sum(squares(range(10))))
sum((n*n for n in range(10)))
sum(n*n for n in range(10))
sum(n*n for n in range(1000000))
sum(n*n for n in range(1000000) if n%2 == 0)
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
n1 = Node(5)
n2 = Node(4)
n3 = Node(2)
n12 = Node(3, n1, n2)
root = Node(10, n12, n3)
def inorder(root):
if root.left:
values = inorder(root.left)
for v in values:
yield v
yield root.value
if root.right:
values = inorder(root.right)
for v in values:
yield v
print(list(inorder(root)))
def inorder(root):
if root.left:
yield from inorder(root.left)
yield root.value
if root.right:
yield from inorder(root.right)
print(list(inorder(root)))
%%file multitask.py
import sys
def f(n, label="f"):
for i in range(n):
print(label, i)
yield
def g(n, label="g"):
for i in range(n):
print(label, i)
yield
def h(n):
print("begin h")
yield from f(n, label="h-f")
print('after f in h')
#yield
yield from g(n, label="h-g")
print("end h")
tasks = []
def start_task(f, *args):
tasks.append(f(*args))
def run_tasks():
while tasks:
for t in tasks[:]:
try:
next(t)
except StopIteration:
tasks.remove(t)
def main():
start_task(f, 4)
#start_task(g, 7)
start_task(h, 2)
run_tasks()
if __name__ == "__main__":
main()
!python multitask.py
!python multitask.py