Mercurial > pytave
annotate @pyobject/cell.m @ 427:750a86973625
Update doctests for display changes
The output of display was recently improved by commit
24546b7021db2ab2553357f691450144c6c5ab50 but the doctests were
not updated.
* @pyobject/dummy.m, @pyobject/cell.m, @pyobject/display.m: update doctests.
* pyargs.m: update doctests.
author | Colin Macdonald <cbm@m.fsf.org> |
---|---|
date | Wed, 25 Oct 2017 13:16:53 -0700 |
parents | 087e7bc3697f |
children | 3af3665348a1 |
rev | line source |
---|---|
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
1 ## Copyright (C) 2016 Colin B. Macdonald |
321
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
2 ## Copyright (C) 2016 Mike Miller |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
3 ## |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
4 ## This file is part of Pytave |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
5 ## |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
6 ## Pytave is free software; you can redistribute it and/or modify |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
7 ## it under the terms of the GNU General Public License as published |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
8 ## by the Free Software Foundation; either version 3 of the License, |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
9 ## or (at your option) any later version. |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
10 ## |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
11 ## This software is distributed in the hope that it will be useful, |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
13 ## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
14 ## the GNU General Public License for more details. |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
15 ## |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
16 ## You should have received a copy of the GNU General Public |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
17 ## License along with this software; see the file COPYING. |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
18 ## If not, see <http://www.gnu.org/licenses/>. |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
19 |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
20 ## -*- texinfo -*- |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
21 ## @documentencoding UTF-8 |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
22 ## @defmethod @@pyobject cell (@var{x}) |
321
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
23 ## Convert a Python list or other object implementing the Sequence protocol |
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
24 ## to a cell array. |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
25 ## |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
26 ## For example, by default Python lists are not automatically |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
27 ## converted into native Octave objects: |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
28 ## @example |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
29 ## @group |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
30 ## L = pyeval ("[10.0, 20.0, 'hello']") |
427
750a86973625
Update doctests for display changes
Colin Macdonald <cbm@m.fsf.org>
parents:
365
diff
changeset
|
31 ## @result{} L = [Python object of type list] |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
32 ## |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
33 ## [10.0, 20.0, 'hello'] |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
34 ## @end group |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
35 ## @end example |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
36 ## |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
37 ## However, we can convert the list to a cell array: |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
38 ## @example |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
39 ## @group |
427
750a86973625
Update doctests for display changes
Colin Macdonald <cbm@m.fsf.org>
parents:
365
diff
changeset
|
40 ## @c TODO: display of cell array in flux: savannah.gnu.org/bugs/?50756 |
750a86973625
Update doctests for display changes
Colin Macdonald <cbm@m.fsf.org>
parents:
365
diff
changeset
|
41 ## @c doctest: +XFAIL |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
42 ## C = cell (L) |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
43 ## @result{} C = |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
44 ## @{ |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
45 ## [1,1] = 10 |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
46 ## [1,2] = 20 |
427
750a86973625
Update doctests for display changes
Colin Macdonald <cbm@m.fsf.org>
parents:
365
diff
changeset
|
47 ## [1,3] = [Python object of type str] |
365
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
48 ## |
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
49 ## hello |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
50 ## @} |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
51 ## @end group |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
52 ## @end example |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
53 ## |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
54 ## The conversion is not recursive, in the following sense: |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
55 ## @example |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
56 ## @group |
427
750a86973625
Update doctests for display changes
Colin Macdonald <cbm@m.fsf.org>
parents:
365
diff
changeset
|
57 ## @c TODO: display of cell array in flux: savannah.gnu.org/bugs/?50756 |
750a86973625
Update doctests for display changes
Colin Macdonald <cbm@m.fsf.org>
parents:
365
diff
changeset
|
58 ## @c doctest: +XFAIL |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
59 ## L = pyeval ("[10.0, 20.0, [33.0, 44.0], 50.0]"); |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
60 ## C = cell (L) |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
61 ## @result{} C = |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
62 ## @{ |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
63 ## [1,1] = 10 |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
64 ## [1,2] = 20 |
427
750a86973625
Update doctests for display changes
Colin Macdonald <cbm@m.fsf.org>
parents:
365
diff
changeset
|
65 ## [1,3] = [Python object of type list] |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
66 ## |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
67 ## [33.0, 44.0] |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
68 ## |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
69 ## [1,4] = 50 |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
70 ## @} |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
71 ## @end group |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
72 ## @end example |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
73 ## |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
74 ## @seealso{cell2mat} |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
75 ## @end defmethod |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
76 |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
77 |
298
df1bddece9d5
Use subsref in "cell" method for casting to cell
Colin Macdonald <cbm@m.fsf.org>
parents:
293
diff
changeset
|
78 function c = cell (x) |
321
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
79 ## FIXME: when subsref returns the right number of output args, this can |
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
80 ## simply be "c = {x{:}}" |
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
81 n = length (x); |
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
82 c = cell (1, n); |
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
83 if (n > 0) |
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
84 [c{:}] = subsref (x, struct ("type", "{}", "subs", {{":"}})); |
298
df1bddece9d5
Use subsref in "cell" method for casting to cell
Colin Macdonald <cbm@m.fsf.org>
parents:
293
diff
changeset
|
85 endif |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
86 endfunction |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
87 |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
88 |
321
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
89 %!assert (cell (pyeval ("[]")), cell (1, 0)) |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
90 %!assert (cell (pyeval ("[1.]")), {1}) |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
91 %!assert (cell (pyeval ("[1., 2., 3.]")), {1, 2, 3}) |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
92 %!assert (cell (pyeval ("(1., 2., 3.)")), {1, 2, 3}) |
365
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
93 %!assert (cellfun (@char, cell (pyobject ("asdf")), "uniformoutput", false), {"a", "s", "d", "f"}) |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
94 |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
95 %!test |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
96 %! c = cell (pyeval ("range(10)")); |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
97 %! c = cellfun (@(x) eval ("double (x)"), c, "uniformoutput", false); |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
98 %! assert (c, num2cell (0:9)) |
321
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
99 |
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
100 %!error cell (pyobject ()) |
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
101 %!error cell (pyeval ("None")) |
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
102 %!error cell (pyobject (1)) |