Mercurial > pytave
view @pyobject/fieldnames.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 |
line wrap: on
line source
## Copyright (C) 2016 Mike Miller ## ## This file is part of Pytave ## ## Pytave is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published ## by the Free Software Foundation; either version 3 of the License, ## or (at your option) any later version. ## ## This software is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty ## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See ## the GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public ## License along with this software; see the file COPYING. ## If not, see <http://www.gnu.org/licenses/>. ## -*- texinfo -*- ## @documentencoding UTF-8 ## @defmethod @@pyobject fieldnames (@var{x}) ## List the properties of a Python object. ## ## Returns a cell array of strings containing the names of the properties ## of @var{x}. ## ## Example: ## @example ## @group ## pyexec ("import sys") ## sys = pyeval ("sys"); ## fieldnames (sys) ## @result{} ans = ## @{ ## [1,1] = ... ## [2,1] = ... ## ... = path ## ... = version ## ... ## @} ## @end group ## @end example ## ## @seealso{fieldnames, @@pyobject/methods} ## @end defmethod function names = fieldnames (x) cmd = pyeval (["lambda x: [a for a in dir(x)" ... " if not callable(getattr(x, a))" ... " and not isinstance(getattr(x, a), __import__('types').ModuleType)" ... " and not a.startswith('_')]"]); names_obj = pycall (cmd, x); ## FIXME: names = cellfun (@char, cell (names_obj), "uniformoutput", false); len = length (names_obj); names = cell (len, 1); if (len > 0) idx = struct ("type", "{}", "subs", {{1:len}}); [names{1:len}] = subsref (names_obj, idx); names = cellfun (@char, names, "uniformoutput", false); endif endfunction %!test %! sys = pycall ("__import__", "sys"); %! lst = fieldnames (sys); %! assert (iscell (lst)) %! assert (length (lst) >= 32) %! assert (any (strcmp (lst, "path"))) %! assert (any (strcmp (lst, "prefix"))) %! assert (any (strcmp (lst, "stderr"))) %! assert (any (strcmp (lst, "stdin"))) %! assert (any (strcmp (lst, "stdout"))) %! assert (any (strcmp (lst, "version"))) %!test %! lst = fieldnames (pyeval ("__builtins__")); %! assert (any (strcmp (lst, "False"))) %! assert (any (strcmp (lst, "None"))) %! assert (any (strcmp (lst, "True"))) %!assert (fieldnames (pyeval ("object()")), cell (0, 1)) %!assert (fieldnames (pyeval ("{}")), cell (0, 1)) %!assert (ismember ("denominator", fieldnames (pycall ("fractions.Fraction"))))