# HG changeset patch # User Mike Miller # Date 1471157989 25200 # Node ID 5c63651b0ec41d4875b5f934966975f360142c7f # Parent 5782d793252961f23264e2cda75c2ced2efd8259# Parent 778c91283a736cc3b7a4d292f29fff239f667007 Merged in genuinelucifer/pytave_main (pull request #40) Add isequal method to allow comparing pyobjects (fixes issue #53) diff -r 5782d7932529 -r 5c63651b0ec4 @pyobject/pyobject.m --- a/@pyobject/pyobject.m Sat Aug 13 23:52:57 2016 -0700 +++ b/@pyobject/pyobject.m Sat Aug 13 23:59:49 2016 -0700 @@ -217,6 +217,17 @@ r = size (x, index_pos); endif endfunction + + function res = isequal (varargin) + assert (nargin >= 2) + res = all (strcmp ("pyobject", cellfun ("class", varargin, "uniformoutput", false))); + for i = 2:nargin + if (! res) + return; + endif + res = res && pycall("bool", pycall ("operator.eq", varargin{1}, varargin{i})); + endfor + endfunction endmethods endclassdef @@ -329,3 +340,15 @@ %!error double (pyobject ("this is not a number")) %!error double (pyobject ()) %!error double (pyeval ("[1, 2, 3]")) + +%!error (isequal (pyobject ())) +%!assert (! isequal (pyobject (1.2), 1.2)) +%!assert (isequal (pyobject ("a string"), pyobject ("a string"))) +%!assert (isequal (pyeval ("None"), pyeval ("None"))) +%!assert (! isequal (pyeval ("None"), pyeval ("None"), pyobject (10))) +%!assert (isequal (pyobject (10), pyobject (10.0), pyobject (int8 (10)))) + +%!test +%! A = pyeval ("[1, 2, 3]"); +%! B = pycall ("list", {1, 2, 3}); +%! assert (isequal (A, B))