Mercurial > pytave
annotate @pyobject/methods.m @ 292:510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
* @pyobject/fieldnames.m, @pyobject/methods.m: Handle conversion of pyobject
attribute list when empty. Add explicit conversion to char in anticipation of
future changes. Add %!tests.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Wed, 03 Aug 2016 16:23:09 -0700 |
parents | 49832ca978fd |
children | df1bddece9d5 |
rev | line source |
---|---|
222
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
1 ## Copyright (C) 2016 Colin B. Macdonald |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
2 ## |
246
4acc64f89d67
maint: fix copyright header comment blocks
Mike Miller <mtmiller@octave.org>
parents:
227
diff
changeset
|
3 ## This file is part of Pytave |
222
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
4 ## |
246
4acc64f89d67
maint: fix copyright header comment blocks
Mike Miller <mtmiller@octave.org>
parents:
227
diff
changeset
|
5 ## Pytave is free software; you can redistribute it and/or modify |
222
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
7 ## by the Free Software Foundation; either version 3 of the License, |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
8 ## or (at your option) any later version. |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
9 ## |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
10 ## This software is distributed in the hope that it will be useful, |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
12 ## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
13 ## the GNU General Public License for more details. |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
14 ## |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
15 ## You should have received a copy of the GNU General Public |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
16 ## License along with this software; see the file COPYING. |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
17 ## If not, see <http://www.gnu.org/licenses/>. |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
18 |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
19 ## -*- texinfo -*- |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
20 ## @documentencoding UTF-8 |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
21 ## @defmethod @@pyobject methods (@var{x}) |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
22 ## List the properties/callables of a Python object. |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
23 ## |
256 | 24 ## Returns a cell array of strings, the names of the ``callables'' |
25 ## of @var{x}. | |
222
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
26 ## |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
27 ## Example: |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
28 ## @example |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
29 ## @group |
250
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
30 ## pyexec ("import os") |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
31 ## os = pyeval ("os"); |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
32 ## methods (os) |
255
9eaed3b11829
Indicate this is a Python class/module in method output
Colin Macdonald <cbm@m.fsf.org>
parents:
254
diff
changeset
|
33 ## @print{} Methods for Python module 'os': |
250
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
34 ## @print{} ... |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
35 ## @print{} chdir ... |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
36 ## @print{} ... |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
37 ## x = methods (os) |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
38 ## @result{} x = |
222
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
39 ## @{ |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
40 ## [1,1] = ... |
254
a3660e7f8dc9
Make methods output shape consistent with fieldnames
Colin Macdonald <cbm@m.fsf.org>
parents:
250
diff
changeset
|
41 ## [2,1] = ... |
250
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
42 ## ... = chdir |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
43 ## ... = getenv |
222
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
44 ## ... |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
45 ## @} |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
46 ## @end group |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
47 ## @end example |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
48 ## |
256 | 49 ## To get the properties (non-callables) of an object, |
50 ## @pxref{@@pyobject/fieldnames}. | |
51 ## | |
222
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
52 ## Note that if you instead want the methods implemented by |
257 | 53 ## the Octave class @code{@@pyobject}, you can always do: |
222
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
54 ## @example |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
55 ## @group |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
56 ## methods pyobject |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
57 ## @print{} Methods for class pyobject: |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
58 ## @print{} display ... subsref |
250
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
59 ## @print{} ... |
222
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
60 ## @comment this doctest may need updating as we add methods |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
61 ## @end group |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
62 ## @end example |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
63 ## |
256 | 64 ## @seealso{methods, @@pyobject/fieldnames} |
222
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
65 ## @end defmethod |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
66 |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
67 |
250
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
68 function mtds = methods (x) |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
69 |
256 | 70 # filter the output of `dir(x)` to get callable methods only |
284
6c133bcac33a
use pyobject instead of accessing the InOct dict
Colin Macdonald <cbm@m.fsf.org>
parents:
257
diff
changeset
|
71 cmd = pyeval (["lambda x: [a for a in dir(x)" ... |
285
06a4b461b141
pyobject: hide methods which start with underscore (fixes issue #23)
Colin Macdonald <cbm@m.fsf.org>
parents:
284
diff
changeset
|
72 " if callable(getattr(x, a)) and not a.startswith('_')]"]); |
250
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
73 |
289
49832ca978fd
Use new subsref indexing to fix the failing tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
285
diff
changeset
|
74 mtds_list_obj = pycall (cmd, x); |
292
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
75 |
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
76 ## FIXME: mtds_list = cellfun (@char, cell (mtds_list_obj), "uniformoutput", false); |
289
49832ca978fd
Use new subsref indexing to fix the failing tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents:
285
diff
changeset
|
77 len = length (mtds_list_obj); |
292
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
78 mtds_list = cell (len, 1); |
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
79 if (len > 0) |
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
80 idx = struct ("type", "{}", "subs", {{1:len}}); |
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
81 [mtds_list{1:len}] = subsref (mtds_list_obj, idx); |
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
82 mtds_list = cellfun (@char, mtds_list, "uniformoutput", false); |
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
83 endif |
250
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
84 |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
85 if (nargout == 0) |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
86 ## FIXME: should this be available as @pyobject/ismodule.m ? |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
87 is_module = pyeval ("lambda x: isinstance(x, __import__('types').ModuleType)"); |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
88 |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
89 if (pycall (is_module, x)) |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
90 modulename = pycall ("getattr", x, "__name__"); |
255
9eaed3b11829
Indicate this is a Python class/module in method output
Colin Macdonald <cbm@m.fsf.org>
parents:
254
diff
changeset
|
91 printf ("Methods for Python module '%s':\n", modulename); |
250
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
92 else |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
93 ## FIXME: should be `class (x)` |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
94 classref = pycall ("getattr", x, "__class__"); |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
95 classname = pycall ("getattr", classref, "__name__"); |
255
9eaed3b11829
Indicate this is a Python class/module in method output
Colin Macdonald <cbm@m.fsf.org>
parents:
254
diff
changeset
|
96 printf ("Methods for Python class '%s':\n", classname); |
250
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
97 endif |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
98 disp (list_in_columns (mtds_list)); |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
99 else |
254
a3660e7f8dc9
Make methods output shape consistent with fieldnames
Colin Macdonald <cbm@m.fsf.org>
parents:
250
diff
changeset
|
100 mtds = mtds_list(:); |
250
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
101 endif |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
102 |
222
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
103 endfunction |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
104 |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
105 |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
106 %!test |
250
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
107 %! sys = pycall ("__import__", "sys"); |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
108 %! m = methods (sys); |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
109 %! assert (iscellstr (m)) |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
110 %! assert (any (strcmp (m, "exit"))) |
222
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
111 |
e2e64ee571b7
pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff
changeset
|
112 %!test |
250
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
113 %! os = pycall ("__import__", "os"); |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
114 %! m = methods (os); |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
115 %! assert (iscellstr (m)) |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
116 %! assert (any (strcmp (m, "chdir"))) |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
117 %! assert (any (strcmp (m, "getcwd"))) |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
118 %! assert (any (strcmp (m, "getenv"))) |
a4a2c214415e
@pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents:
246
diff
changeset
|
119 %! assert (any (strcmp (m, "getpid"))) |
292
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
120 |
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
121 %!assert (methods (pyeval ("object()")), cell (0, 1)) |
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
122 %!assert (ismember ("append", methods (pyeval ("[]")))) |
510ed59a61d3
pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents:
289
diff
changeset
|
123 %!assert (ismember ("keys", methods (pyeval ("{}")))) |