zoukankan      html  css  js  c++  java
  • python 魔术方法

    import time
    func_count_time_dict = {}
    def count_time(func):
        global func_count_time_dict
        func_count_time_dict[func]=[1,time.localtime()]
        def wrapper():
    
            global func_count_time_dict
            func()
            print 'func name: ',func.__name__
            print 'count: ',func_count_time_dict[func][0]
            print 'last call time: ',time.asctime(func_count_time_dict[func][1])
            
            func_count_time_dict[func][0]+=1
            func_count_time_dict[func][1]=time.localtime()
        
        return wrapper
    
    
    @count_time
    def foo():
        print 'in foo()'
     
    @count_time
    def foo_1():
        print 'in foo_1'
    
    foo_1()
    foo_1()
    foo_1()
        
    foo()
    
    foo_1()
    foo()
    foo()
    
    foo_1()
    
    class MyIterator(object):  
        def __init__(self, data_iter):  
            self.data_iter = data_iter  
            self.start = 0
      
        def __iter__(self):  
            return self  
      
        def next(self):    
            print self.start
            if self.start >= len(self.data_iter):  
                raise StopIteration  
            ans = self.data_iter[self.start]
            self.start += 1
            return ans
    
            
    if __name__ == "__main__":  
        iter = MyIterator(range(10))  
        for i in iter:  
            print i  
    
    def fib(max):  
        a, b = 0, 1            
        while a < max:  
            yield a            
            a, b = b, a + b  
      
          
    for i in fib(1000):  
        print(i)  
    
    map_output = map(lambda x: 2 * x, range(10))
    print map_output
    
    print map(lambda x, y : x + y, range(8), range(8)) 
    
    reduce_output = reduce(lambda x, y:x * y, range(1, 10))
    ##pep8?
    print reduce_output
    
    m=lambda x,y:x>y
    print m(1,5)
    
    filter_output = filter(lambda x:x % 2,  range(1, 10))
    print filter_output
    
  • 相关阅读:
    Jenkins
    python爬虫
    git分布式版本控制
    CKA考试认证总结
    gitlab-ci 工具链
    gitlab-ci 模板库实践
    gitlab-cicd
    gitlab配置文件gitlab.rb详解
    局部变量表中的slot简述
    JVM系统线程类型
  • 原文地址:https://www.cnblogs.com/tjsudys/p/4660078.html
Copyright © 2011-2022 走看看