Last active
December 20, 2015 13:19
-
-
Save anandology/6137954 to your computer and use it in GitHub Desktop.
Advanced Python Workshop
August 2-4, 2013
http://advancedpython.hasgeek.com/
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 - Day 1" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Advanced Python Workshop - Day 1\n", | |
"[[Day 1]](http://nbviewer.ipython.org/6137954/Advanced%20Python%20Workshop%20-%20Day%201.ipynb) - \n", | |
"[[Day 2]](http://nbviewer.ipython.org/6137954/Advanced%20Python%20Workshop%20-%20Day%202.ipynb) - \n", | |
"[[Day 3]](http://nbviewer.ipython.org/6137954/Advanced%20Python%20Workshop%20-%20Day%203.ipynb)\n", | |
"\n", | |
"August 2-4, 2013 <br/>\n", | |
"by [Anand Chitipothu](http://anandology.com/) <br/>\n", | |
"<http://advancedpython.hasgeek.com/>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"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": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"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": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = [1, 2]\n", | |
"y = [x, 5]\n", | |
"x = [1, 2, 3]\n", | |
"print y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[1, 2], 5]\n" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def append(x, a):\n", | |
" x.append(a)\n", | |
" \n", | |
"p = [1, 2]\n", | |
"append(p, 3)\n", | |
"print p" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[1, 2, 3]\n" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Default Arguments to functions**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def inc(x, amount=1):\n", | |
" return x + amount\n", | |
"\n", | |
"x = 4\n", | |
"print inc(x)\n", | |
"print inc(x, 3)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"5\n", | |
"7\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def inc2(x, amount=1):\n", | |
" x = x + amount\n", | |
" return x\n", | |
"\n", | |
"x = 4\n", | |
"print inc2(x)\n", | |
"print x\n", | |
"print inc2(x, 3)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"5\n", | |
"4\n", | |
"7\n" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Python is strongly typed." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"1\" + 2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "TypeError", | |
"evalue": "cannot concatenate 'str' and 'int' objects", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-7-e83767cb1e37>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m\"1\"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m: cannot concatenate 'str' and 'int' objects" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lets go back to default args." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def f(x):\n", | |
" print \"f is called with arg\", x\n", | |
" return x\n", | |
"\n", | |
"print \"before defining inc\"\n", | |
"def inc3(x, amount=f(1)):\n", | |
" return x + amount\n", | |
"print \"after defining inc\"\n", | |
"print inc3(5)\n", | |
"print inc3(10)\n", | |
"print \"end\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"before defining inc\n", | |
"f is called with arg 1\n", | |
"after defining inc\n", | |
"6\n", | |
"11\n", | |
"end\n" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"default values are evaluated when the function is defined." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def f(x):\n", | |
" print \"f is called with arg\", x\n", | |
" return x\n", | |
"\n", | |
"y = 1\n", | |
"print \"before defining inc\"\n", | |
"def inc3(x, amount=f(y)):\n", | |
" return x + amount\n", | |
"print \"after defining inc\"\n", | |
"print inc3(5)\n", | |
"y = 2\n", | |
"print inc3(10)\n", | |
"print \"end\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"before defining inc\n", | |
"f is called with arg 1\n", | |
"after defining inc\n", | |
"6\n", | |
"11\n", | |
"end\n" | |
] | |
} | |
], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"inc3" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 10, | |
"text": [ | |
"<function __main__.inc3>" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"dir(inc3)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 11, | |
"text": [ | |
"['__call__',\n", | |
" '__class__',\n", | |
" '__closure__',\n", | |
" '__code__',\n", | |
" '__defaults__',\n", | |
" '__delattr__',\n", | |
" '__dict__',\n", | |
" '__doc__',\n", | |
" '__format__',\n", | |
" '__get__',\n", | |
" '__getattribute__',\n", | |
" '__globals__',\n", | |
" '__hash__',\n", | |
" '__init__',\n", | |
" '__module__',\n", | |
" '__name__',\n", | |
" '__new__',\n", | |
" '__reduce__',\n", | |
" '__reduce_ex__',\n", | |
" '__repr__',\n", | |
" '__setattr__',\n", | |
" '__sizeof__',\n", | |
" '__str__',\n", | |
" '__subclasshook__',\n", | |
" 'func_closure',\n", | |
" 'func_code',\n", | |
" 'func_defaults',\n", | |
" 'func_dict',\n", | |
" 'func_doc',\n", | |
" 'func_globals',\n", | |
" 'func_name']" | |
] | |
} | |
], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"inc3.func_defaults" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 12, | |
"text": [ | |
"(1,)" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"inc3.func_defaults = (2,)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"inc3(5)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 14, | |
"text": [ | |
"7" | |
] | |
} | |
], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def f(a, x=[]):\n", | |
" x.append(a)\n", | |
" return x\n", | |
"\n", | |
"print f(0, [1, 2])\n", | |
"print f(1)\n", | |
"print f(2)\n", | |
"print f(0, [1, 2])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[1, 2, 0]\n", | |
"[1]\n", | |
"[1, 2]\n", | |
"[1, 2, 0]\n" | |
] | |
} | |
], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"How to fix this?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def f(a, x=None):\n", | |
" if x is None:\n", | |
" x = []\n", | |
" x.append(a)\n", | |
" return x\n", | |
"\n", | |
"print f(0, [1, 2])\n", | |
"print f(1)\n", | |
"print f(2)\n", | |
"print f(0, [1, 2])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[1, 2, 0]\n", | |
"[1]\n", | |
"[2]\n", | |
"[1, 2, 0]\n" | |
] | |
} | |
], | |
"prompt_number": 16 | |
}, | |
{ | |
"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": 20 | |
}, | |
{ | |
"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": 21 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for k in {\"x\": 1, \"y\": 2, \"z\": 3}:\n", | |
" print k" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"y\n", | |
"x\n", | |
"z\n" | |
] | |
} | |
], | |
"prompt_number": 22 | |
}, | |
{ | |
"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": 24 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\",\".join([\"a\", \"b\", \"c\"])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 25, | |
"text": [ | |
"'a,b,c'" | |
] | |
} | |
], | |
"prompt_number": 25 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\",\".join((\"a\", \"b\", \"c\"))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 26, | |
"text": [ | |
"'a,b,c'" | |
] | |
} | |
], | |
"prompt_number": 26 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\",\".join({\"x\": 1, \"y\": 2, \"z\": 3})" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 27, | |
"text": [ | |
"'y,x,z'" | |
] | |
} | |
], | |
"prompt_number": 27 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Will the order of keys in dict be same always?\n", | |
"Yes, if you use the same dict." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"d = {\"x\": 1, \"y\": 2, \"z\": 3}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 28 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"d.keys()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 29, | |
"text": [ | |
"['y', 'x', 'z']" | |
] | |
} | |
], | |
"prompt_number": 29 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"max([1, 2, 3, 4])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 33, | |
"text": [ | |
"4" | |
] | |
} | |
], | |
"prompt_number": 33 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"max({\"x\": 1, \"y\": 2, \"z\": 3})" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 34, | |
"text": [ | |
"'z'" | |
] | |
} | |
], | |
"prompt_number": 34 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = iter([1, 2, 3])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 44 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"next(x)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 45, | |
"text": [ | |
"1" | |
] | |
} | |
], | |
"prompt_number": 45 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"next(x)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 46, | |
"text": [ | |
"2" | |
] | |
} | |
], | |
"prompt_number": 46 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"next(x)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 47, | |
"text": [ | |
"3" | |
] | |
} | |
], | |
"prompt_number": 47 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"next(x)" | |
], | |
"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-40-5e4e57af3a97>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mStopIteration\u001b[0m: " | |
] | |
} | |
], | |
"prompt_number": 40 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Before Python 2.6, the next element is got by calling `x.next()`. In Python 2.7, a builtin `next` is added and that is the only way in Python 3." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"y = iter([1, 2, 3])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 49 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"y.next()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 50, | |
"text": [ | |
"1" | |
] | |
} | |
], | |
"prompt_number": 50 | |
}, | |
{ | |
"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": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 53, | |
"text": [ | |
"3" | |
] | |
} | |
], | |
"prompt_number": 53 | |
}, | |
{ | |
"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-54-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": 54 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lets implememt something like `xrange`." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for i in xrange(4):\n", | |
" print i," | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0 1 2 3\n" | |
] | |
} | |
], | |
"prompt_number": 55 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"xrange(4)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 56, | |
"text": [ | |
"xrange(4)" | |
] | |
} | |
], | |
"prompt_number": 56 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class yrange:\n", | |
" def __init__(self, n):\n", | |
" self.n = n\n", | |
" self.i = 0\n", | |
" \n", | |
" def __iter__(self):\n", | |
" return self\n", | |
" \n", | |
" def next(self):\n", | |
" print \"next called\", self.i\n", | |
" i = self.i\n", | |
" if i < self.n:\n", | |
" self.i += 1\n", | |
" return i\n", | |
" raise StopIteration()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 66 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"yrange(4)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 67, | |
"text": [ | |
"<__main__.yrange instance at 0x101f9f6c8>" | |
] | |
} | |
], | |
"prompt_number": 67 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for i in yrange(4): print i," | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0 1 2 3\n" | |
] | |
} | |
], | |
"prompt_number": 60 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"y = yrange(4)\n", | |
"it = iter(y)\n", | |
"print next(it)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"next called 0\n", | |
"0\n" | |
] | |
} | |
], | |
"prompt_number": 68 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print next(it)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"next called 1\n", | |
"1\n" | |
] | |
} | |
], | |
"prompt_number": 69 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print next(it)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"next called 2\n", | |
"2\n" | |
] | |
} | |
], | |
"prompt_number": 70 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print next(it)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"next called 3\n", | |
"3\n" | |
] | |
} | |
], | |
"prompt_number": 71 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print next(it)" | |
], | |
"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-72-b9b0e582689c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mit\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": [ | |
"next called 4\n" | |
] | |
} | |
], | |
"prompt_number": 72 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"What happens when we overwrite a builtin function?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def f(): return 1\n", | |
"def f(): return 2\n", | |
"print f()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"2\n" | |
] | |
} | |
], | |
"prompt_number": 73 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The last defination overwrites the previous ones." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Generators**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def numbers():\n", | |
" print \"begin\"\n", | |
" yield 1\n", | |
" print \"after 1\"\n", | |
" yield 2\n", | |
" print \"after 2\"\n", | |
" yield 3\n", | |
" print \"end\"\n", | |
" " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 76 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for i in numbers(): print i" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"begin\n", | |
"1\n", | |
"after 1\n", | |
"2\n", | |
"after 2\n", | |
"3\n", | |
"end\n" | |
] | |
} | |
], | |
"prompt_number": 77 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = numbers()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 78 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 79, | |
"text": [ | |
"<generator object numbers at 0x101faa0a0>" | |
] | |
} | |
], | |
"prompt_number": 79 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"next(x)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"begin\n" | |
] | |
}, | |
{ | |
"output_type": "pyout", | |
"prompt_number": 80, | |
"text": [ | |
"1" | |
] | |
} | |
], | |
"prompt_number": 80 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"next(x)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"after 1\n" | |
] | |
}, | |
{ | |
"output_type": "pyout", | |
"prompt_number": 81, | |
"text": [ | |
"2" | |
] | |
} | |
], | |
"prompt_number": 81 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"next(x)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"after 2\n" | |
] | |
}, | |
{ | |
"output_type": "pyout", | |
"prompt_number": 82, | |
"text": [ | |
"3" | |
] | |
} | |
], | |
"prompt_number": 82 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"next(x)" | |
], | |
"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-83-5e4e57af3a97>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\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": 83 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Implement `yrange` as a generator function." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file yrange.py\n", | |
"def yrange(n):\n", | |
" i = 0\n", | |
" while i < n:\n", | |
" yield i\n", | |
" i += 1\n", | |
" \n", | |
"for i in yrange(10):\n", | |
" print i," | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting yrange.py\n" | |
] | |
} | |
], | |
"prompt_number": 104 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python yrange.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0 1 2 3 4 5 6 7 8 9\r\n" | |
] | |
} | |
], | |
"prompt_number": 105 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def squares(numbers):\n", | |
" for n in numbers:\n", | |
" yield n*n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 85 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"sum(squares(yrange(1000000)))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 86, | |
"text": [ | |
"333332833333500000" | |
] | |
} | |
], | |
"prompt_number": 86 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The `file` object in Python is iterable." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file numbers.txt\n", | |
"one\n", | |
"two\n", | |
"three\n", | |
"four\n", | |
"five" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting numbers.txt\n" | |
] | |
} | |
], | |
"prompt_number": 88 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"f = open(\"numbers.txt\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 89 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"f" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 90, | |
"text": [ | |
"<open file 'numbers.txt', mode 'r' at 0x101ebbb70>" | |
] | |
} | |
], | |
"prompt_number": 90 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for line in f: print line" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"one\n", | |
"\n", | |
"two\n", | |
"\n", | |
"three\n", | |
"\n", | |
"four\n", | |
"\n", | |
"five\n" | |
] | |
} | |
], | |
"prompt_number": 91 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file grep.py\n", | |
"import sys\n", | |
"def grep(pattern, lines):\n", | |
" for line in lines:\n", | |
" #print \"grep\", repr(line)\n", | |
" if pattern in line:\n", | |
" yield line\n", | |
" \n", | |
"def uppercase(seq):\n", | |
" for x in seq:\n", | |
" #print \"uppercase\", repr(x)\n", | |
" yield x.upper()\n", | |
" \n", | |
"def print_lines(lines):\n", | |
" for line in lines:\n", | |
" #print \"print_lines\", repr(line)\n", | |
" print line.strip(\"\\n\")\n", | |
" \n", | |
"def main():\n", | |
" pattern = sys.argv[1]\n", | |
" filename = sys.argv[2]\n", | |
" f = open(filename)\n", | |
" lines = grep(pattern, f)\n", | |
" print_lines(lines)\n", | |
" \n", | |
"if __name__ == \"__main__\":\n", | |
" main()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing grep.py\n" | |
] | |
} | |
], | |
"prompt_number": 18 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python grep.py def grep.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"def grep(pattern, lines):\r\n", | |
"def uppercase(seq):\r\n", | |
"def print_lines(lines):\r\n", | |
"def main():\r\n" | |
] | |
} | |
], | |
"prompt_number": 120 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file num.txt\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"5\n", | |
"6\n", | |
"7\n", | |
"8\n", | |
"9\n", | |
"10" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing num.txt\n" | |
] | |
} | |
], | |
"prompt_number": 121 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def toint(seq):\n", | |
" for x in seq:\n", | |
" yield int(x)\n", | |
"\n", | |
"sum(toint(open(\"num.txt\")))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 122, | |
"text": [ | |
"55" | |
] | |
} | |
], | |
"prompt_number": 122 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Quick Intro to command line aguments**" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Because we need them everywhere." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file echo.py\n", | |
"import sys\n", | |
"print sys.argv[1]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing echo.py\n" | |
] | |
} | |
], | |
"prompt_number": 22 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python echo.py hello " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"hello\r\n" | |
] | |
} | |
], | |
"prompt_number": 129 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file square.py\n", | |
"import sys\n", | |
"\n", | |
"def square(x):\n", | |
" return x*x\n", | |
"\n", | |
"# Run this code only if executed as a script\n", | |
"# This won't be executed when this file imported\n", | |
"# as a module.\n", | |
"if __name__ == \"__main__\":\n", | |
" n = int(sys.argv[1])\n", | |
" print square(n)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting square.py\n" | |
] | |
} | |
], | |
"prompt_number": 21 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python square.py 5" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"25\r\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem** Write a program add.py that takes 2 arguments and prints their sum." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import square" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"square.square(5)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 7, | |
"text": [ | |
"25" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import time" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"time" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 9, | |
"text": [ | |
"<module 'time' from '/Users/anand/pyenvs/sandbox27/lib/python2.7/lib-dynload/time.so'>" | |
] | |
} | |
], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"time.asctime" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 10, | |
"text": [ | |
"<function time.asctime>" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"time.asctime()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 11, | |
"text": [ | |
"'Fri Aug 2 12:41:21 2013'" | |
] | |
} | |
], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"time.asctime()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 12, | |
"text": [ | |
"'Fri Aug 2 12:41:27 2013'" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file date.py\n", | |
"import time\n", | |
"print time.asctime()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing date.py\n" | |
] | |
} | |
], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python date.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Fri Aug 2 12:42:04 2013\r\n" | |
] | |
} | |
], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python date.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Fri Aug 2 12:42:07 2013\r\n" | |
] | |
} | |
], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Back to generators**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python grep.py def grep.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"def grep(pattern, lines):\r\n", | |
"def uppercase(seq):\r\n", | |
"def print_lines(lines):\r\n", | |
"def main():\r\n" | |
] | |
} | |
], | |
"prompt_number": 20 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file count_functions.py\n", | |
"import grep\n", | |
"import os\n", | |
"\n", | |
"def get_files(dir, suffix):\n", | |
" files = os.listdir(dir)\n", | |
" for f in files:\n", | |
" if f.endswith(suffix):\n", | |
" yield f\n", | |
"\n", | |
"def read_files(filenames):\n", | |
" for f in filenames:\n", | |
" for line in open(f):\n", | |
" yield line\n", | |
" \n", | |
"def main():\n", | |
" filenames = get_files(\".\", \".py\")\n", | |
" lines = read_files(filenames)\n", | |
" lines = grep.grep(\"def\", lines)\n", | |
" grep.print_lines(lines)\n", | |
" \n", | |
"if __name__ == \"__main__\":\n", | |
" main()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting count_functions.py\n" | |
] | |
} | |
], | |
"prompt_number": 25 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python count_functions.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"def get_files(dir, suffix):\r\n", | |
"def read_files(filenames):\r\n", | |
"def main():\r\n", | |
" lines = grep.grep(\"def\", lines)\r\n", | |
"def grep(pattern, lines):\r\n", | |
"def uppercase(seq):\r\n", | |
"def print_lines(lines):\r\n", | |
"def main():\r\n", | |
"def square(x):\r\n" | |
] | |
} | |
], | |
"prompt_number": 26 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Write a program `count_lines.py` to count number of lines in one or more files. The program should accept filenames as command line arguments.\n", | |
"\n", | |
" $ python count_lines.py a.txt b.txt\n", | |
" 23" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Write a program `count_words.py` to count number of words in one or more files. The program should accept filenames as command line arguments.\n", | |
"\n", | |
" $ python count_words.py a.txt b.txt\n", | |
" 46" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file count_lines.py\n", | |
"import sys\n", | |
"\n", | |
"def read_files(filenames):\n", | |
" for f in filenames:\n", | |
" for line in open(f):\n", | |
" yield line\n", | |
"\n", | |
"def count(it):\n", | |
" n = 0\n", | |
" for x in it:\n", | |
" n += 1\n", | |
" return n\n", | |
"\n", | |
"def main():\n", | |
" filenames = sys.argv[1:]\n", | |
" lines = read_files(filenames)\n", | |
" print count(lines)\n", | |
" \n", | |
"if __name__ == \"__main__\":\n", | |
" main()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting count_lines.py\n" | |
] | |
} | |
], | |
"prompt_number": 28 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python count_lines.py date.py grep.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"28\r\n" | |
] | |
} | |
], | |
"prompt_number": 30 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file count_words.py\n", | |
"import sys\n", | |
"from count_lines import read_files, count\n", | |
"\n", | |
"def split_words(lines):\n", | |
" for line in lines:\n", | |
" for w in line.strip().split():\n", | |
" yield w\n", | |
"\n", | |
"def main():\n", | |
" filenames = sys.argv[1:]\n", | |
" lines = read_files(filenames)\n", | |
" words = split_words(lines)\n", | |
" print count(words)\n", | |
" \n", | |
"if __name__ == \"__main__\":\n", | |
" main()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing count_words.py\n" | |
] | |
} | |
], | |
"prompt_number": 32 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python count_words.py date.py grep.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"65\r\n" | |
] | |
} | |
], | |
"prompt_number": 33 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!wc -w date.py grep.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
" 4 date.py\r\n", | |
" 61 grep.py\r\n", | |
" 65 total\r\n" | |
] | |
} | |
], | |
"prompt_number": 34 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Iterator/generator can be consumed only once." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = iter(range(5))\n", | |
"print next(x) # first element is consumed here\n", | |
"print list(x) # remaining all are consumed here\n", | |
"print list(x) # nothing left, so empty list" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0\n", | |
"[1, 2, 3, 4]\n", | |
"[]\n" | |
] | |
} | |
], | |
"prompt_number": 36 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"How to look at an iterator without consuming it?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def peep(it):\n", | |
" for x in it:\n", | |
" print x\n", | |
" yield x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 37 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = iter(range(10))\n", | |
"y = list(peep(x))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0\n", | |
"1\n", | |
"2\n", | |
"3\n", | |
"4\n", | |
"5\n", | |
"6\n", | |
"7\n", | |
"8\n", | |
"9\n" | |
] | |
} | |
], | |
"prompt_number": 40 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 41, | |
"text": [ | |
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" | |
] | |
} | |
], | |
"prompt_number": 41 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem** Write a function `iterjoin`, that takes two iterators as arguments and returns a single iterator.\n", | |
"\n", | |
" >>> x = iter([1, 2, 3])\n", | |
" >>> y = iter([4, 5, 6])\n", | |
" >>> z = iterjoin(x, y)\n", | |
" >>> list(z)\n", | |
" [1, 2, 3, 4, 5, 6]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def gen123():\n", | |
" yield 1\n", | |
" yield 2\n", | |
" yield 3\n", | |
" \n", | |
"g = gen123()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 42 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"next(g)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 43, | |
"text": [ | |
"1" | |
] | |
} | |
], | |
"prompt_number": 43 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Quick Intro to List Comprehensions**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[x*x for x in range(10)]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 45, | |
"text": [ | |
"[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" | |
] | |
} | |
], | |
"prompt_number": 45 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[x*x for x in range(10) if x%2 == 0]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 46, | |
"text": [ | |
"[0, 4, 16, 36, 64]" | |
] | |
} | |
], | |
"prompt_number": 46 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[line for line in open(\"grep.py\") \n", | |
" if line.startswith(\"def\")]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 48, | |
"text": [ | |
"['def grep(pattern, lines):\\n',\n", | |
" 'def uppercase(seq):\\n',\n", | |
" 'def print_lines(lines):\\n',\n", | |
" 'def main():\\n']" | |
] | |
} | |
], | |
"prompt_number": 48 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file a.csv\n", | |
"1,2,3\n", | |
"2,4,6\n", | |
"3,6,9" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing a.csv\n" | |
] | |
} | |
], | |
"prompt_number": 49 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[line.strip().split(\",\") for line in open(\"a.csv\")]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 52, | |
"text": [ | |
"[['1', '2', '3'], ['2', '4', '6'], ['3', '6', '9']]" | |
] | |
} | |
], | |
"prompt_number": 52 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file b.csv\n", | |
"# begin\n", | |
"1,2,3\n", | |
"2,4,6\n", | |
"# after two lines\n", | |
"3,6,9\n", | |
"#end" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting b.csv\n" | |
] | |
} | |
], | |
"prompt_number": 54 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Parse the above csv file by ignoring the lines starting with # character." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"sum([i*i for i in range(1, 101)])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 55, | |
"text": [ | |
"338350" | |
] | |
} | |
], | |
"prompt_number": 55 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def squares(numbers):\n", | |
" return [x*x for x in numbers]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 56 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"squares(range(10))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 57, | |
"text": [ | |
"[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" | |
] | |
} | |
], | |
"prompt_number": 57 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Generator Expressions**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def squares(numbers):\n", | |
" return (x*x for x in numbers)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 58 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"s = squares(range(10))\n", | |
"s" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 59, | |
"text": [ | |
"<generator object <genexpr> at 0x102701c30>" | |
] | |
} | |
], | |
"prompt_number": 59 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"sum((x*x for x in range(1, 101)))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 60, | |
"text": [ | |
"338350" | |
] | |
} | |
], | |
"prompt_number": 60 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"sum(x*x for x in range(1, 101))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 61, | |
"text": [ | |
"338350" | |
] | |
} | |
], | |
"prompt_number": 61 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"sum(x*x for x in xrange(1000000))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 62, | |
"text": [ | |
"333332833333500000" | |
] | |
} | |
], | |
"prompt_number": 62 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def readlines(filenames):\n", | |
" return (line for f in filenames for line in open(f))\n", | |
"\n", | |
"def grep(pattern, lines):\n", | |
" return (line for line in lines if pattern in line)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 63 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem** Implement `count` using generator expressions. The count function should take an iterator as argument and count the number of elements in it.\n", | |
"\n", | |
" >>> count(iter(range(10)))\n", | |
" 10\n", | |
" >>> count(iter(\"hello\"))\n", | |
" 5\n", | |
"\n", | |
"\n", | |
"Solution:\n", | |
"\n", | |
" def count(it):\n", | |
" return sum(1 for x in it)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem** Python module `itertools` has a function `izip`, that zips two iterators together and returns a new iterator.\n", | |
"\n", | |
" import itertools\n", | |
" for a, b in itertools.izip(\"hello\", \"world\"):\n", | |
" print a, b\n", | |
"\n", | |
"Can you implement izip yourself?\n", | |
"\n", | |
"Another sample use:\n", | |
"\n", | |
" z = izip(\"hello\", \"world\")\n", | |
" print next(z) # ('h', 'w')\n", | |
" print next(z) # ('e', 'o')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"zip('hello', 'world')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 64, | |
"text": [ | |
"[('h', 'w'), ('e', 'o'), ('l', 'r'), ('l', 'l'), ('o', 'd')]" | |
] | |
} | |
], | |
"prompt_number": 64 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def izip(it1, it2):\n", | |
" it1 = iter(it1)\n", | |
" it2 = iter(it2)\n", | |
" while True:\n", | |
" yield next(it1), next(it2)\n", | |
" \n", | |
"\n", | |
"for a, b in 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": 65 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## More on Functions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def square(x): return x*x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 67 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"f = square" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 68 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"f(4)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 69, | |
"text": [ | |
"16" | |
] | |
} | |
], | |
"prompt_number": 69 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"max(1, 2, 3)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 70, | |
"text": [ | |
"3" | |
] | |
} | |
], | |
"prompt_number": 70 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def my_max(*args):\n", | |
" m = args[0]\n", | |
" for a in args:\n", | |
" if a > m:\n", | |
" m = a\n", | |
" return m\n", | |
"\n", | |
"my_max(1, 2, 3)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 74, | |
"text": [ | |
"3" | |
] | |
} | |
], | |
"prompt_number": 74 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Write a function `my_sum`, which takes variable number of numbers as arguments and computes their sum.\n", | |
"\n", | |
" >>> my_sum(1)\n", | |
" 1\n", | |
" >>> my_sum(1, 2, 3)\n", | |
" 6\n", | |
" >>> my_sum(1, 2, 3, 4, 5)\n", | |
" 15" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Unpacking arguments\n", | |
"args = [1, 2, 5, 9, 3]\n", | |
"print my_max(*args)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"9\n" | |
] | |
} | |
], | |
"prompt_number": 78 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def add(a, b): return a + b\n", | |
"args = (1, 2)\n", | |
"print add(*args)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"3\n" | |
] | |
} | |
], | |
"prompt_number": 79 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def sprintf(template, *args):\n", | |
" return template % args\n", | |
"\n", | |
"print sprintf(\"hello %s\", \"python\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"hello python\n" | |
] | |
} | |
], | |
"prompt_number": 77 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"chapter %d: %s\" % (1, \"Introduction\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 81, | |
"text": [ | |
"'chapter 1: Introduction'" | |
] | |
} | |
], | |
"prompt_number": 81 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Keyword Arguments**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import os\n", | |
"files = os.listdir(\".\")\n", | |
"print sorted(files)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"['.git', 'Advanced Python Workshop - Day 1.ipynb', 'Advanced Python Workshop - Day 2.ipynb', 'Advanced Python Workshop - Day 3.ipynb', 'Untitled0.ipynb', 'a.csv', 'b.csv', 'count_functions.py', 'count_lines.py', 'count_lines.pyc', 'count_words.py', 'date.py', 'echo.py', 'grep.py', 'grep.pyc', 'square.py', 'square.pyc']\n" | |
] | |
} | |
], | |
"prompt_number": 83 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def getext(filename):\n", | |
" return filename.split(\".\")[-1]\n", | |
"print sorted(files, key=getext)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"['a.csv', 'b.csv', '.git', 'Advanced Python Workshop - Day 1.ipynb', 'Advanced Python Workshop - Day 2.ipynb', 'Advanced Python Workshop - Day 3.ipynb', 'Untitled0.ipynb', 'count_functions.py', 'count_lines.py', 'count_words.py', 'date.py', 'echo.py', 'grep.py', 'square.py', 'count_lines.pyc', 'grep.pyc', 'square.pyc']\n" | |
] | |
} | |
], | |
"prompt_number": 84 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem** How do you sort filenames by length of the filename?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def diff(a, b):\n", | |
" return a - b\n", | |
"\n", | |
"print diff(5, 2)\n", | |
"print diff(5, b=2)\n", | |
"print diff(a=5, b=2)\n", | |
"print diff(b=2, a=5)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"3\n", | |
"3\n", | |
"3\n", | |
"3\n" | |
] | |
} | |
], | |
"prompt_number": 85 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def mydict(**kwargs):\n", | |
" return kwargs\n", | |
"\n", | |
"mydict(x=1, y=2)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 86, | |
"text": [ | |
"{'x': 1, 'y': 2}" | |
] | |
} | |
], | |
"prompt_number": 86 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"chapter %d: %s\" % (1, \"introduction\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 90, | |
"text": [ | |
"'chapter 1: introduction'" | |
] | |
} | |
], | |
"prompt_number": 90 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"chapter %(num)d: %(title)s\" % {\"num\": 1, \"title\": \"introduction\"}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 91, | |
"text": [ | |
"'chapter 1: introduction'" | |
] | |
} | |
], | |
"prompt_number": 91 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"t = \"chapter %(num)d: %(title)s\\n\\nWelcome to %(title)s\"\n", | |
"print t % {\"num\": 1, \"title\": \"introduction\"}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"chapter 1: introduction\n", | |
"\n", | |
"Welcome to introduction\n" | |
] | |
} | |
], | |
"prompt_number": 93 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file hello.tmpl\n", | |
"Hello, %(name)s" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting hello.tmpl\n" | |
] | |
} | |
], | |
"prompt_number": 94 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def render_template(_filename, **kwargs):\n", | |
" t = open(_filename).read()\n", | |
" return t % kwargs\n", | |
"\n", | |
"print render_template(\"hello.tmpl\", name=\"python\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Hello, python\n" | |
] | |
} | |
], | |
"prompt_number": 95 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Implement render_template1 which takes arguments by position and substitutes them.\n", | |
"\n", | |
" print render_template1(\"a.tmpl\", \"hello\", \"world\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Lambda functions**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"names = [\n", | |
" 'python', 'Perl', 'Java', \n", | |
" 'c', 'Haskell', 'ruby']\n", | |
"sorted(names)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 98, | |
"text": [ | |
"['Haskell', 'Java', 'Perl', 'c', 'python', 'ruby']" | |
] | |
} | |
], | |
"prompt_number": 98 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"sorted(names, key=len)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 99, | |
"text": [ | |
"['c', 'Perl', 'Java', 'ruby', 'python', 'Haskell']" | |
] | |
} | |
], | |
"prompt_number": 99 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"How to sort them ignoring the case?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def lower(s): return s.lower()\n", | |
"sorted(names, key=lower)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 101, | |
"text": [ | |
"['c', 'Haskell', 'Java', 'Perl', 'python', 'ruby']" | |
] | |
} | |
], | |
"prompt_number": 101 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"sorted(names, key=lambda s: s.lower())" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 102, | |
"text": [ | |
"['c', 'Haskell', 'Java', 'Perl', 'python', 'ruby']" | |
] | |
} | |
], | |
"prompt_number": 102 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"square = lambda x: x*x\n", | |
"print square(4)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"16\n" | |
] | |
} | |
], | |
"prompt_number": 103 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"functions = {\n", | |
" \"square\": lambda x: x*x,\n", | |
" \"cube\": lambda x: x*x*x,\n", | |
" \"incr\": lambda x: x+1\n", | |
"}\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 105 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def call_func(name, arg):\n", | |
" f = functions[name]\n", | |
" return f(arg)\n", | |
"\n", | |
"call_func(\"incr\", 7)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 106, | |
"text": [ | |
"8" | |
] | |
} | |
], | |
"prompt_number": 106 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem** Write a function `extsort` that takes list of filenames and sorts them by extension.\n", | |
"\n", | |
" >>> extsort(['a.py', 'b.py', 'x.c'])\n", | |
" ['x.c', 'a.py', 'b.py']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def isort(values):\n", | |
" # you can define an inner function like this, if required.\n", | |
" def lower(s):\n", | |
" return s.lower()\n", | |
" return sorted(values, key=lower)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 109 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"(lambda x:x*x)(4)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 110, | |
"text": [ | |
"16" | |
] | |
} | |
], | |
"prompt_number": 110 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment