Created
August 3, 2019 08:33
-
-
Save brydavis/401cf2674f922ee4060147dd1e2ae0a5 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": [ | |
"## AGENDA\n", | |
"\n", | |
"\n", | |
"- lambda\n", | |
"- iterables\n", | |
"- iterators\n", | |
"- generators\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Keywords / Syntax\n", | |
"```py\n", | |
"map\n", | |
"lambda\n", | |
"iter\n", | |
"next\n", | |
"zip\n", | |
"\n", | |
"{}.keys()\n", | |
"{}.values()\n", | |
"{}.items()\n", | |
"\n", | |
"\n", | |
"__getitem__\n", | |
"\n", | |
"__iter__\n", | |
"__next__\n", | |
"\n", | |
"\n", | |
"yield\n", | |
"\n", | |
"\n", | |
"\n", | |
"\n", | |
"itertools\n", | |
"itertools.islice\n", | |
"itertools.chain\n", | |
"\n", | |
"\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Lambda\n", | |
"\n", | |
"Anonymous (e.g. No Name) Functions\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<map at 0x10becf4d0>" | |
] | |
}, | |
"execution_count": 39, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"it = map(lambda x: x+10, [9, 5, 3, 2, 10])\n", | |
"\n", | |
"it" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"19" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"next(it)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"15" | |
] | |
}, | |
"execution_count": 41, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"next(it)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"13" | |
] | |
}, | |
"execution_count": 42, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"next(it)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[12, 20]" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"[x for x in it]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"\n", | |
"<br>Assign lambda to a variable" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1.6" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"func = lambda x,y,z: (x-z)/y\n", | |
"\n", | |
"func(10,5,2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"33.333333333333336" | |
] | |
}, | |
"execution_count": 24, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(lambda a,b,c: (a**b)/c)(10,2,3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"13\n", | |
"89\n", | |
"99\n", | |
"13.0\n", | |
"even\n" | |
] | |
} | |
], | |
"source": [ | |
"def calc_curry(x, op=\"add\"):\n", | |
" return {\n", | |
" \"add\": lambda y: x + y, \n", | |
" \"sub\": lambda y: y - x, \n", | |
" \"mult\": lambda y: x * y, \n", | |
" \"div\": lambda y: y / x, \n", | |
" \"mod\": lambda y: y % x, \n", | |
" }[op]\n", | |
"\n", | |
"add3 = calc_curry(3)\n", | |
"print(add3(10))\n", | |
"\n", | |
"sub10 = calc_curry(10, \"sub\")\n", | |
"print(sub10(99))\n", | |
"\n", | |
"mult9 = calc_curry(9, \"mult\")\n", | |
"print(mult9(11))\n", | |
"\n", | |
"div4 = calc_curry(4, \"div\")\n", | |
"print(div4(52))\n", | |
"\n", | |
"mod2 = calc_curry(2, \"mod\")\n", | |
"if mod2(20) == 0:\n", | |
" print(\"even\") " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<br>You can even use it in a `class`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"hey\n" | |
] | |
} | |
], | |
"source": [ | |
"class Person():\n", | |
" def __init__(self, name, age, occupation):\n", | |
" self.name = name\n", | |
" self.age = age\n", | |
" self.occupation = occupation\n", | |
" \n", | |
" say_hello = lambda self: print(\"hey\")\n", | |
" \n", | |
"jane = Person(\"Jane\", 30, \"scientist\")\n", | |
"jane.say_hello()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Iterables" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Iterators" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"data = [\n", | |
" {\"id\":\"1\",\"name\":\"Tybalt\",\"lastname\":\"Heinsh\",\"address\":\"8 Lien Trail\",\"phone\":\"876935123\",\"email\":\"[email protected]\",\"status\":\"active\",\"credit_limit\":\"2420\"},\n", | |
" {\"id\":\"2\",\"name\":\"Gwennie\",\"lastname\":\"Tennock\",\"address\":\"4608 Steensland Lane\",\"phone\":\"134552899\",\"email\":\"[email protected]\",\"status\":\"active\",\"credit_limit\":\"2343\"},\n", | |
" {\"id\":\"3\",\"name\":\"Euell\",\"lastname\":\"Fidgeon\",\"address\":\"8 Lerdahl Park\",\"phone\":\"009363240\",\"email\":\"[email protected]\",\"status\":\"inactive\",\"credit_limit\":\"1077\"},\n", | |
" {\"id\":\"4\",\"name\":\"Cairistiona\",\"lastname\":\"Ginnell\",\"address\":\"3 Harbort Drive\",\"phone\":\"880717312\",\"email\":\"[email protected]\",\"status\":\"active\",\"credit_limit\":\"1484\"},\n", | |
" {\"id\":\"5\",\"name\":\"Mariel\",\"lastname\":\"Wolfarth\",\"address\":\"47 Loomis Hill\",\"phone\":\"069169873\",\"email\":\"[email protected]\",\"status\":\"inactive\",\"credit_limit\":\"113\"},\n", | |
" {\"id\":\"6\",\"name\":\"Mallorie\",\"lastname\":\"Sammut\",\"address\":\"3614 Fisk Avenue\",\"phone\":\"828591715\",\"email\":\"[email protected]\",\"status\":\"active\",\"credit_limit\":\"3255\"},\n", | |
" {\"id\":\"7\",\"name\":\"Seamus\",\"lastname\":\"Pittford\",\"address\":\"2136 Rigney Park\",\"phone\":\"807689452\",\"email\":\"[email protected]\",\"status\":\"active\",\"credit_limit\":\"3468\"},\n", | |
" {\"id\":\"8\",\"name\":\"Piotr\",\"lastname\":\"Gauson\",\"address\":\"4145 Kensington Park\",\"phone\":\"133398937\",\"email\":\"[email protected]\",\"status\":\"inactive\",\"credit_limit\":\"3625\"},\n", | |
" {\"id\":\"9\",\"name\":\"Teodora\",\"lastname\":\"Loxton\",\"address\":\"40829 Bartillon Crossing\",\"phone\":\"194346240\",\"email\":\"[email protected]\",\"status\":\"inactive\",\"credit_limit\":\"444\"},\n", | |
" {\"id\":\"10\",\"name\":\"Nanine\",\"lastname\":\"Eadmeades\",\"address\":\"8691 Calypso Circle\",\"phone\":\"367839372\",\"email\":\"[email protected]\",\"status\":\"inactive\",\"credit_limit\":\"2459\"},\n", | |
" {\"id\":\"11\",\"name\":\"Marieann\",\"lastname\":\"Calcutt\",\"address\":\"5678 Rutledge Point\",\"phone\":\"245977057\",\"email\":\"[email protected]\",\"status\":\"inactive\",\"credit_limit\":\"2962\"},\n", | |
" {\"id\":\"12\",\"name\":\"Wandis\",\"lastname\":\"Dewdney\",\"address\":\"095 Packers Park\",\"phone\":\"135126295\",\"email\":\"[email protected]\",\"status\":\"active\",\"credit_limit\":\"4742\"},\n", | |
" {\"id\":\"13\",\"name\":\"Tommy\",\"lastname\":\"Wannes\",\"address\":\"59889 Myrtle Avenue\",\"phone\":\"969027317\",\"email\":\"[email protected]\",\"status\":\"inactive\",\"credit_limit\":\"4067\"},\n", | |
" {\"id\":\"14\",\"name\":\"Letti\",\"lastname\":\"Barkworth\",\"address\":\"7 Sundown Place\",\"phone\":\"243310785\",\"email\":\"[email protected]\",\"status\":\"inactive\",\"credit_limit\":\"1697\"},\n", | |
" {\"id\":\"15\",\"name\":\"Danit\",\"lastname\":\"Fayerman\",\"address\":\"9107 Oakridge Point\",\"phone\":\"596550663\",\"email\":\"[email protected]\",\"status\":\"active\",\"credit_limit\":\"2382\"},\n", | |
" {\"id\":\"16\",\"name\":\"Susanna\",\"lastname\":\"Grigaut\",\"address\":\"87 Cascade Avenue\",\"phone\":\"839048392\",\"email\":\"[email protected]\",\"status\":\"inactive\",\"credit_limit\":\"3982\"},\n", | |
" {\"id\":\"17\",\"name\":\"Donal\",\"lastname\":\"Thraves\",\"address\":\"2048 Porter Place\",\"phone\":\"303563873\",\"email\":\"[email protected]\",\"status\":\"inactive\",\"credit_limit\":\"3597\"},\n", | |
" {\"id\":\"18\",\"name\":\"Erina\",\"lastname\":\"Whightman\",\"address\":\"0 Columbus Park\",\"phone\":\"380312722\",\"email\":\"[email protected]\",\"status\":\"active\",\"credit_limit\":\"2007\"},\n", | |
" {\"id\":\"19\",\"name\":\"Rogers\",\"lastname\":\"Dumbare\",\"address\":\"2 Shopko Hill\",\"phone\":\"510504125\",\"email\":\"[email protected]\",\"status\":\"active\",\"credit_limit\":\"371\"},\n", | |
" {\"id\":\"20\",\"name\":\"Kayla\",\"lastname\":\"Coyte\",\"address\":\"4134 Atwood Pass\",\"phone\":\"468104655\",\"email\":\"[email protected]\",\"status\":\"active\",\"credit_limit\":\"4400\"},\n", | |
"]\n", | |
"\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"['id', 'name', 'lastname', 'address', 'phone', 'email', 'status', 'credit_limit'] \n", | |
"\n", | |
"['1', 'Tybalt', 'Heinsh', '8 Lien Trail', '876935123', '[email protected]', 'active', '2420'] \n", | |
"\n", | |
"[('id', '1'), ('name', 'Tybalt'), ('lastname', 'Heinsh'), ('address', '8 Lien Trail'), ('phone', '876935123'), ('email', '[email protected]'), ('status', 'active'), ('credit_limit', '2420')] \n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"for row in data:\n", | |
" print(list(row.keys()), \"\\n\")\n", | |
" print(list(row.values()), \"\\n\")\n", | |
" print(list(row.items()), \"\\n\")\n", | |
" break" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<list_iterator at 0x10bef6c50>" | |
] | |
}, | |
"execution_count": 47, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"it = iter(data)\n", | |
"\n", | |
"it" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'id': '1',\n", | |
" 'name': 'Tybalt',\n", | |
" 'lastname': 'Heinsh',\n", | |
" 'address': '8 Lien Trail',\n", | |
" 'phone': '876935123',\n", | |
" 'email': '[email protected]',\n", | |
" 'status': 'active',\n", | |
" 'credit_limit': '2420'}" | |
] | |
}, | |
"execution_count": 48, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"next(it)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'id': '2',\n", | |
" 'name': 'Gwennie',\n", | |
" 'lastname': 'Tennock',\n", | |
" 'address': '4608 Steensland Lane',\n", | |
" 'phone': '134552899',\n", | |
" 'email': '[email protected]',\n", | |
" 'status': 'active',\n", | |
" 'credit_limit': '2343'}" | |
] | |
}, | |
"execution_count": 49, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"next(it)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'id': '3',\n", | |
" 'name': 'Euell',\n", | |
" 'lastname': 'Fidgeon',\n", | |
" 'address': '8 Lerdahl Park',\n", | |
" 'phone': '009363240',\n", | |
" 'email': '[email protected]',\n", | |
" 'status': 'inactive',\n", | |
" 'credit_limit': '1077'}" | |
] | |
}, | |
"execution_count": 50, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"next(it)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<br>Roll your own Iterator" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class T:\n", | |
" def __getitem__(self, position):\n", | |
" if position > 2 and position < 5:\n", | |
" return position\n", | |
" \n", | |
"it = iter(T())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"next(it)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"next(it)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 54, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"next(it)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3" | |
] | |
}, | |
"execution_count": 55, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"next(it)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4" | |
] | |
}, | |
"execution_count": 56, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"next(it)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"next(it)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<br>Using the `iter` keyword to make an Iterator " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"h\n", | |
"e\n", | |
"l\n", | |
"l\n", | |
"o\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"it = iter(\"hello\")\n", | |
"\n", | |
"while True:\n", | |
" try:\n", | |
" print(next(it))\n", | |
" except Exception as e:\n", | |
" print(e)\n", | |
" break" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"h\n", | |
"e\n", | |
"l\n", | |
"l\n", | |
"o\n" | |
] | |
} | |
], | |
"source": [ | |
"for c in \"hello\":\n", | |
" print(c)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<zip object at 0x1028e13c0>\n", | |
"\n", | |
"\n", | |
"[('John', 'Jenny'), ('Charles', 'Christy'), ('Mike', 'Monica'), ('Devon', 'Michelle')]\n" | |
] | |
} | |
], | |
"source": [ | |
"a = (\n", | |
" \"John\", \n", | |
" \"Charles\", \n", | |
" \"Mike\",\n", | |
" \"Devon\",\n", | |
")\n", | |
"\n", | |
"b = (\n", | |
" \"Jenny\", \n", | |
" \"Christy\", \n", | |
" \"Monica\", \n", | |
" \"Michelle\",\n", | |
")\n", | |
"\n", | |
"x = zip(a, b)\n", | |
"\n", | |
"print(x)\n", | |
"print(\"\\n\")\n", | |
"print(list(x))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"<zip object at 0x103f1f550>\n", | |
"\n", | |
"\n", | |
"[('John', 'Jenny', 'Ruby'), ('Charles', 'Christy', 'Lula')]\n" | |
] | |
} | |
], | |
"source": [ | |
"a = (\n", | |
" \"John\", \n", | |
" \"Charles\", \n", | |
" \"Mike\",\n", | |
" \"Devon\",\n", | |
")\n", | |
"\n", | |
"b = (\n", | |
" \"Jenny\", \n", | |
" \"Christy\", \n", | |
" \"Monica\", \n", | |
"# \"Michelle\",\n", | |
")\n", | |
"\n", | |
"c = (\n", | |
" \"Ruby\", \n", | |
" \"Lula\", \n", | |
"# \"Sidney\", \n", | |
"# \"Bea\",\n", | |
")\n", | |
"\n", | |
"x = zip(a, b, c)\n", | |
"\n", | |
"print(x)\n", | |
"print(\"\\n\")\n", | |
"print(list(x))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<br>Using the `itertools` module" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['A',\n", | |
" 'B',\n", | |
" 'C',\n", | |
" 'D',\n", | |
" 'E',\n", | |
" 'F',\n", | |
" 'G',\n", | |
" 'H',\n", | |
" 'I',\n", | |
" 'J',\n", | |
" 'K',\n", | |
" 'L',\n", | |
" 'M',\n", | |
" 'N',\n", | |
" 'O',\n", | |
" 'P',\n", | |
" 'Q',\n", | |
" 'R',\n", | |
" 'S',\n", | |
" 'T',\n", | |
" 'U',\n", | |
" 'V',\n", | |
" 'W',\n", | |
" 'X',\n", | |
" 'Y',\n", | |
" 'Z']" | |
] | |
}, | |
"execution_count": 60, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import itertools\n", | |
"\n", | |
"# {x:chr(x) for x in range(65,91)} \n", | |
"capital_letters = [chr(x) for x in range(65,91)]\n", | |
"\n", | |
"capital_letters" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T']" | |
] | |
}, | |
"execution_count": 61, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"[i for i in itertools.islice(capital_letters, 10, 20)]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 85, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'K'" | |
] | |
}, | |
"execution_count": 85, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"\n", | |
"m = ['K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T']\n", | |
"it = m.__iter__()\n", | |
"it.__next__()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 86, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'L'" | |
] | |
}, | |
"execution_count": 86, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"it.__next__()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 87, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'M'" | |
] | |
}, | |
"execution_count": 87, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"it.__next__()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 90, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Range:\n", | |
" def __init__(self, stop=5):\n", | |
" self.current = 0\n", | |
" self.stop = stop\n", | |
" def __iter__(self):\n", | |
" return self\n", | |
" def __next__(self):\n", | |
" if self.current < self.stop:\n", | |
" self.current += 1\n", | |
" return self.current\n", | |
" else:\n", | |
" raise StopIteration\n", | |
" \n", | |
"r = Range(10)\n", | |
"it = r.__iter__()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 94, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"4" | |
] | |
}, | |
"execution_count": 94, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"it.__next__()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 95, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"5" | |
] | |
}, | |
"execution_count": 95, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"next(it)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Generators\n", | |
"\n", | |
"Generator functions “yield” a value, rather than returning a value. It *does* ‘return’ a value, but rather than ending execution of the function, it preserves function state so that it can pick up where it left off. In other words, state is preserved between yields." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 97, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def y_range(start, stop, step=1):\n", | |
" i = start\n", | |
" while i < stop:\n", | |
" yield i\n", | |
" i += step" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 99, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]" | |
] | |
}, | |
"execution_count": 99, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"[y for y in y_range(12,25)]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 102, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"12" | |
] | |
}, | |
"execution_count": 102, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# never changes\n", | |
"next(y_range(12,25))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 103, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"12" | |
] | |
}, | |
"execution_count": 103, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# no it works\n", | |
"y_gen = y_range(12,25)\n", | |
"next(y_gen)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 104, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"13" | |
] | |
}, | |
"execution_count": 104, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"next(y_gen)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 105, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"14" | |
] | |
}, | |
"execution_count": 105, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"next(y_gen)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 106, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# https://uwpce-pythoncert.github.io/PythonCertDevel220/modules/lesson04/recap.html" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 107, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"5\n", | |
"6\n", | |
"7\n", | |
"8\n", | |
"9\n" | |
] | |
} | |
], | |
"source": [ | |
"from itertools import *\n", | |
"for i in islice(count(), 5, 10):\n", | |
" print(i)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<br>\n", | |
"\n", | |
"## Coroutines\n", | |
"Let's pass some data around" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def grep(pattern):\n", | |
" print(\"searching for\", pattern)\n", | |
" while True:\n", | |
" line = (yield)\n", | |
" if pattern in line:\n", | |
" print(line)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"searching for data\n" | |
] | |
} | |
], | |
"source": [ | |
"search = grep(\"data\")\n", | |
"next(search)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"search.send(\"I love ice cream\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"search.send(\"I love soda\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"I love data\n" | |
] | |
} | |
], | |
"source": [ | |
"search.send(\"I love data\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def A():\n", | |
" while True:\n", | |
" print((yield))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"a = A()\n", | |
"next(a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"hello\n" | |
] | |
} | |
], | |
"source": [ | |
"a.send(\"hello\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"hello\n" | |
] | |
} | |
], | |
"source": [ | |
"a.send(\"hello\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"a.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "StopIteration", | |
"evalue": "", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-19-0219dbcfda4d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"hello\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mStopIteration\u001b[0m: " | |
] | |
} | |
], | |
"source": [ | |
"a.send(\"hello\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[None, None, None, None]" | |
] | |
}, | |
"execution_count": 31, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import sqlite3\n", | |
"\n", | |
"query_create = \"\"\"\n", | |
" create table people ( \n", | |
" name varchar(50),\n", | |
" email varchar(50),\n", | |
" phone varchar(15) \n", | |
" )\"\"\"\n", | |
"\n", | |
"query_insert1 = \"\"\"\n", | |
" insert into people \n", | |
" values ('Bryan', '[email protected]', '3065551234')\n", | |
"\"\"\"\n", | |
"\n", | |
"query_insert2 = \"\"\"\n", | |
" insert into people \n", | |
" values ('Ruby', '[email protected]', '2025550076')\n", | |
"\"\"\"\n", | |
"\n", | |
"query_insert3 = \"\"\"\n", | |
" insert into people \n", | |
" values ('Lula', '[email protected]', '5255559827')\n", | |
"\"\"\"\n", | |
"\n", | |
"def connection(db):\n", | |
" while True:\n", | |
" with sqlite3.connect(db) as cn:\n", | |
" cn.execute((yield))\n", | |
" \n", | |
"conn = connection(\"demo.db\")\n", | |
"next(conn)\n", | |
"\n", | |
"[conn.send(q) for q in [\n", | |
" query_create, \n", | |
" query_insert1, \n", | |
" query_insert2, \n", | |
" query_insert3,\n", | |
"]]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"conn.send(\"select 1 as one\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"conn.close()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "StopIteration", | |
"evalue": "", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mStopIteration\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-34-a7dca50d061e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mconn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"select 1 as one\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mStopIteration\u001b[0m: " | |
] | |
} | |
], | |
"source": [ | |
"conn.send(\"select 1 as one\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"X = 10\n", | |
"\n", | |
"def update_X():\n", | |
" while True:\n", | |
" x = (yield)\n", | |
" global X\n", | |
" X = x\n", | |
" \n", | |
"ux = update_X()\n", | |
"next(ux) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ux.send(90)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"90" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"X" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"ux.send(2345)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2345" | |
] | |
}, | |
"execution_count": 42, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"X" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 69, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"hello b a\n", | |
"hello a\n" | |
] | |
} | |
], | |
"source": [ | |
"def A():\n", | |
" while True:\n", | |
" x = (yield)\n", | |
" print(x + \" a\")\n", | |
" \n", | |
" \n", | |
"def B():\n", | |
" a = A()\n", | |
" next(a)\n", | |
" while True:\n", | |
" y = (yield)\n", | |
" a.send(y + \" b\") \n", | |
"\n", | |
"# b = B()\n", | |
"# next(b)\n", | |
"# b.send(789)\n", | |
"\n", | |
"def C():\n", | |
" b = B()\n", | |
" next(b)\n", | |
" \n", | |
" a = A()\n", | |
" next(a)\n", | |
" \n", | |
" # c = C()\n", | |
" # next(c)\n", | |
" \n", | |
" while True:\n", | |
" z = (yield)\n", | |
" b.send(z)\n", | |
" a.send(z)\n", | |
" # c.send(z)\n", | |
"\n", | |
" \n", | |
"c = C()\n", | |
"next(c)\n", | |
"\n", | |
"c.send(\"hello\")\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 56, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# import time \n", | |
"# from random import randint\n", | |
"# current_value = 0\n", | |
"\n", | |
"# def A():\n", | |
"# while True:\n", | |
"# x = (yield)\n", | |
"# global current_value\n", | |
"# time.sleep(randint(0, 3))\n", | |
"# current_value = x * 1\n", | |
"\n", | |
"# def B():\n", | |
"# while True:\n", | |
"# x = (yield)\n", | |
"# global current_value\n", | |
"# time.sleep(randint(0, 3))\n", | |
"# current_value = x * 2\n", | |
"\n", | |
"# def C():\n", | |
"# while True:\n", | |
"# x = (yield)\n", | |
"# global current_value\n", | |
"# time.sleep(randint(0, 3))\n", | |
"# current_value = x * 3\n", | |
"\n", | |
" \n", | |
"# def D():\n", | |
"# b = B()\n", | |
"# next(b)\n", | |
" \n", | |
"# a = B()\n", | |
"# next(a)\n", | |
" \n", | |
"# c = C()\n", | |
"# next(c)\n", | |
" \n", | |
"# while True:\n", | |
"# z = (yield)\n", | |
"# a.send(z)\n", | |
"# b.send(z)\n", | |
"# c.send(z)\n", | |
"\n", | |
" \n", | |
"# d = D()\n", | |
"# next(d)\n", | |
"\n", | |
"# d.send(10)\n", | |
"# current_value" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 61, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.7.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment