Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Examples py3 compat #6

Merged
merged 3 commits into from over 1 year ago

2 participants

Thomas Kluyver Brian E. Granger
Thomas Kluyver

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

Brian E. Granger ellisonbg referenced this pull request from a commit January 14, 2013
Commit has since been removed from the repository and is no longer available.
Brian E. Granger ellisonbg merged commit 758edc0 into from January 14, 2013
Brian E. Granger ellisonbg closed this January 14, 2013
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.
6  IPython/core/display.py
@@ -28,6 +28,8 @@
28 28
     publish_javascript, publish_jpeg
29 29
 )
30 30
 
  31
+from IPython.utils.py3compat import string_types
  32
+
31 33
 #-----------------------------------------------------------------------------
32 34
 # Main functions
33 35
 #-----------------------------------------------------------------------------
@@ -254,7 +256,7 @@ def __init__(self, data=None, url=None, filename=None):
254 256
         filename : unicode
255 257
             Path to a local file to load the data from.
256 258
         """
257  
-        if data is not None and data.startswith('http'):
  259
+        if data is not None and isinstance(data, string_types) and data.startswith('http'):
258 260
             self.url = data
259 261
             self.filename = None
260 262
             self.data = None
@@ -475,7 +477,7 @@ def __init__(self, data=None, url=None, filename=None, format=u'png', embed=None
475 477
             ext = self._find_ext(url)
476 478
         elif data is None:
477 479
             raise ValueError("No image data found. Expecting filename, url, or data.")
478  
-        elif data.startswith('http'):
  480
+        elif isinstance(data, string_types) and data.startswith('http'):
479 481
             ext = self._find_ext(data)
480 482
         else:
481 483
             ext = None
4  IPython/utils/py3compat.py
@@ -62,6 +62,8 @@ def wrapper(func_or_str):
62 62
     bytes_to_str = decode
63 63
     cast_bytes_py2 = no_code
64 64
     
  65
+    string_types = (str,)
  66
+    
65 67
     def isidentifier(s, dotted=False):
66 68
         if dotted:
67 69
             return all(isidentifier(a) for a in s.split("."))
@@ -110,6 +112,8 @@ def u_format(s):
110 112
     bytes_to_str = no_code
111 113
     cast_bytes_py2 = cast_bytes
112 114
     
  115
+    string_types = (str, unicode)
  116
+    
113 117
     import re
114 118
     _name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
115 119
     def isidentifier(s, dotted=False):
2  examples/notebooks/Animations Using clear_output.ipynb
@@ -59,7 +59,7 @@
59 59
       "for i in range(10):\n",
60 60
       "    time.sleep(0.25)\n",
61 61
       "    clear_output()\n",
62  
-      "    print i\n",
  62
+      "    print(i)\n",
63 63
       "    sys.stdout.flush()"
64 64
      ],
65 65
      "language": "python",
20  examples/notebooks/Cell Magics.ipynb
@@ -151,9 +151,10 @@
151 151
      "collapsed": false,
152 152
      "input": [
153 153
       "%%capture capt\n",
  154
+      "from __future__ import print_function\n",
154 155
       "import sys\n",
155  
-      "print 'Hello stdout'\n",
156  
-      "print >> sys.stderr, 'and stderr'"
  156
+      "print('Hello stdout')\n",
  157
+      "print('and stderr', file=sys.stderr)"
157 158
      ],
158 159
      "language": "python",
159 160
      "metadata": {},
@@ -217,7 +218,7 @@
217 218
      "collapsed": false,
218 219
      "input": [
219 220
       "%%file foo.py\n",
220  
-      "print 'Hello world'"
  221
+      "print('Hello world')"
221 222
      ],
222 223
      "language": "python",
223 224
      "metadata": {},
@@ -471,8 +472,8 @@
471 472
      "cell_type": "code",
472 473
      "collapsed": false,
473 474
      "input": [
474  
-      "print error\n",
475  
-      "print output"
  475
+      "print(error)\n",
  476
+      "print(output)"
476 477
      ],
477 478
      "language": "python",
478 479
      "metadata": {},
@@ -563,7 +564,7 @@
563 564
      "cell_type": "code",
564 565
      "collapsed": false,
565 566
      "input": [
566  
-      "print ruby_lines.read()"
  567
+      "print(ruby_lines.read())"
567 568
      ],
568 569
      "language": "python",
569 570
      "metadata": {},
@@ -927,7 +928,7 @@
927 928
      "input": [
928 929
       "b = %R a=resid(lm(Y~X))\n",
929 930
       "%Rpull a\n",
930  
-      "print a\n",
  931
+      "print(a)\n",
931 932
       "assert id(b.data) == id(a.data)\n",
932 933
       "%R -o a"
933 934
      ],
@@ -963,10 +964,11 @@
963 964
      "cell_type": "code",
964 965
      "collapsed": false,
965 966
      "input": [
  967
+      "from __future__ import print_function\n",
966 968
       "v1 = %R plot(X,Y); print(summary(lm(Y~X))); vv=mean(X)*mean(Y)\n",
967  
-      "print 'v1 is:', v1\n",
  969
+      "print('v1 is:', v1)\n",
968 970
       "v2 = %R mean(X)*mean(Y)\n",
969  
-      "print 'v2 is:', v2"
  971
+      "print('v2 is:', v2)"
970 972
      ],
971 973
      "language": "python",
972 974
      "metadata": {},
4  examples/notebooks/Custom Display Logic.ipynb
@@ -143,7 +143,7 @@
143 143
       "\n",
144 144
       "    def _repr_svg_(self):\n",
145 145
       "        if self._svg_data is None:\n",
146  
-      "            self._svg_data = self._figure_data('svg')\n",
  146
+      "            self._svg_data = self._figure_data('svg').decode('utf-8')\n",
147 147
       "        return self._svg_data\n",
148 148
       "    \n",
149 149
       "    def _repr_latex_(self):\n",
@@ -162,7 +162,7 @@
162 162
       "        \n",
163 163
       "    @property\n",
164 164
       "    def latex(self):\n",
165  
-      "        return Math(self._repr_svg_())\n",
  165
+      "        return Math(self._repr_latex_())\n",
166 166
       "    \n",
167 167
       "    # An example of using a property to display rich information, in this case\n",
168 168
       "    # the histogram of the distribution.  We've hardcoded the format to be png\n",
6  examples/notebooks/Part 1 - Running Code.ipynb
@@ -52,7 +52,7 @@
52 52
      "cell_type": "code",
53 53
      "collapsed": false,
54 54
      "input": [
55  
-      "print a"
  55
+      "print(a)"
56 56
      ],
57 57
      "language": "python",
58 58
      "metadata": {},
@@ -120,7 +120,7 @@
120 120
       "import sys\n",
121 121
       "from ctypes import CDLL\n",
122 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 124
       "libc = CDLL(\"libc.%s\" % dll) \n",
125 125
       "libc.time(-1)  # BOOM!!"
126 126
      ],
@@ -324,7 +324,7 @@
324 324
      "input": [
325 325
       ">>> the_world_is_flat = 1\n",
326 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 329
      "language": "python",
330 330
      "metadata": {},
37  examples/notebooks/Part 2 - Basic Output.ipynb
@@ -47,7 +47,8 @@
47 47
      "cell_type": "code",
48 48
      "collapsed": false,
49 49
      "input": [
50  
-      "import numpy as np"
  50
+      "import numpy as np\n",
  51
+      "import sys"
51 52
      ],
52 53
      "language": "python",
53 54
      "metadata": {},
@@ -58,7 +59,7 @@
58 59
      "cell_type": "code",
59 60
      "collapsed": false,
60 61
      "input": [
61  
-      "numpy.random.rand(10)"
  62
+      "np.random.rand(10)"
62 63
      ],
63 64
      "language": "python",
64 65
      "metadata": {},
@@ -113,7 +114,7 @@
113 114
      "cell_type": "code",
114 115
      "collapsed": false,
115 116
      "input": [
116  
-      "print \"hi, stdout\""
  117
+      "print(\"hi, stdout\")"
117 118
      ],
118 119
      "language": "python",
119 120
      "metadata": {},
@@ -132,7 +133,8 @@
132 133
      "cell_type": "code",
133 134
      "collapsed": false,
134 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 139
      "language": "python",
138 140
      "metadata": {},
@@ -168,7 +170,7 @@
168 170
      "input": [
169 171
       "import time, sys\n",
170 172
       "for i in range(8):\n",
171  
-      "    print i,\n",
  173
+      "    print(i)\n",
172 174
       "    time.sleep(0.5)"
173 175
      ],
174 176
      "language": "python",
@@ -253,7 +255,7 @@
253 255
      "collapsed": false,
254 256
      "input": [
255 257
       "for i in range(50):\n",
256  
-      "    print i"
  258
+      "    print(i)"
257 259
      ],
258 260
      "language": "python",
259 261
      "metadata": {},
@@ -329,7 +331,7 @@
329 331
      "collapsed": false,
330 332
      "input": [
331 333
       "for i in range(500):\n",
332  
-      "    print 2**i - 1"
  334
+      "    print(2**i - 1)"
333 335
      ],
334 336
      "language": "python",
335 337
      "metadata": {},
@@ -862,6 +864,7 @@
862 864
      "cell_type": "code",
863 865
      "collapsed": false,
864 866
      "input": [
  867
+      "from __future__ import print_function\n",
865 868
       "import sys"
866 869
      ],
867 870
      "language": "python",
@@ -881,8 +884,8 @@
881 884
      "collapsed": false,
882 885
      "input": [
883 886
       "%%capture\n",
884  
-      "print 'hi, stdout'\n",
885  
-      "print >> sys.stderr, 'hi, stderr'"
  887
+      "print('hi, stdout')\n",
  888
+      "print('hi, stderr', file=sys.stderr)"
886 889
      ],
887 890
      "language": "python",
888 891
      "metadata": {},
@@ -901,8 +904,8 @@
901 904
      "collapsed": false,
902 905
      "input": [
903 906
       "%%capture captured\n",
904  
-      "print 'hi, stdout'\n",
905  
-      "print >> sys.stderr, 'hi, stderr'"
  907
+      "print('hi, stdout')\n",
  908
+      "print('hi, stderr', file=sys.stderr)"
906 909
      ],
907 910
      "language": "python",
908 911
      "metadata": {},
@@ -1033,13 +1036,13 @@
1033 1036
      "input": [
1034 1037
       "%%capture wontshutup\n",
1035 1038
       "\n",
1036  
-      "print \"setting up X\"\n",
  1039
+      "print(\"setting up X\")\n",
1037 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 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 1044
       "plt.plot(x,y)\n",
1042  
-      "print \"okay, I'm done now\""
  1045
+      "print(\"okay, I'm done now\")"
1043 1046
      ],
1044 1047
      "language": "python",
1045 1048
      "metadata": {},
@@ -1088,8 +1091,8 @@
1088 1091
      "collapsed": false,
1089 1092
      "input": [
1090 1093
       "%%capture cap --no-stderr\n",
1091  
-      "print 'hi, stdout'\n",
1092  
-      "print >> sys.stderr, \"hello, stderr\""
  1094
+      "print('hi, stdout')\n",
  1095
+      "print(\"hello, stderr\", file=sys.stderr)"
1093 1096
      ],
1094 1097
      "language": "python",
1095 1098
      "metadata": {},
3  examples/notebooks/Part 5 - Rich Display System.ipynb
@@ -452,8 +452,9 @@
452 452
      "collapsed": false,
453 453
      "input": [
454 454
       "from IPython.display import HTML\n",
  455
+      "from base64 import b64encode\n",
455 456
       "video = open(\"animation.m4v\", \"rb\").read()\n",
456  
-      "video_encoded = video.encode(\"base64\")\n",
  457
+      "video_encoded = b64encode(video)\n",
457 458
       "video_tag = '<video controls alt=\"test\" src=\"data:video/x-m4v;base64,{0}\">'.format(video_encoded)\n",
458 459
       "HTML(data=video_tag)"
459 460
      ],
3  examples/notebooks/Progress Bars.ipynb
@@ -88,6 +88,7 @@
88 88
      "cell_type": "code",
89 89
      "collapsed": true,
90 90
      "input": [
  91
+      "from __future__ import print_function\n",
91 92
       "import sys, time\n",
92 93
       "\n",
93 94
       "class ProgressBar:\n",
@@ -99,7 +100,7 @@
99 100
       "        self.__update_amount(0)\n",
100 101
       "\n",
101 102
       "    def animate(self, iter):\n",
102  
-      "        print '\\r', self,\n",
  103
+      "        print('\\r', self, end='')\n",
103 104
       "        sys.stdout.flush()\n",
104 105
       "        self.update_iteration(iter + 1)\n",
105 106
       "\n",
19  examples/notebooks/R Magics.ipynb
@@ -196,7 +196,7 @@
196 196
      "input": [
197 197
       "b = %R a=resid(lm(Y~X))\n",
198 198
       "%Rpull a\n",
199  
-      "print a\n",
  199
+      "print(a)\n",
200 200
       "assert id(b.data) == id(a.data)\n",
201 201
       "%R -o a"
202 202
      ],
@@ -227,8 +227,8 @@
227 227
       "%R d=resid(lm(Y~X)); e=coef(lm(Y~X))\n",
228 228
       "%R -o d -o e\n",
229 229
       "%Rpull e\n",
230  
-      "print d\n",
231  
-      "print e\n",
  230
+      "print(d)\n",
  231
+      "print(e)\n",
232 232
       "import numpy as np\n",
233 233
       "np.testing.assert_almost_equal(d, a)"
234 234
      ],
@@ -320,10 +320,11 @@
320 320
      "cell_type": "code",
321 321
      "collapsed": false,
322 322
      "input": [
  323
+      "from __future__ import print_function\n",
323 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 326
       "v2 = %R mean(X)*mean(Y)\n",
326  
-      "print 'v2 is:', v2"
  327
+      "print('v2 is:', v2)"
327 328
      ],
328 329
      "language": "python",
329 330
      "metadata": {},
@@ -437,7 +438,7 @@
437 438
      "collapsed": false,
438 439
      "input": [
439 440
       "v = %R print(summary(X)); X\n",
440  
-      "print 'v:', v"
  441
+      "print('v:', v)"
441 442
      ],
442 443
      "language": "python",
443 444
      "metadata": {},
@@ -682,9 +683,9 @@
682 683
      "cell_type": "code",
683 684
      "collapsed": false,
684 685
      "input": [
685  
-      "print seq1\n",
  686
+      "print(seq1)\n",
686 687
       "%R -i seq1 -o seq1\n",
687  
-      "print seq1\n",
  688
+      "print(seq1)\n",
688 689
       "seq1[0] = 200\n",
689 690
       "%R print(seq1)\n",
690 691
       "seq1_view = %R seq1\n",
@@ -732,7 +733,7 @@
732 733
       "try:\n",
733 734
       "    %R -n nosuchvar\n",
734 735
       "except Exception as e:\n",
735  
-      "    print e.message\n",
  736
+      "    print(e)\n",
736 737
       "    pass"
737 738
      ],
738 739
      "language": "python",
6  examples/notebooks/Script Magics.ipynb
@@ -205,8 +205,8 @@
205 205
      "cell_type": "code",
206 206
      "collapsed": false,
207 207
      "input": [
208  
-      "print error\n",
209  
-      "print output"
  208
+      "print(error)\n",
  209
+      "print(output)"
210 210
      ],
211 211
      "language": "python",
212 212
      "metadata": {},
@@ -297,7 +297,7 @@
297 297
      "cell_type": "code",
298 298
      "collapsed": false,
299 299
      "input": [
300  
-      "print ruby_lines.read()"
  300
+      "print(ruby_lines.read())"
301 301
      ],
302 302
      "language": "python",
303 303
      "metadata": {},
5  examples/notebooks/Trapezoid Rule.ipynb
@@ -121,10 +121,11 @@
121 121
      "cell_type": "code",
122 122
      "collapsed": false,
123 123
      "input": [
  124
+      "from __future__ import print_function\n",
124 125
       "from scipy.integrate import quad, trapz\n",
125 126
       "integral, error = quad(f, 1, 9)\n",
126  
-      "print \"The integral is:\", integral, \"+/-\", error\n",
127  
-      "print \"The trapezoid approximation with\", len(xint), \"points is:\", trapz(yint, xint)"
  127
+      "print(\"The integral is:\", integral, \"+/-\", error)\n",
  128
+      "print(\"The trapezoid approximation with\", len(xint), \"points is:\", trapz(yint, xint))"
128 129
      ],
129 130
      "language": "python",
130 131
      "metadata": {},
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.