annotate @pyobject/subsref.m @ 275:24faaa3cf5e5

Merged in macdonald/pytave (pull request #23) [wip] partial support for multiple outputs
author Mike Miller <mike@mtmxr.com>
date Fri, 29 Jul 2016 14:39:27 -0700
parents 0a8983533192 decd3bd7da7e
children f5282c87e5d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
1 ## Copyright (C) 2016 Colin B. Macdonald
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
2 ##
246
4acc64f89d67 maint: fix copyright header comment blocks
Mike Miller <mtmiller@octave.org>
parents: 245
diff changeset
3 ## This file is part of Pytave
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
4 ##
246
4acc64f89d67 maint: fix copyright header comment blocks
Mike Miller <mtmiller@octave.org>
parents: 245
diff changeset
5 ## Pytave is free software; you can redistribute it and/or modify
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
7 ## by the Free Software Foundation; either version 3 of the License,
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
8 ## or (at your option) any later version.
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
9 ##
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
10 ## This software is distributed in the hope that it will be useful,
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
12 ## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
13 ## the GNU General Public License for more details.
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
14 ##
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
16 ## License along with this software; see the file COPYING.
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
17 ## If not, see <http://www.gnu.org/licenses/>.
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
18
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
19 ## -*- texinfo -*-
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
20 ## @documentencoding UTF-8
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
21 ## @defop Method @@pyobject subsref (@var{x}, @var{idx})
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
22 ## @defopx Operator @@pyobject {@var{x}.@var{property}} {}
238
c64435b14a6e Minor doc fix
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
23 ## @defopx Operator @@pyobject {@var{x}.@var{method}(@var{a}, @dots{})} {}
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
24 ## @defopx Operator @@pyobject {@var{x}@{@var{i}@}} {}
238
c64435b14a6e Minor doc fix
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
25 ## @defopx Operator @@pyobject {@var{x}@{@var{i}, @var{j}, @dots{}@}} {}
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
26 ## @defopx Operator @@pyobject {@var{x}(@var{a})} {}
238
c64435b14a6e Minor doc fix
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
27 ## @defopx Operator @@pyobject {@var{x}(@var{a}, @var{b}, @dots{})} {}
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
28 ## Call methods and access properties of a Python object.
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
29 ##
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
30 ##
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
31 ## @seealso{@@pyobject/subsasgn}
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
32 ## @end defop
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
33
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
34
244
46382ade33e4 maint: minor cosmetic fixes to cset 708715efa18e
Mike Miller <mtmiller@octave.org>
parents: 243
diff changeset
35 function varargout = subsref (x, idx)
240
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
36
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
37 t = idx(1);
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
38 switch t.type
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
39 case "()"
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
40 r = pycall (x, t.subs{:});
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
41
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
42 case "."
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
43 assert (ischar (t.subs))
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
44 r = pycall ("getattr", x, t.subs);
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
45
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
46 case "{}"
260
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
47 ## Subtract one from index: do this for lists, numpy arrays, etc
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
48 pyexec ("import collections")
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
49 pyexec ("import numpy")
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
50 x_is_list = pycall (pyeval (
262
0a8983533192 * @pyobject/subsref.m: Fix small lambda syntax error with Python 3.
Mike Miller <mtmiller@octave.org>
parents: 260
diff changeset
51 "lambda x: isinstance(x, (collections.Sequence, numpy.ndarray))"),
260
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
52 x);
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
53 for i = 1:length(t.subs)
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
54 j = t.subs{i};
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
55 if (isindex (j) && isnumeric (j) && x_is_list)
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
56 t.subs{i} = cast (j, class (sizemax ())) - 1;
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
57 endif
240
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
58 endfor
260
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
59
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
60 if (isscalar (t.subs))
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
61 ind = t.subs{1};
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
62 else
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
63 ## XXX: after #26, #27, I think its just:
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
64 #ind = pycall ("tuple", t.subs);
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
65 pyexec (["global _temp\n" ...
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
66 "def pystoretemp(x):\n" ...
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
67 " global _temp\n" ...
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
68 " _temp = x"]);
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
69 pycall ("pystoretemp", t.subs);
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
70 pyexec ("_temp = tuple(_temp[0])");
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
71 ind = pyobject.fromPythonVarName ("_temp");
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
72 endif
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
73 gi = pycall ("getattr", x, "__getitem__"); # x.__getitem__
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
74 r = pycall (gi, ind);
240
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
75
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
76 otherwise
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
77 t
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
78 error ("@pyobject/subsref: not implemented")
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
79 endswitch
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
80
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
81 ## deal with additional indexing (might be recursive)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
82 if (length (idx) > 1)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
83 r = subsref (r, idx(2:end));
245
72a2f508f2d8 maint: Use Octave keywords for ending code blocks
Mike Miller <mtmiller@octave.org>
parents: 244
diff changeset
84 endif
240
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
85
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
86 ## unpack results, ensure "ans" works (see also pycall)
248
2e4c9ce0c83c maint: prefer Octave lowercase naming convention to camelCase
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
87 is_none = pyeval ("lambda x: x is None");
274
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
88 if (nargout == 0 && ! pycall (is_none, r))
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
89 varargout{1} = r;
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
90 elseif (nargout == 1)
240
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
91 varargout{1} = r;
274
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
92 elseif (nargout >= 2)
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
93 assert (length (r) == nargout, ...
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
94 "pyobject/subsref: number of outputs must match")
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
95 [varargout{1:nargout}] = r{1:nargout};
245
72a2f508f2d8 maint: Use Octave keywords for ending code blocks
Mike Miller <mtmiller@octave.org>
parents: 244
diff changeset
96 endif
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
97 endfunction
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
98
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
99
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
100 %!test
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
101 %! % list indexing
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
102 %! pyexec ("L = [10, 20]")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
103 %! L = pyobject.fromPythonVarName ("L");
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
104 %! assert (L{1}, 10)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
105 %! assert (L{2}, 20)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
106
260
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
107 %!xtest
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
108 %! % list indexing, slice
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
109 %! pyexec ("L = [10, 20, [30, 40]]")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
110 %! L = pyobject.fromPythonVarName ("L");
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
111 %! L2 = L{:};
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
112 %! assert (L2{1}, 10)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
113 %! assert (L2{2}, 20)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
114 %! assert (L2{3}{1}, 30)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
115 %! assert (L2{3}{2}, 40)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
116
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
117 %!test
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
118 %! % list indexing, nested list
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
119 %! pyexec ("L = [1, 2, [10, 11, 12]]")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
120 %! L = pyobject.fromPythonVarName ("L");
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
121 %! assert (L{2}, 2)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
122 %! assert (L{3}{1}, 10)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
123 %! assert (L{3}{3}, 12)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
124
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
125 %!test
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
126 %! % 2D array indexing
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
127 %! pyexec ("import numpy")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
128 %! pyexec ("A = numpy.array([[1, 2], [3, 4]])")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
129 %! A = pyobject.fromPythonVarName ("A");
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
130 %! assert (A{1, 1}, 1)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
131 %! assert (A{2, 1}, 3)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
132 %! assert (A{1, 2}, 2)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
133
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
134 %!test
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
135 %! % dict: str key access
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
136 %! pyexec ("d = {'one':1, 5:5, 6:6}")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
137 %! d = pyobject.fromPythonVarName ("d");
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
138 %! assert (d{"one"}, 1)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
139
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
140 %!test
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
141 %! % dict: integer key access
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
142 %! pyexec ("d = {5:42, 6:42}")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
143 %! d = pyobject.fromPythonVarName ("d");
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
144 %! assert (d{6}, 42)
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
145
260
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
146 %!test
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
147 %! % dict: integer key should not subtract one
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
148 %! pyexec ("d = {5:40, 6:42}")
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
149 %! d = pyobject.fromPythonVarName ("d");
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
150 %! assert (d{6}, 42)
240
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
151
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
152 %!test
260
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
153 %! % dict: floating point keys should work
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
154 %! pyexec ("d = {5.5:'ok'}")
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
155 %! d = pyobject.fromPythonVarName ("d");
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
156 %! assert (d{5.5}, "ok")
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
157
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
158 %!test
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
159 %! % dict: make sure key ":" doesn't break anything
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
160 %! pyexec ("d = {'a':1, ':':2}")
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
161 %! d = pyobject.fromPythonVarName ("d");
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
162 %! assert (d{'a'}, 1)
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
163 %! assert (d{':'}, 2)
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
164
daf1aff501f1 indexing: fix off-by-one with dicts (fixes issue #10)
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
165 %!test
240
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
166 %! % method call with args
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
167 %! s = pyeval ("set({1, 2})");
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
168 %! s.add (42)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
169 %! assert (length (s) == 3)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
170
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
171 %!test
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
172 %! % get a callable
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
173 %! s = pyeval ("set({1, 2})");
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
174 %! sa = s.add;
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
175 %! assert (isa (sa, "pyobject"))
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
176 %! % and then call it
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
177 %! sa (42)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
178 %! assert (length (s) == 3)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
179
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
180 %!test
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
181 %! % callable can return something
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
182 %! s = pyeval ("set({1, 2})");
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
183 %! v = s.pop ();
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
184 %! assert (length (s) == 1)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
185 %! assert (v == 1 || v == 2)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
186
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
187 %!test
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
188 %! % chain
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
189 %! pyexec ("import sys")
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
190 %! s = pyeval ("set({sys})");
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
191 %! ver = s.pop ().version;
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
192 %! assert (ischar (ver))
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
193
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
194 %!test
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
195 %! % don't set "ans" if no return value
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
196 %! s = pyeval ("set({1, 2})");
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
197 %! sa = s.add;
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
198 %! clear ans
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
199 %! sa (42)
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
200 %! assert (! exist ("ans", "var"))
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
201
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
202 %!test
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
203 %! % *do* set "ans" if return value
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
204 %! s = pyeval ("set({1, 2})");
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
205 %! clear ans
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
206 %! s.pop ();
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
207 %! assert (exist ("ans", "var"))
708715efa18e pyobject: support callables with arguments (fixes issue #18)
Colin Macdonald <cbm@m.fsf.org>
parents: 227
diff changeset
208 %! assert (length (s) == 1)
274
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
209
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
210 %!test
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
211 %! % multiple return values: can get all of them
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
212 %! f = pyeval ("lambda: (1, 2, 3)");
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
213 %! a = f ();
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
214 %! assert (length (a) == 3)
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
215
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
216 %!test
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
217 %! % multiple return values: separate them
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
218 %! f = pyeval ("lambda: (1, 2, 3)");
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
219 %! [a, b, c] = f ();
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
220 %! assert (a, 1)
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
221 %! assert (b, 2)
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
222 %! assert (c, 3)
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
223
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
224 %!test
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
225 %! % multiple return values: set ans
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
226 %! f = pyeval ("lambda: (1, 2, 3)");
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
227 %! f ();
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
228 %! assert (length (ans) == 3)
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
229
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
230 %!error <outputs must match>
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
231 %! % multiple return values: too many outputs
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
232 %! f = pyeval ("lambda: (1, 2)");
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
233 %! [a, b, c] = f ();
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
234
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
235 %!error <outputs must match>
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
236 %! % multiple return values: not enough outputs
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
237 %! f = pyeval ("lambda: (1, 2, 3)");
decd3bd7da7e Partial support for multiple outputs from callables
Colin Macdonald <cbm@m.fsf.org>
parents: 248
diff changeset
238 %! [a, b] = f ();