Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Examples py3 compat #6

Merged
merged 3 commits into from

2 participants

@takluyver

The changes as per ipython/ipython#2627, made against your branch.

@ellisonbg ellisonbg referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@ellisonbg ellisonbg merged commit 758edc0 into ellisonbg:examples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
6 IPython/core/display.py
@@ -28,6 +28,8 @@
publish_javascript, publish_jpeg
)
+from IPython.utils.py3compat import string_types
+
#-----------------------------------------------------------------------------
# Main functions
#-----------------------------------------------------------------------------
@@ -254,7 +256,7 @@ def __init__(self, data=None, url=None, filename=None):
filename : unicode
Path to a local file to load the data from.
"""
- if data is not None and data.startswith('http'):
+ if data is not None and isinstance(data, string_types) and data.startswith('http'):
self.url = data
self.filename = None
self.data = None
@@ -475,7 +477,7 @@ def __init__(self, data=None, url=None, filename=None, format=u'png', embed=None
ext = self._find_ext(url)
elif data is None:
raise ValueError("No image data found. Expecting filename, url, or data.")
- elif data.startswith('http'):
+ elif isinstance(data, string_types) and data.startswith('http'):
ext = self._find_ext(data)
else:
ext = None
View
4 IPython/utils/py3compat.py
@@ -62,6 +62,8 @@ def wrapper(func_or_str):
bytes_to_str = decode
cast_bytes_py2 = no_code
+ string_types = (str,)
+
def isidentifier(s, dotted=False):
if dotted:
return all(isidentifier(a) for a in s.split("."))
@@ -110,6 +112,8 @@ def u_format(s):
bytes_to_str = no_code
cast_bytes_py2 = cast_bytes
+ string_types = (str, unicode)
+
import re
_name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
def isidentifier(s, dotted=False):
View
2  examples/notebooks/Animations Using clear_output.ipynb
@@ -59,7 +59,7 @@
"for i in range(10):\n",
" time.sleep(0.25)\n",
" clear_output()\n",
- " print i\n",
+ " print(i)\n",
" sys.stdout.flush()"
],
"language": "python",
View
20 examples/notebooks/Cell Magics.ipynb
@@ -151,9 +151,10 @@
"collapsed": false,
"input": [
"%%capture capt\n",
+ "from __future__ import print_function\n",
"import sys\n",
- "print 'Hello stdout'\n",
- "print >> sys.stderr, 'and stderr'"
+ "print('Hello stdout')\n",
+ "print('and stderr', file=sys.stderr)"
],
"language": "python",
"metadata": {},
@@ -217,7 +218,7 @@
"collapsed": false,
"input": [
"%%file foo.py\n",
- "print 'Hello world'"
+ "print('Hello world')"
],
"language": "python",
"metadata": {},
@@ -471,8 +472,8 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "print error\n",
- "print output"
+ "print(error)\n",
+ "print(output)"
],
"language": "python",
"metadata": {},
@@ -563,7 +564,7 @@
"cell_type": "code",
"collapsed": false,
"input": [
- "print ruby_lines.read()"
+ "print(ruby_lines.read())"
],
"language": "python",
"metadata": {},
@@ -927,7 +928,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"
],
@@ -963,10 +964,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": {},
View
4 examples/notebooks/Custom Display Logic.ipynb
@@ -143,7 +143,7 @@
"\n",
" def _repr_svg_(self):\n",
" if self._svg_data is None:\n",
- " self._svg_data = self._figure_data('svg')\n",
+ " self._svg_data = self._figure_data('svg').decode('utf-8')\n",
" return self._svg_data\n",
" \n",
" def _repr_latex_(self):\n",
@@ -162,7 +162,7 @@
" \n",
" @property\n",
" def latex(self):\n",
- " return Math(self._repr_svg_())\n",
+ " return Math(self._repr_latex_())\n",
" \n",
" # An example of using a property to display rich information, in this case\n",
" # the histogram of the distribution. We've hardcoded the format to be png\n",
View
6 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": {},
View
37 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": {},
View
3  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)"
],
View
3  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",
View
19 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",
View
6 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": {},
View
5 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": {},
Something went wrong with that request. Please try again.