惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
H
Heimdal Security Blog
K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Schneier on Security
G
GRAHAM CLULEY
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
CERT Recently Published Vulnerability Notes
Google DeepMind News
Google DeepMind News
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
C
Cisco Blogs
Cyberwarzone
Cyberwarzone
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
Security Archives - TechRepublic
Security Archives - TechRepublic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 司徒正美
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V
Visual Studio Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
WordPress大学
WordPress大学
Jina AI
Jina AI
P
Proofpoint News Feed
P
Proofpoint News Feed
有赞技术团队
有赞技术团队
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog
IT之家
IT之家
S
Security Affairs
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
小众软件
小众软件
N
News | PayPal Newsroom
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
NISL@THU
NISL@THU

博客园 - Cavingdeep

Singleton implementation using metaclass 初试IronPython与.NET的集成 Refactoring as a way to improve the existing design 用metaclass来实现AOP 不该用Generics实现Abstract Factory的理由 新兴XML处理方法VTD-XML介绍 Performance Tips I DCG 2.0.72 (Beta1) 发布了 NUnit发布2.2.3兼容.NET 2.0 RTM 如果你想拥有一个可嵌入式模板引擎…… 改进ASP语法打造更高效的模板语言II 改进ASP语法打造更高效的模板语言 XML的特征以及一些用途 Release of DbHelper 1.2.1 深入XML系列技术 DbHelper at Tigris SQLite系列 集合的初始容量与性能 DbHelper basic usage
用metaclass实现AOP风格的Profiler
Cavingdeep · 2006-08-22 · via 博客园 - Cavingdeep

以下是一段通过metaclass实现Profiler的Python代码,很简单,功能不多,目的是为了展示Python的meta programming的能力,这种能力,无疑是很实用的,而且可以将AOP的方面(aspect)概念发挥的很好!下面的Profiler类(metaclass)就可以将方法的profiling在不同的类中复用。

class Profiler(type):
    
def __new__(mcl, name, bases, dict):
        
from time import clock
        
from types import FunctionTypedef timing(func):
            
def wrapper(*args, **kwds):
                start 
= clock()
                value 
= func(*args, **kwds)
                end 
= clock()
                
print func.__name__'takes', (end - start), 'seconds'
                
return value
            
return wrapperfor attr, value in dict.iteritems():
            
if isinstance(value, FunctionType):
                dict[attr] 
= timing(value)return super(Profiler, mcl).__new__(mcl, name, bases, dict)class A(object):
    
__metaclass__ = Profilerdef foo(self):
        total 
= 0
        
for i in range(100000):
            total 
= total+1
        
print totaldef foo2(self):
        
from time import sleep

        total 

= 0
        
for i in range(100000):
            total 
= total+1
            sleep(
0.0001)
        
print totaldef main():
    a 
= A()
    a.foo()
    a.foo2()
if __name__ == '__main__':
    main()

此篇为抛砖引玉,希望大家能制作出更多更强的metaclass来,然后不妨与我们分享!:)