Created
September 24, 2013 13:46
-
-
Save anandology/6684974 to your computer and use it in GitHub Desktop.
Python Training Notes - June 2012
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": "3-classes" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Classes" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Point:\n", | |
" x = 0 \n", | |
" y = 0" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p1 = Point()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p2 = Point()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p1.x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 4, | |
"text": [ | |
"0" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p1.y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 5, | |
"text": [ | |
"0" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p2.x, p2.y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 6, | |
"text": [ | |
"(0, 0)" | |
] | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p1.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-7-9f8bf4473cb3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mp1\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": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p3 = {\"x\": 0, \"y\": 0}\n", | |
"p3[\"x\"]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 8, | |
"text": [ | |
"0" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p1.x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 9, | |
"text": [ | |
"0" | |
] | |
} | |
], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def display_point(p):\n", | |
" print (p.x, p.y)\n", | |
" \n", | |
"display_point(p1)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"(0, 0)\n" | |
] | |
} | |
], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p1.x = 1\n", | |
"p1.y = 3\n", | |
"display_point(p1)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"(1, 3)\n" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"display_point(p2)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"(0, 0)\n" | |
] | |
} | |
], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Point:\n", | |
" x = 0 \n", | |
" y = 0\n", | |
" \n", | |
" def display(p):\n", | |
" print (p.x, p.y)\n", | |
" \n", | |
"p1 = Point()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 18 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"Point.display" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 22, | |
"text": [ | |
"<unbound method Point.display>" | |
] | |
} | |
], | |
"prompt_number": 22 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"Point.display(p1)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"(0, 0)\n" | |
] | |
} | |
], | |
"prompt_number": 19 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p1.display() # this is short hand for Point.display(p1)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"(0, 0)\n" | |
] | |
} | |
], | |
"prompt_number": 21 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"Point.x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 23, | |
"text": [ | |
"0" | |
] | |
} | |
], | |
"prompt_number": 23 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p1.x = 1" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 24 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"p1.x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 25, | |
"text": [ | |
"1" | |
] | |
} | |
], | |
"prompt_number": 25 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"Point.display(p1) " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"(1, 0)\n" | |
] | |
} | |
], | |
"prompt_number": 26 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"Point.x, Point.y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 27, | |
"text": [ | |
"(0, 0)" | |
] | |
} | |
], | |
"prompt_number": 27 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Point:\n", | |
" def __init__(self, x=0, y=0):\n", | |
" self.x = x\n", | |
" self.y = y\n", | |
" \n", | |
" def display(self):\n", | |
" print (self.x, self.y)\n", | |
" \n", | |
" def distance_to(self, p2):\n", | |
" dx = self.x - p2.x\n", | |
" dy = self.y - p2.y\n", | |
" return (dx*dx+dy*dy) ** .5\n", | |
"\n", | |
"p1 = Point(2, 3)\n", | |
"p1.display()\n", | |
"\n", | |
"p2 = Point()\n", | |
"p2.display()\n", | |
"\n", | |
"print p1.distance_to(p2)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"(2, 3)\n", | |
"(0, 0)\n", | |
"3.60555127546\n" | |
] | |
} | |
], | |
"prompt_number": 31 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Example: Timer**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import time\n", | |
"class Timer:\n", | |
" def __init__(self):\n", | |
" self.elapsed = 0\n", | |
" self.numcalls = 0\n", | |
" \n", | |
" def start(self):\n", | |
" self.t0 = time.time()\n", | |
" self.numcalls += 1\n", | |
" \n", | |
" def stop(self):\n", | |
" t1 = time.time()\n", | |
" self.elapsed += t1 - self.t0\n", | |
" \n", | |
" def display(self):\n", | |
" print \"called\", self.numcalls, \"times and took\", self.elapsed, \"seconds\"\n", | |
"\n", | |
"t = Timer()\n", | |
"t2 = Timer()\n", | |
"for i in range(1000):\n", | |
" x = range(1000)\n", | |
" t.start()\n", | |
" 'a' in x\n", | |
" t.stop()\n", | |
" \n", | |
" y = set(x)\n", | |
" t2.start()\n", | |
" 'a' in y\n", | |
" t2.stop()\n", | |
" \n", | |
" \n", | |
"t.display()\n", | |
"t2.display()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"called 1000 times and took 0.0700263977051 seconds\n", | |
"called 1000 times and took 0.0020649433136 seconds\n" | |
] | |
} | |
], | |
"prompt_number": 41 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Example: Formatting Text**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%file formatting.py\n", | |
"def format_file(filename, formatter):\n", | |
" text = open(filename).read()\n", | |
" print formatter.format(text)\n", | |
"\n", | |
"######\n", | |
" \n", | |
"class UpperCaseFormatter:\n", | |
" def format(self, text):\n", | |
" return text.upper()\n", | |
" \n", | |
"class LineFormatter:\n", | |
" def format(self, text):\n", | |
" lines = text.splitlines()\n", | |
" lines = [self.format_line(line) for line in lines]\n", | |
" return \"\\n\".join(lines)\n", | |
" \n", | |
" def format_line(self, line):\n", | |
" return line\n", | |
" \n", | |
"class CenterAlignFormatter(LineFormatter):\n", | |
" def __init__(self, width):\n", | |
" self.width = width\n", | |
" def format_line(self, line):\n", | |
" return line.center(self.width)\n", | |
" \n", | |
"format_file(\"a.txt\", CenterAlignFormatter(20))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Overwriting formatting.py\n" | |
] | |
} | |
], | |
"prompt_number": 54 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"!python formatting.py" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
" one \r\n", | |
" two \r\n", | |
" three \r\n", | |
" four \r\n", | |
" five \r\n" | |
] | |
} | |
], | |
"prompt_number": 55 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Exceptions" | |
] | |
}, | |
{ | |
"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-56-8e249bcb5a8c>\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": 56 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def readfile(filename):\n", | |
" try:\n", | |
" return open(filename).read()\n", | |
" except IOError:\n", | |
" return \"\"\n", | |
"\n", | |
"print readfile(\"nofile.txt\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"prompt_number": 58 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem** Write function `listget` that takes a list, an index and a default value as arguments and returns the value at the specified index in the list if it exists or the default value. The last argument is optional and when not specified, `None` should be used.\n", | |
"\n", | |
" >>> listget(['a', 'b', 'c'], 2, 'not-found')\n", | |
" 'c'\n", | |
" >>> listget(['a', 'b', 'c'], 10, 'not-found')\n", | |
" 'not-found'" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Problem** Write a function `safeint(s, default)`. It string `s` is can be converted to a number, it should be converted and returned. Otherwise, `default` should be returned.\n", | |
"\n", | |
" >>> safeint(\"2\", 0)\n", | |
" 2\n", | |
" >>> safeint(\"badnumber\", 0)\n", | |
" 0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def do_something():\n", | |
" 1/2\n", | |
"\n", | |
"def cleanup():\n", | |
" pass\n", | |
"\n", | |
"try:\n", | |
" do_something()\n", | |
"except:\n", | |
" print \"error\"\n", | |
"else:\n", | |
" print \"success!\"\n", | |
"finally:\n", | |
" cleanup()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"success!\n" | |
] | |
} | |
], | |
"prompt_number": 66 | |
}, | |
{ | |
"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