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

推荐订阅源

MyScale Blog
MyScale Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
爱范儿
爱范儿
小众软件
小众软件
K
Kaspersky official blog
P
Proofpoint News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - Franky
V
Vulnerabilities – Threatpost
博客园_首页
Microsoft Security Blog
Microsoft Security Blog
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX
C
Check Point Blog
S
Schneier on Security
P
Palo Alto Networks Blog
IT之家
IT之家
GbyAI
GbyAI
T
Threat Research - Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
Project Zero
Project Zero
Y
Y Combinator Blog
V
Visual Studio Blog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
S
Securelist
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理

博客园 - 紫色物语

多个nginx之间如何实现反向代理和负责均衡 docker相关知识 spring boot+mybatis 系列 springboot+dockfile 配置nginx输入任何地址都跳转至维护页面 0005python中的静态方法和类方法 0004python中的map,reduce,lambda,filter 0003python中的可变参数 0000python中文乱码解决方案 0002python中dict和list的特殊构造 0001python中特殊的for迭代zip函数 年度热门开源项目 SSO 不同服务器数据库之间的数据操作 自动构建部署 EF 性能调优 断点续传 gis 相关资料 easyui 特殊操作
@log的decorator完美实现(原创)
紫色物语 · 2018-08-24 · via 博客园 - 紫色物语

 1 # -*- coding: utf-8 -*-
 2 
 3 from functools import wraps
 4 from inspect import isfunction
 5 
 6 def beforecalled(*args, **kwargs):
 7     print('before called! {}'.format(len(args)))
 8 
 9     for arg in args:
10         print(arg)        
11 
12 def aftercalled(*args, **kwargs):
13     print('after called! {}'.format(len(args)))
14     for k,v in kwargs.iteritems():
15         print('{0}={1}'.format(k,v))
16 
17     jclist=['-' for _ in xrange(20)]
18     print(''.join(jclist))
19 
20 def logdeco(*decoargs, **decokwargs):
21     def decotator(func):
22         @wraps(func)
23         def wrapper(*funcargs, **funckwargs):
24             beforecalled(*decoargs, **decokwargs)
25             result = func(*funcargs, **funckwargs)
26             aftercalled(*decoargs, **decokwargs)
27             return result
28         return wrapper
29     return decotator
30 
31 def log(*decoargs, **decokwargs):
32     if len(decoargs)==1 and len(decokwargs)==0:
33         if isfunction(decoargs[0]) or hasattr(decoargs[0],'__call__'):
34             return logdeco()(decoargs[0])
35 
36     def wrappered(func):
37         return logdeco(*decoargs, **decokwargs)(func)
38 
39     return wrappered
40 
41 @log('lisa',50,email='lisa@xxx.com')
42 def testmoreparas(x,y):
43     print('x*y={}'.format(x*y))
44 
45 @log()
46 def testemptypara(x,y):
47     print('x*y={}'.format(x*y))
48 
49 @log
50 def testjustlog(x,y):
51     print('x*y={}'.format(x*y))    
52 
53 if __name__=='__main__':
54     testmoreparas(5,6)
55     testemptypara(4,5)
56     testjustlog(3,4)

View Code

 1 Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:22:17) [MSC v.1500 32 bit (Intel)] on win32
 2 Type "copyright", "credits" or "license()" for more information.
 3 >>> 
 4 ============ RESTART: D:\pytest\logdecorator.py ============
 5 before called! 2
 6 lisa
 7 50
 8 x*y=30
 9 after called! 2
10 email=lisa@xxx.com
11 --------------------
12 before called! 0
13 x*y=20
14 after called! 0
15 --------------------
16 before called! 0
17 x*y=12
18 after called! 0
19 --------------------
20 >>> 

test result