changeset 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 d0a7f66393fc
children cfd01e91d8e6
files @pyobject/methods.m
diffstat 1 files changed, 37 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/@pyobject/methods.m	Fri Aug 26 18:51:29 2016 -0700
+++ b/@pyobject/methods.m	Wed Jan 18 17:06:34 2017 +0530
@@ -1,4 +1,5 @@
 ## Copyright (C) 2016 Colin B. Macdonald
+## Copyright (C) 2017 NVS Abhilash
 ##
 ## This file is part of Pytave
 ##
@@ -18,12 +19,15 @@
 
 ## -*- texinfo -*-
 ## @documentencoding UTF-8
-## @defmethod @@pyobject methods (@var{x})
+## @defmethod  @@pyobject methods (@var{x})
+## @defmethodx @@pyobject methods (@var{x}, "-all")
 ## List the properties/callables of a Python object.
 ##
 ## Returns a cell array of strings, the names of the ``callables''
 ## of @var{x}.
 ##
+## If provided with an option @qcode{"-all"}, private methods are also included.
+##
 ## Example:
 ## @example
 ## @group
@@ -64,11 +68,35 @@
 ## @end defmethod
 
 
-function mtds = methods (x)
+function mtds = methods (x, option)
+
+  if (nargin < 1 || nargin > 2)
+    print_usage ();
+  endif
 
-  # filter the output of `dir(x)` to get callable methods only
-  cmd = pyeval (["lambda x: [a for a in dir(x)" ...
-                 " if callable(getattr(x, a)) and not a.startswith('_')]"]);
+  show_all = false;
+  if (nargin == 2)
+    if (ischar (option))
+      switch (tolower (option))
+        case "-all"
+          show_all = true;
+        otherwise
+          warning ("methods: unrecognized OPTION '%s'", option);
+      endswitch
+    else
+      error ("methods: OPTION must be a string");
+    endif
+  endif
+
+  query_end = "";
+  if (! show_all)
+    query_end = "and not a.startswith('_')";
+  endif
+
+  query = sprintf (["lambda x: [a for a in dir(x)" ...
+                    " if callable(getattr(x, a)) %s]"], query_end);
+
+  cmd = pyeval (query);
 
   mtds_list_obj = pycall (cmd, x);
 
@@ -107,3 +135,7 @@
 %!assert (methods (pyeval ("object()")), cell (0, 1))
 %!assert (ismember ("append", methods (pyeval ("[]"))))
 %!assert (ismember ("keys", methods (pyeval ("{}"))))
+%!assert (! ismember ("__getslice__", methods (pyeval ("[]"))))
+
+%!assert (ismember ("__getslice__", methods (pyeval ("[]"), "-all")))
+%!assert (ismember ("__repr__", methods (pyeval ("{}"), "-all")))