changeset 205:98cde0dcf09f

support chains of pyobj methods and properties For now, calling something with arguments will just give a "not implemented" error. But simple calls like `.keys()` will work. So should chains like `.method().prop1.anothermethod().anotherproperty`. * @pyobj/pyobj.m: subsref: support method calls * @pyobj/dummy.m: fix one TODO, add another
author Colin Macdonald <cbm@m.fsf.org>
date Fri, 20 May 2016 23:04:31 -0700
parents 61df785bd8b0
children dfaf3abfbb8f
files @pyobj/dummy.m @pyobj/pyobj.m
diffstat 2 files changed, 35 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/@pyobj/dummy.m	Fri May 20 22:12:32 2016 -0700
+++ b/@pyobj/dummy.m	Fri May 20 23:04:31 2016 -0700
@@ -45,9 +45,7 @@
 %% We can accesss ``callables'' (methods) of objects:
 %% @example
 %% @group
-%% % x.keys()   % FIXME: should be this but its broken
-%% ddotkeys = x.keys;
-%% ddotkeys()
+%% x.keys()
 %%   @result{} ans =
 %%       @{
 %%         [1,1] = two
@@ -77,6 +75,22 @@
 %% @end example
 %%
 %%
+%% TODO: this should return a cell array with a double, a string,
+%% and an @@pyobj in it:
+%% @example
+%% @group
+%% pyeval('[42, "hello", sys]')         % doctest: +XFAIL
+%%   @result{} ans =
+%%       @{
+%%         [1,1] =  42
+%%         [1,2] = hello
+%%         [1,3] =
+%%           [PyObject id ...]
+%%           <module 'sys' (built-in)>
+%%       @}
+%% @end group
+%% @end example
+%%
 %% @seealso{pyobj}
 %% @end defmethod
 
--- a/@pyobj/pyobj.m	Fri May 20 22:12:32 2016 -0700
+++ b/@pyobj/pyobj.m	Fri May 20 23:04:31 2016 -0700
@@ -133,20 +133,25 @@
     end
 
     function r = subsref(x, idx)
-      switch idx.type
-        case '()'
-          if ( ~strcmp (idx.subs, ''))
-	    idx
-            error('not implemented: function calls with arguments')
-          end
-          r = pyeval (sprintf ('__InOct__["%s"]()', x.id));
-        case '.'
-          assert(ischar(idx.subs))
-          r = pyeval (sprintf ('__InOct__["%s"].%s', x.id, idx.subs));
-        otherwise
-          idx
-          error('not implemented')
+      s = '';
+      for i=1:length(idx)
+        t = idx(i);
+        switch t.type
+          case '()'
+            if ( ! isempty (t.subs))
+              t
+              error('not implemented: function calls with arguments')
+            end
+            s = sprintf ('%s()', s);
+          case '.'
+            assert(ischar(t.subs))
+            s = sprintf ('%s.%s', s, t.subs);
+          otherwise
+            t
+            error('not implemented')
+        end
       end
+      r = pyeval (sprintf ('__InOct__["%s"]%s', x.id, s));
     end
 
   end