comparison @pyobject/pyobject.m @ 330:5c63651b0ec4

Merged in genuinelucifer/pytave_main (pull request #40) Add isequal method to allow comparing pyobjects (fixes issue #53)
author Mike Miller <mtmiller@octave.org>
date Sat, 13 Aug 2016 23:59:49 -0700
parents 15c20ab4b80a 778c91283a73
children fe6b9e618c98
comparison
equal deleted inserted replaced
328:5782d7932529 330:5c63651b0ec4
215 r = numel (x); 215 r = numel (x);
216 else 216 else
217 r = size (x, index_pos); 217 r = size (x, index_pos);
218 endif 218 endif
219 endfunction 219 endfunction
220
221 function res = isequal (varargin)
222 assert (nargin >= 2)
223 res = all (strcmp ("pyobject", cellfun ("class", varargin, "uniformoutput", false)));
224 for i = 2:nargin
225 if (! res)
226 return;
227 endif
228 res = res && pycall("bool", pycall ("operator.eq", varargin{1}, varargin{i}));
229 endfor
230 endfunction
220 endmethods 231 endmethods
221 endclassdef 232 endclassdef
222 233
223 234
224 %!test 235 %!test
327 %!assert (double (pycall ("array.array", "d", {31, 32, 33, 34})), [31, 32, 33, 34]) 338 %!assert (double (pycall ("array.array", "d", {31, 32, 33, 34})), [31, 32, 33, 34])
328 339
329 %!error double (pyobject ("this is not a number")) 340 %!error double (pyobject ("this is not a number"))
330 %!error double (pyobject ()) 341 %!error double (pyobject ())
331 %!error double (pyeval ("[1, 2, 3]")) 342 %!error double (pyeval ("[1, 2, 3]"))
343
344 %!error (isequal (pyobject ()))
345 %!assert (! isequal (pyobject (1.2), 1.2))
346 %!assert (isequal (pyobject ("a string"), pyobject ("a string")))
347 %!assert (isequal (pyeval ("None"), pyeval ("None")))
348 %!assert (! isequal (pyeval ("None"), pyeval ("None"), pyobject (10)))
349 %!assert (isequal (pyobject (10), pyobject (10.0), pyobject (int8 (10))))
350
351 %!test
352 %! A = pyeval ("[1, 2, 3]");
353 %! B = pycall ("list", {1, 2, 3});
354 %! assert (isequal (A, B))