Skip to content

Commit 2490e4f

Browse files
author
Shambhu Shrestha
committed
contextlib notebook
1 parent fe24b54 commit 2490e4f

File tree

2 files changed

+240
-0
lines changed

2 files changed

+240
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 4,
6+
"metadata": {
7+
"collapsed": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"from contextlib import contextmanager, closing\n",
12+
"from pprint import pprint as pp\n",
13+
"\n",
14+
"class A:\n",
15+
" def __init__(self):\n",
16+
" self.actions = []\n",
17+
" \n",
18+
" def open(self):\n",
19+
" self.actions.append('open')\n",
20+
" \n",
21+
" def close(self):\n",
22+
" self.actions.append('close')\n",
23+
" \n",
24+
" def do(self, action):\n",
25+
" self.actions.append(action)\n",
26+
" \n",
27+
" \n",
28+
"@contextmanager\n",
29+
"def getA():\n",
30+
" a = A()\n",
31+
" yield a\n",
32+
" pp(a.actions)\n",
33+
" \n",
34+
"\n"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 8,
40+
"metadata": {
41+
"collapsed": false
42+
},
43+
"outputs": [
44+
{
45+
"name": "stdout",
46+
"output_type": "stream",
47+
"text": [
48+
"['open', 'open', 'work', 'close']\n"
49+
]
50+
}
51+
],
52+
"source": [
53+
"with getA() as a:\n",
54+
" with closing(a):\n",
55+
" a.open()\n",
56+
" a.open()\n",
57+
" a.do('work')"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 11,
63+
"metadata": {
64+
"collapsed": false
65+
},
66+
"outputs": [
67+
{
68+
"data": {
69+
"text/plain": [
70+
"'f:[hello world\\n]'"
71+
]
72+
},
73+
"execution_count": 11,
74+
"metadata": {},
75+
"output_type": "execute_result"
76+
}
77+
],
78+
"source": [
79+
"import io\n",
80+
"from contextlib import redirect_stdout\n",
81+
"\n",
82+
"f = io.StringIO()\n",
83+
"with redirect_stdout(f):\n",
84+
" print('hello world')\n",
85+
"\n",
86+
"'f:[{}]'.format(f.getvalue())"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": null,
92+
"metadata": {
93+
"collapsed": true
94+
},
95+
"outputs": [],
96+
"source": []
97+
}
98+
],
99+
"metadata": {
100+
"kernelspec": {
101+
"display_name": "Python 3",
102+
"language": "python",
103+
"name": "python3"
104+
},
105+
"language_info": {
106+
"codemirror_mode": {
107+
"name": "ipython",
108+
"version": 3
109+
},
110+
"file_extension": ".py",
111+
"mimetype": "text/x-python",
112+
"name": "python",
113+
"nbconvert_exporter": "python",
114+
"pygments_lexer": "ipython3",
115+
"version": "3.6.0"
116+
}
117+
},
118+
"nbformat": 4,
119+
"nbformat_minor": 2
120+
}

python/notebooks/Contextlib.ipynb

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 4,
6+
"metadata": {
7+
"collapsed": true
8+
},
9+
"outputs": [],
10+
"source": [
11+
"from contextlib import contextmanager, closing\n",
12+
"from pprint import pprint as pp\n",
13+
"\n",
14+
"class A:\n",
15+
" def __init__(self):\n",
16+
" self.actions = []\n",
17+
" \n",
18+
" def open(self):\n",
19+
" self.actions.append('open')\n",
20+
" \n",
21+
" def close(self):\n",
22+
" self.actions.append('close')\n",
23+
" \n",
24+
" def do(self, action):\n",
25+
" self.actions.append(action)\n",
26+
" \n",
27+
" \n",
28+
"@contextmanager\n",
29+
"def getA():\n",
30+
" a = A()\n",
31+
" yield a\n",
32+
" pp(a.actions)\n",
33+
" \n",
34+
"\n"
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 8,
40+
"metadata": {
41+
"collapsed": false
42+
},
43+
"outputs": [
44+
{
45+
"name": "stdout",
46+
"output_type": "stream",
47+
"text": [
48+
"['open', 'open', 'work', 'close']\n"
49+
]
50+
}
51+
],
52+
"source": [
53+
"with getA() as a:\n",
54+
" with closing(a):\n",
55+
" a.open()\n",
56+
" a.open()\n",
57+
" a.do('work')"
58+
]
59+
},
60+
{
61+
"cell_type": "code",
62+
"execution_count": 11,
63+
"metadata": {
64+
"collapsed": false
65+
},
66+
"outputs": [
67+
{
68+
"data": {
69+
"text/plain": [
70+
"'f:[hello world\\n]'"
71+
]
72+
},
73+
"execution_count": 11,
74+
"metadata": {},
75+
"output_type": "execute_result"
76+
}
77+
],
78+
"source": [
79+
"import io\n",
80+
"from contextlib import redirect_stdout\n",
81+
"\n",
82+
"f = io.StringIO()\n",
83+
"with redirect_stdout(f):\n",
84+
" print('hello world')\n",
85+
"\n",
86+
"'f:[{}]'.format(f.getvalue())"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": null,
92+
"metadata": {
93+
"collapsed": true
94+
},
95+
"outputs": [],
96+
"source": []
97+
}
98+
],
99+
"metadata": {
100+
"kernelspec": {
101+
"display_name": "Python 3",
102+
"language": "python",
103+
"name": "python3"
104+
},
105+
"language_info": {
106+
"codemirror_mode": {
107+
"name": "ipython",
108+
"version": 3
109+
},
110+
"file_extension": ".py",
111+
"mimetype": "text/x-python",
112+
"name": "python",
113+
"nbconvert_exporter": "python",
114+
"pygments_lexer": "ipython3",
115+
"version": "3.6.0"
116+
}
117+
},
118+
"nbformat": 4,
119+
"nbformat_minor": 2
120+
}

0 commit comments

Comments
 (0)