{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SAISOFT PYT-PR: Session 10\n", "\n", "### What Have We Learned?" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "lessons = {\n", " \"1\": \"Python is part of a bigger ecosystem (example: Jupyter Notebooks).\",\n", " \"2\": \"Batteries Included refers to the well-stocked standard library.\",\n", " \"3\": \"Built-ins inside __builtins__ include the basic types such as...\",\n", " \"4\": \"__ribs__ == special names == magic methods (but not all are methods).\",\n", " \"5\": \"3rd Party Python is where a lot of the action is!\",\n", " \"6\": \"'Python fits your brain' means it gets out of your way once you learn it.\"\n", "}\n", "\n", "important_types = [{'Numeric': [\"int\", \"float\", \"Decimal\", \"Fraction\", \"complex\"],\n", " 'Collections': [{\"Sequences\": [\"list\", \"range\", \"tuple\"],\n", " \"Mappings\": ['dict', 'set']}],\n", " 'Descriptors': ['property']}, \n", " {'Other types': ['function', 'class', 'generator']}]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.: Python is part of a bigger ecosystem (example: Jupyter Notebooks).\n", "2.: Batteries Included refers to the well-stocked standard library.\n", "3.: Built-ins inside __builtins__ include the basic types such as...\n", "\n", "int\n", "float\n", "Decimal\n", "Fraction\n", "complex\n", "list\n", "range\n", "tuple\n", "dict\n", "set\n", "\n", "4.: __ribs__ == special names == magic methods (but not all are methods).\n", "5.: 3rd Party Python is where a lot of the action is!\n", "6.: 'Python fits your brain' means it gets out of your way once you learn it.\n" ] } ], "source": [ "for key, value in lessons.items(): # dict method to return all key:value pairs\n", " print(\"{}.: {}\".format(key, value), file=None) # this could be HTML to a file\n", " if key == \"3\":\n", " print()\n", " for the_type in important_types[0]['Numeric']:\n", " print(the_type)\n", " for the_type in important_types[0]['Collections'][0]['Sequences']:\n", " print(the_type) \n", " for the_type in important_types[0]['Collections'][0]['Mappings']:\n", " print(the_type)\n", " print()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Continue to \"doodle and daydream\" as you find the time. Think of ways to describe your day as a Python program. Remember the story of *The Car that Would Not Start*.\n", "\n", "Run this a few times to see the different possible workflows." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Oops, need to charge battery\n" ] } ], "source": [ "import random\n", "class BatteryDead(Exception):\n", " pass\n", "class IgnitionKeyBroken(Exception):\n", " pass\n", "\n", "class Car:\n", " \n", " def start(self):\n", " as_luck_would_have_it = random.randint(0,10)\n", " if as_luck_would_have_it == 10:\n", " raise BatteryDead\n", " elif as_luck_would_have_it == 0:\n", " raise IgnitionKeyBroken\n", " print(\"Car starts!\")\n", "\n", "try:\n", " # might not work\n", " my_car = Car()\n", " my_car.start()\n", "except BatteryDead:\n", " print(\"Oops, need to charge battery\")\n", "except IgnitionKeyBroken:\n", " print(\"Oops, your key just snapped\") " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We also learned about [decorator syntax](https://github.com/4dsolutions/Python5/blob/master/Abducted!.ipynb). Using a decorator, we're able to use a callable as an input to an object that provides a proxy output, likewise callable by the same name." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from functools import wraps\n", "\n", "def decorator(f):\n", " @wraps(f)\n", " def proxy(x):\n", " # proxy\n", " print(\"Look at me!\")\n", " return f(x)\n", " return proxy\n", "\n", "@decorator\n", "def Sqr(x):\n", " \"\"\"Square Dancer\"\"\"\n", " return x * x" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Look at me!\n" ] }, { "data": { "text/plain": [ "100" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Sqr(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "@wraps forwards the ```__doctstring__``` and ```__name__``` of the incoming f argument to the proxy being wrapped.\n", "\n", "### LAB:\n", "\n", "Try commenting out the line with @wraps on it and checking the ```__doctstring__``` and ```__name__``` of Sqr again.\n", "\n", " * Comment out @wraps\n", " * re-run the cell containing the two function definitions\n", " * re-run the cell below, and not changes\n", " * change it back" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function Sqr in module __main__:\n", "\n", "Sqr(x)\n", " Square Dancer\n", "\n" ] } ], "source": [ "help(Sqr)" ] } ], "metadata": { "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }