[Scipy-svn] r3229 - trunk/Lib/weave/examples

scipy-svn@scip... scipy-svn@scip...
Mon Aug 13 14:16:55 CDT 2007


Author: eric
Date: 2007-08-13 14:16:51 -0500 (Mon, 13 Aug 2007)
New Revision: 3229

Modified:
   trunk/Lib/weave/examples/binary_search.py
   trunk/Lib/weave/examples/object.py
Log:
cleaned up a few weave demos

Modified: trunk/Lib/weave/examples/binary_search.py
===================================================================
--- trunk/Lib/weave/examples/binary_search.py	2007-08-13 13:14:46 UTC (rev 3228)
+++ trunk/Lib/weave/examples/binary_search.py	2007-08-13 19:16:51 UTC (rev 3229)
@@ -104,7 +104,7 @@
                {
                    if (max < min )
                    {
-                       return_val = PyInt_FromLong(-1);
+                       return_val = -1;
                        break;
                    }
                    m = (min + max) / 2;
@@ -115,7 +115,7 @@
                        max = m - 1;
                    else
                    {
-                       return_val = PyInt_FromLong(m);
+                       return_val = m;
                        break;
                    }
                }

Modified: trunk/Lib/weave/examples/object.py
===================================================================
--- trunk/Lib/weave/examples/object.py	2007-08-13 13:14:46 UTC (rev 3228)
+++ trunk/Lib/weave/examples/object.py	2007-08-13 19:16:51 UTC (rev 3229)
@@ -1,8 +1,8 @@
-# h:\wrk\scipy\weave\examples>python object.py
-# initial val: 1
-# inc result: 2
-# after set attr: 5
+""" Attribute and method access on Python objects from C++.
 
+    Note: std::cout type operations currently crash python...
+          Not sure what is up with this...
+"""
 import scipy.weave as weave
 
 #----------------------------------------------------------------------------
@@ -13,24 +13,29 @@
     def __init__(self):
         self.val = 1
     def inc(self,amount):
-        self.val += 1
+        self.val += amount
         return self.val
 obj = foo()
 code = """
+       py::tuple result(3);
+
        int i = obj.attr("val");
-       std::cout << "initial val: " << i << std::endl;
+       result[0] = i;
 
        py::tuple args(1);
        args[0] = 2;
        i = obj.mcall("inc",args);
-       std::cout << "inc result: " << i << std::endl;
-
+       result[1] = i;
+       
        obj.set_attr("val",5);
        i = obj.attr("val");
-       std::cout << "after set attr: " << i << std::endl;
+       result[2] = i;
+
+       return_val = result;
        """
-weave.inline(code,['obj'])
 
+print 'initial, inc(2), set(5)/get:', weave.inline(code,['obj'])
+
 #----------------------------------------------------------------------------
 # indexing of values.
 #----------------------------------------------------------------------------
@@ -38,11 +43,11 @@
 obj = UserList([1,[1,2],"hello"])
 code = """
        int i;
-       // find obj length and accesss each of its items
-       std::cout << "UserList items: ";
-       for(i = 0; i < obj.length(); i++)
-           std::cout << obj[i].str() << " ";
-       std::cout << std::endl;
+       // find obj length and access each of its items
+       //std::cout << "UserList items: ";
+       //for(i = 0; i < obj.length(); i++)
+       //    std::cout << obj[i].str() << " ";
+       //std::cout << std::endl;
        // assign new values to each of its items
        for(i = 0; i < obj.length(); i++)
            obj[i] = "goodbye";



More information about the Scipy-svn mailing list