Mercurial > pytave
annotate @pyobject/subsasgn.m @ 266:b337b733d6f8
maint: fix copyright header, cast int, block endings
* @pyobject/subsasgn.m: Fixes from review.
author | Colin Macdonald <cbm@m.fsf.org> |
---|---|
date | Thu, 28 Jul 2016 00:12:03 -0700 |
parents | 07ca24d7627f |
children | c7a7d94a142f |
rev | line source |
---|---|
264
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
1 ## Copyright (C) 2016 Colin B. Macdonald |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
2 ## |
266
b337b733d6f8
maint: fix copyright header, cast int, block endings
Colin Macdonald <cbm@m.fsf.org>
parents:
265
diff
changeset
|
3 ## This file is part of Pytave. |
264
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
4 ## |
266
b337b733d6f8
maint: fix copyright header, cast int, block endings
Colin Macdonald <cbm@m.fsf.org>
parents:
265
diff
changeset
|
5 ## Pytave is free software; you can redistribute it and/or modify |
264
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
7 ## by the Free Software Foundation; either version 3 of the License, |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
8 ## or (at your option) any later version. |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
9 ## |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
10 ## This software is distributed in the hope that it will be useful, |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
12 ## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
13 ## the GNU General Public License for more details. |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
14 ## |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
15 ## You should have received a copy of the GNU General Public |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
16 ## License along with this software; see the file COPYING. |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
17 ## If not, see <http://www.gnu.org/licenses/>. |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
18 |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
19 ## -*- texinfo -*- |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
20 ## @documentencoding UTF-8 |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
21 ## @defop Method @@pyobject subsasgn (@var{x}, @var{idx}, @var{rhs}) |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
22 ## @defopx Operator @@pyobject {@var{x}.@var{property} =} {@var{rhs}} |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
23 ## @defopx Operator @@pyobject {@var{x}@{@var{i}@} =} {@var{rhs}} |
265
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
24 ## @defopx Operator @@pyobject {@var{x}@{@var{i}, @var{j}, @dots{}@} =} {@var{rhs}} |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
25 ## @defopx Operator @@pyobject {@var{x}@{@var{a}@} =} {@var{rhs}} |
264
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
26 ## Indexed assignment to Python objects. |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
27 ## |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
28 ## @seealso{@@pyobject/subsref} |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
29 ## @end defop |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
30 |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
31 function r = subsasgn(x, idx, rhs) |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
32 |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
33 switch idx.type |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
34 case "." |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
35 assert (ischar (idx.subs)) |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
36 pycall ("setattr", x, idx.subs, rhs); |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
37 r = x; |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
38 |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
39 case "{}" |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
40 ## XXX: doesn't support slices or anything like that yet |
265
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
41 |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
42 for i = 1:length (idx.subs) |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
43 j = idx.subs{i}; |
266
b337b733d6f8
maint: fix copyright header, cast int, block endings
Colin Macdonald <cbm@m.fsf.org>
parents:
265
diff
changeset
|
44 if (isindex (j) && isnumeric (j)) |
b337b733d6f8
maint: fix copyright header, cast int, block endings
Colin Macdonald <cbm@m.fsf.org>
parents:
265
diff
changeset
|
45 idx.subs{i} = cast (j, class (sizemax ())) - 1; |
b337b733d6f8
maint: fix copyright header, cast int, block endings
Colin Macdonald <cbm@m.fsf.org>
parents:
265
diff
changeset
|
46 endif |
b337b733d6f8
maint: fix copyright header, cast int, block endings
Colin Macdonald <cbm@m.fsf.org>
parents:
265
diff
changeset
|
47 endfor |
265
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
48 |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
49 if (isscalar (idx.subs)) |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
50 ind = idx.subs{1}; |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
51 else |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
52 error ("not implemented, waiting on #26, #27") |
266
b337b733d6f8
maint: fix copyright header, cast int, block endings
Colin Macdonald <cbm@m.fsf.org>
parents:
265
diff
changeset
|
53 endif |
265
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
54 |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
55 xsi = pycall ("getattr", x, "__setitem__"); # x.__setitem__ |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
56 pycall (xsi, ind, rhs); |
264
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
57 r = x; |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
58 |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
59 otherwise |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
60 idx |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
61 rhs |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
62 error ("@pyobject/subsasgn: not implemented") |
266
b337b733d6f8
maint: fix copyright header, cast int, block endings
Colin Macdonald <cbm@m.fsf.org>
parents:
265
diff
changeset
|
63 endswitch |
b337b733d6f8
maint: fix copyright header, cast int, block endings
Colin Macdonald <cbm@m.fsf.org>
parents:
265
diff
changeset
|
64 endfunction |
264
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
65 |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
66 |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
67 %!test |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
68 %! pyexec ("class MyClass: a = 1") |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
69 %! t = pyeval ("MyClass()"); |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
70 %! t.b = 6; |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
71 %! assert (t.b, 6) |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
72 |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
73 %!test |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
74 %! % list indexing |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
75 %! pyexec ("L = [10, 20]") |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
76 %! L = pyobject.fromPythonVarName ("L"); |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
77 %! L{2} = "Octave"; |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
78 %! assert (length (L) == 2) |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
79 %! assert (L{1}, 10) |
5a516155d730
pyobject: support indexed assignment (fixes issue #17)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
80 %! assert (L{2}, "Octave") |
265
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
81 |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
82 %!test |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
83 %! % dict assignment, adding new keys |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
84 %! pyexec ("d = dict()") |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
85 %! d = pyobject.fromPythonVarName ("d"); |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
86 %! d{"a"} = 3; |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
87 %! d{"b"} = 4; |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
88 %! assert (d{"a"}, 3) |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
89 %! assert (d{"b"}, 4) |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
90 |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
91 %!test |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
92 %! % dict assignment, update existing key |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
93 %! pyexec ("d = {'a':1}") |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
94 %! d = pyobject.fromPythonVarName ("d"); |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
95 %! d{"a"} = 3; |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
96 %! assert (d{"a"}, 3) |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
97 |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
98 %!xtest |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
99 %! % dict assignment, other keys (e.g., Issue #10). |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
100 %! pyexec ("d = dict()") |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
101 %! d = pyobject.fromPythonVarName ("d"); |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
102 %! d{"5"} = 10; |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
103 %! d{5.5} = 11; |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
104 %! d{5} = 12; |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
105 %! assert (d{"5"}, 10) |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
106 %! assert (d{5.5}, 11) |
07ca24d7627f
indexed assignment: support string index, e.g., for dicts
Colin Macdonald <cbm@m.fsf.org>
parents:
264
diff
changeset
|
107 %! assert (d{5}, 12) |