From b62fdeca665764d9bbf759642bb72c40b5cd90cc 2013-01-19 22:22:25 From: Thomas Kluyver <takowl@gmail.com> Date: 2013-01-19 22:22:25 Subject: [PATCH] More changes to example notebooks for Python 3 compatibility --- diff --git a/examples/notebooks/Part 1 - Running Code.ipynb b/examples/notebooks/Part 1 - Running Code.ipynb index 12ea0db..9da3129 100644 --- a/examples/notebooks/Part 1 - Running Code.ipynb +++ b/examples/notebooks/Part 1 - Running Code.ipynb @@ -52,7 +52,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "print a" + "print(a)" ], "language": "python", "metadata": {}, @@ -120,7 +120,7 @@ "import sys\n", "from ctypes import CDLL\n", "# This will crash a Linux or Mac system; equivalent calls can be made on Windows\n", - "dll = 'dylib' if sys.platform == 'darwin' else '.so.6'\n", + "dll = 'dylib' if sys.platform == 'darwin' else 'so.6'\n", "libc = CDLL(\"libc.%s\" % dll) \n", "libc.time(-1) # BOOM!!" ], @@ -324,7 +324,7 @@ "input": [ ">>> the_world_is_flat = 1\n", ">>> if the_world_is_flat:\n", - "... print \"Be careful not to fall off!\"" + "... print(\"Be careful not to fall off!\")" ], "language": "python", "metadata": {}, diff --git a/examples/notebooks/Part 2 - Basic Output.ipynb b/examples/notebooks/Part 2 - Basic Output.ipynb index 009c625..fbe134f 100644 --- a/examples/notebooks/Part 2 - Basic Output.ipynb +++ b/examples/notebooks/Part 2 - Basic Output.ipynb @@ -47,7 +47,8 @@ "cell_type": "code", "collapsed": false, "input": [ - "import numpy as np" + "import numpy as np\n", + "import sys" ], "language": "python", "metadata": {}, @@ -58,7 +59,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "numpy.random.rand(10)" + "np.random.rand(10)" ], "language": "python", "metadata": {}, @@ -113,7 +114,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "print \"hi, stdout\"" + "print(\"hi, stdout\")" ], "language": "python", "metadata": {}, @@ -132,7 +133,8 @@ "cell_type": "code", "collapsed": false, "input": [ - "print >> sys.stderr, 'hi, stderr'" + "from __future__ import print_function\n", + "print('hi, stderr', file=sys.stderr)" ], "language": "python", "metadata": {}, @@ -168,7 +170,7 @@ "input": [ "import time, sys\n", "for i in range(8):\n", - " print i,\n", + " print(i)\n", " time.sleep(0.5)" ], "language": "python", @@ -253,7 +255,7 @@ "collapsed": false, "input": [ "for i in range(50):\n", - " print i" + " print(i)" ], "language": "python", "metadata": {}, @@ -329,7 +331,7 @@ "collapsed": false, "input": [ "for i in range(500):\n", - " print 2**i - 1" + " print(2**i - 1)" ], "language": "python", "metadata": {}, @@ -862,6 +864,7 @@ "cell_type": "code", "collapsed": false, "input": [ + "from __future__ import print_function\n", "import sys" ], "language": "python", @@ -881,8 +884,8 @@ "collapsed": false, "input": [ "%%capture\n", - "print 'hi, stdout'\n", - "print >> sys.stderr, 'hi, stderr'" + "print('hi, stdout')\n", + "print('hi, stderr', file=sys.stderr)" ], "language": "python", "metadata": {}, @@ -901,8 +904,8 @@ "collapsed": false, "input": [ "%%capture captured\n", - "print 'hi, stdout'\n", - "print >> sys.stderr, 'hi, stderr'" + "print('hi, stdout')\n", + "print('hi, stderr', file=sys.stderr)" ], "language": "python", "metadata": {}, @@ -1033,13 +1036,13 @@ "input": [ "%%capture wontshutup\n", "\n", - "print \"setting up X\"\n", + "print(\"setting up X\")\n", "x = np.linspace(0,5,1000)\n", - "print \"step 2: constructing y-data\"\n", + "print(\"step 2: constructing y-data\")\n", "y = np.sin(x)\n", - "print \"step 3: display info about y\"\n", + "print(\"step 3: display info about y\")\n", "plt.plot(x,y)\n", - "print \"okay, I'm done now\"" + "print(\"okay, I'm done now\")" ], "language": "python", "metadata": {}, @@ -1088,8 +1091,8 @@ "collapsed": false, "input": [ "%%capture cap --no-stderr\n", - "print 'hi, stdout'\n", - "print >> sys.stderr, \"hello, stderr\"" + "print('hi, stdout')\n", + "print(\"hello, stderr\", file=sys.stderr)" ], "language": "python", "metadata": {}, diff --git a/examples/notebooks/Part 5 - Rich Display System.ipynb b/examples/notebooks/Part 5 - Rich Display System.ipynb index 71c20b3..4f9ab81 100644 --- a/examples/notebooks/Part 5 - Rich Display System.ipynb +++ b/examples/notebooks/Part 5 - Rich Display System.ipynb @@ -452,8 +452,9 @@ "collapsed": false, "input": [ "from IPython.display import HTML\n", + "from base64 import b64encode\n", "video = open(\"animation.m4v\", \"rb\").read()\n", - "video_encoded = video.encode(\"base64\")\n", + "video_encoded = b64encode(video)\n", "video_tag = '<video controls alt=\"test\" src=\"data:video/x-m4v;base64,{0}\">'.format(video_encoded)\n", "HTML(data=video_tag)" ], diff --git a/examples/notebooks/Progress Bars.ipynb b/examples/notebooks/Progress Bars.ipynb index 6678bc6..199d40f 100644 --- a/examples/notebooks/Progress Bars.ipynb +++ b/examples/notebooks/Progress Bars.ipynb @@ -88,6 +88,7 @@ "cell_type": "code", "collapsed": true, "input": [ + "from __future__ import print_function\n", "import sys, time\n", "\n", "class ProgressBar:\n", @@ -99,7 +100,7 @@ " self.__update_amount(0)\n", "\n", " def animate(self, iter):\n", - " print '\\r', self,\n", + " print('\\r', self, end='')\n", " sys.stdout.flush()\n", " self.update_iteration(iter + 1)\n", "\n", diff --git a/examples/notebooks/R Magics.ipynb b/examples/notebooks/R Magics.ipynb index 7e8387a..af7da77 100644 --- a/examples/notebooks/R Magics.ipynb +++ b/examples/notebooks/R Magics.ipynb @@ -196,7 +196,7 @@ "input": [ "b = %R a=resid(lm(Y~X))\n", "%Rpull a\n", - "print a\n", + "print(a)\n", "assert id(b.data) == id(a.data)\n", "%R -o a" ], @@ -227,8 +227,8 @@ "%R d=resid(lm(Y~X)); e=coef(lm(Y~X))\n", "%R -o d -o e\n", "%Rpull e\n", - "print d\n", - "print e\n", + "print(d)\n", + "print(e)\n", "import numpy as np\n", "np.testing.assert_almost_equal(d, a)" ], @@ -320,10 +320,11 @@ "cell_type": "code", "collapsed": false, "input": [ + "from __future__ import print_function\n", "v1 = %R plot(X,Y); print(summary(lm(Y~X))); vv=mean(X)*mean(Y)\n", - "print 'v1 is:', v1\n", + "print('v1 is:', v1)\n", "v2 = %R mean(X)*mean(Y)\n", - "print 'v2 is:', v2" + "print('v2 is:', v2)" ], "language": "python", "metadata": {}, @@ -437,7 +438,7 @@ "collapsed": false, "input": [ "v = %R print(summary(X)); X\n", - "print 'v:', v" + "print('v:', v)" ], "language": "python", "metadata": {}, @@ -682,9 +683,9 @@ "cell_type": "code", "collapsed": false, "input": [ - "print seq1\n", + "print(seq1)\n", "%R -i seq1 -o seq1\n", - "print seq1\n", + "print(seq1)\n", "seq1[0] = 200\n", "%R print(seq1)\n", "seq1_view = %R seq1\n", @@ -732,7 +733,7 @@ "try:\n", " %R -n nosuchvar\n", "except Exception as e:\n", - " print e.message\n", + " print(e)\n", " pass" ], "language": "python", diff --git a/examples/notebooks/Script Magics.ipynb b/examples/notebooks/Script Magics.ipynb index e0e6f6f..643d557 100644 --- a/examples/notebooks/Script Magics.ipynb +++ b/examples/notebooks/Script Magics.ipynb @@ -205,8 +205,8 @@ "cell_type": "code", "collapsed": false, "input": [ - "print error\n", - "print output" + "print(error)\n", + "print(output)" ], "language": "python", "metadata": {}, @@ -297,7 +297,7 @@ "cell_type": "code", "collapsed": false, "input": [ - "print ruby_lines.read()" + "print(ruby_lines.read())" ], "language": "python", "metadata": {}, diff --git a/examples/notebooks/Trapezoid Rule.ipynb b/examples/notebooks/Trapezoid Rule.ipynb index 6d41d15..7d88649 100644 --- a/examples/notebooks/Trapezoid Rule.ipynb +++ b/examples/notebooks/Trapezoid Rule.ipynb @@ -121,10 +121,11 @@ "cell_type": "code", "collapsed": false, "input": [ + "from __future__ import print_function\n", "from scipy.integrate import quad, trapz\n", "integral, error = quad(f, 1, 9)\n", - "print \"The integral is:\", integral, \"+/-\", error\n", - "print \"The trapezoid approximation with\", len(xint), \"points is:\", trapz(yint, xint)" + "print(\"The integral is:\", integral, \"+/-\", error)\n", + "print(\"The trapezoid approximation with\", len(xint), \"points is:\", trapz(yint, xint))" ], "language": "python", "metadata": {},