{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "from IPython.display import Image\n", "from IPython.display import clear_output\n", "from IPython.display import FileLink, FileLinks" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Introduction to\n", "\n", "![title](img/python-logo-master-flat.png)\n", "\n", "### with Application to Bioinformatics\n", "\n", "#### - Day 4" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### TODAY\n", "\n", "- Loops and functions\n", "- Using somebody else's code (using modules)\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Review\n", "\n", "- Why does it matter what type a variable has?\n", "- How (and when) can you change the type of those? When does it not work?\n", "- You have worked with a number of data containers; lists, sets, dictionaries. What is the difference between them and when should you use which?\n", "- What is a function?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Review\n", "\n", "- Why does it matter what type a variable has?\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "\n", "> Values of different types stores different types of information." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "\n", "> Different types can be used with different operations, functions and methods." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "1+2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"1\"+\"2\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"1\"+2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"1\"*2" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Take extra care when comparing values**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "2<12" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"2\"<\"12\"" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Review\n", "\n", "- How can you change (convert) the type of a value? When does it not work?\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "float(\"1\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "int(\"1\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "str(1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "int(\"2.2\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Converting between strings and lists**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "list(\"hello\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "str(['h', 'e', 'l', 'l', 'o'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "''.join(['h', 'e', 'l', 'l', 'o'])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Review\n", "\n", "\n", "- You have worked with a number of data containers; lists, sets, dictionaries. What is the difference between them and when should you use which?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ " - lists: when order is important\n", " - dictionaries: to keep track of the relation between keys and values\n", " - sets: to check for membership. No order, no duplicates." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mylist = [\"comedy\", \"drama\", \"drama\", \"sci-fi\"]\n", "mydict = {\"genre\": \"drama\", \"title\": \"Toy Story\"}\n", "myset = set(mylist)\n", "print('All genres', myset)\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Review\n", "\n", "- What is a function?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "> A named piece of code that performs a specific task.\n", "\n", "> A relation between input data (parameters) and a result (output data)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Function structure\n", "\n", "\"Drawing\" " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- The `def` keyword" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Arguments" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Indentation!\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- `return`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Scope**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def myfunc(a, b):\n", " print(a)\n", " \n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "a = 5\n", "\n", "def myfunc(a):\n", " a += 2\n", " return a\n", " \n", "b = myfunc(8)\n", "\n", "a, b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "a = 3\n", "print('Globally, a is', a)\n", "\n", "def myfunc(a):\n", " print('In f, a is', a)\n", " a += 2\n", " print('Then a in f is', a)\n", " return a\n", " \n", "print('Calling f with argument 8')\n", "b = myfunc(8)\n", "print('Result of f(8) =', b)\n", "print('Finally the global a is', a)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The local `a` in `myfunc` *shadows* the global `a`." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The variables inside functions are *local*. To avoid confusion, use different variable names." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "global_a = 3\n", "\n", "def myfunc(value):\n", " value += 2\n", " return value\n", " \n", "result = myfunc(8)\n", "\n", "global_a, result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**No confusion!**\n", "\n", " \n", " \n", "`myfunc` has unique variable names." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### More on functions, loops..." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Controlling loops - `break`**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "for x in lines_in_a_big_file:\n", " if x.startswith('>'): # this is the only line I want!\n", " do_something(x)\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "...waste of time!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "for x in lines_in_a_big_file:\n", " if x.startswith('>'): # this is the only line I want!\n", " do_something(x)\n", " break # break the loop\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**
break
**\n", "
\n", "\"break\"\n", "
\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Controlling loops - `continue`**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "for x in lines_in_a_big_file:\n", " if x.startswith('>'): # this is a comment\n", " # just skip this! don't do anything\n", " do_something(x)\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "for x in lines_in_a_big_file:\n", " if x.startswith('>'): # this is a comment\n", " continue # go on to the next iteration\n", " do_something(x)\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "for x in lines_in_a_big_file:\n", " if not x.startswith('>'): # this is *not* a comment\n", " do_something(x)\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**
continue
**\n", "
\n", "\"break\"\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Another control statement: pass** - the placeholder" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def a_function():\n", " # I have not implemented this just yet" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def a_function():\n", " # I have not implemented this just yet\n", " pass\n", "\n", "a_function()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Returning nothing**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def a_function():\n", " pass\n", "\n", "def b_function():\n", " return" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "result_a = a_function()\n", "result_b = b_function()\n", "print('a:', result_a, 'b:', result_b)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Another keyword: `None`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Other keywords: `True`, `import`, `and`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "What is `None`? What type? Why?\n", "When is it used?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- What is it?\n", " - A keyword with a constant value (like `True`, `False`) \n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Why and when?\n", " - To signal \"emtpy values\"\n", " - Functions that don't return anything meaningful\n", " - Variables with no values yet" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### `None`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "- What type?\n", " - The `NoneType`\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(None)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Comparing `None`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "None == True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "None == False" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "value = None\n", "if value:\n", " print('value is something')\n", "else:\n", " print('no value!')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Comparing `None`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "None <= 0 " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "myvalue = None\n", "if myvalue and myvalue <= 0:\n", " print('The value is smaller than or equal to 0')\n", "else:\n", " print('The value is either None or greater to zero')" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Keyword arguments**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "open('../files/250.imdb', 'r', encoding='utf-8')\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "def prettyprinter(name, value, delim=\":\"):\n", " out = \"The \" + name + \" is \" + delim + \" \" + value + \".\"\n", " return out" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Programmer can set default values" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
\n", "\"keyword\"\n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "prettyprinter(\"title\", \"Movie\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "- User can ignore the arguments (default value is used)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "```py\n", "def prettyprinter(name, value, delim=\":\"):\n", " out = \"The \" + name + \" is \" + delim + \" \" + value + \".\"\n", " return out\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ " prettyprinter(\"genre\", \"Drama\", \"=\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "prettyprinter(\"genre\", \"Drama\", delim=\"=\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "def prettyprinter(name, value, delim=\":\", end=None):\n", " out = \"The \" + name + \" is \" + delim + \" \" + value\n", " if end:\n", " out += end\n", " return out\n", "\n", "my_str = prettyprinter(\"title\", \"Movie\")\n", "print(my_str)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "prettyprinter(\"genre\", \"Drama\", \"=\", \".\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "prettyprinter(\"genre\", \"Drama\", delim=\"=\", end=\"!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "prettyprinter(\"genre\", \"Drama\", end=\"!\", delim=\":::\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "prettyprinter(\"genre\", \"Drama\", end=\"!\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "prettyprinter(\"genre\", \"Drama\", delim=\"=\", \".\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "**Positional arguments comes first, keyword arguments after!**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "```py\n", "open(file, mode='r', buffering=-1, encoding=None, errors=None,\n", " newline=None, closefd=True, opener=None)\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Gives better overview" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "open('../files/250.imdb', 'r', encoding='utf-8')\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "open('../files/250.imdb', mode='r', encoding='utf-8')\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "open('../files/250.imdb', encoding='utf-8', mode='r')\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Keyword arguments\n", "\n", "- programmer: set default values\n", "- user: ignore arguments\n", "- better overview" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Exercise 1\n", "\n", "\n", "→ Notebook Day_4_Exercise_1 (~30 minutes) " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Modules\n", "\n", "- Use a module\n", "- Create a module" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### What is a module?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- a python file" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- a organizational unit" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- a library" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Remember?\n", "\n", "\n", "\n", "#### Why functions?\n", "- Cleaner code\n", "- Better defined tasks in code\n", "- Re-usability\n", "- Better structure" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Why modules?\n", "\n", "- Cleaner code\n", "- Better defined tasks in code\n", "- Re-usability\n", "- Better structure\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Collect all related functions in one file\n", "- Import a module to use its functions\n", "- Only need to understand what the functions do, not how" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Example: **sys**\n", "\n", "```py\n", "import sys\n", "\n", "sys.argv[1]\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Python standard modules\n", "\n", "Check out the [module index](https://docs.python.org/3.6/py-modindex.html)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "How to find the right module?\n", "\n", "How to understand it?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "How to find the right module?\n", "\n", "- look at the module index\n", "- search [PyPI](http://pypi.org)\n", "- ask your colleagues\n", "- search the web!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "How to understand it?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "import math\n", "\n", "help(math)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "dir(math)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "help(math.sqrt)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ " math.sqrt(3)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Different ways of importing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Standard:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "import math" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "math.sqrt(3)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Alias:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "import math as m" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "m.sqrt(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import math as c" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "counter = c.Counter()" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Get access to only one function:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "from math import sqrt, ceil" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "sqrt(3)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### When?\n", "\n", "- `import math`: standard\n", "- `import xml.etree.ElementTree as ET`: for long names\n", "- `from collections import defaultdict`: import only one function/class" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Exercise\n", "\n", "\n", "\n", "→ Notebook Day_4_Exercise_2 (~30 minutes) \n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Documentation and commenting your code\n", "\n", "\n", "Remember `help()`?\n", "\n", "Works because somebody else has documented their code!\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "def process_file(filename, chrom, pos):\n", " for line in open(filename):\n", " if not line.startswith('#'):\n", " columns = line.split('\\t')\n", " if col[0] == chrom and col[1] == pos:\n", " print(line)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "**?**" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def process_file(filename, chrom, pos):\n", " \"\"\"\n", " Reads a vcf file and prints the genotype of the samples\n", " at chromosome chrom and position pos.\n", " \"\"\"\n", " for line in open(filename):\n", " if not line.startswith('#'):\n", " columns = line.split('\\t')\n", " if col[0] == chrom and col[1] == pos:\n", " print(line)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "help(process_file)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Your code may have two types of users:\n", "\n", "- library users\n", "- maintainers (maybe yourself!)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Write documentation for both of them!\n", "\n", "- library users:` \"\"\" What does this function do? \"\"\"` (doc strings)\n", "- maintainers: ` # implementation details` (comments)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "def process_file(filename, chrom, pos):\n", " \"\"\"\n", " Reads a vcf file and prints the genotypes of corresponding to\n", " chromosome chrom and position pos.\n", " \"\"\"\n", " for line in open(filename):\n", " if not line.startswith('#'): # skip comments\n", " columns = line.split('\\t') # file is tab separated\n", " # Check if chrom and pos match\n", " if col[0] == chrom and col[1] == pos:\n", " # genotype starts at column index 9\n", " print(col[9:])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Read more:\n", "\n", "https://realpython.com/documenting-python-code/\n", "\n", "https://www.python.org/dev/peps/pep-0008/?#comments\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Your own code as a module\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- Put your code in a file\n", "- Document it\n", "- Write a main function?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Documentation:" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ " - At the beginning of the file\n", " \n", " `\"\"\" This module provides functions for...\"\"\"`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ " - For every function\n", " \n", "
def make_list(x):    \n",
    "        \"\"\" Returns an list of lenght x \"\"\"
\n", " " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Comments:" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ " - Wherever the code is hard to understand\n", " \n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "my_list[5] += other_list[3] # explain why you do this!```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Using your module\n", "\n", "- Save your code in `mymodule.py`\n", "\n", "- Run it: `python3 mymodule.py`\n", "\n", "- Import it: `>>> import mymodule`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### The main function" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "```py\n", "\n", "def main(input_str):\n", " # do something\n", " pass\n", " \n", "# at the end of the file\n", "if __name__ == \"__main__\":\n", " result = main(sys.argv[1]) # get input from command line\n", " print(\"The result is: \", result) # show result to user\n", "```\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- When the module is run a a script: read input, print output\n", "- When the module is imported: get access to `main()`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### The main function" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "**In file** `module.py`**:**\n", "\n", "\n", "--------------\n", "```py\n", "print(\"My name is \" + __name__)\n", "\n", "if __name__ == \"__main__\":\n", " print(\"I'm run as a main module\")\n", "```\n", "------------" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import module as m" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "But if we run the file `module.py` as a script:\n", "\n", "\"mainmodule\"\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "###### Summary\n", "\n", "\n", "\n", "→ Notebook Day_4_Exercise_3 (~30 minutes) \n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Using somebody else's code\n", "\n", "\n", "Written by last year's teacher, Frédéric Haziza (slightly modified).\n", "\n", "\n", "Lets you work with a database with information about real estate around Uppsala.\n", "\n", "Have a look at [the documentation](../files/db_module_documentation.html) and\n", "the [the code](../exercises/day4/db.py)!\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## A lot of new things!\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- classes\n", "\n", "```py\n", "class HomeEntry():\n", " def __init__(self, row):\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- methods\n", "\n", "```py\n", " return self.raw_query(q)\n", "```\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- javascript!\n", "\n", "```js\n", " function createPopup(feature) {\n", " feature.popup = new OpenLayers.Popup.FramedCloud(\"pop\",\n", " ...\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- ..." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "**You don't need to understand these things, as long as you can understand the documentation!**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### The database\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- A house:\n", " - a `HomeEntry`\n", " - id, type, location, adress, date, asked_price, price, rooms, area, rent, latitude, longitude\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- What you need\n", " - **latitude**, **longitude**, **price**, **area**\n", " - `home.get_price()`, `home.get_location()`, `home.get_area()`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Getting houses from the database" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "database = HomeDB('uppsala.sqlite')\n", "database.connect()```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "homes = db.select() # Get everything```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "homes = db.select('price > 1000000') # Select by some criteria```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "homes = db.select('id = \"2447\"')\n", " ```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### The magic - printing maps" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "`plot()` creates a html file, which you can open in a browser." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "```py\n", "plot(selection, # a list of homes to print\n", " output='selection.html', # the name of the output file\n", " special=None, # a very special house\n", " zoom=12, # the zoom levl\n", " latitude=59.83732598851705, # the center position (lat(\n", " longitude=17.64549846959149, # the center position (long)\n", " radius=5000) # circle radius\n", " ```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- `selection`\n", "- `output`\n", "- `special`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "#### Your program\n", "\n", "- get a collection of houses\n", "- sort and organize them\n", "- plot them" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "**Other useful functions**\n", "- `haversine()`\n", "- `sort_by_price()`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Exercise 4\n", "\n", "\n", "→ Notebook Day_4_Exercise_4 (~60 minutes) \n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Sum-up day 4" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- control statements: `break`, `continue`, `pass`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- `None`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- scope: global and local. Be careful with your variable names!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- keyword arguments: `open(filename, encoding=\"utf-8\")`\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Sum-up day 4" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "- importing: Python standard modules, your own modules...\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "- documentation: `help()`, `\"\"\"Doc string\"\"\"`, `# comment`" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "\n", "- main function: what is run when" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" }, "rise": { "height": 900, "width": "90%" } }, "nbformat": 4, "nbformat_minor": 2 }