Last active
December 21, 2015 13:49
-
-
Save jacebrowning/6315001 to your computer and use it in GitHub Desktop.
'Python is Awesome' presentation from BarCampGR 2013. View at: http://nbviewer.ipython.org/6315001
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 is Awesome\n", | |
"### @JaceBrowning\n", | |
"\n", | |
"From Wikipedia:\n", | |
"\n", | |
"> Python is a widely used general-purpose, high-level programming language.\n", | |
"\n", | |
"> Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Everything is an Object\n", | |
"\n", | |
"In Python, everything is an object. Objects are first-class and can be stored in variable and passed to functions." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = [1, 2, 3, 'a', 'b', 'c']\n", | |
"\n", | |
"x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 16, | |
"text": [ | |
"[1, 2, 3, 'a', 'b', 'c']" | |
] | |
} | |
], | |
"prompt_number": 16 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"y = x\n", | |
"\n", | |
"y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 17, | |
"text": [ | |
"[1, 2, 3, 'a', 'b', 'c']" | |
] | |
} | |
], | |
"prompt_number": 17 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x.append(\"this\")\n", | |
"\n", | |
"x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 18, | |
"text": [ | |
"[1, 2, 3, 'a', 'b', 'c', 'this']" | |
] | |
} | |
], | |
"prompt_number": 18 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x.append" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 19, | |
"text": [ | |
"<function append>" | |
] | |
} | |
], | |
"prompt_number": 19 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a_func = x.append\n", | |
"\n", | |
"a_func" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 20, | |
"text": [ | |
"<function append>" | |
] | |
} | |
], | |
"prompt_number": 20 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a_func(\"that\")\n", | |
"\n", | |
"x" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 21, | |
"text": [ | |
"[1, 2, 3, 'a', 'b', 'c', 'this', 'that']" | |
] | |
} | |
], | |
"prompt_number": 21 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"type(x)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 26, | |
"text": [ | |
"list" | |
] | |
} | |
], | |
"prompt_number": 26 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a_class = type(x)\n", | |
"\n", | |
"a_class" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 27, | |
"text": [ | |
"list" | |
] | |
} | |
], | |
"prompt_number": 27 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"z = a_class()\n", | |
"\n", | |
"type(z)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 28, | |
"text": [ | |
"list" | |
] | |
} | |
], | |
"prompt_number": 28 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Useful Built-in Types\n", | |
"\n", | |
"Python's built-in types are good enough in many situations and work together in predictable ways." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"my_int = 42\n", | |
"my_float = 42.0\n", | |
"my_str = \"42\"\n", | |
"my_bool = True" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 29 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"my_int == my_float" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 30, | |
"text": [ | |
"True" | |
] | |
} | |
], | |
"prompt_number": 30 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"str(my_int) == my_str" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 33, | |
"text": [ | |
"True" | |
] | |
} | |
], | |
"prompt_number": 33 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"bool(my_int) == my_bool" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 34, | |
"text": [ | |
"True" | |
] | |
} | |
], | |
"prompt_number": 34 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[my_int, my_float, my_str, my_bool]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 35, | |
"text": [ | |
"[42, 42.0, '42', True]" | |
] | |
} | |
], | |
"prompt_number": 35 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"{'numbers': (my_int, my_float), 'others': (my_str, my_bool)}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 36, | |
"text": [ | |
"{'numbers': (42, 42.0), 'others': ('42', True)}" | |
] | |
} | |
], | |
"prompt_number": 36 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"http://docs.python.org/2/library/stdtypes.html" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Dynamic (Duck) Typing\n", | |
"\n", | |
"Python data types are checked at run-time and not bound to a variable. Functions do not necessarily care about the types of objects passed to them." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"my_list = [1, 4.2, True, \"Hello, world!\"]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 37 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"len(my_list)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 38, | |
"text": [ | |
"4" | |
] | |
} | |
], | |
"prompt_number": 38 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"my_str = my_list[-1]\n", | |
"\n", | |
"my_str" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 39, | |
"text": [ | |
"'Hello, world!'" | |
] | |
} | |
], | |
"prompt_number": 39 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"len(my_str)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 40, | |
"text": [ | |
"13" | |
] | |
} | |
], | |
"prompt_number": 40 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a = my_str.split(',')[0] + '!'\n", | |
"\n", | |
"a" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 41, | |
"text": [ | |
"'Hello!'" | |
] | |
} | |
], | |
"prompt_number": 41 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a = my_list[1:3]\n", | |
"\n", | |
"a" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 42, | |
"text": [ | |
"[4.2, True]" | |
] | |
} | |
], | |
"prompt_number": 42 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Chained Comparison Opperators\n", | |
"\n", | |
"Pretty basic math, but many languages lack this." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x = 42" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 43 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"40 < x < 99" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 44, | |
"text": [ | |
"True" | |
] | |
} | |
], | |
"prompt_number": 44 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"1 <= x < 40" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 45, | |
"text": [ | |
"False" | |
] | |
} | |
], | |
"prompt_number": 45 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Numerical Literals\n", | |
"\n", | |
"Integers can take whatever form you need them in." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"65 == 0x41 == 0b01000001 == ord('A')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 46, | |
"text": [ | |
"True" | |
] | |
} | |
], | |
"prompt_number": 46 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### List Comprehension\n", | |
"\n", | |
"The built-in container types can be dynamically constructed." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[x for x in range(10)]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 47, | |
"text": [ | |
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" | |
] | |
} | |
], | |
"prompt_number": 47 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[x * 1.5 for x in range(10)]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 48, | |
"text": [ | |
"[0.0, 1.5, 3.0, 4.5, 6.0, 7.5, 9.0, 10.5, 12.0, 13.5]" | |
] | |
} | |
], | |
"prompt_number": 48 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"[x * 1.5 for x in range(10) if x > 4]" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 49, | |
"text": [ | |
"[7.5, 9.0, 10.5, 12.0, 13.5]" | |
] | |
} | |
], | |
"prompt_number": 49 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"{x: x * 1.5 for x in range(10)}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 50, | |
"text": [ | |
"{0: 0.0,\n", | |
" 1: 1.5,\n", | |
" 2: 3.0,\n", | |
" 3: 4.5,\n", | |
" 4: 6.0,\n", | |
" 5: 7.5,\n", | |
" 6: 9.0,\n", | |
" 7: 10.5,\n", | |
" 8: 12.0,\n", | |
" 9: 13.5}" | |
] | |
} | |
], | |
"prompt_number": 50 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### String Formatting\n", | |
"\n", | |
"Python contains a mini-language for string formatting (in addition to C-style string formatting)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"vehicle '{0}' has wheel count: {1}\".format('car', 4)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 51, | |
"text": [ | |
"\"vehicle 'car' has wheel count: 4\"" | |
] | |
} | |
], | |
"prompt_number": 51 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"a score of {:.2%}\".format(.96578)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 52, | |
"text": [ | |
"'a score of 96.58%'" | |
] | |
} | |
], | |
"prompt_number": 52 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"{:,}\".format(1000000)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 53, | |
"text": [ | |
"'1,000,000'" | |
] | |
} | |
], | |
"prompt_number": 53 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"\"{:.2e}\".format(1000000)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 54, | |
"text": [ | |
"'1.00e+06'" | |
] | |
} | |
], | |
"prompt_number": 54 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"http://docs.python.org/2/library/string.html#format-specification-mini-language" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Keyword Arguments\n", | |
"\n", | |
"Functions arguments can be applied by position or name." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def total(sub, tip, tax=0.06):\n", | |
" return sub + (sub * tax) + tip" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 55 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"total(10.0, 2.0)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 56, | |
"text": [ | |
"12.6" | |
] | |
} | |
], | |
"prompt_number": 56 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"total(10.0, 2.0, 0.10)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 57, | |
"text": [ | |
"13.0" | |
] | |
} | |
], | |
"prompt_number": 57 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"total(10.0, 2.0, tax=0.05)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 58, | |
"text": [ | |
"12.5" | |
] | |
} | |
], | |
"prompt_number": 58 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"total(10.0, tax=0.09, tip=1.5)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 59, | |
"text": [ | |
"12.4" | |
] | |
} | |
], | |
"prompt_number": 59 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Data Model\n", | |
"\n", | |
"Python's \"magic\" methods make defining custom classes work well with duck typing." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Person(object):\n", | |
" \n", | |
" def __init__(self, name, age):\n", | |
" self.name = name\n", | |
" self.age = age\n", | |
" \n", | |
" def __str__(self):\n", | |
" return \"{n} is {a}\".format(n=self.name, a=self.age)\n", | |
" \n", | |
" def __iadd__(self, years):\n", | |
" self.age += years\n", | |
" return self\n", | |
" \n", | |
" def __cmp__(self, other):\n", | |
" return cmp(self.age, other.age)\n", | |
" " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 69 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"person = Person(\"John Smith\", 42)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 70 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print person" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"John Smith is 42\n" | |
] | |
} | |
], | |
"prompt_number": 71 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"person += 1" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 72 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print person" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"John Smith is 43\n" | |
] | |
} | |
], | |
"prompt_number": 73 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"person > Person(\"Jane Doe\", 42)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 74, | |
"text": [ | |
"True" | |
] | |
} | |
], | |
"prompt_number": 74 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Explicit Namespaces\n", | |
"\n", | |
"In Python, namespaces are explicit. To refernce an object, it must be in your local or global namespace (or built-in)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Foo(object):\n", | |
" \n", | |
" BAR = 1\n", | |
" \n", | |
" def __init__(self, value):\n", | |
" self.value = value # creation of self.value\n", | |
" \n", | |
" def __str__(self):\n", | |
" text = \"\"\n", | |
" text += str(self.value) # instance variable\n", | |
" text += ', '\n", | |
" text += str(self.BAR) # class variable\n", | |
" return text\n", | |
" \n", | |
"print Foo(2)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"2, 1\n" | |
] | |
} | |
], | |
"prompt_number": 76 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Decorators\n", | |
"\n", | |
"The decorator syntax allow dynamic modification of attributes." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def show_call(func):\n", | |
" print \"called {0}\".format(func.__name__)\n", | |
" return func" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 77 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Foo(object):\n", | |
" \n", | |
" BAR = 1\n", | |
" \n", | |
" @show_call\n", | |
" def __init__(self, value):\n", | |
" self.value = value # creation of self.value\n", | |
" \n", | |
" @show_call\n", | |
" def __str__(self):\n", | |
" text = \"\"\n", | |
" text += str(self.value) # instance variable\n", | |
" text += ', '\n", | |
" text += str(self.BAR) # class variable\n", | |
" return text\n", | |
" \n", | |
"print Foo(2)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"called __init__\n", | |
"called __str__\n", | |
"2, 1\n" | |
] | |
} | |
], | |
"prompt_number": 78 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Standard Libraries\n", | |
"\n", | |
"Python's standard library contains many useful modules." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from collections import Counter\n", | |
"\n", | |
"for char, count in Counter(\"The quick brown fox jumped over the lazy dog.\").items():\n", | |
" if count > 1 and char != ' ':\n", | |
" print char, count" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"e 4\n", | |
"d 2\n", | |
"h 2\n", | |
"o 4\n", | |
"r 2\n", | |
"u 2\n" | |
] | |
} | |
], | |
"prompt_number": 79 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from itertools import permutations\n", | |
"\n", | |
"for permutation in permutations(('a', 'b', 'c')):\n", | |
" print permutation" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"('a', 'b', 'c')\n", | |
"('a', 'c', 'b')\n", | |
"('b', 'a', 'c')\n", | |
"('b', 'c', 'a')\n", | |
"('c', 'a', 'b')\n", | |
"('c', 'b', 'a')\n" | |
] | |
} | |
], | |
"prompt_number": 80 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**And many more:**\n", | |
"\n", | |
" - argparse\n", | |
" - unittest\n", | |
" - subprocess\n", | |
" - multiprocessing\n", | |
" - json\n", | |
" - xml\n", | |
" - etc. (http://docs.python.org/2/library)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### External Libraries\n", | |
"\n", | |
"The above concepts and many more allow the creation of useful and friendly 3rd-party libraries." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Primary Hosting**\n", | |
"\n", | |
"http://pypi.python.org\n", | |
"\n", | |
"Most packages can be installed with `pip`:\n", | |
"\n", | |
" pip install ProjectName" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
">>> import requests\n", | |
"\n", | |
">>> requests.get('https://api.github.com/user', auth=('user', 'pass'))\n", | |
">>> r.status_code\n", | |
"200\n", | |
"\n", | |
">>> r.headers['content-type']\n", | |
"'application/json; charset=utf8'\n", | |
"\n", | |
">>> r.encoding\n", | |
"'utf-8'\n", | |
"\n", | |
">>> r.text\n", | |
"u'{\"type\":\"User\"...'\n", | |
"\n", | |
">>> r.json()\n", | |
"{u'private_gists': 419, u'total_private_repos': 77}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "SyntaxError", | |
"evalue": "invalid syntax (<ipython-input-81-04b2ed99ac4a>, line 17)", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-81-04b2ed99ac4a>\"\u001b[0;36m, line \u001b[0;32m17\u001b[0m\n\u001b[0;31m {u'private_gists': 419, u'total_private_repos': 77, ...}\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" | |
] | |
} | |
], | |
"prompt_number": 81 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
">>> from suds.client import Client\n", | |
"\n", | |
">>> url = 'http://localhost:7080/webservices/WebServiceTestBean?wsdl'\n", | |
">>> client = Client(url)\n", | |
"\n", | |
">>> print client\n", | |
"\n", | |
"Service (WebServiceTestBeanService) tns=\"http://test.server.enterprise.rhq.org/\"\n", | |
" Prefixes (1):\n", | |
" ns0 = \"http://test.server.enterprise.rhq.org/\"\n", | |
" Ports (1):\n", | |
" (Soap)\n", | |
" Methods:\n", | |
" addPerson(Person person, )\n", | |
" echo(xs:string arg0, )\n", | |
" getList(xs:string str, xs:int length, )\n", | |
" getPercentBodyFat(xs:string name, xs:int height, xs:int weight)\n", | |
" getPersonByName(Name name, )\n", | |
" hello()\n", | |
" testExceptions()\n", | |
" testListArg(xs:string[] list, )\n", | |
" testVoid()\n", | |
" updatePerson(AnotherPerson person, name name, )\n", | |
" Types (23):\n", | |
" Person\n", | |
" Name\n", | |
" Phone\n", | |
" AnotherPerson" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
">>> from sh import git, ls, wc\n", | |
"\n", | |
">>> git(checkout=\"master\")\n", | |
"\n", | |
">>> print ls(\"-l\")\n", | |
"\n", | |
">>> longest_line = wc(__file__, \"-L\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
">>> import serial\n", | |
"\n", | |
">>> ser = serial.Serial('COM1')\n", | |
">>> ser.write(\"hello\")\n", | |
">>> ser.close()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
">>> from veracity import Repository, WorkingCopy, Item\n", | |
"\n", | |
">>> repo = Repository('veracity', remote='http://public.veracity-scm.com/repos/veracity')\n", | |
">>> print repo.name\n", | |
">>> print repo.users\n", | |
">>> print repo.branches\n", | |
">>> print repo.tags\n", | |
"\n", | |
">>> work = repo.checkout(\"~/v/veracity\")\n", | |
">>> work.delete()\n", | |
"\n", | |
">>> work = WorkingCopy(\"~/v/veracity\", repo='veracity')\n", | |
">>> work.update(branch='master')\n", | |
"\n", | |
">>> item = Item('docs/GettingStarted.txt', work=work)\n", | |
">>> item.lock()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# The End" | |
] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment