Mercurial > pytave
comparison @pyobject/cell.m @ 321:3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
* @pyobject/cell.m: Rewrite, ensuring return value is always a cell array
of the correct size. Add more %!tests.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Thu, 11 Aug 2016 22:35:19 -0700 |
parents | de98627cf8ae |
children | 15c20ab4b80a |
comparison
equal
deleted
inserted
replaced
320:c2aa34730dc9 | 321:3afd890fff6d |
---|---|
1 ## Copyright (C) 2016 Colin B. Macdonald | 1 ## Copyright (C) 2016 Colin B. Macdonald |
2 ## Copyright (C) 2016 Mike Miller | |
2 ## | 3 ## |
3 ## This file is part of Pytave | 4 ## This file is part of Pytave |
4 ## | 5 ## |
5 ## Pytave is free software; you can redistribute it and/or modify | 6 ## Pytave is free software; you can redistribute it and/or modify |
6 ## it under the terms of the GNU General Public License as published | 7 ## it under the terms of the GNU General Public License as published |
17 ## If not, see <http://www.gnu.org/licenses/>. | 18 ## If not, see <http://www.gnu.org/licenses/>. |
18 | 19 |
19 ## -*- texinfo -*- | 20 ## -*- texinfo -*- |
20 ## @documentencoding UTF-8 | 21 ## @documentencoding UTF-8 |
21 ## @defmethod @@pyobject cell (@var{x}) | 22 ## @defmethod @@pyobject cell (@var{x}) |
22 ## Convert an iterable Python object to a cell array. | 23 ## Convert a Python list or other object implementing the Sequence protocol |
24 ## to a cell array. | |
23 ## | 25 ## |
24 ## For example, by default Python lists are not automatically | 26 ## For example, by default Python lists are not automatically |
25 ## converted into native Octave objects: | 27 ## converted into native Octave objects: |
26 ## @example | 28 ## @example |
27 ## @group | 29 ## @group |
66 ## @seealso{cell2mat} | 68 ## @seealso{cell2mat} |
67 ## @end defmethod | 69 ## @end defmethod |
68 | 70 |
69 | 71 |
70 function c = cell (x) | 72 function c = cell (x) |
71 ## FIXME: subsref should take care of this case | 73 ## FIXME: when subsref returns the right number of output args, this can |
72 if (length (x) == 0) | 74 ## simply be "c = {x{:}}" |
73 c = cell (0, 1); | 75 n = length (x); |
74 return | 76 c = cell (1, n); |
77 if (n > 0) | |
78 [c{:}] = subsref (x, struct ("type", "{}", "subs", {{":"}})); | |
75 endif | 79 endif |
76 c = subsref (x, struct ("type", "{}", "subs", {{":"}})); | |
77 endfunction | 80 endfunction |
78 | 81 |
79 | 82 |
80 %!test | 83 %!assert (cell (pyeval ("[]")), cell (1, 0)) |
81 %! L = pyeval ("(1, 2, 3)"); | 84 %!assert (cell (pyeval ("[1]")), {1}) |
82 %! C = cell (L); | 85 %!assert (cell (pyeval ("[1, 2, 3]")), {1, 2, 3}) |
83 %! assert (iscell (C)) | 86 %!assert (cell (pyeval ("(1, 2, 3)")), {1, 2, 3}) |
84 %! assert (C, {1, 2, 3}) | 87 %!assert (cell (pyobject ("asdf")), {"a", "s", "d", "f"}) |
88 %!assert (cell (pyeval ("range(10)")), num2cell (0:9)) | |
89 | |
90 %!error cell (pyobject ()) | |
91 %!error cell (pyeval ("None")) | |
92 %!error cell (pyobject (1)) |