From e42a534e3892397502ad780a04bbf84188fbbded Mon Sep 17 00:00:00 2001 From: David Halter Date: Sun, 24 Jun 2012 00:54:40 +0200 Subject: [PATCH] property / staticmethod / classmethod builtin implementation --- evaluate.py | 4 ++-- mixin/__builtin__.py | 44 ++++++++++++++++++++++++++++++++++++++ test/completion/classes.py | 4 ++-- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/evaluate.py b/evaluate.py index b338290b..3266a53e 100644 --- a/evaluate.py +++ b/evaluate.py @@ -14,8 +14,8 @@ TODO annotations ? how ? type evaluation and return? TODO nonlocal statement TODO getattr / __getattr__ / __getattribute__ ? -TODO descriptors -TODO @staticmethod @classmethod +TODO descriptors (also for classes, for instances it should work) +TODO @staticmethod @classmethod (implement descriptors, builtins are done) TODO variable assignments in classes (see test/completion/classes @230) """ from _compatibility import next, property diff --git a/mixin/__builtin__.py b/mixin/__builtin__.py index e39b76dc..cab49506 100644 --- a/mixin/__builtin__.py +++ b/mixin/__builtin__.py @@ -5,3 +5,47 @@ def next(iterator, default=None): else: return iterator.__next__() return default + +class property(): + def __init__(self, fget, fset = None, fdel = None, doc = None): + self.fget = fget + self.fset = fset + self.fdel = fdel + self.__doc__ = doc + + def __get__(self, obj, cls): + return self.fget(obj) + + def __set__(self, obj, value): + self.fset(obj, value) + + def __delete__(self, obj): + self.fdel(obj) + + def setter(self, func): + self.fset = func + return self + + def getter(self, func): + self.fget = func + return self + + def deleter(self, func): + self.fdel = func + return self + +class staticmethod(): + def __init__(self, func): + self.func = func + + def __get__(self, obj, cls): + return self.func + +class classmethod(): + def __init__(self, func): + self.func = func + + def __get__(self, obj, cls): + def method(*args, **kwargs): + self.func(cls, *args, **kwargs) + return method diff --git a/test/completion/classes.py b/test/completion/classes.py index 7f390014..51473f9f 100644 --- a/test/completion/classes.py +++ b/test/completion/classes.py @@ -189,13 +189,13 @@ class B(): return '' p = Property(t) -##? str() -B().p #? [] B().r() #? int() B().r +##? str() +B().p ##? [] B().p()