zoukankan      html  css  js  c++  java
  • python 自定义装饰器

    1. 提醒用户使用了废弃的函数
    def deprecated(func):
        """This decorator is used to mark functions as deprecated.
        It will result in a warning being emitted when the function is used.
        """
    
        @functools.wraps(func)
        def new_func(*args, **kwargs):
            warnings.simplefilter('always', DeprecationWarning)  # turn off filter
            warnings.warn(
                "Call to deprecated function {}.".format(func.__name__),
                category=DeprecationWarning,
                stacklevel=2
            )
            warnings.simplefilter('default', DeprecationWarning)  # reset filter
            return func(*args, **kwargs)
    
        return new_func
    
    1. 装饰类中函数的装饰器,给函数增加报错功能
    # 装饰类中函数的装饰器第一个参数是self
    def connection_closed_handler(function):
        """Handle ConnectionClosedException."""
        @wraps(function)
        def _wrap(inst, *args, **kwargs):
            try:
                return function(inst, *args, **kwargs)
    
            except (ConnectionClosedException, WebSocketConnectionClosedException):
                inst.logger.warn("the connection lost. Resetting ...")
                inst.reset(args[1])
                return function(inst, *args, **kwargs)
        return _wrap
    
    
  • 相关阅读:
    Photoshop
    前端性能优化
    Angular Cli和npm、node.js命令
    Angular项目结构
    页面布局
    滚动条与height
    1.2 Angular入门
    前端的e2e测试
    Angular的部署
    jQuery插件开发的基本形式
  • 原文地址:https://www.cnblogs.com/amize/p/13767675.html
Copyright © 2011-2022 走看看