Mercurial > pytave
annotate @pyobject/cell.m @ 365:087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
* python_to_octave.cc (pytave::pyobj_to_octvalue): Drop conversion of bytes and
unicode objects.
* __py_struct_from_dict__.cc (F__py_string_value__): New function.
* @pyobject/char.m: Define outside class definition, use __py_string_value__.
* @pyobject/pyobject.m: Delete previous pyobject.char definition.
* @pyobject/methods.m: Apply char conversion to __name__ attribute.
* @py/py.m, @pyobject/cell.m, @pyobject/dummy.m, @pyobject/subsasgn.m,
@pyobject/subsref.m, pyargs.m, pycall.cc, pyeval.cc: Adapt examples and tests
to changes.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Thu, 25 Aug 2016 14:06:56 -0700 |
parents | 15c20ab4b80a |
children | 750a86973625 |
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']") |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
31 ## @result{} L = [pyobject ...] |
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 |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
40 ## C = cell (L) |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
41 ## @result{} C = |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
42 ## @{ |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
43 ## [1,1] = 10 |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
44 ## [1,2] = 20 |
365
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
45 ## = [pyobject ...] |
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
46 ## |
087e7bc3697f
Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents:
327
diff
changeset
|
47 ## hello |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
48 ## @} |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
49 ## @end group |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
50 ## @end example |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
51 ## |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
52 ## 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
|
53 ## @example |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
54 ## @group |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
55 ## 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
|
56 ## C = cell (L) |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
57 ## @result{} C = |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
58 ## @{ |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
59 ## [1,1] = 10 |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
60 ## [1,2] = 20 |
302
de98627cf8ae
Minor fixes for failing doctests
Colin Macdonald <cbm@m.fsf.org>
parents:
298
diff
changeset
|
61 ## = [pyobject ...] |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
62 ## |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
63 ## [33.0, 44.0] |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
64 ## |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
65 ## [1,4] = 50 |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
66 ## @} |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
67 ## @end group |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
68 ## @end example |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
69 ## |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
70 ## @seealso{cell2mat} |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
71 ## @end defmethod |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
72 |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
73 |
298
df1bddece9d5
Use subsref in "cell" method for casting to cell
Colin Macdonald <cbm@m.fsf.org>
parents:
293
diff
changeset
|
74 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
|
75 ## 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
|
76 ## 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
|
77 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
|
78 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
|
79 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
|
80 [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
|
81 endif |
293
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
82 endfunction |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
83 |
3f74eeb0a5da
pyobject: add method for converting pyobject to cell array
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
84 |
321
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
85 %!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
|
86 %!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
|
87 %!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
|
88 %!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
|
89 %!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
|
90 |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
91 %!test |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
321
diff
changeset
|
92 %! 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
|
93 %! 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
|
94 %! 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
|
95 |
3afd890fff6d
Rewrite pyobject.cell, always return a cell array of the right size
Mike Miller <mtmiller@octave.org>
parents:
302
diff
changeset
|
96 %!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
|
97 %!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
|
98 %!error cell (pyobject (1)) |