Last active
January 4, 2016 18:59
-
-
Save anandology/8663909 to your computer and use it in GitHub Desktop.
Notes from python training at LinkedIn (Jan 28-Jan30, 2014) - http://nbviewer.ipython.org/gist/anandology/8663909/
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": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Python Training at LinkedIn - Day 3\n", | |
"\n", | |
"Jan 28-30, 2014 - [Anand Chitipothu](http://anandology.com)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## String Formatting" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"Hello %s\" % \"Python\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 1, | |
"text": [ | |
"'Hello Python'" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"Chapter %d: %s\" % (2, \"Working with Data\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 2, | |
"text": [ | |
"'Chapter 2: Working with Data'" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"'<a href=\"%s\">%s</a>' % (\"http://anandology.com/\",\"http://anandology.com/\") " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 3, | |
"text": [ | |
"'<a href=\"http://anandology.com/\">http://anandology.com/</a>'" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"'<a href=\"%(url)s\">%(url)s</a>' % {\"url\": \"http://anandology.com\"}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 5, | |
"text": [ | |
"'<a href=\"http://anandology.com\">http://anandology.com</a>'" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem** Problem Given a set of photographs, generate one html file for each photo to navigate through those photos. Each html file should have next and prev links.\n", | |
"\n", | |
"Sample photos and sample html file are available at <http://anandology.com/tmp/album.zip>.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file album.py\n", | |
"import sys\n", | |
"import os\n", | |
"\n", | |
"def find_jpeg_files(dirname):\n", | |
" # TODO: fix this to return only .jpg files\n", | |
" return os.listdir(dirname)\n", | |
"\n", | |
"HTML = \"\"\"\n", | |
"<a href=\"%(prev)s\">Prev</a> | <a href=\"%(next)s\">Next</a>\n", | |
"<br/>\n", | |
"<img src=\"%(photo)s\"/>\n", | |
"\"\"\"\n", | |
"\n", | |
"def create_html(dirname, jpgfile, prev_jpg, next_jpg):\n", | |
" prev_html = \"#\" # FIXME\n", | |
" next_html = \"#\" # FIXME\n", | |
" html = HTML % {\"photo\": jpgfile,\n", | |
" \"prev\": prev_html, \n", | |
" \"next\": next_html}\n", | |
" \n", | |
" htmlfile = os.path.join(dirname, jpgfile.replace(\".jpg\", \".html\"))\n", | |
" print \"generating\", htmlfile\n", | |
" f = open(htmlfile, \"w\")\n", | |
" f.write(html)\n", | |
" f.close()\n", | |
"\n", | |
"def main():\n", | |
" dirname = sys.argv[1]\n", | |
" jpegs = find_jpeg_files(dirname)\n", | |
" for f in jpegs:\n", | |
" create_html(dirname, f, \"\", \"\") # FIXME\n", | |
" \n", | |
"if __name__ == \"__main__\":\n", | |
" main()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting album.py\n" | |
] | |
} | |
], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python album.py /tmp/album" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"generating /tmp/album/photo1.html\r\n", | |
"generating /tmp/album/photo2.html\r\n", | |
"generating /tmp/album/photo3.html\r\n", | |
"generating /tmp/album/photo4.html\r\n", | |
"generating /tmp/album/sample-photo2.html\r\n" | |
] | |
} | |
], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Final Solution:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file album2.py\n", | |
"import sys\n", | |
"import os\n", | |
"\n", | |
"def find_jpeg_files(dirname):\n", | |
" return [f for f in os.listdir(dirname) if f.endswith(\".jpg\")]\n", | |
"\n", | |
"HTML = \"\"\"\n", | |
"<a href=\"%(prev)s\">Prev</a> | <a href=\"%(next)s\">Next</a>\n", | |
"<br/>\n", | |
"<img src=\"%(photo)s\"/>\n", | |
"\"\"\"\n", | |
"\n", | |
"def create_html(dirname, jpgfile, prev_jpg, next_jpg):\n", | |
" prev_html = prev_jpg.replace(\".jpg\", \".html\")\n", | |
" next_html = next_jpg.replace(\".jpg\", \".html\")\n", | |
" html = HTML % {\"photo\": jpgfile,\n", | |
" \"prev\": prev_html, \n", | |
" \"next\": next_html}\n", | |
" \n", | |
" htmlfile = os.path.join(dirname, jpgfile.replace(\".jpg\", \".html\"))\n", | |
" print \"generating\", htmlfile\n", | |
" f = open(htmlfile, \"w\")\n", | |
" f.write(html)\n", | |
" f.close()\n", | |
"\n", | |
"def main():\n", | |
" dirname = sys.argv[1]\n", | |
" jpegs = find_jpeg_files(dirname)\n", | |
" for i, f in enumerate(jpegs):\n", | |
" prev = jpegs[i-1]\n", | |
" # rollback to the first image if the current image\n", | |
" # is the last one\n", | |
" next = jpegs[(i+1) % len(jpegs)] \n", | |
" create_html(dirname, f, prev, next)\n", | |
" \n", | |
"if __name__ == \"__main__\":\n", | |
" main()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing album2.py\n" | |
] | |
} | |
], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python album2.py /tmp/album" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"generating /tmp/album/photo1.html\r\n", | |
"generating /tmp/album/photo2.html\r\n", | |
"generating /tmp/album/photo3.html\r\n", | |
"generating /tmp/album/photo4.html\r\n" | |
] | |
} | |
], | |
"prompt_number": 16 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Standard Library Modules (cont..)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**optparse module**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file ls1.py\n", | |
"import os\n", | |
"import sys\n", | |
"\n", | |
"dirname = sys.argv[1]\n", | |
"\n", | |
"for f in os.listdir(dirname):\n", | |
" print f" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing ls1.py\n" | |
] | |
} | |
], | |
"prompt_number": 17 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python ls1.py /tmp/album" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"index.html\r\n", | |
"photo1.html\r\n", | |
"photo1.jpg\r\n", | |
"photo2.html\r\n", | |
"photo2.jpg\r\n", | |
"photo3.html\r\n", | |
"photo3.jpg\r\n", | |
"photo4.html\r\n", | |
"photo4.jpg\r\n", | |
"sample-photo2.html\r\n" | |
] | |
} | |
], | |
"prompt_number": 18 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Lets try to add flag to sort by extension." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file ls2.py\n", | |
"import os\n", | |
"import optparse\n", | |
"\n", | |
"def main():\n", | |
" parser = optparse.OptionParser(description=\"Lists files in a directory\")\n", | |
" parser.add_option(\"-e\", default=False, action=\"store_true\", help=\"sort by extention\")\n", | |
" parser.add_option(\"-l\", default=False, action=\"store_true\", help=\"sort by number of lines\")\n", | |
" parser.add_option(\"-E\", \"--ext\", help=\"limits the display to files of given extension\") \n", | |
" options, args = parser.parse_args()\n", | |
" \n", | |
" # if dirname is not specified, used .\n", | |
" if args:\n", | |
" dirname = args[0]\n", | |
" else:\n", | |
" dirname = \".\"\n", | |
" \n", | |
" files = os.listdir(dirname)\n", | |
" \n", | |
" if options.ext:\n", | |
" files = [f for f in files \n", | |
" if f.endswith(\".\" + options.ext)]\n", | |
" \n", | |
" for f in files:\n", | |
" print f\n", | |
"\n", | |
"if __name__ == \"__main__\":\n", | |
" main()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting ls2.py\n" | |
] | |
} | |
], | |
"prompt_number": 49 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python ls2.py --help" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Usage: ls2.py [options]\r\n", | |
"\r\n", | |
"Lists files in a directory\r\n", | |
"\r\n", | |
"Options:\r\n", | |
" -h, --help show this help message and exit\r\n", | |
" -e sort by extention\r\n", | |
" -l sort by number of lines\r\n", | |
" -E EXT, --ext=EXT limits the display to files of given extension\r\n" | |
] | |
} | |
], | |
"prompt_number": 50 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python ls2.py /tmp/album" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"index.html\r\n", | |
"photo1.html\r\n", | |
"photo1.jpg\r\n", | |
"photo2.html\r\n", | |
"photo2.jpg\r\n", | |
"photo3.html\r\n", | |
"photo3.jpg\r\n", | |
"photo4.html\r\n", | |
"photo4.jpg\r\n", | |
"sample-photo2.html\r\n" | |
] | |
} | |
], | |
"prompt_number": 42 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python ls2.py -E jpg /tmp/album" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"photo1.jpg\r\n", | |
"photo2.jpg\r\n", | |
"photo3.jpg\r\n", | |
"photo4.jpg\r\n" | |
] | |
} | |
], | |
"prompt_number": 43 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python ls2.py --ext html /tmp/album" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"index.html\r\n", | |
"photo1.html\r\n", | |
"photo2.html\r\n", | |
"photo3.html\r\n", | |
"photo4.html\r\n", | |
"sample-photo2.html\r\n" | |
] | |
} | |
], | |
"prompt_number": 45 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Added command-line flags `-c`, `-w` and `-l` to `wc.py`." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!wc -l album.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
" 33 album.py\r\n" | |
] | |
} | |
], | |
"prompt_number": 54 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!wc -c album.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
" 818 album.py\r\n" | |
] | |
} | |
], | |
"prompt_number": 55 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!wc -cl album.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
" 33 818 album.py\r\n" | |
] | |
} | |
], | |
"prompt_number": 56 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!wc -lc album.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
" 33 818 album.py\r\n" | |
] | |
} | |
], | |
"prompt_number": 57 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## `json` module" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"counts = {\"py\": 10, 'txt': 14, \"csv\": 4}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 61 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import json\n", | |
"print json.dumps(counts)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"{\"txt\": 14, \"py\": 10, \"csv\": 4}\n" | |
] | |
} | |
], | |
"prompt_number": 62 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = json.dumps(counts)\n", | |
"x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 63, | |
"text": [ | |
"'{\"txt\": 14, \"py\": 10, \"csv\": 4}'" | |
] | |
} | |
], | |
"prompt_number": 63 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"json.loads(x)['py']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 65, | |
"text": [ | |
"10" | |
] | |
} | |
], | |
"prompt_number": 65 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem** Write a program `myip.py` to find external IP address of your network using <http://httpbin.org/get>." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**re module**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import re" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 66 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = \"123355\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 68 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"re.match(\"[0-9]+\", \"123\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 70, | |
"text": [ | |
"<_sre.SRE_Match at 0x1025bc030>" | |
] | |
} | |
], | |
"prompt_number": 70 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"re.match(\"[0-9]+\", \"123x\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 71, | |
"text": [ | |
"<_sre.SRE_Match at 0x1025bc100>" | |
] | |
} | |
], | |
"prompt_number": 71 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print re.match(\"[0-9]+\", \"y123x\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"None\n" | |
] | |
} | |
], | |
"prompt_number": 73 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"m = re.search(\"[0-9]+\", \"y123x\")\n", | |
"print m.group()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"123\n" | |
] | |
} | |
], | |
"prompt_number": 76 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = \"mathematics\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 77 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# replace all vowels with -\n", | |
"\n", | |
"re.sub('[aeiou]', '-', x)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 78, | |
"text": [ | |
"'m-th-m-t-cs'" | |
] | |
} | |
], | |
"prompt_number": 78 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Example: antihtml**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"html = \"<div><b>Hello</b> <i>world</i>!</div>\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 80 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file antihtml.py\n", | |
"import re\n", | |
"import urllib\n", | |
"\n", | |
"#html = \"<div><b>Hello</b> <i>world</i>!</div>\"\n", | |
"\n", | |
"html = urllib.urlopen(\"https://en.wikipedia.org/wiki/Shimoga\").read()\n", | |
"text = re.sub('<[^<>]*>', '', html)\n", | |
"\n", | |
"#print text\n", | |
"print text[:50]\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting antihtml.py\n" | |
] | |
} | |
], | |
"prompt_number": 119 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python antihtml.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\r\n", | |
"\r\n", | |
"\r\n", | |
"Shimoga - Wikipedia, the free encyclopedia\r\n", | |
"\r\n", | |
"\r\n", | |
"\r\n", | |
"\r\n", | |
"\r\n" | |
] | |
} | |
], | |
"prompt_number": 120 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem** Write a function `make_slug` that takes a title and converts it into a slug that can be used as part a URL. A slug is a string with all white space and special characters replaced with - (hyphen).\n", | |
"\n", | |
" >>> make_slug(\"hello world\")\n", | |
" 'hello-world'\n", | |
" >>> make_slug(\"hello, world!\")\n", | |
" 'hello-world'\n", | |
" >>> make_slug(\"-hello, world!-!-!\")\n", | |
" 'hello-world'\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Installing third-party modules" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"A central index of the third-party Python modules are maintained at <http://pypi.python.org/> and the program `pip` is used to install packages from that.\n", | |
"\n", | |
"First you need to install pip. Download and run `get-pip.py` program from:\n", | |
"\n", | |
"<https://raw.github.com/pypa/pip/master/contrib/get-pip.py>\n", | |
"\n", | |
"(cached copy is available at <http://anandology.com/tmp/get-pip.py>)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!pip --version" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"pip 1.2.1 from /Users/anand/pyenvs/sandbox27/lib/python2.7/site-packages/pip-1.2.1-py2.7.egg (python 2.7)\r\n" | |
] | |
} | |
], | |
"prompt_number": 95 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"* Install virtualenv\n", | |
"\n", | |
" $ pip install virtualenv\n", | |
"\n", | |
"* Create a virtualenv \n", | |
"\n", | |
" $ virtualenv /tmp/testenv\n", | |
" \n", | |
"* activate the virtualenv\n", | |
"\n", | |
" $ source /tmp/testenv/bin/activate\n", | |
" \n", | |
"* ensure the virualenv is activated\n", | |
"\n", | |
" $ which python\n", | |
" /tmp/testenv/bin/python\n", | |
" \n", | |
"* install a third-party library\n", | |
"\n", | |
" $ pip install tablib\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import tablib\n", | |
"\n", | |
"data = tablib.Dataset()\n", | |
"data.append([\"A\", 1])\n", | |
"data.append([\"B\", 2])\n", | |
"data.append([\"C,D\", 3])\n", | |
"\n", | |
"print data.csv\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"A,1\r\n", | |
"B,2\r\n", | |
"\"C,D\",3\r\n", | |
"\n" | |
] | |
} | |
], | |
"prompt_number": 96 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Classes and Objects" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = \"hello\"" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 97 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x.upper()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 98, | |
"text": [ | |
"'HELLO'" | |
] | |
} | |
], | |
"prompt_number": 98 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x.center(10)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 99, | |
"text": [ | |
"' hello '" | |
] | |
} | |
], | |
"prompt_number": 99 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Example: Bank Account**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file account0.py\n", | |
"\n", | |
"balance = 0\n", | |
"\n", | |
"def deposit(amount):\n", | |
" global balance\n", | |
" balance = balance + amount\n", | |
"\n", | |
"def withdraw(amount):\n", | |
" global balance\n", | |
" balance = balance - amount\n", | |
"\n", | |
"deposit(100)\n", | |
"withdraw(40)\n", | |
"print balance" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing account0.py\n" | |
] | |
} | |
], | |
"prompt_number": 100 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python account0.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"60\r\n" | |
] | |
} | |
], | |
"prompt_number": 101 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file account1.py\n", | |
"\n", | |
"def make_account():\n", | |
" return {\"balance\": 0}\n", | |
"\n", | |
"def deposit(account, amount):\n", | |
" account['balance'] += amount\n", | |
" \n", | |
"def withdraw(account, amount):\n", | |
" account['balance'] -= amount\n", | |
"\n", | |
"a = make_account()\n", | |
"b = make_account()\n", | |
"\n", | |
"deposit(a, 100)\n", | |
"deposit(b, 30)\n", | |
"print a['balance'], b['balance']\n", | |
"\n", | |
"withdraw(a, 50)\n", | |
"withdraw(b, 20)\n", | |
"print a['balance'], b['balance']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting account1.py\n" | |
] | |
} | |
], | |
"prompt_number": 105 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python account1.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"100 30\r\n", | |
"50 10\r\n" | |
] | |
} | |
], | |
"prompt_number": 106 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file account2.py\n", | |
"\n", | |
"class BankAccount:\n", | |
" def __init__(self):\n", | |
" self.balance = 0\n", | |
" \n", | |
" def deposit(self, amount):\n", | |
" self.balance += amount\n", | |
" \n", | |
" def withdraw(self, amount):\n", | |
" self.balance -= amount\n", | |
" \n", | |
"a = BankAccount()\n", | |
"b = BankAccount()\n", | |
"\n", | |
"a.deposit(100)\n", | |
"b.deposit(30)\n", | |
"print a.balance, b.balance\n", | |
"\n", | |
"a.withdraw(50)\n", | |
"b.withdraw(20)\n", | |
"print a.balance, b.balance\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting account2.py\n" | |
] | |
} | |
], | |
"prompt_number": 111 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python account2.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"100 30\r\n", | |
"50 10\r\n" | |
] | |
} | |
], | |
"prompt_number": 112 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem: Timer**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Lets say we want to measure time taken by a function.\n", | |
"import time\n", | |
"\n", | |
"class Timer:\n", | |
" def __init__(self):\n", | |
" self.time_taken = 0\n", | |
" self._tstart = 0\n", | |
" \n", | |
" def start(self):\n", | |
" self._tstart = time.time()\n", | |
"\n", | |
" def stop(self):\n", | |
" dt = time.time() - self._tstart\n", | |
" self.time_taken += dt\n", | |
" \n", | |
"tg = Timer()\n", | |
"\n", | |
"def f():\n", | |
" for i in range(1000):\n", | |
" g()\n", | |
" h()\n", | |
" \n", | |
"def g():\n", | |
" tg.start()\n", | |
" sum = 0\n", | |
" for i in range(1000):\n", | |
" sum = sum + i * i\n", | |
" tg.stop() \n", | |
" return sum\n", | |
"\n", | |
"def h():\n", | |
" sum = 0\n", | |
" for i in range(1000):\n", | |
" sum = sum + i ** 2\n", | |
" return sum\n", | |
"\n", | |
"def main():\n", | |
" t0 = time.time()\n", | |
" f()\n", | |
" t1 = time.time()\n", | |
" print \"time taken\", t1-t0\n", | |
" print \"time taken by g\", tg.time_taken\n", | |
" \n", | |
"main()\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"time taken 0.287117958069\n", | |
"time taken by g 0.122266769409\n" | |
] | |
} | |
], | |
"prompt_number": 144 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Point:\n", | |
" x = 0\n", | |
" y = 0" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"prompt_number": 123 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p1 = Point()\n", | |
"p2 = Point()\n", | |
"\n", | |
"print p1.x, p1.y\n", | |
"print p2.x, p2.y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0 0\n", | |
"0 0\n" | |
] | |
} | |
], | |
"prompt_number": 125 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p1.x = 1\n", | |
"p1.y = 2\n", | |
"p2.x = 5\n", | |
"p2.y = 9\n", | |
"print p1.x, p1.y\n", | |
"print p2.x, p2.y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1 2\n", | |
"5 9\n" | |
] | |
} | |
], | |
"prompt_number": 126 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p1.z = 5\n", | |
"print p1.z" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"5\n" | |
] | |
} | |
], | |
"prompt_number": 127 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p2.z" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "AttributeError", | |
"evalue": "Point instance has no attribute 'z'", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-128-b152092c7c5d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mp2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mz\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mAttributeError\u001b[0m: Point instance has no attribute 'z'" | |
] | |
} | |
], | |
"prompt_number": 128 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Point:\n", | |
" x = 0\n", | |
" y = 0\n", | |
" def magnitude(self):\n", | |
" return (self.x*self.x+self.y*self.y) ** 0.5\n", | |
" \n", | |
" def move(self, width, height):\n", | |
" \"\"\"moves the point width pixels along x and\n", | |
" height pixels along y direction.\n", | |
" \n", | |
" >>> p = Point()\n", | |
" >>> p.x, p.y = 1, 2\n", | |
" >>> p.move(10, 10)\n", | |
" >>> p.x, p.y\n", | |
" (11, 12)\n", | |
" \"\"\"\n", | |
" self.x += width\n", | |
" self.y += height\n", | |
"\n", | |
"p1 = Point()\n", | |
"print p1.magnitude()\n", | |
"\n", | |
"p1.x = 3\n", | |
"p1.y = 4\n", | |
"print p1.magnitude()\n", | |
"\n", | |
"p1.move(10, 10)\n", | |
"print p1.x, p1.y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0.0\n", | |
"5.0\n", | |
"13 14\n" | |
] | |
} | |
], | |
"prompt_number": 134 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def create_point(x, y):\n", | |
" p = Point()\n", | |
" p.x = x\n", | |
" p.y = y\n", | |
" return p\n", | |
"\n", | |
"p1 = create_point(1, 2)\n", | |
"p2 = create_point(3, 4)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 135 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Point:\n", | |
" def __init__(self, x, y):\n", | |
" self.x = x\n", | |
" self.y = y\n", | |
" \n", | |
" def __str__(self):\n", | |
" return \"(%d, %d)\" % (self.x, self.y)\n", | |
"\n", | |
" def __repr__(self):\n", | |
" return \"Point(%d, %d)\" % (self.x, self.y)\n", | |
"\n", | |
"p1 = Point(2, 3)\n", | |
"p2 = Point(3, 4)\n", | |
"\n", | |
"print p1.x, p1.y\n", | |
"print p2.x, p2.y\n", | |
"\n", | |
"print p1\n", | |
"\n", | |
"points = [p1, p2]\n", | |
"\n", | |
"print points" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"2 3\n", | |
"3 4\n", | |
"(2, 3)\n", | |
"[Point(2, 3), Point(3, 4)]\n" | |
] | |
} | |
], | |
"prompt_number": 143 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[1, \"2\"]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 140, | |
"text": [ | |
"[1, '2']" | |
] | |
} | |
], | |
"prompt_number": 140 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"repr(1)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 141, | |
"text": [ | |
"'1'" | |
] | |
} | |
], | |
"prompt_number": 141 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"repr(\"2\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 142, | |
"text": [ | |
"\"'2'\"" | |
] | |
} | |
], | |
"prompt_number": 142 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file account3.py\n", | |
"\n", | |
"class BankAccount:\n", | |
" def __init__(self):\n", | |
" self.balance = 0\n", | |
" \n", | |
" def deposit(self, amount):\n", | |
" self.balance += amount\n", | |
" \n", | |
" def withdraw(self, amount):\n", | |
" self.balance -= amount\n", | |
" \n", | |
" \n", | |
"class MinimumBalanceAccount(BankAccount):\n", | |
" def __init__(self, minimum_balance):\n", | |
" # call base class constructor\n", | |
" BankAccount.__init__(self)\n", | |
" \n", | |
" self.minimum_balance = minimum_balance\n", | |
" \n", | |
" def withdraw(self, amount):\n", | |
" balance = self.balance - amount\n", | |
" if balance < self.minimum_balance:\n", | |
" print \"Error: balance can't go below\", self.minimum_balance \n", | |
" else:\n", | |
" self.balance = balance\n", | |
"\n", | |
"if __name__ == \"__main__\":\n", | |
" a = BankAccount()\n", | |
" b = MinimumBalanceAccount(20)\n", | |
" \n", | |
" a.deposit(100)\n", | |
" b.deposit(30)\n", | |
" print a.balance, b.balance\n", | |
" \n", | |
" a.withdraw(50)\n", | |
" b.withdraw(20)\n", | |
" print a.balance, b.balance\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting account3.py\n" | |
] | |
} | |
], | |
"prompt_number": 162 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python account3.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"100 30\r\n", | |
"Error: balance can't go below 20\r\n", | |
"50 30\r\n" | |
] | |
} | |
], | |
"prompt_number": 152 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Exceptions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"foo" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "NameError", | |
"evalue": "name 'foo' is not defined", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-153-d3b07384d113>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfoo\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mNameError\u001b[0m: name 'foo' is not defined" | |
] | |
} | |
], | |
"prompt_number": 153 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = [1, 2, 3]\n", | |
"x[5]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "IndexError", | |
"evalue": "list index out of range", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-154-152a78396aee>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mIndexError\u001b[0m: list index out of range" | |
] | |
} | |
], | |
"prompt_number": 154 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"d = {\"x\": 1}\n", | |
"d['y']" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "KeyError", | |
"evalue": "'y'", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-156-daaeda79b610>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0md\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m\"x\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'y'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mKeyError\u001b[0m: 'y'" | |
] | |
} | |
], | |
"prompt_number": 156 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p = Point(1, 2)\n", | |
"p.foo" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "AttributeError", | |
"evalue": "Point instance has no attribute 'foo'", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-158-0e9025e583a9>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mPoint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfoo\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mAttributeError\u001b[0m: Point instance has no attribute 'foo'" | |
] | |
} | |
], | |
"prompt_number": 158 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"int(\"foo\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "ValueError", | |
"evalue": "invalid literal for int() with base 10: 'foo'", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-159-55b132f9432f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"foo\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'foo'" | |
] | |
} | |
], | |
"prompt_number": 159 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"open(\"no-such-file.txt\").read()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "IOError", | |
"evalue": "[Errno 2] No such file or directory: 'no-such-file.txt'", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mIOError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-160-987fd192939d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"no-such-file.txt\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mIOError\u001b[0m: [Errno 2] No such file or directory: 'no-such-file.txt'" | |
] | |
} | |
], | |
"prompt_number": 160 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def read_config():\n", | |
" \"\"\"reads config file if it exists.\n", | |
" \"\"\"\n", | |
" try:\n", | |
" filename = \"config.txt\"\n", | |
" text = open(filename).read()\n", | |
" # parse the text and populate result\n", | |
" result = {}\n", | |
" return result\n", | |
" except IOError:\n", | |
" print \"WARN: config file not found\"\n", | |
" return {}\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 161 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem** Write a function `safeint` that takes a string and a default value as arguments and convert the string to number and returns it. If there is any error in conversion, default value is returned.\n", | |
"\n", | |
" >>> safeint(\"5\", 0)\n", | |
" 5\n", | |
" >>> safeint(\"bad-number\", 0)\n", | |
" 0" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Write a program `csvsum.py` to compute sum of a column in a csv file. The program should accept the csv filename and column number as arguments and prints the sum of all values in that column. If any of the values is not a number, it should print a warning message.\n", | |
"\n", | |
"<pre>\n", | |
"$ cat a.csv\n", | |
"a,aa,1\n", | |
"b,bb,2\n", | |
"c,cc,N/A\n", | |
"d,dd,4\n", | |
"\n", | |
"$ python csvsum.py a.csv 3\n", | |
"WARNING: bad number N/A\n", | |
"7\n", | |
"</pre>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file csvsum.py\n", | |
"import sys\n", | |
"\n", | |
"def parse_csv(filename):\n", | |
" return [line.strip().split(\",\") for line in open(filename).readlines()]\n", | |
"\n", | |
"def safeint(value, default):\n", | |
" try:\n", | |
" return int(value)\n", | |
" except ValueError:\n", | |
" print \"WARN: invalid number\", repr(value)\n", | |
" return default\n", | |
"\n", | |
"def main():\n", | |
" filename = sys.argv[1]\n", | |
" colindex = int(sys.argv[2])-1\n", | |
" \n", | |
" data = parse_csv(filename)\n", | |
" column = [safeint(row[colindex], 0) for row in data]\n", | |
" print sum(column)\n", | |
"\n", | |
"if __name__ == \"__main__\":\n", | |
" main()\n", | |
" " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting csvsum.py\n" | |
] | |
} | |
], | |
"prompt_number": 171 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file a1.csv\n", | |
"a,1\n", | |
"b,2\n", | |
"c,3\n", | |
"d,4" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing a1.csv\n" | |
] | |
} | |
], | |
"prompt_number": 164 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python csvsum.py a1.csv 2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"10\r\n" | |
] | |
} | |
], | |
"prompt_number": 167 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file a2.csv\n", | |
"a,1\n", | |
"b,2\n", | |
"c,three\n", | |
"d,4" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Writing a2.csv\n" | |
] | |
} | |
], | |
"prompt_number": 168 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python csvsum.py a2.csv 2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"WARN: invalid number 'three'\r\n", | |
"7\r\n" | |
] | |
} | |
], | |
"prompt_number": 172 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Invoking other binaries" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import os\n", | |
"\n", | |
"os.system(\"date\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 173, | |
"text": [ | |
"0" | |
] | |
} | |
], | |
"prompt_number": 173 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"fd = os.popen(\"date\")\n", | |
"x = fd.read()\n", | |
"x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 174, | |
"text": [ | |
"'Thu Jan 30 16:25:00 IST 2014\\n'" | |
] | |
} | |
], | |
"prompt_number": 174 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import subprocess\n", | |
"\n", | |
"p = subprocess.Popen(\"date\", stdout=subprocess.PIPE)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 175 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p.stdout.read()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 177, | |
"text": [ | |
"'Thu Jan 30 16:26:02 IST 2014\\n'" | |
] | |
} | |
], | |
"prompt_number": 177 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p.wait()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 178, | |
"text": [ | |
"0" | |
] | |
} | |
], | |
"prompt_number": 178 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p = subprocess.Popen([\"sleep\", \"100\"])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 180 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p.kill()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 181 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p.wait()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 182, | |
"text": [ | |
"-9" | |
] | |
} | |
], | |
"prompt_number": 182 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Crete process to send input and read output at the same time." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p = subprocess.Popen(\"wc\", stdin=subprocess.PIPE, stdout=subprocess.PIPE)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 183 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p.stdin.write(\"one\\n\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 184 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p.stdin.write(\"one two\\n\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 185 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p.stdin.write(\"one two three\\n\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 186 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p.stdin.close()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 187 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p.stdout.read()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 188, | |
"text": [ | |
"' 3 6 26\\n'" | |
] | |
} | |
], | |
"prompt_number": 188 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p.wait()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 189, | |
"text": [ | |
"0" | |
] | |
} | |
], | |
"prompt_number": 189 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Multi-threading" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file threads1.py\n", | |
"import threading\n", | |
"\n", | |
"def f():\n", | |
" for i in range(10):\n", | |
" print i\n", | |
" \n", | |
" \n", | |
"def main():\n", | |
" print \"before thread creation\"\n", | |
" t1 = threading.Thread(target=f)\n", | |
" print \"before start\"\n", | |
" t1.start()\n", | |
" print \"after start\"\n", | |
" \n", | |
" t1.join()\n", | |
" print \"after join\"\n", | |
"\n", | |
"if __name__ == \"__main__\":\n", | |
" main()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting threads1.py\n" | |
] | |
} | |
], | |
"prompt_number": 193 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python threads1.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"before thread creation\r\n", | |
"before start\r\n", | |
"after start\r\n", | |
" 0\r\n", | |
"1\r\n", | |
"2\r\n", | |
"3\r\n", | |
"4\r\n", | |
"5\r\n", | |
"6\r\n", | |
"7\r\n", | |
"8\r\n", | |
"9\r\n", | |
"after join\r\n" | |
] | |
} | |
], | |
"prompt_number": 194 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file threads2.py\n", | |
"import threading\n", | |
"\n", | |
"def f(arg):\n", | |
" name = threading.currentThread().getName()\n", | |
" for i in range(10):\n", | |
" line = \"%s %s %s\" % (name, arg, i)\n", | |
" print line\n", | |
" \n", | |
"def main():\n", | |
" print \"before thread creation\"\n", | |
" t1 = threading.Thread(target=f, args=(\"t1\",))\n", | |
" t2 = threading.Thread(target=f, args=(\"t2\",)) \n", | |
" print \"before start\"\n", | |
" t1.start()\n", | |
" t2.start()\n", | |
" print \"after start\"\n", | |
" \n", | |
" t1.join()\n", | |
" t2.join()\n", | |
" print \"after join\"\n", | |
"\n", | |
"if __name__ == \"__main__\":\n", | |
" main()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting threads2.py\n" | |
] | |
} | |
], | |
"prompt_number": 202 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python threads2.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"before thread creation\r\n", | |
"before start\r\n", | |
"Thread-1 t1 0\r\n", | |
"Thread-1 t1 1\r\n", | |
" Thread-2 t2 0\r\n", | |
"Thread-1 t1 2after start\r\n", | |
"Thread-2 t2 1\r\n", | |
"\r\n", | |
"Thread-2 t2 2\r\n", | |
"Thread-1 t1 3\r\n", | |
"Thread-2 t2 3\r\n", | |
"Thread-1 t1 4\r\n", | |
"Thread-1 t1 5\r\n", | |
"Thread-2 t2 4\r\n", | |
"Thread-1 t1 6\r\n", | |
"Thread-2 t2 5\r\n", | |
"Thread-1 t1 7\r\n", | |
"Thread-2 t2 6\r\n", | |
"Thread-1 t1 8\r\n", | |
"Thread-1 t1 9\r\n", | |
"Thread-2 t2 7\r\n", | |
"Thread-2 t2 8\r\n", | |
"Thread-2 t2 9\r\n", | |
"after join\r\n" | |
] | |
} | |
], | |
"prompt_number": 203 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem:** Write a program `pwget.py` to download URLs in parallel using multi-threading. The program should take a filename containing the URLs to be downloaded and download each URL in a separate thread.\n", | |
"\n", | |
" $ python pwget.py urls.txt\n", | |
" ..." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment