annotate @pyobject/pyobject.m @ 373:0e4097c66788

Report a Python object's fully qualified class name correctly * oct-py-util.cc (pytave::py_object_class_name): Return the correct fully qualified class name, including module prefix. (pytave::is_py_kwargs_argument): Check for class name "__main__._OctaveKwargs". * __py_struct_from_dict__.cc (F__py_class_name__): New function. * @pyobject/methods.m (pyobject.methods): Use __py_class_name__. * @pyobject/pyobject.m (pyobject.class): Likewise.
author Mike Miller <mtmiller@octave.org>
date Fri, 26 Aug 2016 14:05:37 -0700
parents 9d7188514f2c
children d362cdd1ddeb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
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
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
29
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
30 classdef pyobject < handle
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
31 properties
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
32 m_id
245
72a2f508f2d8 maint: Use Octave keywords for ending code blocks
Mike Miller <mtmiller@octave.org>
parents: 236
diff changeset
33 endproperties
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
34
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
35
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
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");
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
40 elseif (nargin == 1)
282
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
41 ## 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
42 if (isa (x, "pyobject"))
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
43 obj = x;
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
44 else
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
45 obj.m_id = __py_objstore_put__ (x);
282
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
46 endif
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
47 elseif (nargin == 2)
282
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
48 ## 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
49 ## 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
50 ## Warning: not intended for casual use: you must also insert the
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
51 ## object into the Python object store with key `id`.
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
52 obj.m_id = id;
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
53 else
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
54 error ("pyobject: unexpected input to the constructor")
282
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
55 endif
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
56 endfunction
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
57
218
02da92723889 Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents: 217
diff changeset
58 function delete (x)
223
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
59 # 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
60 # handle; not called at all for "value classes".
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
61 #
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
62 # FIXME: #46497 this is never called!
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
63 # 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
64 # 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
65 # @example
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
66 # d = pyobject (...);
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
67 # d2 = d;
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
68 # delete (d)
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
69 # clear d
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
70 # d2
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
71 # @print{} ... KeyError ...
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
72 # @end example
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
73
223
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
74 #disp ("delete")
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
75
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
76 __py_objstore_del__ (x.m_id);
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
77 endfunction
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
78
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
79 # methods defined in external files
365
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 362
diff changeset
80 char (x)
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
81 dummy (x)
216
3b2b7db7d709 Improve display
Colin Macdonald <cbm@m.fsf.org>
parents: 213
diff changeset
82 display (x)
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents: 220
diff changeset
83 subsref (x, idx)
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
84
362
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
85 function r = id (x);
b0677c492655 Overhaul Python object storage and wrapping in pyobject
Mike Miller <mtmiller@octave.org>
parents: 358
diff changeset
86 r = x.m_id;
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
87 endfunction
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
88
218
02da92723889 Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents: 217
diff changeset
89 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
90 s = char (x);
216
3b2b7db7d709 Improve display
Colin Macdonald <cbm@m.fsf.org>
parents: 213
diff changeset
91 if (nargout == 0)
3b2b7db7d709 Improve display
Colin Macdonald <cbm@m.fsf.org>
parents: 213
diff changeset
92 disp (s)
3b2b7db7d709 Improve display
Colin Macdonald <cbm@m.fsf.org>
parents: 213
diff changeset
93 else
3b2b7db7d709 Improve display
Colin Macdonald <cbm@m.fsf.org>
parents: 213
diff changeset
94 varargout = {s};
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
95 endif
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
96 endfunction
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
97
287
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
98 function s = class (x)
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 367
diff changeset
99 s = __py_class_name__ (x);
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 367
diff changeset
100 s = sprintf ("py.%s", s);
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
101 endfunction
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
102
324
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
103 function y = double (x)
367
9d7188514f2c Use pyobject.isa instead of lambda to check Python types
Mike Miller <mtmiller@octave.org>
parents: 366
diff changeset
104 if (isa (x, "py.array.array"))
324
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
105 c = cell (x);
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
106 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
107 else
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
108 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
109 endif
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
110 endfunction
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
111
356
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
112 function y = int64 (x)
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
113 y = __py_int64_scalar_value__ (x);
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
114 endfunction
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
115
358
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
116 function y = isa (x, typestr)
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
117 assert (nargin == 2);
366
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
118 assert (ischar (typestr) || iscellstr (typestr));
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
119
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
120 if (ischar (typestr))
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
121 typestr = { typestr };
358
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
122 endif
366
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
123
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
124 y = false (size (typestr));
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
125
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
126 for i = 1:numel (typestr)
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
127 if ((numel (typestr{i}) > 3) && (typestr{i}(1:3) == "py."))
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
128 y(i) = __py_isinstance__ (x, typestr{i});
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
129 else
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
130 y(i) = builtin ("isa", x, typestr{i});
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
131 endif
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
132 endfor
358
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
133 endfunction
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
134
346
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
135 function y = struct (x)
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
136 y = __py_struct_from_dict__ (x);
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
137 endfunction
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
138
218
02da92723889 Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents: 217
diff changeset
139 function vargout = help (x)
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
140 idx = struct ("type", ".", "subs", "__doc__");
218
02da92723889 Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents: 217
diff changeset
141 s = subsref (x, idx);
213
a54ab59c491d merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents: 209
diff changeset
142 if (nargout == 0)
218
02da92723889 Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents: 217
diff changeset
143 disp (s)
213
a54ab59c491d merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents: 209
diff changeset
144 else
a54ab59c491d merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents: 209
diff changeset
145 vargout = {s};
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
146 endif
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
147 endfunction
236
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
148
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
149 function len = length (x)
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
150 try
327
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 324
diff changeset
151 len = double (pycall ("len", x));
236
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
152 catch
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
153 len = 1;
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
154 end_try_catch
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
155 endfunction
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
156
296
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
157
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
158 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
159 assert (nargin <= 2)
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
160 try
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
161 idx = struct ("type", ".", "subs", "shape");
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
162 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
163 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
164 catch
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
165 ## 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
166 sz = [1 length(x)];
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
167 end_try_catch
296
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
168
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
169 ## simplest case
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
170 if (nargout <= 1 && nargin == 1)
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
171 n = sz;
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
172 return
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
173 endif
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
174
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
175 ## quirk: pad extra dimensions with ones
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
176 if (nargin < 2)
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
177 d = 1;
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
178 endif
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
179 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
180
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
181 if (nargin > 1)
296
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
182 assert (nargout <= 1)
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
183 n = sz(d);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
184 return
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
185 endif
296
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
186
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
187 ## multiple outputs
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
188 n = sz(1);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
189 for i = 2:(nargout-1)
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
190 varargout{i-1} = sz(i);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
191 endfor
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
192 ## last is product of all remaining
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
193 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
194 endfunction
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
195
296
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
196
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
197 function n = numel (x)
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
198 assert (nargin == 1)
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
199 sz = size (x);
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
200 n = prod (sz);
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
201 endfunction
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
202
295
116edbde7329 pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents: 294
diff changeset
203 function n = ndims (x)
116edbde7329 pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents: 294
diff changeset
204 assert (nargin == 1)
116edbde7329 pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents: 294
diff changeset
205 n = length (size (x));
116edbde7329 pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents: 294
diff changeset
206 endfunction
116edbde7329 pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents: 294
diff changeset
207
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
208 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
209 assert (nargin == 3)
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
210 assert (isscalar (index_pos))
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
211 if (num_indices == 1)
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
212 r = numel (x);
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
213 else
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
214 r = size (x, index_pos);
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
215 endif
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
216 endfunction
329
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
217
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
218 function res = isequal (varargin)
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
219 assert (nargin >= 2)
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
220 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
221 for i = 2:nargin
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
222 if (! res)
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
223 return;
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
224 endif
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
225 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
226 endfor
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
227 endfunction
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
228 endmethods
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
229 endclassdef
213
a54ab59c491d merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents: 209
diff changeset
230
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
231
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
232 %!test
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
233 %! pyexec ("import sys")
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
234 %! A = pyeval ("sys");
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
235 %! assert (isa (A, "pyobject"))
236
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
236
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
237 %!test
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
238 %! 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
239 %! assert (isa (pyobj, "pyobject"))
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
240 %! assert (length (pyobj), 3)
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
241
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
242 %!test
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
243 %! pyexec ("import sys");
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
244 %! pyobj = pyeval ("sys");
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
245 %! 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
246
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
247 %!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
248 %!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
249 %!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
250 %!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
251
296
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
252 %!test
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
253 %! L = pyeval ("[10, 20, 30]");
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
254 %! a = size (L);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
255 %! assert (a, [1, 3])
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
256 %! [a b] = size (L);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
257 %! assert ([a b], [1 3])
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
258 %! [a b c] = size (L);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
259 %! 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
260
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
261 %!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
262
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
263 %!test
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
264 %! 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
265 %! 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
266 %! 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
267
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
268 %!test
300
6a28a5a62206 Comment to clarify purpose of test
Colin Macdonald <cbm@m.fsf.org>
parents: 296
diff changeset
269 %! % 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
270 %! myrange = pyeval ( ...
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
271 %! "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
272 %! 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
273 %! assert (double (R{end}), 9)
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
274
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
275 %!shared a
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
276 %! 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
277 %! a = pyeval ("_myclass()");
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
278 %!assert (size (a), [3 4 5])
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
279 %!assert (size (a, 3), 5)
296
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
280 %!test
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
281 %! s = size (a);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
282 %! assert (s, [3 4 5])
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 %! [n m] = size (a);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
285 %! assert ([n m], [3 20])
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 o] = size (a);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
288 %! 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
289 %!test
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
290 %! [n m o p] = 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 p], [3 4 5 1])
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
292 %!assert (numel (a), 60)
295
116edbde7329 pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents: 294
diff changeset
293 %!assert (ndims (a), 3)
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
294 %!shared
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
295
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
296 ## 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
297 %!shared s1
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
298 %! s1.name = "Octave";
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
299 %! s1.value = 42;
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
300 %! s1.counts = {1, 2, 3};
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
301 %! s1.ok = true;
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
302 %!assert (isa (pyobject (s1), "pyobject"))
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
303 %!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
304 %!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
305 %!assert (pyobject (s1){"value"}, 42)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
306 %!assert (pyobject (s1){"ok"}, true)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
307
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
308 %!error pyobject (struct ("a", {}))
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
309 %!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
310
280
3bf799e80ca5 pyobject: add char conversion method, refactor disp and display to use char
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
311 %!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
312 %!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
313 %!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
314 %!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
315 %!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
316
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
317 %!assert (isa (pyobject (), "pyobject"))
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
318 %!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
319 %!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
320 %!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
321 %!assert (isa (pyobject (pyobject ()), "pyobject"))
287
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
322
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
323 %!assert (class (pyeval ("{}")), "py.dict")
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
324 %!assert (class (pyeval ("[]")), "py.list")
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
325 %!assert (class (pyeval ("()")), "py.tuple")
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
326 %!assert (class (pyeval ("set()")), "py.set")
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
327 %!assert (class (pyeval ("None")), "py.NoneType")
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
328 %!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
329
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
330 ## 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
331 %!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
332 %!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
333 %!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
334 %!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
335 %!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
336
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
337 %!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
338 %!error double (pyobject ())
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
339 %!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
340
358
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
341 ## Test class type check method pyobject.isa
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
342 %!assert (isa (pyobject (), "handle"))
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
343 %!assert (isa (pyobject (), "pyobject"))
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
344 %!assert (! isa (pyobject (), "py.None"))
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
345 %!assert (isa (pyobject (0), "handle"))
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
346 %!assert (isa (pyobject (0), "pyobject"))
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
347 %!assert (isa (pyobject (0), "py.float"))
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
348 %!assert (isa (pyobject (int32 (0)), "py.int"))
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
349 %!assert (isa (pyobject (true), "py.bool"))
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
350 %!assert (isa (pyobject ("a string"), "py.str"))
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
351 %!assert (isa (pyobject (struct ()), "py.dict"))
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
352 %!assert (isa (pyobject (cell ()), "py.tuple"))
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
353 %!assert (isa (pyobject ([1, 2, 3, 4]), "py.array.array"))
366
5c900b8383c4 pyobject.isa: work with a cell array of type names
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
354 %!assert (all (isa (pyobject (0), {"pyobject", "py.float", "py.numbers.Number"})))
358
d41fc23d4b9f pyobject.isa: New overload method to handle Python types (fixes issue #49)
Mike Miller <mtmiller@octave.org>
parents: 356
diff changeset
355
356
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
356 ## Test conversion method pyobject.int64
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
357 %!assert (int64 (pyobject (int8 (0))), int64 (0))
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
358 %!assert (int64 (pyobject (int64 (42))), int64 (42))
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
359 %!assert (int64 (pyobject (intmax ("int64"))), intmax ("int64"))
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
360 %!assert (int64 (pyobject (intmin ("int64"))), intmin ("int64"))
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
361 %!assert (int64 (pycall ("int", 1e100)), intmax ("int64"))
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
362 %!assert (int64 (pycall ("int", -1e100)), intmin ("int64"))
6cd581661176 pyobject.int64: New method to convert integer object to Octave int64
Mike Miller <mtmiller@octave.org>
parents: 350
diff changeset
363
346
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
364 ## Test conversion method pyobject.struct
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
365 %!assert (struct (pycall ("dict")), struct ())
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
366 %!assert (struct (pyobject (struct ())), struct ())
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
367 %!test
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
368 %! a = struct ("a", 1, "b", 2, "three", 3);
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
369 %! b = pyobject (a);
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
370 %! c = struct (b);
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
371 %! assert (c, a)
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
372 %!test
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
373 %! a = struct ("a", 1, "b", 2, "three", 3);
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
374 %! b = pycall ("dict", pyargs ("a", 1, "b", 2, "three", 3));
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
375 %! c = struct (b);
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
376 %! assert (c, a)
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
377
343
fe6b9e618c98 Add %!xtests for known failures due to bugs in Octave
Mike Miller <mtmiller@octave.org>
parents: 330
diff changeset
378 ## 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
379 %!xtest
fe6b9e618c98 Add %!xtests for known failures due to bugs in Octave
Mike Miller <mtmiller@octave.org>
parents: 330
diff changeset
380 %! fn = @double;
fe6b9e618c98 Add %!xtests for known failures due to bugs in Octave
Mike Miller <mtmiller@octave.org>
parents: 330
diff changeset
381 %! x = pyobject (int64 (42));
fe6b9e618c98 Add %!xtests for known failures due to bugs in Octave
Mike Miller <mtmiller@octave.org>
parents: 330
diff changeset
382 %! 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
383
329
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
384 %!error (isequal (pyobject ()))
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
385 %!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
386 %!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
387 %!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
388 %!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
389 %!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
390
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
391 %!test
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
392 %! 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
393 %! 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
394 %! assert (isequal (A, B))