zoukankan      html  css  js  c++  java
  • python 备忘(内置装饰器)

    1. @classmethod ,加入这个装饰器表示第一个参数永远就是self
    在stackoverflow看到的直接贴过来,有时候出现类似:required argument **cls** 可能是这个原因
    

    1. @property ,加入这个装饰器表示把一个方法变成属性
    1 class Foo:
     2     @property
     3     def AAA(self):
     4         print('get的时候运行我啊')
     5 
     6     @AAA.setter
     7     def AAA(self,value):
     8         print('set的时候运行我啊')
     9 
    10     @AAA.deleter
    11     def AAA(self):
    12         print('delete的时候运行我啊')
    13 
    14 #只有在属性AAA定义property后才能定义AAA.setter,AAA.deleter
    15 f1=Foo()
    16 f1.AAA
    17 f1.AAA='aaa'
    18 del f1.AAA
    
    
    
    1. @contextlib.contextmanager 装饰器,由它修饰的方法会有两部分构成,中间由yield关键字分开。在代码块执行前会先执行yield上面的语句;在代码块执行后会再执行yield下面的语句。
    import contextlib
    import time
    
    @contextlib.contextmanager
    def timeit():
        start = time.time()
        print('before yield ')
        yield start
        print('after yield stating')
        end = time.time()
        usedTime = (end - start) * 1000
        print ('Use time %d ms' % usedTime)
        print('after yield ending')
    
    with timeit() as stattime:
        print('in with staring')
        time.sleep(1)
        print('in with doing something')
        print('in with ending')
    
    '''
    结果:
    before yield 
    in with staring
    in with doing something
    in with ending
    after yield stating
    Use time 1000 ms
    after yield ending
    '''
    
    

    unittest 中的源码应用:
    case.py中的with outcome.testPartExecutor(self) 会触发调用

    '''
    case.py
    '''
    with outcome.testPartExecutor(self):
          self._callSetUp()
    
    
    '''
    @contextlib.contextmanager的代码
    '''
    class _Outcome(object):
        def __init__(self, result=None):
            self.expecting_failure = False
            self.result = result
            self.result_supports_subtests = hasattr(result, "addSubTest")
            self.success = True
            self.skipped = []
            self.expectedFailure = None
            self.errors = []
    
        @contextlib.contextmanager
        def testPartExecutor(self, test_case, isTest=False):
            old_success = self.success
            self.success = True
            try:
                yield
            except KeyboardInterrupt:
                raise
            except SkipTest as e:
                self.success = False
                self.skipped.append((test_case, str(e)))
            except _ShouldStop:
                pass
            except:
                exc_info = sys.exc_info()
                if self.expecting_failure:
                    self.expectedFailure = exc_info
                else:
                    self.success = False
                    self.errors.append((test_case, exc_info))
                # explicitly break a reference cycle:
                # exc_info -> frame -> exc_info
                exc_info = None
            else:
                if self.result_supports_subtests and self.success:
                    self.errors.append((test_case, None))
            finally:
                self.success = self.success and old_success
    
  • 相关阅读:
    一起谈.NET技术,用PagePaser创建Page作为HttpHandler 狼人:
    一起谈.NET技术,Visual Studio 2010构建Web浏览器应用程序 狼人:
    一起谈.NET技术,C#中的lock关键字 狼人:
    一起谈.NET技术,OnLoad与Page_Load的差异分析 狼人:
    一起谈.NET技术,使用LINQ Expression构建Query Object 狼人:
    一起谈.NET技术,Visual Studio 2008单元测试_数据库测试 狼人:
    一起谈.NET技术,ASP.NET绑定的技巧 狼人:
    动态加载js和css
    错误:该行已经属于另一个表
    c#中转义符总结
  • 原文地址:https://www.cnblogs.com/amize/p/13266032.html
Copyright © 2011-2022 走看看