comparison @pyobject/pyobject.m @ 366:5c900b8383c4

pyobject.isa: work with a cell array of type names * @pyobject/pyobject.m (pyobject.isa): Accept a cell array of type names for compatibility with Octave's default isa function.
author Mike Miller <mtmiller@octave.org>
date Thu, 25 Aug 2016 14:57:29 -0700
parents 087e7bc3697f
children 9d7188514f2c
comparison
equal deleted inserted replaced
365:087e7bc3697f 366:5c900b8383c4
117 y = __py_int64_scalar_value__ (x); 117 y = __py_int64_scalar_value__ (x);
118 endfunction 118 endfunction
119 119
120 function y = isa (x, typestr) 120 function y = isa (x, typestr)
121 assert (nargin == 2); 121 assert (nargin == 2);
122 assert (ischar (typestr)); 122 assert (ischar (typestr) || iscellstr (typestr));
123 if ((numel (typestr) > 3) && (typestr(1:3) == "py.")) 123
124 y = __py_isinstance__ (x, typestr); 124 if (ischar (typestr))
125 else 125 typestr = { typestr };
126 y = builtin ("isa", x, typestr); 126 endif
127 endif 127
128 y = false (size (typestr));
129
130 for i = 1:numel (typestr)
131 if ((numel (typestr{i}) > 3) && (typestr{i}(1:3) == "py."))
132 y(i) = __py_isinstance__ (x, typestr{i});
133 else
134 y(i) = builtin ("isa", x, typestr{i});
135 endif
136 endfor
128 endfunction 137 endfunction
129 138
130 function y = struct (x) 139 function y = struct (x)
131 y = __py_struct_from_dict__ (x); 140 y = __py_struct_from_dict__ (x);
132 endfunction 141 endfunction
344 %!assert (isa (pyobject (true), "py.bool")) 353 %!assert (isa (pyobject (true), "py.bool"))
345 %!assert (isa (pyobject ("a string"), "py.str")) 354 %!assert (isa (pyobject ("a string"), "py.str"))
346 %!assert (isa (pyobject (struct ()), "py.dict")) 355 %!assert (isa (pyobject (struct ()), "py.dict"))
347 %!assert (isa (pyobject (cell ()), "py.tuple")) 356 %!assert (isa (pyobject (cell ()), "py.tuple"))
348 %!assert (isa (pyobject ([1, 2, 3, 4]), "py.array.array")) 357 %!assert (isa (pyobject ([1, 2, 3, 4]), "py.array.array"))
358 %!assert (all (isa (pyobject (0), {"pyobject", "py.float", "py.numbers.Number"})))
349 359
350 ## Test conversion method pyobject.int64 360 ## Test conversion method pyobject.int64
351 %!assert (int64 (pyobject (int8 (0))), int64 (0)) 361 %!assert (int64 (pyobject (int8 (0))), int64 (0))
352 %!assert (int64 (pyobject (int64 (42))), int64 (42)) 362 %!assert (int64 (pyobject (int64 (42))), int64 (42))
353 %!assert (int64 (pyobject (intmax ("int64"))), intmax ("int64")) 363 %!assert (int64 (pyobject (intmax ("int64"))), intmax ("int64"))