[Scipy-svn] r4488 - trunk/scipy/sandbox/mkufunc

scipy-svn at scipy.org scipy-svn at scipy.org
Fri Jun 27 21:34:30 EDT 2008


Author: ilan
Date: 2008-06-27 20:34:29 -0500 (Fri, 27 Jun 2008)
New Revision: 4488

Modified:
   trunk/scipy/sandbox/mkufunc/mkufunc.py
Log:
started work on typecasting

Modified: trunk/scipy/sandbox/mkufunc/mkufunc.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/mkufunc.py	2008-06-27 22:29:20 UTC (rev 4487)
+++ trunk/scipy/sandbox/mkufunc/mkufunc.py	2008-06-28 01:34:29 UTC (rev 4488)
@@ -6,6 +6,7 @@
 import sys
 import re
 import cStringIO
+from types import *
 
 import numpy
 import scipy.weave as weave
@@ -103,7 +104,6 @@
         assert len(found) == 1
         return found[0]
 
-
     def ufunc_support_code(self):
         arg0type = typedict[self.sig[0]][1]
         rettype = typedict[self.sig[-1]][1]
@@ -251,6 +251,7 @@
 
 
 def test2():
+    from numpy import array
 
     def sqr(x):
         return x * x
@@ -271,23 +272,48 @@
     print "y =", y, y.dtype
 
 
-def mkufunc(signatures):
+def mkufunc(arg0):
     """ The actual API function, to be used as decorator function.
         
     """
-    #print 'signatures', signatures
-    
     class Compile(object):
         
         def __init__(self, f):
+            print 'sigs:', signatures
             self.ufunc = genufunc(f, signatures)
+            #self.ufunc = f
 
         def __call__(self, *args):
             return self.ufunc(*args)
 
-    return Compile
+    if isinstance(arg0, FunctionType):
+        f = arg0
+        signatures = [float]
+        return Compile(f)
+    
+    elif isinstance(arg0, ListType):
+        signatures = arg0
+        return Compile
 
+    elif arg0 in typedict:
+        signatures = [arg0]
+        return Compile
 
+    else:
+        raise TypeError("first argument has to be a function, a type, or "
+                        "a list of signatures")
+
+
 if __name__ == '__main__':
     import doctest
-    doctest.testmod()
+    #doctest.testmod()
+
+    def sqr(x):
+        return x * x
+
+    #sqr = mkufunc({})(sqr)
+    sqr = mkufunc([(float, float)])(sqr)
+    #sqr = mkufunc(int)(sqr)
+    #sqr = mkufunc(sqr)
+
+    print sqr(8)




More information about the Scipy-svn mailing list