annotate @pyobject/methods.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 d1e00e614b4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
5589b04e9056 Docs fixes for methods
Colin Macdonald <cbm@m.fsf.org>
parents: 255
diff changeset
24 ## Returns a cell array of strings, the names of the ``callables''
5589b04e9056 Docs fixes for methods
Colin Macdonald <cbm@m.fsf.org>
parents: 255
diff changeset
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
5589b04e9056 Docs fixes for methods
Colin Macdonald <cbm@m.fsf.org>
parents: 255
diff changeset
49 ## To get the properties (non-callables) of an object,
5589b04e9056 Docs fixes for methods
Colin Macdonald <cbm@m.fsf.org>
parents: 255
diff changeset
50 ## @pxref{@@pyobject/fieldnames}.
5589b04e9056 Docs fixes for methods
Colin Macdonald <cbm@m.fsf.org>
parents: 255
diff changeset
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
e61bee04f468 Fix typos
Colin Macdonald <cbm@m.fsf.org>
parents: 256
diff changeset
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:
302
de98627cf8ae Minor fixes for failing doctests
Colin Macdonald <cbm@m.fsf.org>
parents: 298
diff changeset
58 ## @print{} ... display ...
222
e2e64ee571b7 pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
59 ## @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
60 ## @end group
e2e64ee571b7 pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
61 ## @end example
e2e64ee571b7 pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
62 ##
256
5589b04e9056 Docs fixes for methods
Colin Macdonald <cbm@m.fsf.org>
parents: 255
diff changeset
63 ## @seealso{methods, @@pyobject/fieldnames}
222
e2e64ee571b7 pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
64 ## @end defmethod
e2e64ee571b7 pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
65
e2e64ee571b7 pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
66
250
a4a2c214415e @pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
67 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
68
256
5589b04e9056 Docs fixes for methods
Colin Macdonald <cbm@m.fsf.org>
parents: 255
diff changeset
69 # 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
70 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
71 " 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
72
289
49832ca978fd Use new subsref indexing to fix the failing tests
Abhinav Tripathi <genuinelucifer@gmail.com>
parents: 285
diff changeset
73 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
74
298
df1bddece9d5 Use subsref in "cell" method for casting to cell
Colin Macdonald <cbm@m.fsf.org>
parents: 292
diff changeset
75 mtds_list = cellfun (@char, cell (mtds_list_obj), "uniformoutput", false);
250
a4a2c214415e @pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
76
a4a2c214415e @pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
77 if (nargout == 0)
367
9d7188514f2c Use pyobject.isa instead of lambda to check Python types
Mike Miller <mtmiller@octave.org>
parents: 365
diff changeset
78 if (isa (x, "py.types.ModuleType"))
365
087e7bc3697f Do not automatically convert Python strings to Octave strings (fixes issue #65)
Mike Miller <mtmiller@octave.org>
parents: 302
diff changeset
79 modulename = char (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
80 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
81 else
373
0e4097c66788 Report a Python object's fully qualified class name correctly
Mike Miller <mtmiller@octave.org>
parents: 367
diff changeset
82 printf ("Methods for Python class '%s':\n", __py_class_name__ (x));
250
a4a2c214415e @pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
83 endif
a4a2c214415e @pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
84 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
85 else
254
a3660e7f8dc9 Make methods output shape consistent with fieldnames
Colin Macdonald <cbm@m.fsf.org>
parents: 250
diff changeset
86 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
87 endif
a4a2c214415e @pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
88
222
e2e64ee571b7 pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
89 endfunction
e2e64ee571b7 pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
90
e2e64ee571b7 pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
91
e2e64ee571b7 pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
92 %!test
250
a4a2c214415e @pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
93 %! 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
94 %! m = methods (sys);
a4a2c214415e @pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
95 %! assert (iscellstr (m))
a4a2c214415e @pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
96 %! assert (any (strcmp (m, "exit")))
222
e2e64ee571b7 pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
97
e2e64ee571b7 pyobject: rename whatmethods to methods (fixes #21)
Colin Macdonald <cbm@m.fsf.org>
parents:
diff changeset
98 %!test
250
a4a2c214415e @pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
99 %! 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
100 %! m = methods (os);
a4a2c214415e @pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
101 %! assert (iscellstr (m))
a4a2c214415e @pyobject/methods: Only list callable methods, format output with no lvalue
Mike Miller <mtmiller@octave.org>
parents: 246
diff changeset
102 %! 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
103 %! 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
104 %! 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
105 %! 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
106
510ed59a61d3 pyobject: fix fieldnames and methods when result is an empty list
Mike Miller <mtmiller@octave.org>
parents: 289
diff changeset
107 %!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
108 %!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
109 %!assert (ismember ("keys", methods (pyeval ("{}"))))