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

scipy-svn at scipy.org scipy-svn at scipy.org
Sun Jun 29 20:07:42 EDT 2008


Author: ilan
Date: 2008-06-29 19:07:36 -0500 (Sun, 29 Jun 2008)
New Revision: 4501

Added:
   trunk/scipy/sandbox/mkufunc/test_funcutil.py
Modified:
   trunk/scipy/sandbox/mkufunc/funcutil.py
Log:
Moved tests into seperate file

Modified: trunk/scipy/sandbox/mkufunc/funcutil.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/funcutil.py	2008-06-29 22:21:36 UTC (rev 4500)
+++ trunk/scipy/sandbox/mkufunc/funcutil.py	2008-06-30 00:07:36 UTC (rev 4501)
@@ -3,7 +3,7 @@
 
 def disassemble(co):
     """ Given a code object, return output from dis.disassemble as a string.
-
+    
     (dis.disassemble writes its output to stdout.)
     """
     tmp = sys.stdout
@@ -13,10 +13,11 @@
     sys.stdout = tmp
     return res
 
+
 pat_norep = re.compile(r'<[^<>]*>')
 pat_white = re.compile(r'\s+')
 
-def disassemble2(co):
+def dis2(co):
     acc = cStringIO.StringIO()
     for line in disassemble(co).splitlines():
         line = line[16:].strip()
@@ -42,41 +43,18 @@
     return res
 
 
-def func_hash(f, extra=None):
-    txt = disassemble2(f.func_code) + repr(extra)
-    #print txt
+def func_hash(f, salt=None, verbose=0):
+    """ Return the MD5 hash for a function object as string.
+
+    'salt' can be any object that has a representation
+    """
+    txt = dis2(f.func_code) + repr(salt)
+    if verbose:
+        print txt
+    
     txt = pat_white.sub(' ', txt)
     return hashlib.md5(txt).hexdigest()
 
-if __name__ == '__main__':
-#    import math
-#    from math import *
-    
-    md5sums = []
-    
-    b = 3.14159
-    g = lambda x: x
-    def h(n):
-        return n + 3
-    
-    for a in xrange(2):
-        def f(x):
-            inner1 = lambda t: t/3.0
-            def inner2(): return
-            t = b + g(42) + h(4)
-            return sin(pi * x) + a + t
-        md5sums.append(func_hash(f))
-   
 
-    def f(x):
-        return math.sin(x)
-    md5sums.append(func_hash(f))
-
-    def f(x):
-        return sin(x)
-    md5sums.append(func_hash(f, float))
-    
-    print md5sums
-    #assert md5sums == ['91d13599d610a554dccd6b44cb5ef1f0',
-    #                   'be0c54b477180f897cbf7604fc565d18',
-    #                   '732d1ef6c1ce8cc92a7f28917496d292']
+if __name__ == '__main__':
+    pass

Added: trunk/scipy/sandbox/mkufunc/test_funcutil.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/test_funcutil.py	2008-06-29 22:21:36 UTC (rev 4500)
+++ trunk/scipy/sandbox/mkufunc/test_funcutil.py	2008-06-30 00:07:36 UTC (rev 4501)
@@ -0,0 +1,47 @@
+import unittest
+
+from funcutil import func_hash
+
+
+class Tests(unittest.TestCase):
+    
+    def test_simple(self):
+        
+        def f(x):
+            return 2.5 * x * x + 4.7 * x
+        
+        self.assertEqual(func_hash(f),
+                         'f8c94c2e2dd69226706f90c2f4293497')
+        
+        
+    def test_extra(self):
+        
+        def f(x):
+            return 2.5 * x * x + 4.7 * x
+        
+        self.assertEqual(func_hash(f, salt=[(int, int), (float, float)]),
+                         'd81db2e37ade51a430e47b72c55e197e')
+        
+    def test_const(self):
+        
+        def add_a(b):
+            return a + b   # a in globals
+        
+        self.assertEqual(func_hash(add_a),
+                         '55a68633f905a1373f61659b41402f02')
+        
+    def test_inner(self):
+
+        def foo(x):
+            inner1 = lambda t: t/3.0
+            def inner2(n):
+                return n + 3
+            return inner1(x) + inner2(int(x))
+
+        #func_hash(foo, verbose=1)
+        self.assertEqual(func_hash(foo),
+                         'a836c2dbe1b202bd68e1fe3affe1ce7a')
+
+
+if __name__ == '__main__':
+    unittest.main()




More information about the Scipy-svn mailing list