annotate @pyobject/pyobject.m @ 349:140e37e8e95a

Don't create pyobjects within the pyobject ctor It seems to work, but maybe there are subtle bugs. * @pyobject/pyobject.m: Use an insert function.
author Colin Macdonald <cbm@m.fsf.org>
date Thu, 11 Aug 2016 10:15:26 -0700
parents 7f039ffe501b
children e89a8a37fd8a
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
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
32 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");
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
347
7f039ffe501b Store the _InOctave dict only in main
Colin Macdonald <cbm@m.fsf.org>
parents: 346
diff changeset
48 ## Ensure _InOctave dict exists
7f039ffe501b Store the _InOctave dict only in main
Colin Macdonald <cbm@m.fsf.org>
parents: 346
diff changeset
49 cmd = [ "if not getattr(__import__('__main__'), '_InOctave', None):\n" ...
7f039ffe501b Store the _InOctave dict only in main
Colin Macdonald <cbm@m.fsf.org>
parents: 346
diff changeset
50 " __import__('__main__')._InOctave = dict()" ];
7f039ffe501b Store the _InOctave dict only in main
Colin Macdonald <cbm@m.fsf.org>
parents: 346
diff changeset
51 pyexec (cmd);
7f039ffe501b Store the _InOctave dict only in main
Colin Macdonald <cbm@m.fsf.org>
parents: 346
diff changeset
52
349
140e37e8e95a Don't create pyobjects within the pyobject ctor
Colin Macdonald <cbm@m.fsf.org>
parents: 347
diff changeset
53 ## Function to insert and return the hex id
140e37e8e95a Don't create pyobjects within the pyobject ctor
Colin Macdonald <cbm@m.fsf.org>
parents: 347
diff changeset
54 cmd = [ "def _in_octave_insert(x):\n" ...
140e37e8e95a Don't create pyobjects within the pyobject ctor
Colin Macdonald <cbm@m.fsf.org>
parents: 347
diff changeset
55 " h = hex(id(x))\n" ...
140e37e8e95a Don't create pyobjects within the pyobject ctor
Colin Macdonald <cbm@m.fsf.org>
parents: 347
diff changeset
56 " __import__('__main__')._InOctave[h] = x\n" ...
140e37e8e95a Don't create pyobjects within the pyobject ctor
Colin Macdonald <cbm@m.fsf.org>
parents: 347
diff changeset
57 " return h" ];
140e37e8e95a Don't create pyobjects within the pyobject ctor
Colin Macdonald <cbm@m.fsf.org>
parents: 347
diff changeset
58 pyexec (cmd);
347
7f039ffe501b Store the _InOctave dict only in main
Colin Macdonald <cbm@m.fsf.org>
parents: 346
diff changeset
59
349
140e37e8e95a Don't create pyobjects within the pyobject ctor
Colin Macdonald <cbm@m.fsf.org>
parents: 347
diff changeset
60 id = pycall ("_in_octave_insert", x);
282
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
61 obj = pyobject (0, id);
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
62 endif
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
63 return
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
64 endif
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
65
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
66 if (nargin == 2)
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
67 ## 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
68 ## 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
69 ## Warning: not intended for casual use: you must also insert the
347
7f039ffe501b Store the _InOctave dict only in main
Colin Macdonald <cbm@m.fsf.org>
parents: 346
diff changeset
70 ## object into the Python `_InOctave` dict with key `id`.
282
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
71 obj.id = id;
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
72 return
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
73 endif
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
74
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
75 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
76 endfunction
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
77
218
02da92723889 Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents: 217
diff changeset
78 function delete (x)
223
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
79 # 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
80 # handle; not called at all for "value classes".
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
81 #
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
82 # FIXME: #46497 this is never called!
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
83 # 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
84 # 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
85 # @example
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
86 # d = pyobject (...);
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
87 # d2 = d;
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
88 # delete (d)
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
89 # clear d
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
90 # d2
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
91 # @print{} ... KeyError ...
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
92 # @end example
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
93
223
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
94 #disp ("delete")
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
95
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
96 # throws KeyError if it wasn't in there for some reason
347
7f039ffe501b Store the _InOctave dict only in main
Colin Macdonald <cbm@m.fsf.org>
parents: 346
diff changeset
97 cmd = sprintf ("__import__('__main__')._InOctave.pop('%s')", x.id);
223
06835a3e3384 pyobject: remove unnecessary forcedelete method
Colin Macdonald <cbm@m.fsf.org>
parents: 222
diff changeset
98 pyexec (cmd)
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
99 endfunction
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
100
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
101 # methods defined in external files
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
102 dummy (x)
216
3b2b7db7d709 Improve display
Colin Macdonald <cbm@m.fsf.org>
parents: 213
diff changeset
103 display (x)
221
a76d75683455 pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents: 220
diff changeset
104 subsref (x, idx)
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
105
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
106 function r = getid (x)
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
107 r = x.id;
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
108 endfunction
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
109
280
3bf799e80ca5 pyobject: add char conversion method, refactor disp and display to use char
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
110 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
111 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
112 endfunction
3bf799e80ca5 pyobject: add char conversion method, refactor disp and display to use char
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
113
218
02da92723889 Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents: 217
diff changeset
114 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
115 s = char (x);
216
3b2b7db7d709 Improve display
Colin Macdonald <cbm@m.fsf.org>
parents: 213
diff changeset
116 if (nargout == 0)
3b2b7db7d709 Improve display
Colin Macdonald <cbm@m.fsf.org>
parents: 213
diff changeset
117 disp (s)
3b2b7db7d709 Improve display
Colin Macdonald <cbm@m.fsf.org>
parents: 213
diff changeset
118 else
3b2b7db7d709 Improve display
Colin Macdonald <cbm@m.fsf.org>
parents: 213
diff changeset
119 varargout = {s};
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
120 endif
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
121 endfunction
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
122
287
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
123 function s = class (x)
284
6c133bcac33a use pyobject instead of accessing the InOct dict
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
124 idx = struct ("type", ".", "subs", "__class__");
287
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
125 class_ref = subsref (x, idx);
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
126 idx = struct ("type", ".", "subs", "__name__");
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
127 s = subsref (class_ref, idx);
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
128 s = sprintf ("py.%s", char (s));
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
129 endfunction
209
57807b5fa7bf Change name of pyobj to pyobject
genuinelucifer
parents:
diff changeset
130
324
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
131 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
132 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
133 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
134 c = cell (x);
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
135 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
136 else
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
137 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
138 endif
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
139 endfunction
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
140
346
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
141 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
142 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
143 endfunction
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
144
218
02da92723889 Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents: 217
diff changeset
145 function vargout = help (x)
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
146 idx = struct ("type", ".", "subs", "__doc__");
218
02da92723889 Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents: 217
diff changeset
147 s = subsref (x, idx);
213
a54ab59c491d merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents: 209
diff changeset
148 if (nargout == 0)
218
02da92723889 Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents: 217
diff changeset
149 disp (s)
213
a54ab59c491d merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents: 209
diff changeset
150 else
a54ab59c491d merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents: 209
diff changeset
151 vargout = {s};
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
152 endif
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
153 endfunction
236
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
154
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
155 function len = length (x)
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
156 try
327
15c20ab4b80a Do not automatically convert Python integer types to Octave (see issue #56)
Mike Miller <mtmiller@octave.org>
parents: 324
diff changeset
157 len = double (pycall ("len", x));
236
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
158 catch
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
159 len = 1;
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
160 end_try_catch
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
161 endfunction
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
162
296
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
163
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
164 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
165 assert (nargin <= 2)
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
166 try
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
167 idx = struct ("type", ".", "subs", "shape");
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
168 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
169 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
170 catch
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
171 ## 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
172 sz = [1 length(x)];
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
173 end_try_catch
296
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 ## simplest case
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
176 if (nargout <= 1 && nargin == 1)
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
177 n = sz;
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
178 return
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
179 endif
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
180
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
181 ## quirk: pad extra dimensions with ones
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
182 if (nargin < 2)
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
183 d = 1;
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
184 endif
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
185 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
186
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
187 if (nargin > 1)
296
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
188 assert (nargout <= 1)
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
189 n = sz(d);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
190 return
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
191 endif
296
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
192
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
193 ## multiple outputs
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
194 n = sz(1);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
195 for i = 2:(nargout-1)
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
196 varargout{i-1} = sz(i);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
197 endfor
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
198 ## last is product of all remaining
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
199 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
200 endfunction
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
201
296
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
202
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
203 function n = numel (x)
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
204 assert (nargin == 1)
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
205 sz = size (x);
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
206 n = prod (sz);
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
207 endfunction
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
208
295
116edbde7329 pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents: 294
diff changeset
209 function n = ndims (x)
116edbde7329 pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents: 294
diff changeset
210 assert (nargin == 1)
116edbde7329 pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents: 294
diff changeset
211 n = length (size (x));
116edbde7329 pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents: 294
diff changeset
212 endfunction
116edbde7329 pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents: 294
diff changeset
213
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
214 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
215 assert (nargin == 3)
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
216 assert (isscalar (index_pos))
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
217 if (num_indices == 1)
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
218 r = numel (x);
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
219 else
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
220 r = size (x, index_pos);
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
221 endif
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
222 endfunction
329
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
223
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
224 function res = isequal (varargin)
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
225 assert (nargin >= 2)
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
226 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
227 for i = 2:nargin
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
228 if (! res)
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
229 return;
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
230 endif
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
231 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
232 endfor
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
233 endfunction
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
234 endmethods
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
235 endclassdef
213
a54ab59c491d merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents: 209
diff changeset
236
224
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
237
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
238 %!test
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
239 %! pyexec ("import sys")
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
240 %! A = pyeval ("sys");
7feece80fbfa pyobject: Octave-style formatting, minor edits
Colin Macdonald <cbm@m.fsf.org>
parents: 223
diff changeset
241 %! assert (isa (A, "pyobject"))
236
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
242
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
243 %!test
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
244 %! 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
245 %! assert (isa (pyobj, "pyobject"))
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
246 %! assert (length (pyobj), 3)
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
247
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
248 %!test
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
249 %! pyexec ("import sys");
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
250 %! pyobj = pyeval ("sys");
9da4ca336dda Add 'length' property to @pyobject (fixes issue #9)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 227
diff changeset
251 %! 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
252
294
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]")), [1 3])
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
254 %!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
255 %!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
256 %!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
257
296
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
258 %!test
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
259 %! L = pyeval ("[10, 20, 30]");
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
260 %! a = size (L);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
261 %! assert (a, [1, 3])
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
262 %! [a b] = size (L);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
263 %! assert ([a b], [1 3])
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
264 %! [a b c] = size (L);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
265 %! 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
266
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
267 %!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
268
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
269 %!test
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
270 %! 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
271 %! 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
272 %! 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
273
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
274 %!test
300
6a28a5a62206 Comment to clarify purpose of test
Colin Macdonald <cbm@m.fsf.org>
parents: 296
diff changeset
275 %! % 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
276 %! myrange = pyeval ( ...
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
277 %! "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
278 %! 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
279 %! assert (double (R{end}), 9)
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
280
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
281 %!shared a
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
282 %! 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
283 %! a = pyeval ("_myclass()");
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
284 %!assert (size (a), [3 4 5])
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
285 %!assert (size (a, 3), 5)
296
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 %! s = size (a);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
288 %! assert (s, [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] = size (a);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
291 %! assert ([n m], [3 20])
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] = 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], [3 4 5])
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
295 %!test
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
296 %! [n m o p] = size (a);
fadb4475e75d pyobject: support size's quirky output behaviour
Colin Macdonald <cbm@m.fsf.org>
parents: 295
diff changeset
297 %! 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
298 %!assert (numel (a), 60)
295
116edbde7329 pyobject: Add overloaded ndims
Colin Macdonald <cbm@m.fsf.org>
parents: 294
diff changeset
299 %!assert (ndims (a), 3)
294
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
300 %!shared
c7e1cb518372 pyobject: add methods size, numel and end
Colin Macdonald <cbm@m.fsf.org>
parents: 287
diff changeset
301
320
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
302 ## 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
303 %!shared s1
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
304 %! s1.name = "Octave";
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
305 %! s1.value = 42;
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
306 %! s1.counts = {1, 2, 3};
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
307 %! s1.ok = true;
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
308 %!assert (isa (pyobject (s1), "pyobject"))
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
309 %!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
310 %!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
311 %!assert (pyobject (s1){"value"}, 42)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
312 %!assert (pyobject (s1){"ok"}, true)
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
313
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
314 %!error pyobject (struct ("a", {}))
c2aa34730dc9 Convert scalar struct into Python dict (fixes issue #57)
Mike Miller <mtmiller@octave.org>
parents: 315
diff changeset
315 %!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
316
280
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 ("None")), "None")
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 ("'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
319 %!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
320 %!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
321 %!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
322
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
323 %!assert (isa (pyobject (), "pyobject"))
4c4747646e12 Make pyobject callable by users (to convert input)
Colin Macdonald <cbm@m.fsf.org>
parents: 280
diff changeset
324 %!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
325 %!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
326 %!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
327 %!assert (isa (pyobject (pyobject ()), "pyobject"))
287
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
328
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
329 %!assert (class (pyeval ("{}")), "py.dict")
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
330 %!assert (class (pyeval ("[]")), "py.list")
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
331 %!assert (class (pyeval ("()")), "py.tuple")
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
332 %!assert (class (pyeval ("set()")), "py.set")
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
333 %!assert (class (pyeval ("None")), "py.NoneType")
ccd9fcaae5f8 pyobject: rename "whatclass" to "class", make Matlab compatible
Mike Miller <mtmiller@octave.org>
parents: 286
diff changeset
334 %!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
335
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
336 ## 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
337 %!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
338 %!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
339 %!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
340 %!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
341 %!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
342
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
343 %!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
344 %!error double (pyobject ())
2b2460295218 pyobject.double: New method to convert numeric object to Octave double
Mike Miller <mtmiller@octave.org>
parents: 320
diff changeset
345 %!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
346
346
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
347 ## 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
348 %!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
349 %!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
350 %!test
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
351 %! 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
352 %! 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
353 %! 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
354 %! 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
355 %!test
b1eff49bd139 pyobject.struct: move into pyobject class definition, convert dict properly (fixes issue #62)
Mike Miller <mtmiller@octave.org>
parents: 343
diff changeset
356 %! 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
357 %! 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
358 %! 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
359 %! 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
360
343
fe6b9e618c98 Add %!xtests for known failures due to bugs in Octave
Mike Miller <mtmiller@octave.org>
parents: 330
diff changeset
361 ## 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
362 %!xtest
fe6b9e618c98 Add %!xtests for known failures due to bugs in Octave
Mike Miller <mtmiller@octave.org>
parents: 330
diff changeset
363 %! fn = @double;
fe6b9e618c98 Add %!xtests for known failures due to bugs in Octave
Mike Miller <mtmiller@octave.org>
parents: 330
diff changeset
364 %! x = pyobject (int64 (42));
fe6b9e618c98 Add %!xtests for known failures due to bugs in Octave
Mike Miller <mtmiller@octave.org>
parents: 330
diff changeset
365 %! 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
366
329
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
367 %!error (isequal (pyobject ()))
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
368 %!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
369 %!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
370 %!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
371 %!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
372 %!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
373
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
374 %!test
778c91283a73 Add isequal method to allow comparing pyobjects (fixes issue #53)
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 324
diff changeset
375 %! 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
376 %! 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
377 %! assert (isequal (A, B))