Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pymatbridge/compat.py
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions pymatbridge/matlab_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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')



Expand Down Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions pymatbridge/pymatbridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -127,23 +127,23 @@ 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

# Stop the Matlab server
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
Expand All @@ -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:
Expand All @@ -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):
Expand Down
1 change: 1 addition & 0 deletions pymatbridge/tests/test_json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pymatbridge as pymat
from pymatbridge.compat import unichr
import numpy.testing as npt
import test_utils as tu

Expand Down
25 changes: 13 additions & 12 deletions pymatbridge/tests/test_run_code.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pymatbridge as pymat
from pymatbridge.compat import text_type
import numpy.testing as npt
import test_utils as tu

Expand Down Expand Up @@ -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):
Expand Down