Skip to content

Instantly share code, notes, and snippets.

@ohenrik
Last active July 7, 2016 18:05
Show Gist options
  • Save ohenrik/45d9023f3aff44345d514e850e3be9f6 to your computer and use it in GitHub Desktop.
Save ohenrik/45d9023f3aff44345d514e850e3be9f6 to your computer and use it in GitHub Desktop.
Chaos course - Iterators
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Defining the iterator generator function"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def iteration_generator(seed, equation=None):\n",
" # First check that an equation is provided\n",
" if equation == None:\n",
" raise ValueError(\"Missing equation, please provide a function.\")\n",
" # Initiate result\n",
" result = seed\n",
" # Deliver the result when requested\n",
" while True:\n",
" yield result\n",
" # create the next result.\n",
" result = equation(result)\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"-----"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# This is the funciton provided to the iterator_generator\n",
"def double_it(x):\n",
" return x*2"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 4, 8, 16]\n",
"\n",
"Running the last line again would return the next 5 results\n",
"[32, 64, 128, 256, 512]\n"
]
}
],
"source": [
"# First create the generator object, passing in the seed (1) and the function (double_it)\n",
"g = iteration_generator(1, double_it)\n",
"\n",
"# Then create an array of the first 5 results\n",
"print([next(g) for _ in range(5)])\n",
"print(\"\")\n",
"print(\"Running the last line again would return the next 5 results\")\n",
"print([next(g) for _ in range(5)])"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 0, 0, 0, 0]\n"
]
}
],
"source": [
"g = iteration_generator(0, double_it)\n",
"print([next(g) for _ in range(5)])"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 4, 8, 16, 32]\n"
]
}
],
"source": [
"g = iteration_generator(2, double_it)\n",
"print([next(g) for _ in range(5)])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"------"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def square_it(x):\n",
" return x**2"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 0, 0, 0, 0]\n"
]
}
],
"source": [
"g = iteration_generator(0, square_it)\n",
"print([next(g) for _ in range(5)])"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.5, 0.25, 0.0625, 0.00390625, 1.52587890625e-05]\n"
]
}
],
"source": [
"g = iteration_generator(0.5, square_it)\n",
"print([next(g) for _ in range(5)])"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 1, 1, 1, 1]\n"
]
}
],
"source": [
"g = iteration_generator(1, square_it)\n",
"print([next(g) for _ in range(5)])"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 4, 16, 256, 65536]\n"
]
}
],
"source": [
"g = iteration_generator(2, square_it)\n",
"print([next(g) for _ in range(5)])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"------"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def func_1(x):\n",
" return 4*x - 12"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, -12, -60, -252, -1020]\n"
]
}
],
"source": [
"g = iteration_generator(0, func_1)\n",
"print([next(g) for _ in range(5)])"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5, 8, 20, 68, 260]\n"
]
}
],
"source": [
"g = iteration_generator(5, func_1)\n",
"print([next(g) for _ in range(5)])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def func_2(x):\n",
" return x/2 + 10"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[4, 12, 16, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19]"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g = iteration_generator(4, func_2)\n",
"series = [next(g) for _ in range(20)]\n",
"series"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" <div class=\"bk-root\">\n",
" <a href=\"http://bokeh.pydata.org\" target=\"_blank\" class=\"bk-logo bk-logo-small bk-logo-notebook\"></a>\n",
" <span id=\"0e831bf1-1e24-4d79-a997-079984011ab1\">Loading BokehJS ...</span>\n",
" </div>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"\n",
"(function(global) {\n",
" function now() {\n",
" return new Date();\n",
" }\n",
"\n",
" if (typeof (window._bokeh_onload_callbacks) === \"undefined\") {\n",
" window._bokeh_onload_callbacks = [];\n",
" }\n",
"\n",
" function run_callbacks() {\n",
" window._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n",
" delete window._bokeh_onload_callbacks\n",
" console.info(\"Bokeh: all callbacks have finished\");\n",
" }\n",
"\n",
" function load_libs(js_urls, callback) {\n",
" window._bokeh_onload_callbacks.push(callback);\n",
" if (window._bokeh_is_loading > 0) {\n",
" console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
" return null;\n",
" }\n",
" if (js_urls == null || js_urls.length === 0) {\n",
" run_callbacks();\n",
" return null;\n",
" }\n",
" console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
" window._bokeh_is_loading = js_urls.length;\n",
" for (var i = 0; i < js_urls.length; i++) {\n",
" var url = js_urls[i];\n",
" var s = document.createElement('script');\n",
" s.src = url;\n",
" s.async = false;\n",
" s.onreadystatechange = s.onload = function() {\n",
" window._bokeh_is_loading--;\n",
" if (window._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: all BokehJS libraries loaded\");\n",
" run_callbacks()\n",
" }\n",
" };\n",
" s.onerror = function() {\n",
" console.warn(\"failed to load library \" + url);\n",
" };\n",
" console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" }\n",
" };\n",
"\n",
" var js_urls = ['https://cdn.pydata.org/bokeh/release/bokeh-0.12.0.min.js', 'https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.0.min.js', 'https://cdn.pydata.org/bokeh/release/bokeh-compiler-0.12.0.min.js'];\n",
"\n",
" var inline_js = [\n",
" function(Bokeh) {\n",
" Bokeh.set_log_level(\"info\");\n",
" },\n",
" \n",
" function(Bokeh) {\n",
" Bokeh.$(\"#0e831bf1-1e24-4d79-a997-079984011ab1\").text(\"BokehJS successfully loaded\");\n",
" },\n",
" function(Bokeh) {\n",
" console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.0.min.css\");\n",
" Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.0.min.css\");\n",
" console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.0.min.css\");\n",
" Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.0.min.css\");\n",
" }\n",
" ];\n",
"\n",
" function run_inline_js() {\n",
" for (var i = 0; i < inline_js.length; i++) {\n",
" inline_js[i](window.Bokeh);\n",
" }\n",
" }\n",
"\n",
" if (window._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
" run_inline_js();\n",
" } else {\n",
" load_libs(js_urls, function() {\n",
" console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n",
" run_inline_js();\n",
" });\n",
" }\n",
"}(this));"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from bokeh.charts import TimeSeries, show, output_notebook\n",
"output_notebook()"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"plt = TimeSeries(series)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/var/pyenv/versions/2.7.11/lib/python2.7/site-packages/ipykernel/__main__.py:1: BokehDeprecationWarning: bokeh.charts.chart.show was deprecated in Bokeh 0.11; please use bokeh.io.show instead\n",
" if __name__ == '__main__':\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
" <div class=\"bk-root\">\n",
" <div class=\"plotdiv\" id=\"bec31baf-1a08-4f69-8a07-0220d0e76748\"></div>\n",
" </div>\n",
"<script type=\"text/javascript\">\n",
" \n",
" (function(global) {\n",
" function now() {\n",
" return new Date();\n",
" }\n",
" \n",
" if (typeof (window._bokeh_onload_callbacks) === \"undefined\") {\n",
" window._bokeh_onload_callbacks = [];\n",
" }\n",
" \n",
" function run_callbacks() {\n",
" window._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n",
" delete window._bokeh_onload_callbacks\n",
" console.info(\"Bokeh: all callbacks have finished\");\n",
" }\n",
" \n",
" function load_libs(js_urls, callback) {\n",
" window._bokeh_onload_callbacks.push(callback);\n",
" if (window._bokeh_is_loading > 0) {\n",
" console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n",
" return null;\n",
" }\n",
" if (js_urls == null || js_urls.length === 0) {\n",
" run_callbacks();\n",
" return null;\n",
" }\n",
" console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n",
" window._bokeh_is_loading = js_urls.length;\n",
" for (var i = 0; i < js_urls.length; i++) {\n",
" var url = js_urls[i];\n",
" var s = document.createElement('script');\n",
" s.src = url;\n",
" s.async = false;\n",
" s.onreadystatechange = s.onload = function() {\n",
" window._bokeh_is_loading--;\n",
" if (window._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: all BokehJS libraries loaded\");\n",
" run_callbacks()\n",
" }\n",
" };\n",
" s.onerror = function() {\n",
" console.warn(\"failed to load library \" + url);\n",
" };\n",
" console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n",
" document.getElementsByTagName(\"head\")[0].appendChild(s);\n",
" }\n",
" };var element = document.getElementById(\"bec31baf-1a08-4f69-8a07-0220d0e76748\");\n",
" if (element == null) {\n",
" console.log(\"Bokeh: ERROR: autoload.js configured with elementid 'bec31baf-1a08-4f69-8a07-0220d0e76748' but no matching script tag was found. \")\n",
" return false;\n",
" }\n",
" \n",
" var js_urls = [];\n",
" \n",
" var inline_js = [\n",
" function(Bokeh) {\n",
" Bokeh.$(function() {\n",
" var docs_json = {\"adff4544-1fad-4c2e-bd8e-0519b85c9576\":{\"roots\":{\"references\":[{\"attributes\":{\"plot\":null,\"text\":null},\"id\":\"0a494350-fb6b-4bda-b60b-52e9cc470278\",\"type\":\"Title\"},{\"attributes\":{\"axis_label\":\"value\",\"formatter\":{\"id\":\"e458f6c9-2b51-4ed9-9cff-b3d434766633\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"7c1444e8-d7ca-4dd2-b758-11a12388529f\",\"subtype\":\"Chart\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"fcb0f197-2aef-4122-ae62-2e0939b1dc54\",\"type\":\"BasicTicker\"}},\"id\":\"b5b15ade-6370-4b38-8545-00d2b454ab48\",\"type\":\"LinearAxis\"},{\"attributes\":{\"axis_label\":\"index\",\"formatter\":{\"id\":\"5083233e-ba17-4811-8676-1b75b4002ecd\",\"type\":\"BasicTickFormatter\"},\"plot\":{\"id\":\"7c1444e8-d7ca-4dd2-b758-11a12388529f\",\"subtype\":\"Chart\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"c8cc4801-a0a6-432b-bef1-1d725f2de463\",\"type\":\"BasicTicker\"}},\"id\":\"01780dfc-513a-41f7-a492-1db97f5513c0\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"5083233e-ba17-4811-8676-1b75b4002ecd\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"plot\":{\"id\":\"7c1444e8-d7ca-4dd2-b758-11a12388529f\",\"subtype\":\"Chart\",\"type\":\"Plot\"}},\"id\":\"13c61847-c096-4bbd-bb80-f8a96a3a6e06\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"fcb0f197-2aef-4122-ae62-2e0939b1dc54\",\"type\":\"BasicTicker\"},{\"attributes\":{\"active_drag\":\"auto\",\"active_scroll\":\"auto\",\"active_tap\":\"auto\",\"tools\":[{\"id\":\"8e1068ba-c8db-45d9-9bdb-dba1e5f9fd08\",\"type\":\"PanTool\"},{\"id\":\"13c61847-c096-4bbd-bb80-f8a96a3a6e06\",\"type\":\"WheelZoomTool\"},{\"id\":\"5e943d4d-f89c-48f4-8f86-ecf5a297548a\",\"type\":\"BoxZoomTool\"},{\"id\":\"63e625cc-9e0e-4bd5-b354-6f45d2df6651\",\"type\":\"SaveTool\"},{\"id\":\"ad13fd43-01b7-45c4-b60a-738e21d77098\",\"type\":\"ResetTool\"},{\"id\":\"dbfd94c7-e7b8-468b-9170-aebd588088df\",\"type\":\"HelpTool\"}]},\"id\":\"62257ada-9a9b-4c36-9e49-0e3747c32ba8\",\"type\":\"Toolbar\"},{\"attributes\":{},\"id\":\"0f6ea3ba-8588-4911-937b-7e067fe11a10\",\"type\":\"ToolEvents\"},{\"attributes\":{},\"id\":\"e458f6c9-2b51-4ed9-9cff-b3d434766633\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"dimension\":1,\"plot\":{\"id\":\"7c1444e8-d7ca-4dd2-b758-11a12388529f\",\"subtype\":\"Chart\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"fcb0f197-2aef-4122-ae62-2e0939b1dc54\",\"type\":\"BasicTicker\"}},\"id\":\"dd6e4463-59fd-4228-bc2b-f91d77135ed1\",\"type\":\"Grid\"},{\"attributes\":{\"callback\":null,\"column_names\":[\"x_values\",\"y_values\"],\"data\":{\"chart_index\":[{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"},{\"series\":\"a\"}],\"series\":[\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\",\"a\"],\"x_values\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19],\"y_values\":[4,12,16,18,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19]}},\"id\":\"9a2ba2a1-f11a-4444-bfde-ad69bbb77eed\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"plot\":{\"id\":\"7c1444e8-d7ca-4dd2-b758-11a12388529f\",\"subtype\":\"Chart\",\"type\":\"Plot\"}},\"id\":\"dbfd94c7-e7b8-468b-9170-aebd588088df\",\"type\":\"HelpTool\"},{\"attributes\":{\"overlay\":{\"id\":\"73f2965a-f0cd-4d30-8074-e96d99dc9e5d\",\"type\":\"BoxAnnotation\"},\"plot\":{\"id\":\"7c1444e8-d7ca-4dd2-b758-11a12388529f\",\"subtype\":\"Chart\",\"type\":\"Plot\"}},\"id\":\"5e943d4d-f89c-48f4-8f86-ecf5a297548a\",\"type\":\"BoxZoomTool\"},{\"attributes\":{\"below\":[{\"id\":\"01780dfc-513a-41f7-a492-1db97f5513c0\",\"type\":\"LinearAxis\"}],\"left\":[{\"id\":\"b5b15ade-6370-4b38-8545-00d2b454ab48\",\"type\":\"LinearAxis\"}],\"renderers\":[{\"id\":\"73f2965a-f0cd-4d30-8074-e96d99dc9e5d\",\"type\":\"BoxAnnotation\"},{\"id\":\"a3ac17be-e5a6-4cd0-a6c7-02d0d02eb577\",\"type\":\"GlyphRenderer\"},{\"id\":\"ca43afc1-f490-46b9-9e4f-511ca08242f3\",\"type\":\"Legend\"},{\"id\":\"01780dfc-513a-41f7-a492-1db97f5513c0\",\"type\":\"LinearAxis\"},{\"id\":\"b5b15ade-6370-4b38-8545-00d2b454ab48\",\"type\":\"LinearAxis\"},{\"id\":\"57a5644e-3f5e-4546-86dd-071a7eb83f60\",\"type\":\"Grid\"},{\"id\":\"dd6e4463-59fd-4228-bc2b-f91d77135ed1\",\"type\":\"Grid\"}],\"title\":{\"id\":\"0a494350-fb6b-4bda-b60b-52e9cc470278\",\"type\":\"Title\"},\"tool_events\":{\"id\":\"0f6ea3ba-8588-4911-937b-7e067fe11a10\",\"type\":\"ToolEvents\"},\"toolbar\":{\"id\":\"62257ada-9a9b-4c36-9e49-0e3747c32ba8\",\"type\":\"Toolbar\"},\"x_mapper_type\":\"auto\",\"x_range\":{\"id\":\"5b563c79-45cf-4e85-a39f-c62cc5330fff\",\"type\":\"Range1d\"},\"y_mapper_type\":\"auto\",\"y_range\":{\"id\":\"b9df0dfc-80c9-4cf4-808d-ea4952b95b46\",\"type\":\"Range1d\"}},\"id\":\"7c1444e8-d7ca-4dd2-b758-11a12388529f\",\"subtype\":\"Chart\",\"type\":\"Plot\"},{\"attributes\":{\"callback\":null,\"end\":20.5,\"start\":2.5},\"id\":\"b9df0dfc-80c9-4cf4-808d-ea4952b95b46\",\"type\":\"Range1d\"},{\"attributes\":{\"line_color\":{\"value\":\"#f22c40\"},\"line_width\":{\"value\":2},\"x\":{\"field\":\"x_values\"},\"y\":{\"field\":\"y_values\"}},\"id\":\"b01ba61b-cc16-4b1b-b6e5-f52d8e63e8be\",\"type\":\"Line\"},{\"attributes\":{\"plot\":{\"id\":\"7c1444e8-d7ca-4dd2-b758-11a12388529f\",\"subtype\":\"Chart\",\"type\":\"Plot\"}},\"id\":\"8e1068ba-c8db-45d9-9bdb-dba1e5f9fd08\",\"type\":\"PanTool\"},{\"attributes\":{\"bottom_units\":\"screen\",\"fill_alpha\":{\"value\":0.5},\"fill_color\":{\"value\":\"lightgrey\"},\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":{\"value\":1.0},\"line_color\":{\"value\":\"black\"},\"line_dash\":[4,4],\"line_width\":{\"value\":2},\"plot\":null,\"render_mode\":\"css\",\"right_units\":\"screen\",\"top_units\":\"screen\"},\"id\":\"73f2965a-f0cd-4d30-8074-e96d99dc9e5d\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"data_source\":{\"id\":\"9a2ba2a1-f11a-4444-bfde-ad69bbb77eed\",\"type\":\"ColumnDataSource\"},\"glyph\":{\"id\":\"b01ba61b-cc16-4b1b-b6e5-f52d8e63e8be\",\"type\":\"Line\"},\"hover_glyph\":null,\"nonselection_glyph\":null,\"selection_glyph\":null},\"id\":\"a3ac17be-e5a6-4cd0-a6c7-02d0d02eb577\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"plot\":{\"id\":\"7c1444e8-d7ca-4dd2-b758-11a12388529f\",\"subtype\":\"Chart\",\"type\":\"Plot\"}},\"id\":\"ad13fd43-01b7-45c4-b60a-738e21d77098\",\"type\":\"ResetTool\"},{\"attributes\":{\"plot\":{\"id\":\"7c1444e8-d7ca-4dd2-b758-11a12388529f\",\"subtype\":\"Chart\",\"type\":\"Plot\"}},\"id\":\"63e625cc-9e0e-4bd5-b354-6f45d2df6651\",\"type\":\"SaveTool\"},{\"attributes\":{\"plot\":{\"id\":\"7c1444e8-d7ca-4dd2-b758-11a12388529f\",\"subtype\":\"Chart\",\"type\":\"Plot\"},\"ticker\":{\"id\":\"c8cc4801-a0a6-432b-bef1-1d725f2de463\",\"type\":\"BasicTicker\"}},\"id\":\"57a5644e-3f5e-4546-86dd-071a7eb83f60\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"c8cc4801-a0a6-432b-bef1-1d725f2de463\",\"type\":\"BasicTicker\"},{\"attributes\":{\"legends\":[[\"a\",[{\"id\":\"a3ac17be-e5a6-4cd0-a6c7-02d0d02eb577\",\"type\":\"GlyphRenderer\"}]]],\"location\":\"top_left\",\"plot\":{\"id\":\"7c1444e8-d7ca-4dd2-b758-11a12388529f\",\"subtype\":\"Chart\",\"type\":\"Plot\"}},\"id\":\"ca43afc1-f490-46b9-9e4f-511ca08242f3\",\"type\":\"Legend\"},{\"attributes\":{\"callback\":null,\"end\":20.9,\"start\":-1.9000000000000001},\"id\":\"5b563c79-45cf-4e85-a39f-c62cc5330fff\",\"type\":\"Range1d\"}],\"root_ids\":[\"7c1444e8-d7ca-4dd2-b758-11a12388529f\"]},\"title\":\"Bokeh Application\",\"version\":\"0.12.0\"}};\n",
" var render_items = [{\"docid\":\"adff4544-1fad-4c2e-bd8e-0519b85c9576\",\"elementid\":\"bec31baf-1a08-4f69-8a07-0220d0e76748\",\"modelid\":\"7c1444e8-d7ca-4dd2-b758-11a12388529f\",\"notebook_comms_target\":\"2d981577-cc7a-4ad1-92a5-c6ac6ecb47a7\"}];\n",
" \n",
" Bokeh.embed.embed_items(docs_json, render_items);\n",
" });\n",
" },\n",
" function(Bokeh) {\n",
" }\n",
" ];\n",
" \n",
" function run_inline_js() {\n",
" for (var i = 0; i < inline_js.length; i++) {\n",
" inline_js[i](window.Bokeh);\n",
" }\n",
" }\n",
" \n",
" if (window._bokeh_is_loading === 0) {\n",
" console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n",
" run_inline_js();\n",
" } else {\n",
" load_libs(js_urls, function() {\n",
" console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n",
" run_inline_js();\n",
" });\n",
" }\n",
" }(this));\n",
"</script>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
},
"widgets": {
"state": {},
"version": "1.1.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
# coding: utf-8
# ## Defining the iterator generator function
# In[63]:
def iteration_generator(seed, equation=None):
# First check that an equation is provided
if equation == None:
raise ValueError("Missing equation, please provide a function.")
# Initiate result
result = seed
# Deliver the result when requested
while True:
yield result
# create the next result.
result = equation(result)
# -----
# In[64]:
# This is the funciton provided to the iterator_generator
def double_it(x):
return x*2
# In[65]:
# First create the generator object, passing in the seed (1) and the function (double_it)
g = iteration_generator(1, double_it)
# Then create an array of the first 5 results
print([next(g) for _ in range(5)])
print("")
print("Running the last line again would return the next 5 results")
print([next(g) for _ in range(5)])
# In[66]:
g = iteration_generator(0, double_it)
print([next(g) for _ in range(5)])
# In[67]:
g = iteration_generator(2, double_it)
print([next(g) for _ in range(5)])
# ------
# In[68]:
def square_it(x):
return x**2
# In[69]:
g = iteration_generator(0, square_it)
print([next(g) for _ in range(5)])
# In[70]:
g = iteration_generator(0.5, square_it)
print([next(g) for _ in range(5)])
# In[71]:
g = iteration_generator(1, square_it)
print([next(g) for _ in range(5)])
# In[72]:
g = iteration_generator(2, square_it)
print([next(g) for _ in range(5)])
# ------
# In[73]:
def func_1(x):
return 4*x - 12
# In[74]:
g = iteration_generator(0, func_1)
print([next(g) for _ in range(5)])
# In[75]:
g = iteration_generator(5, func_1)
print([next(g) for _ in range(5)])
# ----
# In[76]:
def func_2(x):
return x/2 + 10
# In[77]:
g = iteration_generator(4, func_2)
series = [next(g) for _ in range(20)]
series
# In[78]:
from bokeh.charts import TimeSeries, show, output_notebook
output_notebook()
# In[45]:
plt = TimeSeries(series)
# In[46]:
plt.show()
# In[ ]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment