Skip to content

Commit 9f5416f

Browse files
authored
Extract duplicated helper code from the notebooks spawning python interpreters (#307)
1 parent e5c7583 commit 9f5416f

File tree

3 files changed

+36
-50
lines changed

3 files changed

+36
-50
lines changed

examples/ipython/LaTeX.ipynb

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,9 @@
66
"metadata": {},
77
"outputs": [],
88
"source": [
9-
"import subprocess\n",
10-
"import sys\n",
119
"import os\n",
12-
"from IPython.display import display_pretty\n",
13-
"\n",
14-
"os.chdir('../LaTeX')\n",
15-
"\n",
16-
"def check(name):\n",
17-
" # todo: use subprocess.run once we drop Python 2.7\n",
18-
" p = subprocess.Popen([sys.executable, name + '.py'], stderr=subprocess.PIPE, universal_newlines=True)\n",
19-
" try:\n",
20-
" stdout, stderr = p.communicate()\n",
21-
" except:\n",
22-
" p.kill()\n",
23-
" if p.poll():\n",
24-
" raise RuntimeError(\"The script raised an exception:\\n\\n\" + stderr)\n",
25-
"\n",
26-
" with open(name + '.tex', 'r') as f:\n",
27-
" # can't use display.Latex here, it would result in CSS comparisons in the output.\n",
28-
" \n",
29-
" # using `display` forces this to be a separate output to any stdout from above.\n",
30-
" display_pretty(f.read(), raw=True)"
10+
"from galgebra_ipython_helpers import check\n",
11+
"os.chdir('../LaTeX')"
3112
]
3213
},
3314
{

examples/ipython/Old Format.ipynb

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,9 @@
66
"metadata": {},
77
"outputs": [],
88
"source": [
9-
"import subprocess\n",
10-
"import sys\n",
119
"import os\n",
12-
"from IPython.display import display_pretty\n",
13-
"\n",
14-
"os.chdir('../Old Format')\n",
15-
"\n",
16-
"def check_latex(name):\n",
17-
" # todo: use subprocess.run once we drop Python 2.7\n",
18-
" p = subprocess.Popen([sys.executable, name + '.py'], stderr=subprocess.PIPE, universal_newlines=True)\n",
19-
" try:\n",
20-
" stdout, stderr = p.communicate()\n",
21-
" except:\n",
22-
" p.kill()\n",
23-
" if p.poll():\n",
24-
" raise RuntimeError(\"The script raised an exception:\\n\\n\" + stderr)\n",
25-
"\n",
26-
" with open(name + '.tex', 'r') as f:\n",
27-
" # can't use display.Latex here, it would result in CSS comparisons in the output.\n",
28-
" \n",
29-
" # using `display` forces this to be a separate output to any stdout from above.\n",
30-
" display_pretty(f.read(), raw=True)"
10+
"from galgebra_ipython_helpers import check as check_latex, run\n",
11+
"os.chdir('../Old Format')"
3112
]
3213
},
3314
{
@@ -50,7 +31,7 @@
5031
}
5132
],
5233
"source": [
53-
"!python bad_example.py"
34+
"run('bad_example')"
5435
]
5536
},
5637
{
@@ -86,7 +67,7 @@
8667
}
8768
],
8869
"source": [
89-
"!python eval_check.py"
70+
"run('eval_check')"
9071
]
9172
},
9273
{
@@ -106,7 +87,7 @@
10687
}
10788
],
10889
"source": [
109-
"!python exp_check.py"
90+
"run('exp_check')"
11091
]
11192
},
11293
{
@@ -618,7 +599,7 @@
618599
}
619600
],
620601
"source": [
621-
"!python mv_setup_options.py"
602+
"run('mv_setup_options')"
622603
]
623604
},
624605
{
@@ -925,7 +906,7 @@
925906
}
926907
],
927908
"source": [
928-
"!python prob_not_solenoidal.py"
909+
"run('prob_not_solenoidal')"
929910
]
930911
},
931912
{
@@ -1113,7 +1094,7 @@
11131094
}
11141095
],
11151096
"source": [
1116-
"!python products_latex.py"
1097+
"run('products_latex')"
11171098
]
11181099
},
11191100
{
@@ -1295,7 +1276,7 @@
12951276
}
12961277
],
12971278
"source": [
1298-
"!python simple_check.py"
1279+
"run('simple_check')"
12991280
]
13001281
},
13011282
{
@@ -1548,7 +1529,7 @@
15481529
}
15491530
],
15501531
"source": [
1551-
"!python terminal_check.py"
1532+
"run('terminal_check')"
15521533
]
15531534
}
15541535
],
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import subprocess
2+
import sys
3+
from IPython.display import display_pretty
4+
5+
6+
def check(name):
7+
# todo: use subprocess.run once we drop Python 2.7
8+
p = subprocess.Popen([sys.executable, name + '.py'], stderr=subprocess.PIPE, universal_newlines=True)
9+
try:
10+
stdout, stderr = p.communicate()
11+
except:
12+
p.kill()
13+
if p.poll():
14+
raise RuntimeError("The script raised an exception:\n\n" + stderr)
15+
16+
with open(name + '.tex', 'r') as f:
17+
# can't use display.Latex here, it would result in CSS comparisons in the output.
18+
19+
# using `display` forces this to be a separate output to any stdout from above.
20+
display_pretty(f.read(), raw=True)
21+
22+
23+
def run(name):
24+
get_ipython().system('python {}.py'.format(name))

0 commit comments

Comments
 (0)