Mercurial > pytave
changeset 329:778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
* @pyobject/pyobject.m: Add an 'isequal' method to fix a BIST test and some
more tests.
author | Abhinav Tripathi <genuinelucifer@gmail.com> |
---|---|
date | Fri, 12 Aug 2016 05:45:06 -0700 |
parents | 2b2460295218 |
children | 5c63651b0ec4 |
files | @pyobject/pyobject.m |
diffstat | 1 files changed, 23 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/@pyobject/pyobject.m Fri Aug 12 00:57:46 2016 -0700 +++ b/@pyobject/pyobject.m Fri Aug 12 05:45:06 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))