diff options
author | Sylvain <syt@logilab.fr> | 2008-06-26 15:58:54 +0200 |
---|---|---|
committer | Sylvain <syt@logilab.fr> | 2008-06-26 15:58:54 +0200 |
commit | bfa2ba01ff28072880203cbf61d2705574e2489d (patch) | |
tree | 47eb9877c18c720c87a4e2d85dda893df0a567ba | |
parent | 09256f8fbf9845b6f0534166b95bace4a2a665ae (diff) | |
download | logilab-common-bfa2ba01ff28072880203cbf61d2705574e2489d.tar.gz |
backport iclassmethod decorator from gingo.goa
-rw-r--r-- | decorators.py | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/decorators.py b/decorators.py index b952a56..1a659c3 100644 --- a/decorators.py +++ b/decorators.py @@ -18,6 +18,9 @@ """ __docformat__ = "restructuredtext en" +from types import MethodType +from time import clock + # XXX rewrite so we can use the decorator syntax when keyarg has to be specified def cached(callableobj, keyarg=None): @@ -113,7 +116,19 @@ class classproperty(object): return self.get(cls) -from time import clock +class iclassmethod(object): + '''descriptor for method which should be available as class method if called + on the class or instance method if called on an instance + ''' + def __init__(self, func): + self.func = func + def __get__(self, instance, objtype): + if instance is None: + return MethodType(self.func, objtype, objtype.__class__) + return MethodType(self.func, instance, objtype) + def __set__(self, instance, value): + raise AttributeError("can't set attribute") + def timed(f): def wrap(*args, **kwargs): |