{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook was put together by [Jake Vanderplas](http://www.vanderplas.com) for UW's [Astro 599](http://www.astro.washington.edu/users/vanderplas/Astr599/) course. Source and license info is on [GitHub](https://github.com/jakevdp/2013_fall_ASTR599/)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Functions and Modules\n", "## But first, an important video...\n", "" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Functions and Modules\n", "An important part of coding (in Python and in other modern language) is organizing code in easily re-used chunks.\n", "\n", "Python can work within both a *procedural* and an *object-oriented* style.\n", "\n", "- **Procedural** programming is using functions\n", "\n", "- **Object-oriented** programming is using classes\n", "\n", "We'll come back to classes later, and look at functions now." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Functions\n", "\n", "Function definitions in Python look like this:\n", "\n", "``` python\n", "def function_name(arg1, arg2, ...,\n", " kw1=val1, kw2=val2, ...)\n", "```\n", "\n", "*(Note that line-breaks between the parentheses are ignored)*\n", "\n", "**argX** are *arguments*, and are required\n", "\n", "**kwX** are *keyword arguments*, and are optional" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Functions\n", "\n", "The function name can be anything, as long as it:\n", "\n", "- contains only numbers, letters, and underscores\n", "- does not start with a number\n", "- is not the name of a built-in keyword (like ``print``, or ``for``)\n", "\n", "Note for IDL users: there is no difference between *functions* and *procedures*. All Python functions return a value: if no return is specified, it returns ``None``\n", " " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Some Examples\n", "\n", "#### A function with two arguments:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def addnums(x, y):\n", " return x + y" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "result = addnums(1, 2)\n", "print result" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "print addnums(1, y=2)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "print addnums(\"A\", \"B\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "AB\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Note that the variable types are not declared (as we've discussed Python is a *dynamic* language)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Examples\n", "\n", "#### A function with a keyword" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def scale(x, factor=2.0):\n", " return x * factor" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "scale(4)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "8.0" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "scale(4, 10)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "40" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "scale(4, factor=10)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "40" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Arguments and Keyword arguments can either be specified by order or by name, but an unnamed argument cannot come after a named argument:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "scale(x=4, 10)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "SyntaxError", "evalue": "non-keyword arg after keyword arg (, line 1)", "output_type": "pyerr", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m scale(x=4, 10)\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m non-keyword arg after keyword arg\n" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Return Values\n", "\n", "Returned values can be anything, which allows a lot of flexibility:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def build_dict(x, y):\n", " return {'x':x, 'y':y}\n", "\n", "build_dict(4, 5)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 26, "text": [ "{'x': 4, 'y': 5}" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "def no_return_value():\n", " pass\n", "\n", "x = no_return_value()\n", "print x" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "None\n" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Keyword Arguments\n", "\n", "Keyword arguments can be a very handy way to grow new functionality without breaking old code.\n", "\n", "Imagine, for example, you had the ``build_dict`` function from above:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def build_dict(x, y):\n", " return {'x':x, 'y':y}\n", "\n", "build_dict(1, 2)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 28, "text": [ "{'x': 1, 'y': 2}" ] } ], "prompt_number": 28 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Now what if you want to change the names of the variables in the dictionary? Adding a keyword argument can allow this flexibility without breaking old code:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def build_dict(x, y, xname='x', yname='y'):\n", " return {xname:x, yname:y}\n", "\n", "build_dict(1, 2) # old call still works" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'y': 2, 'x': 1}\n" ] } ], "prompt_number": 29 }, { "cell_type": "code", "collapsed": false, "input": [ "build_dict(1, 2, xname='spam', yname='eggs')" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 30, "text": [ "{'eggs': 2, 'spam': 1}" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "This is admittedly a silly example, but it shows how keywords can be used to add flexibility without breaking old APIs." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Variable Scope\n", "\n", "Python functions have their own local variables list:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def modify_x(x):\n", " x += 5\n", " return x" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "x = 10\n", "y = modify_x(x)\n", "\n", "print x\n", "print y" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10\n", "15\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Modifying a variable in the function does not modify the variable globally... unless you use the ``global`` declaration" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def add_a(x):\n", " global a\n", " a += 1\n", " return x + a\n", "\n", "a = 10\n", "print add_a(5)\n", "print a" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "16\n", "11\n" ] } ], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Potential Gotcha: Simple vs Compound types\n", "\n", "Warning: Simple and Compound types are treated differently!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def add_one(x):\n", " x += 1\n", " \n", "x = 4\n", "add_one(x)\n", "print x" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "4\n" ] } ], "prompt_number": 18 }, { "cell_type": "code", "collapsed": false, "input": [ "def add_element(L):\n", " L.append(4)\n", " \n", "L = [1, 2]\n", "add_element(L)\n", "print L" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 4]\n" ] } ], "prompt_number": 19 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Simple types (int, long, float, complex, string) are passed **by value.**\n", "\n", "Compound types (list, dict, set, tuple, user-defined objects) are passed **by reference.**\n", "\n", "Question to think about: why would this be?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Catch-all: \\*args and \\*\\*kwargs" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def cheeseshop(kind, *args, **kwargs):\n", " print \"Do you have any\", kind, \"?\"\n", " print \"I'm sorry, we're all out of\", kind\n", " \n", " for arg in args:\n", " print arg\n", " \n", " print 40 * \"=\"\n", " \n", " for kw in kwargs:\n", " print kw, \":\", kwargs[kw]" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "cheeseshop(\"Limburger\", \"It's very runny, sir.\",\n", " \"It's really very, VERY runny, sir.\",\n", " shopkeeper=\"Michael Palin\",\n", " client=\"John Cleese\",\n", " sketch=\"Cheese Shop Sketch\")" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Do you have any Limburger ?\n", "I'm sorry, we're all out of Limburger\n", "It's very runny, sir.\n", "It's really very, VERY runny, sir.\n", "========================================\n", "shopkeeper : Michael Palin\n", "sketch : Cheese Shop Sketch\n", "client : John Cleese\n" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "*(example from [Python docs](http://docs.python.org/2/tutorial/controlflow.html#keyword-arguments))*" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Documentation (\"doc strings\")\n", "\n", "Documentation is not required, but your future self (and anybody else using your code) will thank you." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def power_of_difference(x, y, p=2.0):\n", " \"\"\"Return the power of the difference of x and y\n", " \n", " Parameters\n", " ----------\n", " x, y : float\n", " the values to be differenced\n", " p : float (optional)\n", " the exponent (default = 2.0)\n", " \n", " Returns\n", " -------\n", " result: float\n", " (x - y) ** p\n", " \"\"\"\n", " diff = x - y\n", " return diff ** p\n", "\n", "power_of_difference(10.0, 5.0)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 38, "text": [ "25" ] } ], "prompt_number": 38 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "*(Note that this example follows the [Numpy documentation standard](https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt))*\n", "\n", "With documentation specified this way, the IPython ``help`` command will be helpful!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "power_of_difference?" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 39 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Automatically building HTML documentation" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%file myfile.py\n", "\n", "def power_of_difference(x, y, p=2.0):\n", " \"\"\"Return the power of the difference of x and y\n", " \n", " Parameters\n", " ----------\n", " x, y : float\n", " the values to be differenced\n", " p : float (optional)\n", " the exponent (default = 2.0)\n", " \n", " Returns\n", " -------\n", " result: float\n", " (x - y) ** p\n", " \"\"\"\n", " diff = x - y\n", " return diff ** p" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Writing myfile.py\n" ] } ], "prompt_number": 41 }, { "cell_type": "code", "collapsed": false, "input": [ "# Pydoc is a command-line program bundled with Python\n", "!pydoc -w myfile" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "wrote myfile.html\r\n" ] } ], "prompt_number": 42 }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import HTML\n", "HTML(open('myfile.html').read())" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "html": [ "\n", "\n", "Python: module myfile\n", "\n", "\n", "\n", "\n", "
 
\n", " 
myfile
index
/Users/jakevdp/Opensource/2013_fall_ASTR599/notebooks/myfile.py
\n", "

\n", "

\n", "\n", "\n", "\n", " \n", "\n", "
 
\n", "Functions
       
power_of_difference(x, y, p=2.0)
Return the power of the difference of x and y
\n", " 
\n", "Parameters
\n", "----------
\n", "x, y : float
\n", "    the values to be differenced
\n", "p : float (optional)
\n", "    the exponent (default = 2.0)
\n", " 
\n", "Returns
\n", "-------
\n", "result: float
\n", "    (x - y) ** p
\n", "
\n", "" ], "metadata": {}, "output_type": "pyout", "prompt_number": 46, "text": [ "" ] } ], "prompt_number": 46 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Any remaining questions about functions?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Modules\n", "\n", "Modules are **organized units of code** which contain functions, classes, statements, and other definitions.\n", "\n", "Any file ending in ``.py`` is treated as a module (e.g. our file ``myfile.py`` above).\n", "\n", "Variables in modules have their own **scope**: using a name in one module will not affect variables of that name in another module." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%file mymodule.py\n", "# A simple demonstration module\n", "\n", "def add_numbers(x, y):\n", " \"\"\"add x and y\"\"\"\n", " return x + y\n", "\n", "def subtract_numbers(x, y):\n", " \"\"\"subtract y from x\"\"\"\n", " return x - y" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Writing mymodule.py\n" ] } ], "prompt_number": 49 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Modules are accessed using ``import module_name`` (with no ``.py``)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import mymodule" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 50 }, { "cell_type": "code", "collapsed": false, "input": [ "print '1 + 2 =', mymodule.add_numbers(1, 2)\n", "print '5 - 3 =', mymodule.subtract_numbers(5, 3)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1 + 2 = 3\n", "5 - 3 = 2\n" ] } ], "prompt_number": 52 }, { "cell_type": "code", "collapsed": false, "input": [ "print add_numbers(1, 2)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "ename": "NameError", "evalue": "name 'add_numbers' 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\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0madd_numbers\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[0m", "\u001b[0;31mNameError\u001b[0m: name 'add_numbers' is not defined" ] } ], "prompt_number": 53 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Several ways to import from modules" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "#### As a separate namespace:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import mymodule\n", "mymodule.add_numbers(1, 2)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 54, "text": [ "3" ] } ], "prompt_number": 54 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "#### Importing a single function or name:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from mymodule import add_numbers\n", "add_numbers(1, 2)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 55, "text": [ "3" ] } ], "prompt_number": 55 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "#### Renaming module contents" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from mymodule import add_numbers as silly_function\n", "silly_function(1, 2)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 57, "text": [ "3" ] } ], "prompt_number": 57 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "#### The Kitchen sink:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from mymodule import *\n", "subtract_numbers(5, 3)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 56, "text": [ "2" ] } ], "prompt_number": 56 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "This final method can be convenient, but should generally be avoided as it can cause name collisions and makes debugging difficult." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Module level code and documentation\n", "\n", "Your modules can have their own documentation, can define module-level variables, and can execute code when they load. For example:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%file mymodule2.py\n", "\"\"\"\n", "Example module with some variables and startup code\n", "\"\"\"\n", "# this code runs when the module is loaded\n", "print \"mymodule2 in the house!\"\n", "pi = 3.1415926\n", "favorite_food = \"spam, of course\"\n", "\n", "def multiply(a, b):\n", " return a * b" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Writing mymodule2.py\n" ] } ], "prompt_number": 58 }, { "cell_type": "code", "collapsed": false, "input": [ "import mymodule2" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "mymodule2 in the house!\n" ] } ], "prompt_number": 65 }, { "cell_type": "code", "collapsed": false, "input": [ "# import again and the initial code does not execute!\n", "import mymodule2" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 66 }, { "cell_type": "code", "collapsed": false, "input": [ "# access module-level documentation\n", "mymodule2?" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "prompt_number": 67 }, { "cell_type": "code", "collapsed": false, "input": [ "print mymodule2.multiply(2, 3)\n", "print mymodule2.pi" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "6\n" ] } ], "prompt_number": 61 }, { "cell_type": "code", "collapsed": false, "input": [ "# module variables can be modified\n", "print mymodule2.favorite_food" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "spam, of course\n" ] } ], "prompt_number": 68 }, { "cell_type": "code", "collapsed": false, "input": [ "mymodule2.favorite_food = \"eggs. No spam.\"\n", "print mymodule2.favorite_food" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "eggs. No spam.\n" ] } ], "prompt_number": 69 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## A Few Built-in Modules\n", "\n", "- ``sys``: exposes interactions with the system (environment, file I/O, etc.)\n", "- ``os``: exposes platform-specific operations (file statistics, directories, paths, etc.)\n", "- ``math``: exposes basic mathematical functions and constants" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "help(sys)" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Help on built-in module sys:\n", "\n", "NAME\n", " sys\n", "\n", "FILE\n", " (built-in)\n", "\n", "MODULE DOCS\n", " http://docs.python.org/library/sys\n", "\n", "DESCRIPTION\n", " This module provides access to some objects used or maintained by the\n", " interpreter and to functions that interact strongly with the interpreter.\n", " \n", " Dynamic objects:\n", " \n", " argv -- command line arguments; argv[0] is the script pathname if known\n", " path -- module search path; path[0] is the script directory, else ''\n", " modules -- dictionary of loaded modules\n", " \n", " displayhook -- called to show results in an interactive session\n", " excepthook -- called to handle any uncaught exception other than SystemExit\n", " To customize printing in an interactive session or to install a custom\n", " top-level exception handler, assign other functions to replace these.\n", " \n", " exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n", " Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n", " \n", " stdin -- standard input file object; used by raw_input() and input()\n", " stdout -- standard output file object; used by the print statement\n", " stderr -- standard error object; used for error messages\n", " By assigning other file objects (or objects that behave like files)\n", " to these, it is possible to redirect all of the interpreter's I/O.\n", " \n", " last_type -- type of last uncaught exception\n", " last_value -- value of last uncaught exception\n", " last_traceback -- traceback of last uncaught exception\n", " These three are only available in an interactive session after a\n", " traceback has been printed.\n", " \n", " exc_type -- type of exception currently being handled\n", " exc_value -- value of exception currently being handled\n", " exc_traceback -- traceback of exception currently being handled\n", " The function exc_info() should be used instead of these three,\n", " because it is thread-safe.\n", " \n", " Static objects:\n", " \n", " float_info -- a dict with information about the float inplementation.\n", " long_info -- a struct sequence with information about the long implementation.\n", " maxint -- the largest supported integer (the smallest is -maxint-1)\n", " maxsize -- the largest supported length of containers.\n", " maxunicode -- the largest supported character\n", " builtin_module_names -- tuple of module names built into this interpreter\n", " version -- the version of this interpreter as a string\n", " version_info -- version information as a named tuple\n", " hexversion -- version information encoded as a single integer\n", " copyright -- copyright notice pertaining to this interpreter\n", " platform -- platform identifier\n", " executable -- absolute path of the executable binary of the Python interpreter\n", " prefix -- prefix used to find the Python library\n", " exec_prefix -- prefix used to find the machine-specific Python library\n", " float_repr_style -- string indicating the style of repr() output for floats\n", " __stdin__ -- the original stdin; don't touch!\n", " __stdout__ -- the original stdout; don't touch!\n", " __stderr__ -- the original stderr; don't touch!\n", " __displayhook__ -- the original displayhook; don't touch!\n", " __excepthook__ -- the original excepthook; don't touch!\n", " \n", " Functions:\n", " \n", " displayhook() -- print an object to the screen, and save it in __builtin__._\n", " excepthook() -- print an exception and its traceback to sys.stderr\n", " exc_info() -- return thread-safe information about the current exception\n", " exc_clear() -- clear the exception state for the current thread\n", " exit() -- exit the interpreter by raising SystemExit\n", " getdlopenflags() -- returns flags to be used for dlopen() calls\n", " getprofile() -- get the global profiling function\n", " getrefcount() -- return the reference count for an object (plus one :-)\n", " getrecursionlimit() -- return the max recursion depth for the interpreter\n", " getsizeof() -- return the size of an object in bytes\n", " gettrace() -- get the global debug tracing function\n", " setcheckinterval() -- control how often the interpreter checks for events\n", " setdlopenflags() -- set the flags to be used for dlopen() calls\n", " setprofile() -- set the global profiling function\n", " setrecursionlimit() -- set the max recursion depth for the interpreter\n", " settrace() -- set the global debug tracing function\n", "\n", "FUNCTIONS\n", " __displayhook__ = displayhook(...)\n", " displayhook(object) -> None\n", " \n", " Print an object to sys.stdout and also save it in __builtin__._\n", " \n", " __excepthook__ = excepthook(...)\n", " excepthook(exctype, value, traceback) -> None\n", " \n", " Handle an exception by displaying it with a traceback on sys.stderr.\n", " \n", " call_tracing(...)\n", " call_tracing(func, args) -> object\n", " \n", " Call func(*args), while tracing is enabled. The tracing state is\n", " saved, and restored afterwards. This is intended to be called from\n", " a debugger from a checkpoint, to recursively debug some other code.\n", " \n", " callstats(...)\n", " callstats() -> tuple of integers\n", " \n", " Return a tuple of function call statistics, if CALL_PROFILE was defined\n", " when Python was built. Otherwise, return None.\n", " \n", " When enabled, this function returns detailed, implementation-specific\n", " details about the number of function calls executed. The return value is\n", " a 11-tuple where the entries in the tuple are counts of:\n", " 0. all function calls\n", " 1. calls to PyFunction_Type objects\n", " 2. PyFunction calls that do not create an argument tuple\n", " 3. PyFunction calls that do not create an argument tuple\n", " and bypass PyEval_EvalCodeEx()\n", " 4. PyMethod calls\n", " 5. PyMethod calls on bound methods\n", " 6. PyType calls\n", " 7. PyCFunction calls\n", " 8. generator calls\n", " 9. All other calls\n", " 10. Number of stack pops performed by call_function()\n", " \n", " exc_clear(...)\n", " exc_clear() -> None\n", " \n", " Clear global information on the current exception. Subsequent calls to\n", " exc_info() will return (None,None,None) until another exception is raised\n", " in the current thread or the execution stack returns to a frame where\n", " another exception is being handled.\n", " \n", " exc_info(...)\n", " exc_info() -> (type, value, traceback)\n", " \n", " Return information about the most recent exception caught by an except\n", " clause in the current stack frame or in an older stack frame.\n", " \n", " exit(...)\n", " exit([status])\n", " \n", " Exit the interpreter by raising SystemExit(status).\n", " If the status is omitted or None, it defaults to zero (i.e., success).\n", " If the status is numeric, it will be used as the system exit status.\n", " If it is another kind of object, it will be printed and the system\n", " exit status will be one (i.e., failure).\n", " \n", " getcheckinterval(...)\n", " getcheckinterval() -> current check interval; see setcheckinterval().\n", " \n", " getdefaultencoding(...)\n", " getdefaultencoding() -> string\n", " \n", " Return the current default string encoding used by the Unicode \n", " implementation.\n", " \n", " getdlopenflags(...)\n", " getdlopenflags() -> int\n", " \n", " Return the current value of the flags that are used for dlopen calls.\n", " The flag constants are defined in the ctypes and DLFCN modules.\n", " \n", " getfilesystemencoding(...)\n", " getfilesystemencoding() -> string\n", " \n", " Return the encoding used to convert Unicode filenames in\n", " operating system filenames.\n", " \n", " getprofile(...)\n", " getprofile()\n", " \n", " Return the profiling function set with sys.setprofile.\n", " See the profiler chapter in the library manual.\n", " \n", " getrecursionlimit(...)\n", " getrecursionlimit()\n", " \n", " Return the current value of the recursion limit, the maximum depth\n", " of the Python interpreter stack. This limit prevents infinite\n", " recursion from causing an overflow of the C stack and crashing Python.\n", " \n", " getrefcount(...)\n", " getrefcount(object) -> integer\n", " \n", " Return the reference count of object. The count returned is generally\n", " one higher than you might expect, because it includes the (temporary)\n", " reference as an argument to getrefcount().\n", " \n", " getsizeof(...)\n", " getsizeof(object, default) -> int\n", " \n", " Return the size of object in bytes.\n", " \n", " gettrace(...)\n", " gettrace()\n", " \n", " Return the global debug tracing function set with sys.settrace.\n", " See the debugger chapter in the library manual.\n", " \n", " setcheckinterval(...)\n", " setcheckinterval(n)\n", " \n", " Tell the Python interpreter to check for asynchronous events every\n", " n instructions. This also affects how often thread switches occur.\n", " \n", " setdlopenflags(...)\n", " setdlopenflags(n) -> None\n", " \n", " Set the flags used by the interpreter for dlopen calls, such as when the\n", " interpreter loads extension modules. Among other things, this will enable\n", " a lazy resolving of symbols when importing a module, if called as\n", " sys.setdlopenflags(0). To share symbols across extension modules, call as\n", " sys.setdlopenflags(ctypes.RTLD_GLOBAL). Symbolic names for the flag modules\n", " can be either found in the ctypes module, or in the DLFCN module. If DLFCN\n", " is not available, it can be generated from /usr/include/dlfcn.h using the\n", " h2py script.\n", " \n", " setprofile(...)\n", " setprofile(function)\n", " \n", " Set the profiling function. It will be called on each function call\n", " and return. See the profiler chapter in the library manual.\n", " \n", " setrecursionlimit(...)\n", " setrecursionlimit(n)\n", " \n", " Set the maximum depth of the Python interpreter stack to n. This\n", " limit prevents infinite recursion from causing an overflow of the C\n", " stack and crashing Python. The highest possible limit is platform-\n", " dependent.\n", " \n", " settrace(...)\n", " settrace(function)\n", " \n", " Set the global debug tracing function. It will be called on each\n", " function call. See the debugger chapter in the library manual.\n", "\n", "DATA\n", " __stderr__ = ', mode 'w'>\n", " __stdin__ = ', mode 'r'>\n", " __stdout__ = ', mode 'w'>\n", " api_version = 1013\n", " argv = ['-c', '-f', '/Users/jakevdp/.ipython/profile_default/security/...\n", " builtin_module_names = ('__builtin__', '__main__', '_ast', '_codecs', ...\n", " byteorder = 'little'\n", " copyright = 'Copyright (c) 2001-2013 Python Software Foundati...ematis...\n", " displayhook = is a built-in module\"...\n", " exec_prefix = '/Users/jakevdp/anaconda'\n", " executable = '/Users/jakevdp/anaconda/bin/python'\n", " flags = sys.flags(debug=0, py3k_warning=0, division_warn...unicode=0, ...\n", " float_info = sys.float_info(max=1.7976931348623157e+308, max_...epsilo...\n", " float_repr_style = 'short'\n", " hexversion = 34014704\n", " last_value = AttributeError(\"'module' object has no attribute 'mulitpl...\n", " long_info = sys.long_info(bits_per_digit=30, sizeof_digit=4)\n", " maxint = 9223372036854775807\n", " maxsize = 9223372036854775807\n", " maxunicode = 65535\n", " meta_path = []\n", " modules = {'ConfigParser': ]\n", " path_importer_cache = {'': None, '/Users/jakevdp/.ipython/extensions':...\n", " platform = 'darwin'\n", " prefix = '/Users/jakevdp/anaconda'\n", " ps1 = 'In : '\n", " ps2 = '...: '\n", " ps3 = 'Out: '\n", " py3kwarning = False\n", " stderr = \n", " stdin = ', mode 'r'>\n", " stdout = \n", " subversion = ('CPython', '', '')\n", " version = '2.7.5 |Anaconda 1.4.0 (x86_64)| (default, Jun 28...3, 22:20...\n", " version_info = sys.version_info(major=2, minor=7, micro=5, releaseleve...\n", " warnoptions = []\n", "\n", "\n" ] } ], "prompt_number": 72 }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "import os\n", "\n", "print \"You are using Python version\", sys.version\n", "print 40 * '-'\n", "print \"Current working directory is:\"\n", "print os.getcwd()\n", "print 40 * '-'\n", "print \"Files in the current directory:\"\n", "for f in os.listdir(os.getcwd()):\n", " print f" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "You are using Python version 2.7.5 |Anaconda 1.4.0 (x86_64)| (default, Jun 28 2013, 22:20:13) \n", "[GCC 4.0.1 (Apple Inc. build 5493)]\n", "----------------------------------------\n", "Current working directory is:\n", "/Users/jakevdp/Opensource/2013_fall_ASTR599/notebooks\n", "----------------------------------------\n", "Files in the current directory:\n", ".ipynb_checkpoints\n", "00_intro.ipynb\n", "01_basic_training.ipynb\n", "02_advanced_data_structures.ipynb\n", "03_IPython_intro.ipynb\n", "04_Functions_and_modules.ipynb\n", "data\n", "Healpy.ipynb\n", "images\n", "Markdown Cells.ipynb\n", "myfile.html\n", "myfile.py\n", "myfile.pyc\n", "mymodule.py\n", "mymodule.pyc\n", "mymodule2.py\n", "mymodule2.pyc\n", "number_game.py\n", "README.txt\n", "style.css\n" ] } ], "prompt_number": 74 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Built-in modules are listed at\n", "[http://docs.python.org/2/py-modindex.html](http://docs.python.org/2/py-modindex.html)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Python builtin modules are awesome...\n", "\n", "\n", "\n", "(source: http://xkcd.com/353/)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# try importing antigravity..." ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Making a script executable\n", "\n", "When a script or module is run directly from the command-line (i.e. not imported) a special variable called ``__name__`` is set to ``\"__main__\"``.\n", "\n", "So, in your module, if you want some part of the code to *only* run when the script is executed directly, then you can make it look like this:\n", "\n", "``` python\n", "# all module stuff\n", "\n", "# at the bottom, put this:\n", "if __name__ == '__main__':\n", " # do some things\n", " print \"I was called from the command-line!\"\n", "```\n", "\n", "Here's a longer example of this in action:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%file modfun.py\n", "\n", "\"\"\"\n", "Some functions written to demonstrate a bunch of concepts\n", "like modules, import and command-line programming\n", "\"\"\"\n", "import os\n", "import sys\n", "\n", "\n", "def getinfo(path=\".\",show_version=True):\n", " \"\"\"\n", " Purpose: make simple us of os and sys modules\n", " Input: path (default = \".\"), the directory you want to list\n", " \"\"\"\n", " if show_version:\n", " print \"-\" * 40\n", " print \"You are using Python version \",\n", " print sys.version\n", " print \"-\" * 40\n", " \n", " print \"Files in the directory \" + str(os.path.abspath(path)) + \":\"\n", " for f in os.listdir(path):\n", " print \" \" + f\n", " print \"*\" * 40\n", " \n", " \n", "if __name__ == \"__main__\":\n", " \"\"\"\n", " Executed only if run from the command line.\n", " call with\n", " modfun.py ...\n", " If no dirname is given then list the files in the current path\n", " \"\"\"\n", " if len(sys.argv) == 1:\n", " getinfo(\".\",show_version=True)\n", " else:\n", " for i,dir in enumerate(sys.argv[1:]):\n", " if os.path.isdir(dir):\n", " # if we have a directory then operate on it\n", " # only show the version info\n", " # if it's the first directory\n", " getinfo(dir,show_version=(i==0))\n", " else:\n", " print \"Directory: \" + str(dir) + \" does not exist.\"" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Writing modfun.py\n" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "# now execute from the command-line\n", "%run modfun.py" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "----------------------------------------\n", "You are using Python version 2.7.5 |Anaconda 1.4.0 (x86_64)| (default, Jun 28 2013, 22:20:13) \n", "[GCC 4.0.1 (Apple Inc. build 5493)]\n", "----------------------------------------\n", "Files in the directory /Users/jakevdp/Opensource/2013_fall_ASTR599/notebooks:\n", " .ipynb_checkpoints\n", " 00_intro.ipynb\n", " 01_basic_training.ipynb\n", " 02_advanced_data_structures.ipynb\n", " 03_IPython_intro.ipynb\n", " 04_Functions_and_modules.ipynb\n", " data\n", " Healpy.ipynb\n", " images\n", " Markdown Cells.ipynb\n", " modfun.py\n", " myfile.html\n", " myfile.py\n", " myfile.pyc\n", " mymodule.py\n", " mymodule.pyc\n", " mymodule2.py\n", " mymodule2.pyc\n", " number_game.py\n", " README.txt\n", " style.css\n", "****************************************\n" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Note some of the ``sys`` and ``os`` commands used in this script!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Breakout Session:\n", "## Exploring Built-in Modules\n", "\n", "This breakout will give you a chance to explore some of the builtin modules offered by Python. For this session, please use your text editor to create the files. You'll have to \n", "\n", "1. Create and edit a new file called ``age.py``. Though you can do this via the ``%%file`` magic used above, here you should use your text editor.\n", "\n", " - within ``age.py``, import the ``datetime`` module\n", " - use ``datetime.datetime()`` to create a variable representing your birthday\n", " - use ``datetime.datetime.now()`` to create a variable representing the present date\n", " - subtract the two (this forms a ``datetime.timedelta()`` object) and print that variable.\n", " \n", " - Use this object to answer these questions:\n", " \n", " 1. How many days have you been alive?\n", " \n", " 2. How many hours have you been alive?\n", " \n", " 3. What will be the date 1000 days from now?\n", " \n", "2. Create and edit a new file called ``age1.py``. When run from the command-line with one argument, ``age1.py`` should print out the date in that many days from now. If run with three arguments, print the time in days since that date.\n", "\n", "```\n", "[~]$ python age1.py 1000\n", "date in 1000 days 2016-06-06 14:46:09.548831\n", "\n", "[~]$ python age1.py 1981 6 12\n", "days since then: 11779\n", "```" ] } ], "metadata": {} } ] }