From 45cbed0a2ea8581440d6e94de483cd40a8161e1c Mon Sep 17 00:00:00 2001 From: Ismail Badawi Date: Mon, 11 Aug 2014 11:45:55 -0400 Subject: [PATCH] Make codebase python3 compatible. --- pymatbridge/compat.py | 10 ++++++++++ pymatbridge/matlab_magic.py | 6 +++--- pymatbridge/pymatbridge.py | 16 ++++++++-------- pymatbridge/tests/test_json.py | 1 + pymatbridge/tests/test_run_code.py | 25 +++++++++++++------------ 5 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 pymatbridge/compat.py diff --git a/pymatbridge/compat.py b/pymatbridge/compat.py new file mode 100644 index 0000000..bca21fc --- /dev/null +++ b/pymatbridge/compat.py @@ -0,0 +1,10 @@ +import sys + +PY3 = sys.version_info[0] == 3 + +if PY3: + text_type = str + unichr = chr +else: + text_type = unicode + unichr = unichr diff --git a/pymatbridge/matlab_magic.py b/pymatbridge/matlab_magic.py index b7d7f92..4bc2eae 100644 --- a/pymatbridge/matlab_magic.py +++ b/pymatbridge/matlab_magic.py @@ -17,7 +17,6 @@ from glob import glob from shutil import rmtree from getopt import getopt -from urllib2 import URLError import numpy as np try: @@ -37,6 +36,7 @@ from IPython.utils.py3compat import str_to_unicode, unicode_to_str, PY3 import pymatbridge as pymat +from .compat import text_type class MatlabInterperterError(RuntimeError): @@ -56,7 +56,7 @@ def __unicode__(self): __str__ = __unicode__ else: def __str__(self): - return unicode_to_str(unicode(self), 'utf-8') + return unicode_to_str(text_type(self), 'utf-8') @@ -210,7 +210,7 @@ def matlab(self, line, cell=None, local_ns=None): else: e_s = "There was an error running the code:\n %s"%code result_dict = self.eval(code) - except URLError: + except: e_s += "\n-----------------------" e_s += "\nAre you sure Matlab is started?" raise RuntimeError(e_s) diff --git a/pymatbridge/pymatbridge.py b/pymatbridge/pymatbridge.py index 8cd7fb4..1c5a1fb 100644 --- a/pymatbridge/pymatbridge.py +++ b/pymatbridge/pymatbridge.py @@ -114,8 +114,8 @@ def _run_server(self): # Start server/client session and make the connection def start(self): # Start the MATLAB server in a new process - print "Starting %s on ZMQ socket %s" % (self._program_name(), self.socket_addr) - print "Send 'exit' command to kill the server" + print("Starting %s on ZMQ socket %s" % (self._program_name(), self.socket_addr)) + print("Send 'exit' command to kill the server") self._run_server() # Start the client @@ -127,15 +127,15 @@ def start(self): # Test if connection is established if self.is_connected(): - print "%s started and connected!" % self._program_name() + print("%s started and connected!" % self._program_name()) return True else: - print "%s failed to start" % self._program_name() + print("%s failed to start" % self._program_name()) return False def _response(self, **kwargs): req = json.dumps(kwargs, cls=ComplexEncoder) - self.socket.send(req) + self.socket.send_string(req) resp = self.socket.recv_string() return resp @@ -143,7 +143,7 @@ def _response(self, **kwargs): def stop(self): # Matlab should respond with "exit" if successful if self._response(cmd='exit') == "exit": - print "%s closed" % self._program_name() + print("%s closed" % self._program_name()) self.started = False return True @@ -155,7 +155,7 @@ def is_connected(self): return False req = json.dumps(dict(cmd="connect"), cls=ComplexEncoder) - self.socket.send(req) + self.socket.send_string(req) start_time = time.time() while True: @@ -166,7 +166,7 @@ def is_connected(self): sys.stdout.write('.') time.sleep(1) if time.time() - start_time > self.maxtime: - print "%s session timed out after %d seconds" % (self._program_name(), self.maxtime) + print("%s session timed out after %d seconds" % (self._program_name(), self.maxtime)) return False def is_function_processor_working(self): diff --git a/pymatbridge/tests/test_json.py b/pymatbridge/tests/test_json.py index 86973b8..4028aff 100644 --- a/pymatbridge/tests/test_json.py +++ b/pymatbridge/tests/test_json.py @@ -1,4 +1,5 @@ import pymatbridge as pymat +from pymatbridge.compat import unichr import numpy.testing as npt import test_utils as tu diff --git a/pymatbridge/tests/test_run_code.py b/pymatbridge/tests/test_run_code.py index 2693ce1..6f929bb 100644 --- a/pymatbridge/tests/test_run_code.py +++ b/pymatbridge/tests/test_run_code.py @@ -1,4 +1,5 @@ import pymatbridge as pymat +from pymatbridge.compat import text_type import numpy.testing as npt import test_utils as tu @@ -38,19 +39,19 @@ def test_basic_operation(self): result_division = self.mlab.run_code("c = a / b")['content']['stdout'] if tu.on_octave(): - npt.assert_equal(result_assignment_a, unicode("a = 21.235\n")) - npt.assert_equal(result_assignment_b, unicode("b = 347.75\n")) - npt.assert_equal(result_sum, unicode("ans = 368.98\n")) - npt.assert_equal(result_diff, unicode("ans = -326.51\n")) - npt.assert_equal(result_product, unicode("ans = 7384.2\n")) - npt.assert_equal(result_division, unicode("c = 0.061063\n")) + npt.assert_equal(result_assignment_a, text_type("a = 21.235\n")) + npt.assert_equal(result_assignment_b, text_type("b = 347.75\n")) + npt.assert_equal(result_sum, text_type("ans = 368.98\n")) + npt.assert_equal(result_diff, text_type("ans = -326.51\n")) + npt.assert_equal(result_product, text_type("ans = 7384.2\n")) + npt.assert_equal(result_division, text_type("c = 0.061063\n")) else: - npt.assert_equal(result_assignment_a, unicode("\na =\n\n 21.2345\n\n")) - npt.assert_equal(result_assignment_b, unicode("\nb =\n\n 347.7450\n\n")) - npt.assert_equal(result_sum, unicode("\nans =\n\n 368.9795\n\n")) - npt.assert_equal(result_diff, unicode("\nans =\n\n -326.5105\n\n")) - npt.assert_equal(result_product, unicode("\nans =\n\n 7.3842e+03\n\n")) - npt.assert_equal(result_division, unicode("\nc =\n\n 0.0611\n\n")) + npt.assert_equal(result_assignment_a, text_type("\na =\n\n 21.2345\n\n")) + npt.assert_equal(result_assignment_b, text_type("\nb =\n\n 347.7450\n\n")) + npt.assert_equal(result_sum, text_type("\nans =\n\n 368.9795\n\n")) + npt.assert_equal(result_diff, text_type("\nans =\n\n -326.5105\n\n")) + npt.assert_equal(result_product, text_type("\nans =\n\n 7.3842e+03\n\n")) + npt.assert_equal(result_division, text_type("\nc =\n\n 0.0611\n\n")) # Put in some undefined code def test_undefined_code(self):