Mercurial > pytave
annotate @pyobject/pyobject.m @ 343:fe6b9e618c98
Add %!xtests for known failures due to bugs in Octave
* @py/py.m, @pyobject/pyobject.m, @pyobject/subsasgn.m, @pyobject/subsref.m:
Add %!xtests for known failures due to bugs in Octave.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Tue, 16 Aug 2016 13:54:25 -0700 |
parents | 5c63651b0ec4 |
children | b1eff49bd139 |
rev | line source |
---|---|
224
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
1 ## Copyright (C) 2016 Colin B. Macdonald |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
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 |
224
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
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 |
224
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
7 ## by the Free Software Foundation; either version 3 of the License, |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
8 ## or (at your option) any later version. |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
9 ## |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
10 ## This software is distributed in the hope that it will be useful, |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
12 ## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
13 ## the GNU General Public License for more details. |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
14 ## |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
15 ## You should have received a copy of the GNU General Public |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
16 ## License along with this software; see the file COPYING. |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
17 ## If not, see <http://www.gnu.org/licenses/>. |
209 | 18 |
224
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
19 ## -*- texinfo -*- |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
20 ## @documentencoding UTF-8 |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
21 ## @defun pyobject (@var{s}) |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
22 ## Wrap a Python object. |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
23 ## |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
24 ## TODO: where/how to document classdef classes? |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
25 ## |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
26 ## @seealso{pyexec, pyeval} |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
27 ## @end defun |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
28 |
209 | 29 |
30 classdef pyobject < handle | |
31 properties | |
32 id | |
245
72a2f508f2d8
maint: Use Octave keywords for ending code blocks
Mike Miller <mtmiller@octave.org>
parents:
236
diff
changeset
|
33 endproperties |
209 | 34 |
35 | |
36 methods | |
282
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
37 function obj = pyobject (x, id) |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
38 if (nargin == 0) |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
39 obj = pyeval ("None"); |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
40 return |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
41 endif |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
42 |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
43 if (nargin == 1) |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
44 ## Convert the input to a pyobject |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
45 if (isa (x, "pyobject")) |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
46 obj = x; |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
47 else |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
48 ## XXX: perhaps not the ideal implementation! |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
49 vars = pyeval ("__import__('__main__').__dict__"); |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
50 ## this is vars{"_temp"} = x |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
51 idx = struct ("type", "{}", "subs", {{"_temp"}}); |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
52 vars = subsasgn (vars, idx, x); |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
53 cmd = [ ... |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
54 "if not ('__InOct__' in vars() or '__InOct__' in globals()):\n" ... |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
55 " __InOct__ = dict()\n" ... |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
56 " # FIXME: make it accessible elsewhere?\n" ... |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
57 " import __main__\n" ... |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
58 " __main__.__InOct__ = __InOct__\n" ... |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
59 "__InOct__[hex(id(_temp))] = _temp" ]; |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
60 pyexec (cmd); |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
61 id = pyeval (["hex(id(_temp))"]); |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
62 obj = pyobject (0, id); |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
63 endif |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
64 return |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
65 endif |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
66 |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
67 if (nargin == 2) |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
68 ## The actual constructor. Nicer to split this off to static method |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
69 ## like `pyobject.new` but I don't know how to call from pycall.cc. |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
70 ## Warning: not intended for casual use: you must also insert the |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
71 ## object into the Python `__InOct__` dict with key `id`. |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
72 obj.id = id; |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
73 return |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
74 endif |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
75 |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
76 error ("pyobject: unexpected input to the constructor") |
224
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
77 endfunction |
209 | 78 |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
79 function delete (x) |
223
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
80 # Called on clear of the last reference---for subclasses of |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
81 # handle; not called at all for "value classes". |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
82 # |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
83 # FIXME: #46497 this is never called! |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
84 # Workaround: call @code{delete(x)} right before @code{clear x}. But |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
85 # be careful, @code{x} needs to be the last reference: don't do this: |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
86 # @example |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
87 # d = pyobject (...); |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
88 # d2 = d; |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
89 # delete (d) |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
90 # clear d |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
91 # d2 |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
92 # @print{} ... KeyError ... |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
93 # @end example |
209 | 94 |
223
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
95 #disp ("delete") |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
96 |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
97 # throws KeyError if it wasn't in there for some reason |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
98 cmd = sprintf ("__InOct__.pop('%s')", x.id); |
06835a3e3384
pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents:
222
diff
changeset
|
99 pyexec (cmd) |
224
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
100 endfunction |
209 | 101 |
224
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
102 # methods defined in external files |
209 | 103 dummy (x) |
216 | 104 display (x) |
221
a76d75683455
pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
220
diff
changeset
|
105 subsref (x, idx) |
209 | 106 |
107 function r = getid (x) | |
108 r = x.id; | |
224
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
109 endfunction |
209 | 110 |
280
3bf799e80ca5
pyobject: add char conversion method, refactor disp and display to use char
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
111 function s = char (x) |
3bf799e80ca5
pyobject: add char conversion method, refactor disp and display to use char
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
112 s = pycall ("str", x); |
3bf799e80ca5
pyobject: add char conversion method, refactor disp and display to use char
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
113 endfunction |
3bf799e80ca5
pyobject: add char conversion method, refactor disp and display to use char
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
114 |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
115 function varargout = disp (x) |
280
3bf799e80ca5
pyobject: add char conversion method, refactor disp and display to use char
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
116 s = char (x); |
216 | 117 if (nargout == 0) |
118 disp (s) | |
119 else | |
120 varargout = {s}; | |
224
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
121 endif |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
122 endfunction |
209 | 123 |
287
ccd9fcaae5f8
pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents:
286
diff
changeset
|
124 function s = class (x) |
284
6c133bcac33a
use pyobject instead of accessing the InOct dict
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
125 idx = struct ("type", ".", "subs", "__class__"); |
287
ccd9fcaae5f8
pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents:
286
diff
changeset
|
126 class_ref = subsref (x, idx); |
ccd9fcaae5f8
pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents:
286
diff
changeset
|
127 idx = struct ("type", ".", "subs", "__name__"); |
ccd9fcaae5f8
pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents:
286
diff
changeset
|
128 s = subsref (class_ref, idx); |
ccd9fcaae5f8
pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents:
286
diff
changeset
|
129 s = sprintf ("py.%s", char (s)); |
224
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
130 endfunction |
209 | 131 |
324
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
132 function y = double (x) |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
133 fn = pyeval ("lambda x: isinstance(x, __import__('array').array)"); |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
134 if (pycall (fn, x)) |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
135 c = cell (x); |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
136 y = cellfun (@(t) eval ("double (t)"), c); |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
137 else |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
138 y = pycall ("float", x); |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
139 endif |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
140 endfunction |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
141 |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
142 function vargout = help (x) |
224
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
143 idx = struct ("type", ".", "subs", "__doc__"); |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
144 s = subsref (x, idx); |
213
a54ab59c491d
merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents:
209
diff
changeset
|
145 if (nargout == 0) |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
146 disp (s) |
213
a54ab59c491d
merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents:
209
diff
changeset
|
147 else |
a54ab59c491d
merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents:
209
diff
changeset
|
148 vargout = {s}; |
224
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
149 endif |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
150 endfunction |
236
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
151 |
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
152 function len = length (x) |
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
153 try |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
324
diff
changeset
|
154 len = double (pycall ("len", x)); |
236
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
155 catch |
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
156 len = 1; |
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
157 end_try_catch |
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
158 endfunction |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
159 |
296
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
160 |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
161 function [n, varargout] = size (x, d) |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
162 assert (nargin <= 2) |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
163 try |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
164 idx = struct ("type", ".", "subs", "shape"); |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
165 sz = subsref (x, idx); |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
324
diff
changeset
|
166 sz = cellfun (@(x) eval ("double (x)"), cell (sz)); |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
167 catch |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
168 ## if it had no shape, make it a row vector |
296
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
169 sz = [1 length(x)]; |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
170 end_try_catch |
296
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
171 |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
172 ## simplest case |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
173 if (nargout <= 1 && nargin == 1) |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
174 n = sz; |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
175 return |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
176 endif |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
177 |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
178 ## quirk: pad extra dimensions with ones |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
179 if (nargin < 2) |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
180 d = 1; |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
181 endif |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
182 sz(end+1:max (d,nargout-end)) = 1; |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
183 |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
184 if (nargin > 1) |
296
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
185 assert (nargout <= 1) |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
186 n = sz(d); |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
187 return |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
188 endif |
296
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
189 |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
190 ## multiple outputs |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
191 n = sz(1); |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
192 for i = 2:(nargout-1) |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
193 varargout{i-1} = sz(i); |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
194 endfor |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
195 ## last is product of all remaining |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
196 varargout{nargout-1} = prod (sz(nargout:end)); |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
197 endfunction |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
198 |
296
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
199 |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
200 function n = numel (x) |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
201 assert (nargin == 1) |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
202 sz = size (x); |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
203 n = prod (sz); |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
204 endfunction |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
205 |
295
116edbde7329
pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents:
294
diff
changeset
|
206 function n = ndims (x) |
116edbde7329
pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents:
294
diff
changeset
|
207 assert (nargin == 1) |
116edbde7329
pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents:
294
diff
changeset
|
208 n = length (size (x)); |
116edbde7329
pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents:
294
diff
changeset
|
209 endfunction |
116edbde7329
pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents:
294
diff
changeset
|
210 |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
211 function r = end (x, index_pos, num_indices) |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
212 assert (nargin == 3) |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
213 assert (isscalar (index_pos)) |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
214 if (num_indices == 1) |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
215 r = numel (x); |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
216 else |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
217 r = size (x, index_pos); |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
218 endif |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
219 endfunction |
329
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
220 |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
221 function res = isequal (varargin) |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
222 assert (nargin >= 2) |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
223 res = all (strcmp ("pyobject", cellfun ("class", varargin, "uniformoutput", false))); |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
224 for i = 2:nargin |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
225 if (! res) |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
226 return; |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
227 endif |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
228 res = res && pycall("bool", pycall ("operator.eq", varargin{1}, varargin{i})); |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
229 endfor |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
230 endfunction |
224
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
231 endmethods |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
232 endclassdef |
213
a54ab59c491d
merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents:
209
diff
changeset
|
233 |
224
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
234 |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
235 %!test |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
236 %! pyexec ("import sys") |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
237 %! A = pyeval ("sys"); |
7feece80fbfa
pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents:
223
diff
changeset
|
238 %! assert (isa (A, "pyobject")) |
236
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
239 |
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
240 %!test |
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
241 %! pyobj = pyeval ("{1:2, 2:3, 3:4}"); |
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
242 %! assert (isa (pyobj, "pyobject")) |
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
243 %! assert (length (pyobj), 3) |
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
244 |
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
245 %!test |
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
246 %! pyexec ("import sys"); |
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
247 %! pyobj = pyeval ("sys"); |
9da4ca336dda
Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
227
diff
changeset
|
248 %! assert (length (pyobj), 1) |
280
3bf799e80ca5
pyobject: add char conversion method, refactor disp and display to use char
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
249 |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
250 %!assert (size (pyeval ("[10, 20, 30]")), [1 3]) |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
251 %!assert (size (pyeval ("[10, 20, 30]"), 1), 1) |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
252 %!assert (size (pyeval ("[10, 20, 30]"), 2), 3) |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
253 %!assert (size (pyeval ("[10, 20, 30]"), 3), 1) |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
254 |
296
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
255 %!test |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
256 %! L = pyeval ("[10, 20, 30]"); |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
257 %! a = size (L); |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
258 %! assert (a, [1, 3]) |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
259 %! [a b] = size (L); |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
260 %! assert ([a b], [1 3]) |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
261 %! [a b c] = size (L); |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
262 %! assert ([a b c], [1 3 1]) |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
263 |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
264 %!assert (numel (pyeval ("[10, 20, 30]")), 3) |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
265 |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
266 %!test |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
267 %! L = pyeval ("[10, 20, 30]"); |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
324
diff
changeset
|
268 %! assert (double (L{end}), 30) |
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
324
diff
changeset
|
269 %! assert (double (L{end-1}), 20) |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
270 |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
271 %!test |
300
6a28a5a62206
Comment to clarify purpose of test
Colin Macdonald <cbm@m.fsf.org>
parents:
296
diff
changeset
|
272 %! % ensure "end" works for iterables that are not lists |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
273 %! myrange = pyeval ( ... |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
274 %! "range if __import__('sys').hexversion >= 0x03000000 else xrange"); |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
275 %! R = pycall (myrange, int32 (5), int32 (10), int32 (2)); |
327
15c20ab4b80a
Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents:
324
diff
changeset
|
276 %! assert (double (R{end}), 9) |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
277 |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
278 %!shared a |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
279 %! pyexec ("class _myclass(): shape = (3, 4, 5)") |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
280 %! a = pyeval ("_myclass()"); |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
281 %!assert (size (a), [3 4 5]) |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
282 %!assert (size (a, 3), 5) |
296
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
283 %!test |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
284 %! s = size (a); |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
285 %! assert (s, [3 4 5]) |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
286 %!test |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
287 %! [n m] = size (a); |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
288 %! assert ([n m], [3 20]) |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
289 %!test |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
290 %! [n m o] = size (a); |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
291 %! assert ([n m o], [3 4 5]) |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
292 %!test |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
293 %! [n m o p] = size (a); |
fadb4475e75d
pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents:
295
diff
changeset
|
294 %! assert ([n m o p], [3 4 5 1]) |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
295 %!assert (numel (a), 60) |
295
116edbde7329
pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents:
294
diff
changeset
|
296 %!assert (ndims (a), 3) |
294
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
297 %!shared |
c7e1cb518372
pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents:
287
diff
changeset
|
298 |
320
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
299 ## Test conversion of scalar struct into a Python dict |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
300 %!shared s1 |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
301 %! s1.name = "Octave"; |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
302 %! s1.value = 42; |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
303 %! s1.counts = {1, 2, 3}; |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
304 %! s1.ok = true; |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
305 %!assert (isa (pyobject (s1), "pyobject")) |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
306 %!assert (class (pyobject (s1)), "py.dict") |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
307 %!assert (char (pyobject (s1){"name"}), "Octave") |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
308 %!assert (pyobject (s1){"value"}, 42) |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
309 %!assert (pyobject (s1){"ok"}, true) |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
310 |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
311 %!error pyobject (struct ("a", {})) |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
312 %!error pyobject (struct ("a", {1, 2})) |
c2aa34730dc9
Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents:
315
diff
changeset
|
313 |
280
3bf799e80ca5
pyobject: add char conversion method, refactor disp and display to use char
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
314 %!assert (char (pyeval ("None")), "None") |
3bf799e80ca5
pyobject: add char conversion method, refactor disp and display to use char
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
315 %!assert (char (pyeval ("'this is a string'")), "this is a string") |
3bf799e80ca5
pyobject: add char conversion method, refactor disp and display to use char
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
316 %!assert (char (pyeval ("[1, 2, 3, 4, 5]")), "[1, 2, 3, 4, 5]") |
3bf799e80ca5
pyobject: add char conversion method, refactor disp and display to use char
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
317 %!assert (char (pyeval ("(1, 2, 3, 4, 5)")), "(1, 2, 3, 4, 5)") |
3bf799e80ca5
pyobject: add char conversion method, refactor disp and display to use char
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
318 %!assert (char (pyeval ("__import__('sys')")), "<module 'sys' (built-in)>") |
282
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
319 |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
320 %!assert (isa (pyobject (), "pyobject")) |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
321 %!assert (isa (pyobject ("a string"), "pyobject")) |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
322 %!assert (isa (pyobject (42.2), "pyobject")) |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
323 %!assert (isa (pyobject (int32 (42)), "pyobject")) |
4c4747646e12
Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents:
280
diff
changeset
|
324 %!assert (isa (pyobject (pyobject ()), "pyobject")) |
287
ccd9fcaae5f8
pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents:
286
diff
changeset
|
325 |
ccd9fcaae5f8
pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents:
286
diff
changeset
|
326 %!assert (class (pyeval ("{}")), "py.dict") |
ccd9fcaae5f8
pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents:
286
diff
changeset
|
327 %!assert (class (pyeval ("[]")), "py.list") |
ccd9fcaae5f8
pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents:
286
diff
changeset
|
328 %!assert (class (pyeval ("()")), "py.tuple") |
ccd9fcaae5f8
pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents:
286
diff
changeset
|
329 %!assert (class (pyeval ("set()")), "py.set") |
ccd9fcaae5f8
pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents:
286
diff
changeset
|
330 %!assert (class (pyeval ("None")), "py.NoneType") |
ccd9fcaae5f8
pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents:
286
diff
changeset
|
331 %!assert (class (pyeval ("2.5")), "double") |
324
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
332 |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
333 ## Test conversion method pyobject.double |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
334 %!assert (double (pyobject (2.5)), 2.5) |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
335 %!assert (double (pyobject (42)), 42) |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
336 %!assert (double (pyobject ("42")), 42) |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
337 %!assert (double (pyobject (false)), 0) |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
338 %!assert (double (pycall ("array.array", "d", {31, 32, 33, 34})), [31, 32, 33, 34]) |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
339 |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
340 %!error double (pyobject ("this is not a number")) |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
341 %!error double (pyobject ()) |
2b2460295218
pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents:
320
diff
changeset
|
342 %!error double (pyeval ("[1, 2, 3]")) |
329
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
343 |
343
fe6b9e618c98
Add %!xtests for known failures due to bugs in Octave
Mike Miller <mtmiller@octave.org>
parents:
330
diff
changeset
|
344 ## Octave fails to resolve function overloads via function handles |
fe6b9e618c98
Add %!xtests for known failures due to bugs in Octave
Mike Miller <mtmiller@octave.org>
parents:
330
diff
changeset
|
345 %!xtest |
fe6b9e618c98
Add %!xtests for known failures due to bugs in Octave
Mike Miller <mtmiller@octave.org>
parents:
330
diff
changeset
|
346 %! fn = @double; |
fe6b9e618c98
Add %!xtests for known failures due to bugs in Octave
Mike Miller <mtmiller@octave.org>
parents:
330
diff
changeset
|
347 %! x = pyobject (int64 (42)); |
fe6b9e618c98
Add %!xtests for known failures due to bugs in Octave
Mike Miller <mtmiller@octave.org>
parents:
330
diff
changeset
|
348 %! assert (fn (x), double (x)) |
fe6b9e618c98
Add %!xtests for known failures due to bugs in Octave
Mike Miller <mtmiller@octave.org>
parents:
330
diff
changeset
|
349 |
329
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
350 %!error (isequal (pyobject ())) |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
351 %!assert (! isequal (pyobject (1.2), 1.2)) |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
352 %!assert (isequal (pyobject ("a string"), pyobject ("a string"))) |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
353 %!assert (isequal (pyeval ("None"), pyeval ("None"))) |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
354 %!assert (! isequal (pyeval ("None"), pyeval ("None"), pyobject (10))) |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
355 %!assert (isequal (pyobject (10), pyobject (10.0), pyobject (int8 (10)))) |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
356 |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
357 %!test |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
358 %! A = pyeval ("[1, 2, 3]"); |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
359 %! B = pycall ("list", {1, 2, 3}); |
778c91283a73
Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
324
diff
changeset
|
360 %! assert (isequal (A, B)) |