# HG changeset patch # User Colin Macdonald # Date 1469866682 25200 # Node ID c7e1cb5183721ba9aaaa07594181e0e75207e3b0 # Parent 3f74eeb0a5dac120dfca0a224cc56289959591f6 pyobject: add methods size, numel and end * @pyobject/pyobject.m: New methods. diff -r 3f74eeb0a5da -r c7e1cb518372 @pyobject/pyobject.m --- a/@pyobject/pyobject.m Sat Jul 30 01:14:00 2016 -0700 +++ b/@pyobject/pyobject.m Sat Jul 30 01:18:02 2016 -0700 @@ -170,6 +170,43 @@ len = 1; end_try_catch endfunction + + function sz = size (x, d) + assert (nargin <= 2) + try + idx = struct ("type", ".", "subs", "shape"); + sz = subsref (x, idx); + sz = cell2mat (cell (sz)); + catch + ## if it had no shape, make it a row vector + n = length (x); + sz = [1 n]; + end_try_catch + if (nargin > 1) + if (d > length (sz)) + ## standard Octave behaviour: do we really want this? + sz = 1; + else + sz = sz(d); + endif + endif + endfunction + + function n = numel (x) + assert (nargin == 1) + sz = size (x); + n = prod (sz); + endfunction + + function r = end (x, index_pos, num_indices) + assert (nargin == 3) + assert (isscalar (index_pos)) + if (num_indices == 1) + r = numel (x); + else + r = size (x, index_pos); + endif + endfunction endmethods endclassdef @@ -189,6 +226,32 @@ %! pyobj = pyeval ("sys"); %! assert (length (pyobj), 1) +%!assert (size (pyeval ("[10, 20, 30]")), [1 3]) +%!assert (size (pyeval ("[10, 20, 30]"), 1), 1) +%!assert (size (pyeval ("[10, 20, 30]"), 2), 3) +%!assert (size (pyeval ("[10, 20, 30]"), 3), 1) + +%!assert (numel (pyeval ("[10, 20, 30]")), 3) + +%!test +%! L = pyeval ("[10, 20, 30]"); +%! assert (L{end}, 30) +%! assert (L{end-1}, 20) + +%!test +%! myrange = pyeval ( ... +%! "range if __import__('sys').hexversion >= 0x03000000 else xrange"); +%! R = pycall (myrange, int32 (5), int32 (10), int32 (2)); +%! assert (R{end}, 9) + +%!shared a +%! pyexec ("class _myclass(): shape = (3, 4, 5)") +%! a = pyeval ("_myclass()"); +%!assert (size (a), [3 4 5]) +%!assert (size (a, 3), 5) +%!assert (numel (a), 60) +%!shared + %!assert (char (pyeval ("None")), "None") %!assert (char (pyeval ("'this is a string'")), "this is a string") %!assert (char (pyeval ("[1, 2, 3, 4, 5]")), "[1, 2, 3, 4, 5]")