Created
May 24, 2013 11:52
-
-
Save anandology/5642985 to your computer and use it in GitHub Desktop.
Advanced Python Workshop - Class Notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "Advanced Python Workshop" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Advanced Python Workshop - Lecture Notes\n", | |
"May 24, 2013\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lets start with warm up problems.\n", | |
"<http://anandology.com/apy/slides/python-warmup.html>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# problem 1\n", | |
"x = 1\n", | |
"y = x\n", | |
"x = 2\n", | |
"print x, y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"2 1\n" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# problem 2\n", | |
"x = [1, 2]\n", | |
"y = [x, 5]\n", | |
"x.append(3)\n", | |
"print y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[1, 2, 3], 5]\n" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Functions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def square(x): \n", | |
" return x*x\n", | |
"print square(4)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"16\n" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"f = square\n", | |
"print f(4)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"16\n" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def fxy(f, x, y):\n", | |
" return f(x) + f(y)\n", | |
"\n", | |
"print fxy(square, 3, 4)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"25\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"f = lambda x: x*x\n", | |
"print f(3)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"9\n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print fxy(lambda x: x*x*x, 3, 4)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"91\n" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = ['python', 'perl', \n", | |
" 'java', 'c', \n", | |
" 'haskell', 'ruby']\n", | |
"print sorted(x)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"['c', 'haskell', 'java', 'perl', 'python', 'ruby']\n" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print sorted(x, key=len)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"['c', 'perl', 'java', 'ruby', 'python', 'haskell']\n" | |
] | |
} | |
], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Default Arguments**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def inc(x, amount=1):\n", | |
" return x+amount\n", | |
"\n", | |
"print inc(5)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"6\n" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print inc(5, 4)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"9\n" | |
] | |
} | |
], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print inc(x=5, amount=4)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"9\n" | |
] | |
} | |
], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lets find out when is default value computed. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def f(x):\n", | |
" print \"f is called with\", x\n", | |
" return x\n", | |
"\n", | |
"print \"before defining inc\"\n", | |
"\n", | |
"def inc(x, amount=f(1)):\n", | |
" return x + amount\n", | |
"\n", | |
"print \"after defining inc\"\n", | |
"print inc(5)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"before defining inc\n", | |
"f is called with 1\n", | |
"after defining inc\n", | |
"6\n" | |
] | |
} | |
], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Iterators and Generators" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for a in [1, 2, 3, 4]: \n", | |
" print a" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n" | |
] | |
} | |
], | |
"prompt_number": 17 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for a in (1, 2, 3, 4):\n", | |
" print a" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n" | |
] | |
} | |
], | |
"prompt_number": 18 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for k in {\"a\": 1, \"b\": 2}: \n", | |
" print k" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"a\n", | |
"b\n" | |
] | |
} | |
], | |
"prompt_number": 19 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for c in \"hello\":\n", | |
" print c" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"h\n", | |
"e\n", | |
"l\n", | |
"l\n", | |
"o\n" | |
] | |
} | |
], | |
"prompt_number": 20 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\",\".join([\"a\", \"b\", \"c\"])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 21, | |
"text": [ | |
"'a,b,c'" | |
] | |
} | |
], | |
"prompt_number": 21 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\",\".join({\"a\": 1, \"b\": 2})" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 22, | |
"text": [ | |
"'a,b'" | |
] | |
} | |
], | |
"prompt_number": 22 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\",\".join(\"hello\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 23, | |
"text": [ | |
"'h,e,l,l,o'" | |
] | |
} | |
], | |
"prompt_number": 23 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"max([1, 2, 3, 4])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 24, | |
"text": [ | |
"4" | |
] | |
} | |
], | |
"prompt_number": 24 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"max(\"hello\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 25, | |
"text": [ | |
"'o'" | |
] | |
} | |
], | |
"prompt_number": 25 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"max({\"a\": 1, \"b\": 2})" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 26, | |
"text": [ | |
"'b'" | |
] | |
} | |
], | |
"prompt_number": 26 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lets try to understand how iteration works. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = iter([1, 2, 3, 4])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 27 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 29, | |
"text": [ | |
"1" | |
] | |
} | |
], | |
"prompt_number": 29 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 30, | |
"text": [ | |
"2" | |
] | |
} | |
], | |
"prompt_number": 30 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 31, | |
"text": [ | |
"3" | |
] | |
} | |
], | |
"prompt_number": 31 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 32, | |
"text": [ | |
"4" | |
] | |
} | |
], | |
"prompt_number": 32 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "StopIteration", | |
"evalue": "", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-33-e05f366da090>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mStopIteration\u001b[0m: " | |
] | |
} | |
], | |
"prompt_number": 33 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = iter(\"abc\")\n", | |
"x.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 34, | |
"text": [ | |
"'a'" | |
] | |
} | |
], | |
"prompt_number": 34 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 35, | |
"text": [ | |
"'b'" | |
] | |
} | |
], | |
"prompt_number": 35 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 36, | |
"text": [ | |
"'c'" | |
] | |
} | |
], | |
"prompt_number": 36 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "StopIteration", | |
"evalue": "", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-37-e05f366da090>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mStopIteration\u001b[0m: " | |
] | |
} | |
], | |
"prompt_number": 37 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class yrange:\n", | |
" def __init__(self, n):\n", | |
" self.i = 0\n", | |
" self.n = n\n", | |
" \n", | |
" def __iter__(self):\n", | |
" return self\n", | |
" \n", | |
" def next(self):\n", | |
" i = self.i\n", | |
" if i < self.n:\n", | |
" self.i = i + 1\n", | |
" return i\n", | |
" else:\n", | |
" raise StopIteration()\n", | |
" \n", | |
"y = yrange(5)\n", | |
"for a in y:\n", | |
" print a" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n" | |
] | |
} | |
], | |
"prompt_number": 41 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lets try to see how for loop is behind the scenes.\n", | |
" \n", | |
" for a in x:\n", | |
" print a\n", | |
"\n", | |
"Translate this in to while loop.\n", | |
"\n", | |
" it = iter(x)\n", | |
" while True:\n", | |
" try:\n", | |
" a = it.next()\n", | |
" except StopIteration:\n", | |
" break\n", | |
" print a" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# [1, 2, 3, 4] is an iterable object.\n", | |
"# x = iter([1, 2, 3, 4]) gives an iterator.\n", | |
"# next() method can be called on an iterator.\n", | |
"\n", | |
"y = yrange(5)\n", | |
"print list(y)\n", | |
"print list(y)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[0, 1, 2, 3, 4]\n", | |
"[]\n" | |
] | |
} | |
], | |
"prompt_number": 44 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class zrange:\n", | |
" def __init__(self, n):\n", | |
" self.n = n\n", | |
" def __iter__(self):\n", | |
" return yrange(self.n)\n", | |
" \n", | |
"z = zrange(5)\n", | |
"print list(z)\n", | |
"print list(z)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[0, 1, 2, 3, 4]\n", | |
"[0, 1, 2, 3, 4]\n" | |
] | |
} | |
], | |
"prompt_number": 46 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Generators" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def yrange(n):\n", | |
" i = 0\n", | |
" while i < n:\n", | |
" yield i\n", | |
" i += 1\n", | |
"\n", | |
"y = yrange(3)\n", | |
"print y.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0\n" | |
] | |
} | |
], | |
"prompt_number": 50 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"y.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 51, | |
"text": [ | |
"1" | |
] | |
} | |
], | |
"prompt_number": 51 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"y.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 52, | |
"text": [ | |
"2" | |
] | |
} | |
], | |
"prompt_number": 52 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"y.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "StopIteration", | |
"evalue": "", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-53-75a92ee8313a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0my\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mStopIteration\u001b[0m: " | |
] | |
} | |
], | |
"prompt_number": 53 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def f():\n", | |
" print \"begin f\"\n", | |
" yield 1\n", | |
" print \"after yielding 1\"\n", | |
" yield 2\n", | |
" print \"end\"\n", | |
" \n", | |
"a = f()\n", | |
"print a" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"<generator object f at 0x10270f050>\n" | |
] | |
} | |
], | |
"prompt_number": 55 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"begin f\n" | |
] | |
}, | |
{ | |
"output_type": "pyout", | |
"prompt_number": 56, | |
"text": [ | |
"1" | |
] | |
} | |
], | |
"prompt_number": 56 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"after yielding 1\n" | |
] | |
}, | |
{ | |
"output_type": "pyout", | |
"prompt_number": 57, | |
"text": [ | |
"2" | |
] | |
} | |
], | |
"prompt_number": 57 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "StopIteration", | |
"evalue": "", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-58-aa817a57a973>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mStopIteration\u001b[0m: " | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"end\n" | |
] | |
} | |
], | |
"prompt_number": 58 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"max(yrange(4))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 59, | |
"text": [ | |
"3" | |
] | |
} | |
], | |
"prompt_number": 59 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"sum(yrange(4))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 60, | |
"text": [ | |
"6" | |
] | |
} | |
], | |
"prompt_number": 60 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def squares(numbers):\n", | |
" for n in numbers:\n", | |
" yield n*n\n", | |
" \n", | |
"print sum(squares(xrange(1000000)))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"333332833333500000\n" | |
] | |
} | |
], | |
"prompt_number": 61 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file a.txt\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"5" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing a.txt\n" | |
] | |
} | |
], | |
"prompt_number": 62 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def toint(strings):\n", | |
" for s in strings:\n", | |
" yield int(s)\n", | |
" \n", | |
"print sum(toint(open(\"a.txt\")))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"15\n" | |
] | |
} | |
], | |
"prompt_number": 63 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print sum(squares(toint(open(\"a.txt\"))))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"55\n" | |
] | |
} | |
], | |
"prompt_number": 64 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# the regular way is\n", | |
"result = 0\n", | |
"for line in open(\"a.txt\"):\n", | |
" n = int(line)\n", | |
" result += n\n", | |
"print result" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"15\n" | |
] | |
} | |
], | |
"prompt_number": 66 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Write a function `joiniters`, that takes 2 iterators and returns a combined iterator.\n", | |
"\n", | |
" print sum(joiniters([1, 2, 3], [4, 5, 6]))\n", | |
" for a in joiniters([1, 2, 3], \"hello\"):\n", | |
" print a\n", | |
"\n", | |
" print list(joiniters(iter([1, 2]), iter([3, 4])))\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Write a function `iterappend`, that takes 2 arguments, an iterator and a value and return a new iterator containing all the elements of the given iterator and the given value.\n", | |
"\n", | |
" >>> list(iterappend([1, 2], 3))\n", | |
" [1, 2, 3]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Solution to joiniters\n", | |
"def joiniters(x, y):\n", | |
" for a in x:\n", | |
" yield a\n", | |
" for b in y:\n", | |
" yield b\n", | |
" \n", | |
"# solution to iterappend\n", | |
"def iterappend(x, end):\n", | |
" return joiniters(x, [end])\n", | |
"\n", | |
"print sum(iterappend([1, 2], 3))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 67 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Quick Introduction to List Comprehensions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = range(10)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 68 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[a*a for a in x]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 71, | |
"text": [ | |
"[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" | |
] | |
} | |
], | |
"prompt_number": 71 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[a*a for a in x if a % 2 == 0]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 72, | |
"text": [ | |
"[0, 4, 16, 36, 64]" | |
] | |
} | |
], | |
"prompt_number": 72 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file square.py\n", | |
"def square(x):\n", | |
" return x*x\n", | |
"def cube(x):\n", | |
" return x*x*x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing square.py\n" | |
] | |
} | |
], | |
"prompt_number": 73 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Find all line containing function definations\n", | |
"[line for line in open(\"square.py\") if line.startswith(\"def\")]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 74, | |
"text": [ | |
"['def square(x):\\n', 'def cube(x):\\n']" | |
] | |
} | |
], | |
"prompt_number": 74 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# fine all function names" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 75 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[line.split(\"(\")[0][len(\"def \"):] for line in open(\"square.py\") \n", | |
" if line.startswith(\"def\")]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 78, | |
"text": [ | |
"['square', 'cube']" | |
] | |
} | |
], | |
"prompt_number": 78 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file a.csv\n", | |
"a,b,c\n", | |
"1,2,3\n", | |
"1,4,9\n", | |
"1,8,27" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing a.csv\n" | |
] | |
} | |
], | |
"prompt_number": 79 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[line.strip(\"\\n\").split(\",\") for line in open(\"a.csv\")]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 83, | |
"text": [ | |
"[['a', 'b', 'c'], ['1', '2', '3'], ['1', '4', '9'], ['1', '8', '27']]" | |
] | |
} | |
], | |
"prompt_number": 83 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def squares(values):\n", | |
" return [x*x for x in values]\n", | |
"print sum(squares(xrange(1000000)))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"333332833333500000\n" | |
] | |
} | |
], | |
"prompt_number": 84 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Generator Expressions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"squares_list = [x*x for x in xrange(1000000)]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 85 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"squares_gen = (x*x for x in xrange(1000000))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 86 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"squares_gen" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 87, | |
"text": [ | |
"<generator object <genexpr> at 0x10270f460>" | |
] | |
} | |
], | |
"prompt_number": 87 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"sum(squares_gen)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 89, | |
"text": [ | |
"333332833333500000" | |
] | |
} | |
], | |
"prompt_number": 89 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"sum((x*x for x in xrange(1000000)))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 91, | |
"text": [ | |
"333332833333500000" | |
] | |
} | |
], | |
"prompt_number": 91 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"sum(x*x for x in xrange(1000000))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 92, | |
"text": [ | |
"333332833333500000" | |
] | |
} | |
], | |
"prompt_number": 92 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem** Write function `squares` that takes a iterable over numbers as argument and returns an iterator over their squares. Use generator expressions for doing this.\n", | |
"\n", | |
" print sum(squares(xrange(1000)))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Example: Reading multiple files" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def grep(pattern, fileobj):\n", | |
" return (line for line in fileobj if pattern in line)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 93 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def printlines(lines):\n", | |
" for line in lines:\n", | |
" print line.strip(\"\\n\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 94 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"fileobj = open(\"square.py\")\n", | |
"lines = grep(\"def\", fileobj)\n", | |
"printlines(lines)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"def square(x):\n", | |
"def cube(x):\n" | |
] | |
} | |
], | |
"prompt_number": 100 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lets try to make the program search in multiple files instead of just single one." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file hello.py\n", | |
"def hello(name):\n", | |
" print \"hello\", name" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing hello.py\n" | |
] | |
} | |
], | |
"prompt_number": 101 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def joiniters(x, y):\n", | |
" for a in x: yield a\n", | |
" for b in y: yield b\n", | |
" \n", | |
"fileobj1 = open(\"square.py\")\n", | |
"fileobj2 = open(\"hello.py\")\n", | |
"lines = joiniters(fileobj1, fileobj2)\n", | |
"lines = grep(\"def\", lines)\n", | |
"printlines(lines)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"def square(x):\n", | |
"def cube(x):\n", | |
"def hello(name):\n" | |
] | |
} | |
], | |
"prompt_number": 105 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lets extract that into useful function." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def readfiles(filenames):\n", | |
" \"\"\"Reads all files and returns iterator over lines.\"\"\"\n", | |
" for filename in filenames:\n", | |
" for line in open(filename):\n", | |
" yield line" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 106 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"lines = readfiles([\"square.py\", \"hello.py\"])\n", | |
"lines = grep(\"def\", lines)\n", | |
"printlines(lines)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"def square(x):\n", | |
"def cube(x):\n", | |
"def hello(name):\n" | |
] | |
} | |
], | |
"prompt_number": 107 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lets say some files are compressed using gzip and we want our program to read them as well." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!gzip hello.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"gzip: hello.py: No such file or directory\r\n" | |
] | |
} | |
], | |
"prompt_number": 114 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!ls *.gz" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"hello.py.gz\r\n" | |
] | |
} | |
], | |
"prompt_number": 115 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import gzip\n", | |
"def xopen(filename):\n", | |
" if filename.endswith(\".gz\"):\n", | |
" return gzip.open(filename)\n", | |
" else:\n", | |
" return open(filename)\n", | |
" \n", | |
"def readfiles(filenames):\n", | |
" \"\"\"Reads all files and returns iterator over lines.\"\"\"\n", | |
" for filename in filenames:\n", | |
" for line in xopen(filename):\n", | |
" yield line\n", | |
" \n", | |
"lines = readfiles([\"square.py\", \"hello.py.gz\"])\n", | |
"lines = grep(\"def\", lines)\n", | |
"printlines(lines)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"def square(x):\n", | |
"def cube(x):\n", | |
"def hello(name):\n" | |
] | |
} | |
], | |
"prompt_number": 113 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Write a function `countiter` to count number of elements in an iterator.\n", | |
" \n", | |
" >>> countiter(xrange(100))\n", | |
" 100\n", | |
" >>> countiter(x for x in xrange(100) if x % 2 == 0)\n", | |
" 50" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Write a function to `linecount` to count number of lines in a given file.\n", | |
"\n", | |
" print linecount(\"square.py\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Write a function `wordcount` to count number of words in a file.\n", | |
"\n", | |
" print wordcount(\"square.py\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# countiter solution\n", | |
"def countiter(it):\n", | |
" count = 0\n", | |
" for x in it:\n", | |
" count += 1\n", | |
" return count\n", | |
"\n", | |
"def countiter(it):\n", | |
" return sum(1 for x in it)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 112 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### The itertools module" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import itertools\n", | |
"\n", | |
"print list(itertools.chain([1, 2, 3, 4], [5, 6]))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[1, 2, 3, 4, 5, 6]\n" | |
] | |
} | |
], | |
"prompt_number": 116 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for a, b in itertools.izip(\"hello\", \"world\"):\n", | |
" print a, b" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"h w\n", | |
"e o\n", | |
"l r\n", | |
"l l\n", | |
"o d\n" | |
] | |
} | |
], | |
"prompt_number": 117 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" **Problem:** Implement `izip` function." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = itertools.izip(\"hello\", \"world\")\n", | |
"print x.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"('h', 'w')\n" | |
] | |
} | |
], | |
"prompt_number": 118 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Implement a function `numbers` that generate an infinite sequence of numbers starting from 0.\n", | |
"\n", | |
" >>> n = numbers()\n", | |
" >>> n.next()\n", | |
" 0\n", | |
" >>> n.next()\n", | |
" 1\n", | |
" >>> n.next()\n", | |
" 2 " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# solution to izip\n", | |
"\n", | |
"def izip(x, y):\n", | |
" x = iter(x)\n", | |
" y = iter(y)\n", | |
" while True:\n", | |
" yield x.next(), y.next()\n", | |
" \n", | |
"for a, b in izip([1, 2, 3], \"hello\"):\n", | |
" print a, b" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1 h\n", | |
"2 e\n", | |
"3 l\n" | |
] | |
} | |
], | |
"prompt_number": 121 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for i, c in enumerate(\"hello\"):\n", | |
" print i, c" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0 h\n", | |
"1 e\n", | |
"2 l\n", | |
"3 l\n", | |
"4 o\n" | |
] | |
} | |
], | |
"prompt_number": 122 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def myenumerate(it):\n", | |
" return izip(numbers(), it)\n", | |
"\n", | |
"def numbers():\n", | |
" i = 0\n", | |
" while True:\n", | |
" yield i\n", | |
" i += 1\n", | |
"\n", | |
"for i, c in myenumerate(\"hello\"):\n", | |
" print i, c" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0 h\n", | |
"1 e\n", | |
"2 l\n", | |
"3 l\n", | |
"4 o\n" | |
] | |
} | |
], | |
"prompt_number": 124 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Functional Programming" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Recursion" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def exp(x, n):\n", | |
" print \"exp\", x, n\n", | |
" if n == 0:\n", | |
" return 1\n", | |
" else:\n", | |
" return x * exp(x, n-1)\n", | |
" \n", | |
"print exp(2, 10)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"exp 2 10\n", | |
"exp 2 9\n", | |
"exp 2 8\n", | |
"exp 2 7\n", | |
"exp 2 6\n", | |
"exp 2 5\n", | |
"exp 2 4\n", | |
"exp 2 3\n", | |
"exp 2 2\n", | |
"exp 2 1\n", | |
"exp 2 0\n", | |
"1024\n" | |
] | |
} | |
], | |
"prompt_number": 131 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def fast_exp(x, n):\n", | |
" print \"fast_exp\", x, n\n", | |
" if n == 0:\n", | |
" return 1\n", | |
" elif n % 2 == 0:\n", | |
" return fast_exp(x*x, n/2)\n", | |
" else:\n", | |
" return x * fast_exp(x, n-1)\n", | |
"\n", | |
"print fast_exp(2, 100)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"fast_exp 2 100\n", | |
"fast_exp 4 50\n", | |
"fast_exp 16 25\n", | |
"fast_exp 16 24\n", | |
"fast_exp 256 12\n", | |
"fast_exp 65536 6\n", | |
"fast_exp 4294967296 3\n", | |
"fast_exp 4294967296 2\n", | |
"fast_exp 18446744073709551616 1\n", | |
"fast_exp 18446744073709551616 0\n", | |
"1267650600228229401496703205376\n" | |
] | |
} | |
], | |
"prompt_number": 135 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Product:** Write a function `product` to compute product of 2 numbers, using `+` and `-` operators only." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Example: Flatten list**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def flatten_list(x, result=None):\n", | |
" \"\"\"Flattens a nested list.\n", | |
"\n", | |
" >>> flatten_list([[1, 2], [3, 4, [5]]])\n", | |
" [1, 2, 3, 4, 5]\n", | |
" \"\"\"\n", | |
" if result is None:\n", | |
" result = []\n", | |
" \n", | |
" for a in x:\n", | |
" if isinstance(a, list):\n", | |
" flatten_list(a, result)\n", | |
" else:\n", | |
" result.append(a)\n", | |
" return result\n", | |
"\n", | |
"print flatten_list([1, 2, 3])\n", | |
"print flatten_list([[1, 2], [3, 4, [5]]])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[1, 2, 3]\n", | |
"[1, 2, 3, 4, 5]\n" | |
] | |
} | |
], | |
"prompt_number": 141 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Write a function `flatten_dict` to flatten a nested dictionary by joining the keys with `.` character.\n", | |
"\n", | |
" >>> flatten_dict({'a': 1, 'b': {'x': 2, 'y': 3}, 'c': 4})\n", | |
" {'a': 1, 'b.x': 2, 'b.y': 3, 'c': 4}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def flatten_dict(d, result=None, prefix=None):\n", | |
" if result is None:\n", | |
" result = {}\n", | |
" \n", | |
" for k, v in d.items():\n", | |
" if prefix is None:\n", | |
" key = k\n", | |
" else:\n", | |
" key = prefix + \".\" + k\n", | |
" if isinstance(v, dict):\n", | |
" flatten_dict(v, result, prefix=key)\n", | |
" else:\n", | |
" result[key] = v\n", | |
" return result\n", | |
"\n", | |
"flatten_dict({'a': 1, 'b': {'x': 2, 'y': 3, 'z': {'p': 5}}, 'c': 4})" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 148, | |
"text": [ | |
"{'a': 1, 'b.x': 2, 'b.y': 3, 'b.z.p': 5, 'c': 4}" | |
] | |
} | |
], | |
"prompt_number": 148 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Example: JSON Encode" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def json_encode(data):\n", | |
" if isinstance(data, bool):\n", | |
" if data:\n", | |
" return \"true\"\n", | |
" else:\n", | |
" return \"false\"\n", | |
" elif isinstance(data, (int, float)):\n", | |
" return str(data)\n", | |
" elif isinstance(data, str):\n", | |
" return '\"' + data + '\"'\n", | |
" elif isinstance(data, list):\n", | |
" elements = [json_encode(d) for d in data]\n", | |
" values = \", \".join(elements)\n", | |
" return \"[\" + values + \"]\"\n", | |
" \n", | |
"print json_encode(True)\n", | |
"print json_encode(1.234)\n", | |
"print json_encode([1, 2, 3, True, \"hello\", [3, 4]])\n", | |
"print json_encode({\"a\": [1, True], \"b\": {\"name\": \"hello\"}})\n", | |
"# {\"a\": [1, true], \"b\": {\"name\": \"hello\"}}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"true\n", | |
"1.234\n", | |
"[1, 2, 3, true, \"hello\", [3, 4]]\n" | |
] | |
} | |
], | |
"prompt_number": 151 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Higher Order Functions" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"#### Example: Tracing Function Calls" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\n", | |
"indent = 0\n", | |
"def trace(f):\n", | |
" def g(n):\n", | |
" global indent\n", | |
" print \"| \" * indent + \"|-- \" + f.__name__, n\n", | |
" indent += 1\n", | |
" value = f(n)\n", | |
" indent -= 1\n", | |
" return value\n", | |
" return g\n", | |
"\n", | |
"def memoize(f):\n", | |
" cache = {}\n", | |
" def g(n):\n", | |
" if n not in cache:\n", | |
" cache[n] = f(n)\n", | |
" return cache[n]\n", | |
" return g\n", | |
"\n", | |
"import time\n", | |
"#fib = trace(fib)\n", | |
"#fib = memoize(fib)\n", | |
"\n", | |
"@memoize\n", | |
"@trace\n", | |
"def fib(n):\n", | |
" if n == 0 or n == 1:\n", | |
" return 1\n", | |
" else:\n", | |
" return fib(n-1) + fib(n-2)\n", | |
"\n", | |
"t0 = time.time()\n", | |
"print fib(5)\n", | |
"t1 = time.time()\n", | |
"print \"took %f seconds\" % (t1-t0)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"|-- fib 5\n", | |
"| |-- fib 4\n", | |
"| | |-- fib 3\n", | |
"| | | |-- fib 2\n", | |
"| | | | |-- fib 1\n", | |
"| | | | |-- fib 0\n", | |
"8\n", | |
"took 0.000574 seconds\n" | |
] | |
} | |
], | |
"prompt_number": 187 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def profile(f):\n", | |
" def g():\n", | |
" ...\n", | |
" return g\n", | |
" \n", | |
"def timepass():\n", | |
" for i in range(100000):\n", | |
" for j in range(100):\n", | |
" x = i*j\n", | |
"\n", | |
"timepass = profile(timepass)\n", | |
"timepass()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment