Mercurial > pytave
changeset 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 |
files | @pyobject/methods.m @pyobject/pyobject.m |
diffstat | 2 files changed, 83 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/@pyobject/methods.m Fri Jun 17 16:13:36 2016 -0700 @@ -0,0 +1,83 @@ +## Copyright (C) 2016 Colin B. Macdonald +## +## This file is part of PyTave. +## +## OctSymPy 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 methods (@var{x}) +## List the properties/callables of a Python object. +## +## Returns a cell array of strings, the names of the properties +## and ``callables'' of @var{x}. +## +## Example: +## @example +## @group +## pyexec ("import sys") +## sys = pyeval ("sys"); +## methods (sys) +## @result{} ans = +## @{ +## [1,1] = ... +## [1,2] = ... +## ... = path +## ... = version +## ... +## @} +## @end group +## @end example +## +## Note that if you instead want the methods implemented by +## the Octave class @code{@@pyobject}, use can always do: +## @example +## @group +## methods pyobject +## @print{} Methods for class pyobject: +## @print{} display ... subsref +## @comment this doctest may need updating as we add methods +## @end group +## @end example +## +## @seealso{methods} +## @end defmethod + + +function L = methods (x) + # filter the output of `dir(x)` + # (to get properties only: + # [a for a in dir(x) if not callable(getattr(x, a)) and not a.startswith('__')] + cmd = sprintf ( ... + "[a for a in dir(__InOct__['%s']) if not a.startswith('__')]", ... + getid(x)); + # TODO: may need to convert from Python list to Octave list + L = pyeval (cmd); +endfunction + + +%!test +%! pyexec ("import sys") +%! sys = pyeval ("sys"); +%! L = methods (sys); +%! % sys has lots of methods +%! assert (length (L) >= 32) +%! % version is one of them +%! assert (any (strcmp (L, "version"))) + +%!test +%! pyexec ("import sys") +%! L = methods (pyeval ("sys")); +%! assert (iscell (L))
--- a/@pyobject/pyobject.m Thu Jun 16 23:56:23 2016 -0700 +++ b/@pyobject/pyobject.m Fri Jun 17 16:13:36 2016 -0700 @@ -108,16 +108,6 @@ s = pyeval (sprintf ('str(__InOct__["%s"].__class__)', x.id)); end - function lst = whatmethods (x) - % filter the output of `dir(x)` - % properties only: - % [a for a in dir(x) if not callable(getattr(x, a)) and not a.startswith("__")] - cmd = sprintf ( ... - '[a for a in dir(__InOct__["%s"]) if not a.startswith("__")]', ... - x.id); - lst = pyeval (cmd); - end - function vargout = help (x) idx = struct ('type', '.', 'subs', '__doc__'); s = subsref (x, idx);