Mercurial > pytave
annotate @pyobject/pyobject.m @ 222:e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Usually `methods` would give the methods of the Octave class.
Here we use it to get the properties/callables of a Python object.
Add documentation which explains how to get the former behaviour.
Add BIST.
@pyobject/pyobject.m: remove original function
@pyobject/methods.m: new file
author | Colin Macdonald <cbm@m.fsf.org> |
---|---|
date | Fri, 17 Jun 2016 16:13:36 -0700 |
parents | a76d75683455 |
children | 06835a3e3384 |
rev | line source |
---|---|
209 | 1 %% Copyright (C) 2016 Colin B. Macdonald |
2 %% | |
3 %% This file is part of PyTave. | |
4 %% | |
5 %% OctSymPy is free software; you can redistribute it and/or modify | |
6 %% it under the terms of the GNU General Public License as published | |
7 %% by the Free Software Foundation; either version 3 of the License, | |
8 %% or (at your option) any later version. | |
9 %% | |
10 %% This software is distributed in the hope that it will be useful, | |
11 %% but WITHOUT ANY WARRANTY; without even the implied warranty | |
12 %% of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | |
13 %% the GNU General Public License for more details. | |
14 %% | |
15 %% You should have received a copy of the GNU General Public | |
16 %% License along with this software; see the file COPYING. | |
17 %% If not, see <http://www.gnu.org/licenses/>. | |
18 | |
19 %% -*- texinfo -*- | |
20 %% @documentencoding UTF-8 | |
21 %% @defun pyobject (@var{s}) | |
22 %% Wrap a Python object. | |
23 %% | |
24 %% TODO: where/how to document classdef classes? | |
25 %% | |
26 %% @seealso{pyexec, pyeval} | |
27 %% @end defun | |
28 | |
29 classdef pyobject < handle | |
30 properties | |
31 id | |
32 end | |
33 | |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
34 methods (Static) |
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
35 function x = fromPythonVarName (pyvarname) |
209 | 36 % if @var{pyvarname} is a string, its assumed to be a variable |
37 % name, e.g., previously created with pyexec. This must exist | |
38 % at the time of construction but it can disappear later (we | |
39 % will keep track of the reference). | |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
40 if (! ischar(pyvarname)) |
209 | 41 error('pyobject: currently we only take variable names as input') |
42 end | |
43 cmd = sprintf ([ ... | |
44 'if not ("__InOct__" in vars() or "__InOct__" in globals()):\n' ... | |
217
69abda471c67
Attempt at moving pyobject from octave to python
Colin Macdonald <cbm@m.fsf.org>
parents:
216
diff
changeset
|
45 ' __InOct__ = dict()\n' ... |
69abda471c67
Attempt at moving pyobject from octave to python
Colin Macdonald <cbm@m.fsf.org>
parents:
216
diff
changeset
|
46 ' # FIXME: make it accessible elsewhere?\n' ... |
69abda471c67
Attempt at moving pyobject from octave to python
Colin Macdonald <cbm@m.fsf.org>
parents:
216
diff
changeset
|
47 ' import __main__\n' ... |
69abda471c67
Attempt at moving pyobject from octave to python
Colin Macdonald <cbm@m.fsf.org>
parents:
216
diff
changeset
|
48 ' __main__.__InOct__ = __InOct__\n' ... |
209 | 49 '__InOct__[hex(id(%s))] = %s' ], ... |
50 pyvarname, pyvarname); | |
51 pyexec (cmd); | |
52 id = pyeval (['hex(id(' pyvarname '))']); | |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
53 x = pyobject (id); |
209 | 54 end |
55 end | |
56 | |
57 | |
58 methods | |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
59 function x = pyobject (id) |
209 | 60 % warning: not intended for casual use: you must also insert |
61 % the object into the Python `__InOct__` dict with key `id`. | |
62 x.id = id; | |
63 end | |
64 | |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
65 function delete (x) |
209 | 66 % called on clear of the last reference---for subclasses of |
67 % handle; not called at all for "value classes". | |
68 % FIXME: #46497 this is never called! | |
69 %save('proof_of_delete', 6, x.id) | |
70 disp ('delete') | |
71 % throws KeyError if it wasn't in there for some reason | |
72 cmd = sprintf ('__InOct__.pop("%s")', x.id); | |
73 pyexec (cmd); | |
74 end | |
75 | |
76 function force_delete (x) | |
77 % Manual workaround for #46497: call right before @code{clear x}. But | |
78 % be careful, @code{x} needs to be the last reference: don't do this: | |
79 % @example | |
80 % d = pyobject (...); | |
81 % d2 = d; | |
82 % force_delete (d) | |
83 % clear d | |
84 % d2 | |
85 % @print{} ... KeyError ... | |
86 % @end example | |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
87 delete (x) |
209 | 88 end |
89 | |
90 dummy (x) | |
216 | 91 display (x) |
221
a76d75683455
pyobject: split subsref to its own file, reformat
Colin Macdonald <cbm@m.fsf.org>
parents:
220
diff
changeset
|
92 subsref (x, idx) |
209 | 93 |
94 function r = getid (x) | |
95 r = x.id; | |
96 end | |
97 | |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
98 function varargout = disp (x) |
216 | 99 s = pyeval (sprintf ('str(__InOct__["%s"])', x.id)); |
100 if (nargout == 0) | |
101 disp (s) | |
102 else | |
103 varargout = {s}; | |
104 end | |
209 | 105 end |
106 | |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
107 function s = whatclass (x) |
209 | 108 s = pyeval (sprintf ('str(__InOct__["%s"].__class__)', x.id)); |
109 end | |
110 | |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
111 function vargout = help (x) |
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
112 idx = struct ('type', '.', 'subs', '__doc__'); |
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
113 s = subsref (x, idx); |
213
a54ab59c491d
merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents:
209
diff
changeset
|
114 if (nargout == 0) |
218
02da92723889
Change style, close to Octave conventions
Colin Macdonald <cbm@m.fsf.org>
parents:
217
diff
changeset
|
115 disp (s) |
213
a54ab59c491d
merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents:
209
diff
changeset
|
116 else |
a54ab59c491d
merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents:
209
diff
changeset
|
117 vargout = {s}; |
a54ab59c491d
merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents:
209
diff
changeset
|
118 end |
a54ab59c491d
merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents:
209
diff
changeset
|
119 end |
a54ab59c491d
merge genuinelucifer/cbm_pytave to macdonald/cbm_pytave
Colin Macdonald <cbm@m.fsf.org>
parents:
209
diff
changeset
|
120 |
209 | 121 end |
122 end |