Lets say some files are compressed using gzip and we want our program to read them as well.

In [114]:

!gzip hello.py

gzip: hello.py: No such file or directory

In [115]:

!ls *.gz

hello.py.gz

In [113]:

importgzipdefxopen(filename):iffilename.endswith(".gz"):returngzip.open(filename)else:returnopen(filename)defreadfiles(filenames):"""Reads all files and returns iterator over lines."""forfilenameinfilenames:forlineinxopen(filename):yieldlinelines=readfiles(["square.py","hello.py.gz"])lines=grep("def",lines)printlines(lines)

def square(x):
def cube(x):
def hello(name):

Problem: Write a function countiter to count number of elements in an iterator.