##// END OF EJS Templates
More changes to example notebooks for Python 3 compatibility
Thomas Kluyver -
Show More
@@ -52,7 +52,7 b''
52 "cell_type": "code",
52 "cell_type": "code",
53 "collapsed": false,
53 "collapsed": false,
54 "input": [
54 "input": [
55 "print a"
55 "print(a)"
56 ],
56 ],
57 "language": "python",
57 "language": "python",
58 "metadata": {},
58 "metadata": {},
@@ -120,7 +120,7 b''
120 "import sys\n",
120 "import sys\n",
121 "from ctypes import CDLL\n",
121 "from ctypes import CDLL\n",
122 "# This will crash a Linux or Mac system; equivalent calls can be made on Windows\n",
122 "# This will crash a Linux or Mac system; equivalent calls can be made on Windows\n",
123 "dll = 'dylib' if sys.platform == 'darwin' else '.so.6'\n",
123 "dll = 'dylib' if sys.platform == 'darwin' else 'so.6'\n",
124 "libc = CDLL(\"libc.%s\" % dll) \n",
124 "libc = CDLL(\"libc.%s\" % dll) \n",
125 "libc.time(-1) # BOOM!!"
125 "libc.time(-1) # BOOM!!"
126 ],
126 ],
@@ -324,7 +324,7 b''
324 "input": [
324 "input": [
325 ">>> the_world_is_flat = 1\n",
325 ">>> the_world_is_flat = 1\n",
326 ">>> if the_world_is_flat:\n",
326 ">>> if the_world_is_flat:\n",
327 "... print \"Be careful not to fall off!\""
327 "... print(\"Be careful not to fall off!\")"
328 ],
328 ],
329 "language": "python",
329 "language": "python",
330 "metadata": {},
330 "metadata": {},
@@ -47,7 +47,8 b''
47 "cell_type": "code",
47 "cell_type": "code",
48 "collapsed": false,
48 "collapsed": false,
49 "input": [
49 "input": [
50 "import numpy as np"
50 "import numpy as np\n",
51 "import sys"
51 ],
52 ],
52 "language": "python",
53 "language": "python",
53 "metadata": {},
54 "metadata": {},
@@ -58,7 +59,7 b''
58 "cell_type": "code",
59 "cell_type": "code",
59 "collapsed": false,
60 "collapsed": false,
60 "input": [
61 "input": [
61 "numpy.random.rand(10)"
62 "np.random.rand(10)"
62 ],
63 ],
63 "language": "python",
64 "language": "python",
64 "metadata": {},
65 "metadata": {},
@@ -113,7 +114,7 b''
113 "cell_type": "code",
114 "cell_type": "code",
114 "collapsed": false,
115 "collapsed": false,
115 "input": [
116 "input": [
116 "print \"hi, stdout\""
117 "print(\"hi, stdout\")"
117 ],
118 ],
118 "language": "python",
119 "language": "python",
119 "metadata": {},
120 "metadata": {},
@@ -132,7 +133,8 b''
132 "cell_type": "code",
133 "cell_type": "code",
133 "collapsed": false,
134 "collapsed": false,
134 "input": [
135 "input": [
135 "print >> sys.stderr, 'hi, stderr'"
136 "from __future__ import print_function\n",
137 "print('hi, stderr', file=sys.stderr)"
136 ],
138 ],
137 "language": "python",
139 "language": "python",
138 "metadata": {},
140 "metadata": {},
@@ -168,7 +170,7 b''
168 "input": [
170 "input": [
169 "import time, sys\n",
171 "import time, sys\n",
170 "for i in range(8):\n",
172 "for i in range(8):\n",
171 " print i,\n",
173 " print(i)\n",
172 " time.sleep(0.5)"
174 " time.sleep(0.5)"
173 ],
175 ],
174 "language": "python",
176 "language": "python",
@@ -253,7 +255,7 b''
253 "collapsed": false,
255 "collapsed": false,
254 "input": [
256 "input": [
255 "for i in range(50):\n",
257 "for i in range(50):\n",
256 " print i"
258 " print(i)"
257 ],
259 ],
258 "language": "python",
260 "language": "python",
259 "metadata": {},
261 "metadata": {},
@@ -329,7 +331,7 b''
329 "collapsed": false,
331 "collapsed": false,
330 "input": [
332 "input": [
331 "for i in range(500):\n",
333 "for i in range(500):\n",
332 " print 2**i - 1"
334 " print(2**i - 1)"
333 ],
335 ],
334 "language": "python",
336 "language": "python",
335 "metadata": {},
337 "metadata": {},
@@ -862,6 +864,7 b''
862 "cell_type": "code",
864 "cell_type": "code",
863 "collapsed": false,
865 "collapsed": false,
864 "input": [
866 "input": [
867 "from __future__ import print_function\n",
865 "import sys"
868 "import sys"
866 ],
869 ],
867 "language": "python",
870 "language": "python",
@@ -881,8 +884,8 b''
881 "collapsed": false,
884 "collapsed": false,
882 "input": [
885 "input": [
883 "%%capture\n",
886 "%%capture\n",
884 "print 'hi, stdout'\n",
887 "print('hi, stdout')\n",
885 "print >> sys.stderr, 'hi, stderr'"
888 "print('hi, stderr', file=sys.stderr)"
886 ],
889 ],
887 "language": "python",
890 "language": "python",
888 "metadata": {},
891 "metadata": {},
@@ -901,8 +904,8 b''
901 "collapsed": false,
904 "collapsed": false,
902 "input": [
905 "input": [
903 "%%capture captured\n",
906 "%%capture captured\n",
904 "print 'hi, stdout'\n",
907 "print('hi, stdout')\n",
905 "print >> sys.stderr, 'hi, stderr'"
908 "print('hi, stderr', file=sys.stderr)"
906 ],
909 ],
907 "language": "python",
910 "language": "python",
908 "metadata": {},
911 "metadata": {},
@@ -1033,13 +1036,13 b''
1033 "input": [
1036 "input": [
1034 "%%capture wontshutup\n",
1037 "%%capture wontshutup\n",
1035 "\n",
1038 "\n",
1036 "print \"setting up X\"\n",
1039 "print(\"setting up X\")\n",
1037 "x = np.linspace(0,5,1000)\n",
1040 "x = np.linspace(0,5,1000)\n",
1038 "print \"step 2: constructing y-data\"\n",
1041 "print(\"step 2: constructing y-data\")\n",
1039 "y = np.sin(x)\n",
1042 "y = np.sin(x)\n",
1040 "print \"step 3: display info about y\"\n",
1043 "print(\"step 3: display info about y\")\n",
1041 "plt.plot(x,y)\n",
1044 "plt.plot(x,y)\n",
1042 "print \"okay, I'm done now\""
1045 "print(\"okay, I'm done now\")"
1043 ],
1046 ],
1044 "language": "python",
1047 "language": "python",
1045 "metadata": {},
1048 "metadata": {},
@@ -1088,8 +1091,8 b''
1088 "collapsed": false,
1091 "collapsed": false,
1089 "input": [
1092 "input": [
1090 "%%capture cap --no-stderr\n",
1093 "%%capture cap --no-stderr\n",
1091 "print 'hi, stdout'\n",
1094 "print('hi, stdout')\n",
1092 "print >> sys.stderr, \"hello, stderr\""
1095 "print(\"hello, stderr\", file=sys.stderr)"
1093 ],
1096 ],
1094 "language": "python",
1097 "language": "python",
1095 "metadata": {},
1098 "metadata": {},
@@ -452,8 +452,9 b''
452 "collapsed": false,
452 "collapsed": false,
453 "input": [
453 "input": [
454 "from IPython.display import HTML\n",
454 "from IPython.display import HTML\n",
455 "from base64 import b64encode\n",
455 "video = open(\"animation.m4v\", \"rb\").read()\n",
456 "video = open(\"animation.m4v\", \"rb\").read()\n",
456 "video_encoded = video.encode(\"base64\")\n",
457 "video_encoded = b64encode(video)\n",
457 "video_tag = '<video controls alt=\"test\" src=\"data:video/x-m4v;base64,{0}\">'.format(video_encoded)\n",
458 "video_tag = '<video controls alt=\"test\" src=\"data:video/x-m4v;base64,{0}\">'.format(video_encoded)\n",
458 "HTML(data=video_tag)"
459 "HTML(data=video_tag)"
459 ],
460 ],
@@ -88,6 +88,7 b''
88 "cell_type": "code",
88 "cell_type": "code",
89 "collapsed": true,
89 "collapsed": true,
90 "input": [
90 "input": [
91 "from __future__ import print_function\n",
91 "import sys, time\n",
92 "import sys, time\n",
92 "\n",
93 "\n",
93 "class ProgressBar:\n",
94 "class ProgressBar:\n",
@@ -99,7 +100,7 b''
99 " self.__update_amount(0)\n",
100 " self.__update_amount(0)\n",
100 "\n",
101 "\n",
101 " def animate(self, iter):\n",
102 " def animate(self, iter):\n",
102 " print '\\r', self,\n",
103 " print('\\r', self, end='')\n",
103 " sys.stdout.flush()\n",
104 " sys.stdout.flush()\n",
104 " self.update_iteration(iter + 1)\n",
105 " self.update_iteration(iter + 1)\n",
105 "\n",
106 "\n",
@@ -196,7 +196,7 b''
196 "input": [
196 "input": [
197 "b = %R a=resid(lm(Y~X))\n",
197 "b = %R a=resid(lm(Y~X))\n",
198 "%Rpull a\n",
198 "%Rpull a\n",
199 "print a\n",
199 "print(a)\n",
200 "assert id(b.data) == id(a.data)\n",
200 "assert id(b.data) == id(a.data)\n",
201 "%R -o a"
201 "%R -o a"
202 ],
202 ],
@@ -227,8 +227,8 b''
227 "%R d=resid(lm(Y~X)); e=coef(lm(Y~X))\n",
227 "%R d=resid(lm(Y~X)); e=coef(lm(Y~X))\n",
228 "%R -o d -o e\n",
228 "%R -o d -o e\n",
229 "%Rpull e\n",
229 "%Rpull e\n",
230 "print d\n",
230 "print(d)\n",
231 "print e\n",
231 "print(e)\n",
232 "import numpy as np\n",
232 "import numpy as np\n",
233 "np.testing.assert_almost_equal(d, a)"
233 "np.testing.assert_almost_equal(d, a)"
234 ],
234 ],
@@ -320,10 +320,11 b''
320 "cell_type": "code",
320 "cell_type": "code",
321 "collapsed": false,
321 "collapsed": false,
322 "input": [
322 "input": [
323 "from __future__ import print_function\n",
323 "v1 = %R plot(X,Y); print(summary(lm(Y~X))); vv=mean(X)*mean(Y)\n",
324 "v1 = %R plot(X,Y); print(summary(lm(Y~X))); vv=mean(X)*mean(Y)\n",
324 "print 'v1 is:', v1\n",
325 "print('v1 is:', v1)\n",
325 "v2 = %R mean(X)*mean(Y)\n",
326 "v2 = %R mean(X)*mean(Y)\n",
326 "print 'v2 is:', v2"
327 "print('v2 is:', v2)"
327 ],
328 ],
328 "language": "python",
329 "language": "python",
329 "metadata": {},
330 "metadata": {},
@@ -437,7 +438,7 b''
437 "collapsed": false,
438 "collapsed": false,
438 "input": [
439 "input": [
439 "v = %R print(summary(X)); X\n",
440 "v = %R print(summary(X)); X\n",
440 "print 'v:', v"
441 "print('v:', v)"
441 ],
442 ],
442 "language": "python",
443 "language": "python",
443 "metadata": {},
444 "metadata": {},
@@ -682,9 +683,9 b''
682 "cell_type": "code",
683 "cell_type": "code",
683 "collapsed": false,
684 "collapsed": false,
684 "input": [
685 "input": [
685 "print seq1\n",
686 "print(seq1)\n",
686 "%R -i seq1 -o seq1\n",
687 "%R -i seq1 -o seq1\n",
687 "print seq1\n",
688 "print(seq1)\n",
688 "seq1[0] = 200\n",
689 "seq1[0] = 200\n",
689 "%R print(seq1)\n",
690 "%R print(seq1)\n",
690 "seq1_view = %R seq1\n",
691 "seq1_view = %R seq1\n",
@@ -732,7 +733,7 b''
732 "try:\n",
733 "try:\n",
733 " %R -n nosuchvar\n",
734 " %R -n nosuchvar\n",
734 "except Exception as e:\n",
735 "except Exception as e:\n",
735 " print e.message\n",
736 " print(e)\n",
736 " pass"
737 " pass"
737 ],
738 ],
738 "language": "python",
739 "language": "python",
@@ -205,8 +205,8 b''
205 "cell_type": "code",
205 "cell_type": "code",
206 "collapsed": false,
206 "collapsed": false,
207 "input": [
207 "input": [
208 "print error\n",
208 "print(error)\n",
209 "print output"
209 "print(output)"
210 ],
210 ],
211 "language": "python",
211 "language": "python",
212 "metadata": {},
212 "metadata": {},
@@ -297,7 +297,7 b''
297 "cell_type": "code",
297 "cell_type": "code",
298 "collapsed": false,
298 "collapsed": false,
299 "input": [
299 "input": [
300 "print ruby_lines.read()"
300 "print(ruby_lines.read())"
301 ],
301 ],
302 "language": "python",
302 "language": "python",
303 "metadata": {},
303 "metadata": {},
@@ -121,10 +121,11 b''
121 "cell_type": "code",
121 "cell_type": "code",
122 "collapsed": false,
122 "collapsed": false,
123 "input": [
123 "input": [
124 "from __future__ import print_function\n",
124 "from scipy.integrate import quad, trapz\n",
125 "from scipy.integrate import quad, trapz\n",
125 "integral, error = quad(f, 1, 9)\n",
126 "integral, error = quad(f, 1, 9)\n",
126 "print \"The integral is:\", integral, \"+/-\", error\n",
127 "print(\"The integral is:\", integral, \"+/-\", error)\n",
127 "print \"The trapezoid approximation with\", len(xint), \"points is:\", trapz(yint, xint)"
128 "print(\"The trapezoid approximation with\", len(xint), \"points is:\", trapz(yint, xint))"
128 ],
129 ],
129 "language": "python",
130 "language": "python",
130 "metadata": {},
131 "metadata": {},
General Comments 0
You need to be logged in to leave comments. Login now