changeset 236:9da4ca336dda

Add 'length' property to @pyobject (fixes issue #9) * @pyobject/pyobject.m: Add the new length property and tests
author Abhinav Tripathi <genuinelucifer@gmail.com>
date Wed, 20 Jul 2016 23:33:35 -0700
parents 237b71aaf6e7
children 89a1f5a82467
files @pyobject/pyobject.m
diffstat 1 files changed, 18 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/@pyobject/pyobject.m	Wed Jul 20 14:24:09 2016 -0700
+++ b/@pyobject/pyobject.m	Wed Jul 20 23:33:35 2016 -0700
@@ -118,6 +118,14 @@
         vargout = {s};
       endif
     endfunction
+
+    function len = length (x)
+      try
+        len = pycall ("len", x);
+      catch
+        len = 1;
+      end_try_catch
+    endfunction
   endmethods
 endclassdef
 
@@ -126,3 +134,13 @@
 %! pyexec ("import sys")
 %! A = pyeval ("sys");
 %! assert (isa (A, "pyobject"))
+
+%!test
+%! pyobj = pyeval ("{1:2, 2:3, 3:4}");
+%! assert (isa (pyobj, "pyobject"))
+%! assert (length (pyobj), 3)
+
+%!test
+%! pyexec ("import sys");
+%! pyobj = pyeval ("sys");
+%! assert (length (pyobj), 1)