首頁 >後端開發 >Python教學 >蟒蛇裝飾器

蟒蛇裝飾器

巴扎黑
巴扎黑原創
2016-12-09 13:31:331295瀏覽

Contains: 

1、decorators

2、functools


首先我們看下tornado中使用的裝飾器 
1、@tornado.web. that the user be logged in.

Python程式碼  

def authenticated(method):  
    """Decorate methods with this to require that the user be logged in."""  
    @functools.wraps(method)  
    def wrapper(self, *args, **kwargs):  
        if not self.current_user:  
            if self.request.method in ("GET", "HEAD"):  
                url = self.get_login_url()  
                if "?" not in url:  
                    if urlparse.urlsplit(url).scheme:  
                        # if login url is absolute, make next absolute too  
                        next_url = self.request.full_url()  
                    else:  
                        next_url = self.request.uri  
                    url += "?" + urllib.urlencode(dict(next=next_url))  
                self.redirect(url)  
                return  
            raise HTTPError(403)  
        return method(self, *args, **kwargs)  
    return wrapper


接下來程式碼需要驗證使用者登陸的方法都可以使用這個裝飾器,透過使用這個裝飾器可以簡化許多重複驗證的程式碼,只需要在方法上面加上@tornado.web.authenticated就ok了。 

2、@tornado.web.asynchronous 

Python代碼  

def asynchronous(method):  
    @functools.wraps(method)  
    def wrapper(self, *args, **kwargs):  
        if self.application._wsgi:  
            raise Exception("@asynchronous is not supported for WSGI apps")  
        self._auto_finish = False  
        with stack_context.ExceptionStackContext(  
            self._stack_context_handle_exception):  
            return method(self, *args, **kwargs)  
    return wrapper


這個裝飾器的會把self._auto_finish 置為 False。

接下來,我們寫一個單利模式的裝飾器: 

Python代碼  

def singleton(cls):  
    instances = {}  
    def get_instance():  
        if cls not in instances:  
            instances[cls] = cls()  
        return instances[cls]  
    return get_instance  
 
@singleton  
class Foo:  
    def __init__(self):  
        pass  
  
class Bar:  
    def __init__(self):  
        pass  
  
f = Foo()  
m = Foo()  
print f,m,f == m  
  
a = Bar()  
b = Bar()  
print a,b,a == b


result is : 

<__main__.foo instance at main__.bar> <__main__.bar instance at> False 

@singleton這個裝飾器實作了類別的單例模式,可以確保類別只會被實例化一次。

使用裝飾器對參數以及方法回傳結果的驗證方法: 


Python代碼  

#-*-coding:utf-8-*-  
  
def accepts(*types):  
    def check_accepts(f):  
#        assert len(types) == f.func_code.co_argcount  
        def new_f(*args, **kwds):  
            for (a, t) in zip(args, types):  
                assert isinstance(a, t), \  
                       "arg %r does not match %s" % (a,t)  
            return f(*args, **kwds)  
        new_f.func_name = f.func_name  
        return new_f  
    return check_accepts  
  
def returns(rtype):  
    def check_returns(f):  
        def new_f(*args, **kwds):  
            result = f(*args, **kwds)  
            assert isinstance(result, rtype), \  
                   "return value %r does not match %s" % (result,rtype)  
            return result  
        new_f.func_name = f.func_name  
        return new_f  
    return check_returns  
 
@accepts(int, (int,float))  
@returns((int,float))  
def func(arg1, arg2):  
    return arg1 * arg2  
  
print func(1,2.0)



Pyt gt 100000" % a 

AssertionError: arg 5000 must gt 100000 



引用

Design Goals: 

The new syntax should 🜆 labion
? les and the existing builtins classmethod() and staticmethod(). This requirement also means that a decorator syntax must support passing arguments to the wrapper constructor 
   * work with multiple wrappers per definition 
   * make it obvious is s obion​​aly that ob​​bol​​ion 文本 obvious that oby ​​ibe al​​iony obvious s 集 obvious ignore it when writing their own code 
   * be a syntax "that ... [is] easy to remember once explained" 

   * not make future extensions more difficult 

硬體 * qu qu.

   * not make it more difficult to scan through code quickly. It should still be easy to search for all definitions, a particular definition, or the arguments that a function accepts 

語 comp​​an shed notuchwem comp " toy parser tools out there [12]" 
   * allow future compilers to optimize for decorators. With the hope of a JIT compiler for Python coming into existence at some point this ten to reuncator tofal for reception  move from the end of the function, where it's currently hidden, to the front where it is more in your face [13]



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:蟒蛇技巧下一篇:蟒蛇技巧