comparison @pyobject/methods.m @ 376:d1e00e614b4c

* @pyobject/methods.m: OPTION to view private methods added (fixes issue #22)
author NVS Abhilash <nvs232@gmail.com>
date Wed, 18 Jan 2017 17:06:34 +0530
parents 0e4097c66788
children cfd01e91d8e6
comparison
equal deleted inserted replaced
375:d0a7f66393fc 376:d1e00e614b4c
1 ## Copyright (C) 2016 Colin B. Macdonald 1 ## Copyright (C) 2016 Colin B. Macdonald
2 ## Copyright (C) 2017 NVS Abhilash
2 ## 3 ##
3 ## This file is part of Pytave 4 ## This file is part of Pytave
4 ## 5 ##
5 ## Pytave is free software; you can redistribute it and/or modify 6 ## Pytave is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published 7 ## it under the terms of the GNU General Public License as published
16 ## License along with this software; see the file COPYING. 17 ## License along with this software; see the file COPYING.
17 ## If not, see <http://www.gnu.org/licenses/>. 18 ## If not, see <http://www.gnu.org/licenses/>.
18 19
19 ## -*- texinfo -*- 20 ## -*- texinfo -*-
20 ## @documentencoding UTF-8 21 ## @documentencoding UTF-8
21 ## @defmethod @@pyobject methods (@var{x}) 22 ## @defmethod @@pyobject methods (@var{x})
23 ## @defmethodx @@pyobject methods (@var{x}, "-all")
22 ## List the properties/callables of a Python object. 24 ## List the properties/callables of a Python object.
23 ## 25 ##
24 ## Returns a cell array of strings, the names of the ``callables'' 26 ## Returns a cell array of strings, the names of the ``callables''
25 ## of @var{x}. 27 ## of @var{x}.
28 ##
29 ## If provided with an option @qcode{"-all"}, private methods are also included.
26 ## 30 ##
27 ## Example: 31 ## Example:
28 ## @example 32 ## @example
29 ## @group 33 ## @group
30 ## pyexec ("import os") 34 ## pyexec ("import os")
62 ## 66 ##
63 ## @seealso{methods, @@pyobject/fieldnames} 67 ## @seealso{methods, @@pyobject/fieldnames}
64 ## @end defmethod 68 ## @end defmethod
65 69
66 70
67 function mtds = methods (x) 71 function mtds = methods (x, option)
68 72
69 # filter the output of `dir(x)` to get callable methods only 73 if (nargin < 1 || nargin > 2)
70 cmd = pyeval (["lambda x: [a for a in dir(x)" ... 74 print_usage ();
71 " if callable(getattr(x, a)) and not a.startswith('_')]"]); 75 endif
76
77 show_all = false;
78 if (nargin == 2)
79 if (ischar (option))
80 switch (tolower (option))
81 case "-all"
82 show_all = true;
83 otherwise
84 warning ("methods: unrecognized OPTION '%s'", option);
85 endswitch
86 else
87 error ("methods: OPTION must be a string");
88 endif
89 endif
90
91 query_end = "";
92 if (! show_all)
93 query_end = "and not a.startswith('_')";
94 endif
95
96 query = sprintf (["lambda x: [a for a in dir(x)" ...
97 " if callable(getattr(x, a)) %s]"], query_end);
98
99 cmd = pyeval (query);
72 100
73 mtds_list_obj = pycall (cmd, x); 101 mtds_list_obj = pycall (cmd, x);
74 102
75 mtds_list = cellfun (@char, cell (mtds_list_obj), "uniformoutput", false); 103 mtds_list = cellfun (@char, cell (mtds_list_obj), "uniformoutput", false);
76 104
105 %! assert (any (strcmp (m, "getpid"))) 133 %! assert (any (strcmp (m, "getpid")))
106 134
107 %!assert (methods (pyeval ("object()")), cell (0, 1)) 135 %!assert (methods (pyeval ("object()")), cell (0, 1))
108 %!assert (ismember ("append", methods (pyeval ("[]")))) 136 %!assert (ismember ("append", methods (pyeval ("[]"))))
109 %!assert (ismember ("keys", methods (pyeval ("{}")))) 137 %!assert (ismember ("keys", methods (pyeval ("{}"))))
138 %!assert (! ismember ("__getslice__", methods (pyeval ("[]"))))
139
140 %!assert (ismember ("__getslice__", methods (pyeval ("[]"), "-all")))
141 %!assert (ismember ("__repr__", methods (pyeval ("{}"), "-all")))