[Scipy-svn] r4506 - trunk/scipy/sandbox/mkufunc
scipy-svn at scipy.org
scipy-svn at scipy.org
Mon Jun 30 15:19:56 EDT 2008
Author: ilan
Date: 2008-06-30 14:19:55 -0500 (Mon, 30 Jun 2008)
New Revision: 4506
Added:
trunk/scipy/sandbox/mkufunc/test_func_hash.py
Removed:
trunk/scipy/sandbox/mkufunc/funcutil.py
trunk/scipy/sandbox/mkufunc/test_funcutil.py
Modified:
trunk/scipy/sandbox/mkufunc/mkufunc.py
Log:
Simplyfied func_hash and put into main file
Deleted: trunk/scipy/sandbox/mkufunc/funcutil.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/funcutil.py 2008-06-30 16:21:16 UTC (rev 4505)
+++ trunk/scipy/sandbox/mkufunc/funcutil.py 2008-06-30 19:19:55 UTC (rev 4506)
@@ -1,60 +0,0 @@
-import sys, re, dis, hashlib, cStringIO
-
-
-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
- sys.stdout = cStringIO.StringIO()
- dis.disassemble(co)
- res = sys.stdout.getvalue()
- sys.stdout = tmp
- return res
-
-
-pat_norep = re.compile(r'<[^<>]*>')
-pat_white = re.compile(r'\s+')
-
-def dis2(co):
- acc = cStringIO.StringIO()
- for line in disassemble(co).splitlines():
- line = line[16:].strip()
- if line:
- acc.write(line+'\n')
-
- acc.write('co_names:\n')
- for name in co.co_names:
- try:
- tmp = str(eval(name))
- except NameError:
- tmp = 'EVAL_FAILED'
- acc.write('%8s: %s\n' % (name, tmp))
-
- res = acc.getvalue()
-
- while True:
- tmp = pat_norep.sub('NO_REPRESENTATION', res)
- if tmp == res:
- break
- res = tmp
-
- return res
-
-
-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__':
- pass
Modified: trunk/scipy/sandbox/mkufunc/mkufunc.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/mkufunc.py 2008-06-30 16:21:16 UTC (rev 4505)
+++ trunk/scipy/sandbox/mkufunc/mkufunc.py 2008-06-30 19:19:55 UTC (rev 4506)
@@ -7,14 +7,23 @@
import re
import os, os.path
import cStringIO
+import hashlib
from types import FunctionType
import numpy
from scipy import weave
-from funcutil import func_hash
+verbose = 0
+def func_hash(f, salt=None):
+ """ Return a MD5 hash for a function object as string.
+ """
+ co = f.func_code
+ return hashlib.md5(co.co_code + repr(co.co_names) + repr(salt)
+ ).hexdigest()
+
+
def translate(f, argtypes):
""" Return pypy's C output for a given function and argument types.
@@ -174,7 +183,7 @@
def support_code(cfuncs):
- """ Given a list of Cfunc instances, return the support_code for weave.
+ """ Given a list of Cfunc instances, return the support code for weave.
"""
acc = cStringIO.StringIO()
@@ -207,7 +216,12 @@
static char %(fname)s_types[] = {
%(types)s};
''' % locals())
-
+
+ if verbose:
+ print '------------------ start support_code -----------------'
+ print acc.getvalue()
+ print '------------------- end support_code ------------------'
+
return acc.getvalue()
@@ -219,7 +233,7 @@
fname = f.__name__
fhash = func_hash(f)
- return '''
+ res = '''
import_ufunc();
/****************************************************************************
@@ -241,7 +255,14 @@
0);
''' % locals()
+ if verbose:
+ print '---------------------- start code ---------------------'
+ print res
+ print '----------------------- end code ----------------------'
+ return res
+
+
def genufunc(f, signatures):
""" Return the Ufunc Python object for given function and signatures.
"""
@@ -256,7 +277,7 @@
ufunc_info.add_header('"numpy/ufuncobject.h"')
return weave.inline(code(f, signatures),
- verbose=0,
+ verbose=verbose,
support_code=support_code(cfuncs),
customize=ufunc_info)
Added: trunk/scipy/sandbox/mkufunc/test_func_hash.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/test_func_hash.py 2008-06-30 16:21:16 UTC (rev 4505)
+++ trunk/scipy/sandbox/mkufunc/test_func_hash.py 2008-06-30 19:19:55 UTC (rev 4506)
@@ -0,0 +1,47 @@
+import unittest
+
+from mkufunc 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),
+ '5f12e97debf1d2cb9e0a2f92e045b1fb')
+
+
+ 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)]),
+ 'e637d9825ef20cb56d364041118ca72e')
+
+ def test_const(self):
+
+ def add_a(b):
+ return a + b # a in globals
+
+ self.assertEqual(func_hash(add_a),
+ '9ff237f372bf233470ce940edd58f60d')
+
+ 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),
+ '814c113dfc77e7ebb52915dd3ce9c37a')
+
+
+if __name__ == '__main__':
+ unittest.main()
Deleted: trunk/scipy/sandbox/mkufunc/test_funcutil.py
===================================================================
--- trunk/scipy/sandbox/mkufunc/test_funcutil.py 2008-06-30 16:21:16 UTC (rev 4505)
+++ trunk/scipy/sandbox/mkufunc/test_funcutil.py 2008-06-30 19:19:55 UTC (rev 4506)
@@ -1,47 +0,0 @@
-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