Update of /cvsroot/modeling/ProjectModeling/Modeling
In directory sc8-pr-cvs1:/tmp/cvs-serv19299
Modified Files:
utils.py
Log Message:
Added cache_simple_methods() and methods_to_cache() for use in the future caching mechanism in ModelSet and ClassDescription
Index: utils.py
===================================================================
RCS file: /cvsroot/modeling/ProjectModeling/Modeling/utils.py,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** utils.py 7 Jul 2003 14:24:20 -0000 1.12
--- utils.py 23 Jul 2003 15:44:29 -0000 1.13
***************
*** 50,54 ****
def capitalizeFirstLetter(aName):
"Return the same string with the first letter capitalized"
! return string.capitalize(aName[0])+aName[1:]
def lower(aString):
--- 50,54 ----
def capitalizeFirstLetter(aName):
"Return the same string with the first letter capitalized"
! return aName[0].upper()+aName[1:]
def lower(aString):
***************
*** 178,179 ****
--- 178,228 ----
import warnings
warnings.warn(msg, DeprecationWarning, 3)
+
+ def cache_simple_methods(anInstance, exclude=None):
+ """
+ Takes an instance object and caches the result for all simple methods,
+ i.e. methods that takes no arguments except 'self'. The caching is made by
+ substituting the method with a lambda returning the value the original
+ method returned.
+
+ Parameters:
+
+ anInstance -- the instance to examine
+
+ exclude -- a sequence of methods' names not to cache
+
+ See also: methods_to_cache()
+ """
+ from new import instancemethod
+ for m in methods_to_cache(anInstance.__class__, exclude):
+ try:
+ res=m(anInstance)
+ except (NotImplementedError, 'Unimplemented','Nonsense'):
+ pass
+ l=lambda self, res=res: res
+ cached_m=instancemethod(l, anInstance, anInstance.__class__)
+ setattr(anInstance, m.__name__, cached_m)
+
+ def methods_to_cache(aClass, exclude=None):
+ """
+ Searches and returns within 'aClass' the methods that accepts no arguments
+ except 'self'.
+
+ Parameters:
+
+ aClass -- the class to examine
+
+ exclude -- a sequence of methods' names to exclude from the result set
+
+ """
+ if exclude is None:
+ exclude=[]
+ import inspect
+ isfunction=inspect.isfunction
+ classDict=aClass.__dict__
+ methods=[(func, inspect.getargspec(func))
+ for func in classDict.values()
+ if isfunction(func) and func.__name__!='__init__']
+ methods=[m[0] for m in methods if len(m[1][0])==1]
+ methods=[m for m in methods if m.__name__ not in exclude]
+ return methods
|