Skip to content

Instantly share code, notes, and snippets.

@gravesm
Created September 26, 2016 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gravesm/78e111986b7a61b836fc8578df3e23fd to your computer and use it in GitHub Desktop.
Save gravesm/78e111986b7a61b836fc8578df3e23fd to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b\n",
"a\n",
"z\n"
]
}
],
"source": [
"for c in 'baz':\n",
" print(c)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<generator object foo at 0x7f87aca43308>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def foo(s):\n",
" for c in s:\n",
" yield c\n",
"foo('baz')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"g = foo('baz')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b\n",
"a\n",
"z\n"
]
}
],
"source": [
"for c in g:\n",
" print(c)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def foo(s):\n",
" print('entering foo')\n",
" for c in s:\n",
" print('top of foo loop')\n",
" yield c\n",
"\n",
"g = foo('baz')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"entering foo\n",
"top of foo loop\n"
]
},
{
"data": {
"text/plain": [
"'b'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.__next__()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"top of foo loop\n"
]
},
{
"data": {
"text/plain": [
"'a'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.__next__()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"top of foo loop\n"
]
},
{
"data": {
"text/plain": [
"'z'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"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-9-5f315c5de15b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mStopIteration\u001b[0m: "
]
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<generator object bar at 0x7f87ac1b1728>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def foo(s):\n",
" print('entering foo')\n",
" for c in s:\n",
" print('top of foo loop')\n",
" yield c\n",
" print('bottom of foo loop')\n",
"\n",
"def bar(s):\n",
" print('entering bar')\n",
" for c in s:\n",
" print('top of bar loop')\n",
" yield c.upper()\n",
" print('bottom of bar loop')\n",
"\n",
"f = foo('baz')\n",
"g = bar(f)\n",
"g"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"entering bar\n",
"entering foo\n",
"top of foo loop\n",
"top of bar loop\n"
]
},
{
"data": {
"text/plain": [
"'B'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"bottom of bar loop\n",
"bottom of foo loop\n",
"top of foo loop\n",
"top of bar loop\n"
]
},
{
"data": {
"text/plain": [
"'A'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['B', 'Z']\n"
]
}
],
"source": [
"def foo(s):\n",
" for c in s:\n",
" yield c\n",
"\n",
"def bar(s):\n",
" for c in s:\n",
" yield c.upper()\n",
"\n",
"def filter_a(s):\n",
" for c in s:\n",
" if c != 'a':\n",
" yield c\n",
"\n",
"g = bar(filter_a(foo('baz')))\n",
"\n",
"s = list(g)\n",
"print(s)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<generator object <genexpr> at 0x7f87ac1b1938>"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"foo = (c for c in 'baz')\n",
"foo"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"['B', 'Z']"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"foo = (c for c in 'baz')\n",
"filter_a = (c for c in foo if c != 'a')\n",
"bar = (c.upper() for c in filter_a)\n",
"list(bar)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"entering foo\n",
"get ready for f\n",
"baz\n",
"that was exciting\n",
"leaving foo\n"
]
}
],
"source": [
"from contextlib import contextmanager\n",
"\n",
"@contextmanager\n",
"def foo(s):\n",
" print('entering foo')\n",
" yield s\n",
" print('leaving foo')\n",
"\n",
"with foo('baz') as f:\n",
" print('get ready for f')\n",
" print(f)\n",
" print('that was exciting')"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"here comes f\n",
"baz\n",
"there it went\n"
]
}
],
"source": [
"class CM(object):\n",
" def __init__(self, s):\n",
" self.s = s\n",
" def __enter__(self):\n",
" return self.s\n",
" def __exit__(self, exc, val, tb):\n",
" return True\n",
"\n",
"with CM('baz') as f:\n",
" print('here comes f')\n",
" print(f)\n",
" print('there it went')"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'baz'"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def foo(s):\n",
" yield s\n",
"\n",
"g = foo('baz')\n",
"result = next(g)\n",
"result"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"entering context\n",
"entering foo\n",
"here comes f\n",
"baz\n",
"there went f\n",
"leaving context\n"
]
}
],
"source": [
"class CM(object):\n",
" def __init__(self, gen):\n",
" self.gen = gen\n",
" def __enter__(self):\n",
" print('entering context')\n",
" return next(self.gen)\n",
" def __exit__(self, exc, val, tb):\n",
" print('leaving context')\n",
" return True\n",
"\n",
"def foo(s):\n",
" print('entering foo')\n",
" yield s\n",
" print('leaving foo')\n",
"\n",
"with CM(foo('baz')) as f:\n",
" print('here comes f')\n",
" print(f)\n",
" print('there went f')"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"<generator object foo at 0x7f87ac1b1780>"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def foo():\n",
" while True:\n",
" s = yield\n",
" print(s)\n",
"\n",
"g = foo()\n",
"g"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"baz\n"
]
}
],
"source": [
"g.send('baz')"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def foo():\n",
" print('entering foo')\n",
" while True:\n",
" print('top of foo loop')\n",
" s = yield\n",
" print(s)\n",
" print('bottom of foo loop')\n",
"\n",
" \n",
"g = foo()"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"entering foo\n",
"top of foo loop\n"
]
}
],
"source": [
"next(g)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"baz\n",
"bottom of foo loop\n",
"top of foo loop\n"
]
}
],
"source": [
"g.send('baz')"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"quux\n",
"bottom of foo loop\n",
"top of foo loop\n"
]
}
],
"source": [
"g.send('quux')"
]
}
],
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment