Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 45cbed0

Browse files
committed
Make codebase python3 compatible.
1 parent aa58cf9 commit 45cbed0

File tree

5 files changed

+35
-23
lines changed

5 files changed

+35
-23
lines changed

pymatbridge/compat.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import sys
2+
3+
PY3 = sys.version_info[0] == 3
4+
5+
if PY3:
6+
text_type = str
7+
unichr = chr
8+
else:
9+
text_type = unicode
10+
unichr = unichr

pymatbridge/matlab_magic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from glob import glob
1818
from shutil import rmtree
1919
from getopt import getopt
20-
from urllib2 import URLError
2120

2221
import numpy as np
2322
try:
@@ -37,6 +36,7 @@
3736
from IPython.utils.py3compat import str_to_unicode, unicode_to_str, PY3
3837

3938
import pymatbridge as pymat
39+
from .compat import text_type
4040

4141

4242
class MatlabInterperterError(RuntimeError):
@@ -56,7 +56,7 @@ def __unicode__(self):
5656
__str__ = __unicode__
5757
else:
5858
def __str__(self):
59-
return unicode_to_str(unicode(self), 'utf-8')
59+
return unicode_to_str(text_type(self), 'utf-8')
6060

6161

6262

@@ -210,7 +210,7 @@ def matlab(self, line, cell=None, local_ns=None):
210210
else:
211211
e_s = "There was an error running the code:\n %s"%code
212212
result_dict = self.eval(code)
213-
except URLError:
213+
except:
214214
e_s += "\n-----------------------"
215215
e_s += "\nAre you sure Matlab is started?"
216216
raise RuntimeError(e_s)

pymatbridge/pymatbridge.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ def _run_server(self):
114114
# Start server/client session and make the connection
115115
def start(self):
116116
# Start the MATLAB server in a new process
117-
print "Starting %s on ZMQ socket %s" % (self._program_name(), self.socket_addr)
118-
print "Send 'exit' command to kill the server"
117+
print("Starting %s on ZMQ socket %s" % (self._program_name(), self.socket_addr))
118+
print("Send 'exit' command to kill the server")
119119
self._run_server()
120120

121121
# Start the client
@@ -127,23 +127,23 @@ def start(self):
127127

128128
# Test if connection is established
129129
if self.is_connected():
130-
print "%s started and connected!" % self._program_name()
130+
print("%s started and connected!" % self._program_name())
131131
return True
132132
else:
133-
print "%s failed to start" % self._program_name()
133+
print("%s failed to start" % self._program_name())
134134
return False
135135

136136
def _response(self, **kwargs):
137137
req = json.dumps(kwargs, cls=ComplexEncoder)
138-
self.socket.send(req)
138+
self.socket.send_string(req)
139139
resp = self.socket.recv_string()
140140
return resp
141141

142142
# Stop the Matlab server
143143
def stop(self):
144144
# Matlab should respond with "exit" if successful
145145
if self._response(cmd='exit') == "exit":
146-
print "%s closed" % self._program_name()
146+
print("%s closed" % self._program_name())
147147

148148
self.started = False
149149
return True
@@ -155,7 +155,7 @@ def is_connected(self):
155155
return False
156156

157157
req = json.dumps(dict(cmd="connect"), cls=ComplexEncoder)
158-
self.socket.send(req)
158+
self.socket.send_string(req)
159159

160160
start_time = time.time()
161161
while True:
@@ -166,7 +166,7 @@ def is_connected(self):
166166
sys.stdout.write('.')
167167
time.sleep(1)
168168
if time.time() - start_time > self.maxtime:
169-
print "%s session timed out after %d seconds" % (self._program_name(), self.maxtime)
169+
print("%s session timed out after %d seconds" % (self._program_name(), self.maxtime))
170170
return False
171171

172172
def is_function_processor_working(self):

pymatbridge/tests/test_json.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pymatbridge as pymat
2+
from pymatbridge.compat import unichr
23
import numpy.testing as npt
34
import test_utils as tu
45

pymatbridge/tests/test_run_code.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pymatbridge as pymat
2+
from pymatbridge.compat import text_type
23
import numpy.testing as npt
34
import test_utils as tu
45

@@ -38,19 +39,19 @@ def test_basic_operation(self):
3839
result_division = self.mlab.run_code("c = a / b")['content']['stdout']
3940

4041
if tu.on_octave():
41-
npt.assert_equal(result_assignment_a, unicode("a = 21.235\n"))
42-
npt.assert_equal(result_assignment_b, unicode("b = 347.75\n"))
43-
npt.assert_equal(result_sum, unicode("ans = 368.98\n"))
44-
npt.assert_equal(result_diff, unicode("ans = -326.51\n"))
45-
npt.assert_equal(result_product, unicode("ans = 7384.2\n"))
46-
npt.assert_equal(result_division, unicode("c = 0.061063\n"))
42+
npt.assert_equal(result_assignment_a, text_type("a = 21.235\n"))
43+
npt.assert_equal(result_assignment_b, text_type("b = 347.75\n"))
44+
npt.assert_equal(result_sum, text_type("ans = 368.98\n"))
45+
npt.assert_equal(result_diff, text_type("ans = -326.51\n"))
46+
npt.assert_equal(result_product, text_type("ans = 7384.2\n"))
47+
npt.assert_equal(result_division, text_type("c = 0.061063\n"))
4748
else:
48-
npt.assert_equal(result_assignment_a, unicode("\na =\n\n 21.2345\n\n"))
49-
npt.assert_equal(result_assignment_b, unicode("\nb =\n\n 347.7450\n\n"))
50-
npt.assert_equal(result_sum, unicode("\nans =\n\n 368.9795\n\n"))
51-
npt.assert_equal(result_diff, unicode("\nans =\n\n -326.5105\n\n"))
52-
npt.assert_equal(result_product, unicode("\nans =\n\n 7.3842e+03\n\n"))
53-
npt.assert_equal(result_division, unicode("\nc =\n\n 0.0611\n\n"))
49+
npt.assert_equal(result_assignment_a, text_type("\na =\n\n 21.2345\n\n"))
50+
npt.assert_equal(result_assignment_b, text_type("\nb =\n\n 347.7450\n\n"))
51+
npt.assert_equal(result_sum, text_type("\nans =\n\n 368.9795\n\n"))
52+
npt.assert_equal(result_diff, text_type("\nans =\n\n -326.5105\n\n"))
53+
npt.assert_equal(result_product, text_type("\nans =\n\n 7.3842e+03\n\n"))
54+
npt.assert_equal(result_division, text_type("\nc =\n\n 0.0611\n\n"))
5455

5556
# Put in some undefined code
5657
def test_undefined_code(self):

0 commit comments

Comments
 (0)