[Assorted-commits] SF.net SVN: assorted: [687] python-commons/trunk/src/commons/decs.py
Brought to you by:
yangzhang
|
From: <yan...@us...> - 2008-04-29 00:32:35
|
Revision: 687
http://assorted.svn.sourceforge.net/assorted/?rev=687&view=rev
Author: yangzhang
Date: 2008-04-28 17:32:42 -0700 (Mon, 28 Apr 2008)
Log Message:
-----------
added file_memoized, file_string_memoized, pickle_memoized
Modified Paths:
--------------
python-commons/trunk/src/commons/decs.py
Modified: python-commons/trunk/src/commons/decs.py
===================================================================
--- python-commons/trunk/src/commons/decs.py 2008-04-29 00:31:41 UTC (rev 686)
+++ python-commons/trunk/src/commons/decs.py 2008-04-29 00:32:42 UTC (rev 687)
@@ -7,7 +7,9 @@
@todo: Move the actual decorators to modules based on their topic.
"""
+from __future__ import with_statement
import functools, inspect, xmlrpclib
+from cPickle import *
def wrap_callable(any_callable, before, after):
"""
@@ -90,3 +92,65 @@
else:
return 0
return wrapper
+
+##########################################################
+
+def file_memoized(serializer, deserializer, pathfunc):
+ """
+ The string result of the given function is saved to the given path.
+
+ Example::
+
+ @file_memoized(lambda x,f: f.write(x),
+ lambda f: f.read(),
+ lambda: "/tmp/cache")
+ def foo(): return "hello"
+
+ @file_memoized(pickle.dump,
+ pickle.load,
+ lambda x,y: "/tmp/cache-%d-%d" % (x,y))
+ def foo(x,y): return "hello %d %d" % (x,y)
+
+ @param serializer: The function to serialize the return value into a
+ string. This should take the return value object and
+ the file object.
+ @type serializer: function
+
+ @param deserializer: The function te deserialize the cache file contents
+ into the return value. This should take the file
+ object and return a string.
+ type: deserializer: function
+
+ @param pathfunc: Returns the path where the files should be saved. This
+ should be able to take the same arguments as the original
+ function.
+ @type pathfunc: str
+ """
+ def dec(func):
+ @functools.wraps(func)
+ def wrapper(*args, **kwargs):
+ p = pathfunc(*args, **kwargs)
+ try:
+ with file(p) as f:
+ return deserializer(f)
+ except IOError, (errno, errstr):
+ if errno != 2: raise
+ with file(p, 'w') as f:
+ x = func(*args, **kwargs)
+ serializer(x, f)
+ return x
+ return wrapper
+ return dec
+
+def file_string_memoized(pathfunc):
+ """
+ Wrapper around L{file_memoized} that expects the decorated function to
+ return strings, so the string is written verbatim.
+ """
+ return file_memoized(lambda x,f: f.write(x), lambda f: f.read(), pathfunc)
+
+def pickle_memoized(pathfunc):
+ """
+ Wrapper around L{file_memoized} that uses pickle.
+ """
+ return file_memoized(dump, load, pathfunc)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|