Created
May 1, 2017 19:23
-
-
Save arbennett/adb36d1de1034ecc98a0b7d882039481 to your computer and use it in GitHub Desktop.
This file contains 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Object Oriented Examples" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 77, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"class Landscape(object):\n", | |
" density = 5.0 \n", | |
" \n", | |
" def __init__(self, temperature, soil_moist):\n", | |
" self.temperature = temperature\n", | |
" self.soil_moist = soil_moist\n", | |
" \n", | |
" def absorb_rad(self, rad):\n", | |
" self.temperature += 0.25 * rad\n", | |
" \n", | |
" def absorb_precip(self, precip):\n", | |
" self.soil_moist += 0.1 * precip\n", | |
" \n", | |
" def print_summary(self):\n", | |
" print(\"Summary of \" + self.__class__.__name__)\n", | |
" print(\"--------------------------------------\")\n", | |
" print(\"Density: \", self.density)\n", | |
" print(\"Temperature: \", self.temperature)\n", | |
" print(\"Soil Moisture: \", self.soil_moist)\n", | |
" print()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 78, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Summary of Landscape\n", | |
"--------------------------------------\n", | |
"Density: 8.0\n", | |
"Temperature: 30.0\n", | |
"Soil Moisture: 5\n", | |
"\n", | |
"Summary of Landscape\n", | |
"--------------------------------------\n", | |
"Density: 5.0\n", | |
"Temperature: 10.0\n", | |
"Soil Moisture: 5\n", | |
"\n", | |
"\n", | |
"After changing class level definition:\n", | |
"Summary of Landscape\n", | |
"--------------------------------------\n", | |
"Density: 8.0\n", | |
"Temperature: 30.0\n", | |
"Soil Moisture: 5\n", | |
"\n", | |
"Summary of Landscape\n", | |
"--------------------------------------\n", | |
"Density: 4.0\n", | |
"Temperature: 10.0\n", | |
"Soil Moisture: 5\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"l1 = Landscape(5,5)\n", | |
"l1.absorb_rad(100)\n", | |
"l1.density = 8.0\n", | |
"\n", | |
"l2 = Landscape(5,5)\n", | |
"l2.absorb_rad(20)\n", | |
"\n", | |
"l1.print_summary()\n", | |
"l2.print_summary()\n", | |
"\n", | |
"Landscape.density = 4.0\n", | |
"print()\n", | |
"print(\"After changing class level definition:\")\n", | |
"l1.print_summary()\n", | |
"l2.print_summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"class Desert(Landscape):\n", | |
" def absorb_rad(self, rad):\n", | |
" self.temperature += 0.5 * rad\n", | |
" \n", | |
"class Forest(Landscape):\n", | |
" def absorb_precip(self, precip):\n", | |
" self.soil_moist += 0.25 * precip" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Summary of Desert\n", | |
"--------------------------------------\n", | |
"Density: 4.0\n", | |
"Temperature: 15.0\n", | |
"Soil Moisture: 5\n", | |
"\n", | |
"Summary of Forest\n", | |
"--------------------------------------\n", | |
"Density: 4.0\n", | |
"Temperature: 5\n", | |
"Soil Moisture: 7.5\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"d1 = Desert(5,5)\n", | |
"d1.absorb_rad(20)\n", | |
"\n", | |
"f1 = Forest(5,5)\n", | |
"f1.absorb_precip(10)\n", | |
"\n", | |
"d1.print_summary()\n", | |
"f1.print_summary()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Functional Examples" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 102, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0 1 2\n" | |
] | |
} | |
], | |
"source": [ | |
"def ho_fun(oldfun, arg):\n", | |
" return oldfun(arg)\n", | |
"\n", | |
"def increment(a):\n", | |
" return a+1\n", | |
"\n", | |
"start = 0\n", | |
"mid = ho_fun(increment, start)\n", | |
"end = ho_fun(increment, ho_fun(increment, start))\n", | |
"print(start, mid, end)\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 83, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 83, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"def add_numbers(a, b):\n", | |
" return a+b\n", | |
"\n", | |
"def add_five(a):\n", | |
" return add_numbers(a, 5)\n", | |
"\n", | |
"add_numbers(1, 5) == add_five(1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 103, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.000400543212890625\n", | |
"0.003988504409790039\n", | |
"0.039566755294799805\n" | |
] | |
} | |
], | |
"source": [ | |
" def timer(newfun):\n", | |
" import time\n", | |
" def wrapper(args):\n", | |
" t1 = time.time()\n", | |
" newfun(args)\n", | |
" t2 = time.time()\n", | |
" print(t2-t1)\n", | |
" return wrapper\n", | |
"\n", | |
"@timer\n", | |
"def random_srt(n):\n", | |
" import numpy.random\n", | |
" a = numpy.random.rand(n)\n", | |
" return sorted(a)\n", | |
"\n", | |
"random_srt(500)\n", | |
"random_srt(5000)\n", | |
"random_srt(50000)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 98, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def multi(dispatch_fn):\n", | |
" \"\"\"\n", | |
" Keeps a function `inner` that holds a dictionary\n", | |
" with function pointers.\n", | |
" \"\"\"\n", | |
" def inner(arg):\n", | |
" # Get a function out of the dictionary\n", | |
" # If the key doesn't exist, just return a \n", | |
" # function that always returns None\n", | |
" return inner.__multi__.get(\n", | |
" dispatch_fn(arg),\n", | |
" lambda arg: None\n", | |
" )(arg)\n", | |
" # Initialize the dictionary\n", | |
" inner.__multi__ = {}\n", | |
" return inner\n", | |
"\n", | |
"def method(dispatch_fn, key=None):\n", | |
" \"\"\"\n", | |
" Put new key, value pairs into the `multi` function's\n", | |
" inner function for retrieval later.\n", | |
" \"\"\"\n", | |
" def apply_decorator(fn):\n", | |
" # This is assigning to the __multi__ dictionary\n", | |
" dispatch_fn.__multi__[key] = fn\n", | |
" return dispatch_fn\n", | |
" return apply_decorator\n", | |
"\n", | |
"import xarray as xr\n", | |
"import pandas as pd\n", | |
"\n", | |
"@multi\n", | |
"def read(filename):\n", | |
" return filename.split('.')[-1]\n", | |
"\n", | |
"@method(read, 'nc')\n", | |
"def read(filename):\n", | |
" return xr.open_dataset(filename)\n", | |
"\n", | |
"@method(read, 'txt')\n", | |
"def read(filename):\n", | |
" return pd.read_table(filename, header=None, delim_whitespace=True)\n", | |
"\n", | |
"read_dict = {}\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 99, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Frozen(SortedKeysDict({'lat': 4, 'lon': 5}))" | |
] | |
}, | |
"execution_count": 99, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ncfile = read(\"/home/bzq/workspace/test/stehekin.nc\")\n", | |
"ncfile.dims" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 100, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>0</th>\n", | |
" <th>1</th>\n", | |
" <th>2</th>\n", | |
" <th>3</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>3.675</td>\n", | |
" <td>-4.20</td>\n", | |
" <td>-13.080000</td>\n", | |
" <td>4.51</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>3.675</td>\n", | |
" <td>-4.20</td>\n", | |
" <td>-13.080000</td>\n", | |
" <td>4.51</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>0.000</td>\n", | |
" <td>-4.72</td>\n", | |
" <td>-23.030001</td>\n", | |
" <td>2.63</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>0.000</td>\n", | |
" <td>-4.72</td>\n", | |
" <td>-23.030001</td>\n", | |
" <td>2.63</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>0.000</td>\n", | |
" <td>-17.33</td>\n", | |
" <td>-20.990000</td>\n", | |
" <td>0.64</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" 0 1 2 3\n", | |
"0 3.675 -4.20 -13.080000 4.51\n", | |
"1 3.675 -4.20 -13.080000 4.51\n", | |
"2 0.000 -4.72 -23.030001 2.63\n", | |
"3 0.000 -4.72 -23.030001 2.63\n", | |
"4 0.000 -17.33 -20.990000 0.64" | |
] | |
}, | |
"execution_count": 100, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"txtfile = read(\"/home/bzq/workspace/test/data.txt\")\n", | |
"txtfile.head()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 101, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"None\n" | |
] | |
} | |
], | |
"source": [ | |
"badfile = read(\"I don't have an entry for this.\")\n", | |
"print(badfile)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def read_nc(fn):\n", | |
" pass\n", | |
"read_dict = {\"netcdf\": read_nc}\n", | |
"\n", | |
"read_dict['netcdf'](fn)" | |
] | |
} | |
], | |
"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.1" | |
}, | |
"latex_envs": { | |
"LaTeX_envs_menu_present": true, | |
"bibliofile": "biblio.bib", | |
"cite_by": "apalike", | |
"current_citInitial": 1, | |
"eqLabelWithNumbers": true, | |
"eqNumInitial": 1, | |
"hotkeys": { | |
"equation": "Ctrl-E", | |
"itemize": "Ctrl-I" | |
}, | |
"labels_anchors": false, | |
"latex_user_defs": false, | |
"report_style_numbering": false, | |
"user_envs_cfg": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment