@log的decorator完美实现(原创)
# -*- coding: utf-8 -*- from functools import wraps
from inspect import isfunction def beforecalled(*args, **kwargs):
print('before called! {}'.format(len(args))) for arg in args:
print(arg) def aftercalled(*args, **kwargs):
print('after called! {}'.format(len(args)))
for k,v in kwargs.iteritems():
print('{0}={1}'.format(k,v)) jclist=['-' for _ in xrange(20)]
print(''.join(jclist)) def logdeco(*decoargs, **decokwargs):
def decotator(func):
@wraps(func)
def wrapper(*funcargs, **funckwargs):
beforecalled(*decoargs, **decokwargs)
result = func(*funcargs, **funckwargs)
aftercalled(*decoargs, **decokwargs)
return result
return wrapper
return decotator def log(*decoargs, **decokwargs):
if len(decoargs)==1 and len(decokwargs)==0:
if isfunction(decoargs[0]) or hasattr(decoargs[0],'__call__'):
return logdeco()(decoargs[0]) def wrappered(func):
return logdeco(*decoargs, **decokwargs)(func) return wrappered @log('lisa',50,email='lisa@xxx.com')
def testmoreparas(x,y):
print('x*y={}'.format(x*y)) @log()
def testemptypara(x,y):
print('x*y={}'.format(x*y)) @log
def testjustlog(x,y):
print('x*y={}'.format(x*y)) if __name__=='__main__':
testmoreparas(5,6)
testemptypara(4,5)
testjustlog(3,4)
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:22:17) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
============ RESTART: D:\pytest\logdecorator.py ============
before called! 2
lisa
50
x*y=30
after called! 2
email=lisa@xxx.com
--------------------
before called! 0
x*y=20
after called! 0
--------------------
before called! 0
x*y=12
after called! 0
--------------------
>>>
test result
@log的decorator完美实现(原创)的更多相关文章
- bootstrap菜单完美解决---原创
由于bootstrap的各方优点,偶的“点金项目细化分包管理平台”决定采用它.但在使用中遇到了一些问题,比如菜单的问题,这个菜单是用的一个JQuery的一个效果,点击后,所点击的链接处的class要加 ...
- Decorator:从原理到实践
前言 原文链接:Nealyang/personalBlog ES6 已经不必在过多介绍,在 ES6 之前,装饰器可能并没有那么重要,因为你只需要加一层 wrapper 就好了,但是现在,由于语法糖 c ...
- 6 小时 Python 入门
6 小时 Python 入门 以下操作均在 Windows 环境下进行操作,先说明一下哈 一.安装 Python 1.官网下载 Python 进入官网(https://www.python.org), ...
- RealProxy深入
Program.cs class Program { static void Main(string[] args) { NoMethodLogging(); Console.WriteLine(&q ...
- python基础——装饰器
python基础——装饰器 由于函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数. >>> def now(): ... print('2015-3-25 ...
- Python3 函数式编程
函数式编程就是一种抽象程度很高的编程范式,纯粹的函数式编程语言编写的函数没有变量,因此,任意一个函数,只要输入是确定的,输出就是确定的,这种纯函数我们称之为没有副作用.而允许使用变量的程序设计语言,由 ...
- [置顶] IOS7状态栏StatusBar官方标准适配方法
IOS7状态栏StatusBar官方标准适配方法 hello,大家好,ios7正式版已经发布,相信大家都在以各种方式来适配ios7. 如果你已经下载了xcode5,正准备使用,你会发现各种布局的改变. ...
- Python 3 学习笔记2
教程链接:http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000 高级特性 切片 取一个l ...
- python多版本的pip共存问题解决办法
python pip 多版本 问题情景 最开始学python的时候用的是py2,且一直用pip来安装库函数.后来py3出来了,所以就装上了,但是一装上出问题了,主要有两个主要的问题.下面将详细说明. ...
随机推荐
- Aspose------导入Excel
代码: public List<T> ImportExcelToList<T>() { HttpContext context = HttpContext.Current; ) ...
- Docker background
什么是 Docker? Docker 提供了一个可以运行你的应用程序的封套(envelope),或者说容器.它原本是 dotCloud 启动的一个业余项目,并在前些时候开源了.它吸引了大量的关注和讨论 ...
- [AX2012]代码更改默认财务维度
在前文(http://www.cnblogs.com/duanshuiliu/p/3243048.html)最后演示了如何使用代码更改默认财务维度,那段代码模拟了创建各数据表记录的过程,实际上AX提供 ...
- Java调用MQ队列
IBM MQ 6.0中设置两个队列,(远程队列.通道之类都不设置). 队列管理器是XIR_QM_1502 队列名称是ESBREQ IP地址是10.23.117.134(远程的一台电脑,跟我的电脑不在一 ...
- RabbitMq 之简单队列
简单队列类似于我们的生产者,消费者, 一个生产者,对应一个消费者. 直接上代码: package com.j1.rabbitmq.simple; import com.j1.rabbitmq.util ...
- C语言中一个字符数组里面的所有元素变成一个字符串
#include <string.h> int main() // 这里为了方便直接用main函数 { char array[] = { 'h', 'e', 'l', 'l', ' ...
- Lua 迭代器与closure
所谓“迭代器”就是一种可以遍历(iterate over)一种极和中所有元素的机制.在Lua中,通常将迭代其表示为函数.每调用一次函数,即返回集合中的“下一个”元素.每个迭代器都需要在每次成功调用之间 ...
- Windows虚拟地址转物理地址(原理+源码实现,附简单小工具)
...
- js replace replaceAll
今天因为一些原因来看这篇博文,测试了一下根本不对.replace根本没有string.replace("字符","字符")这样的写法,而是stringObjec ...
- 【Linux】使用 telnet 提示 Escape character is '^]'的意义
在linux/unix下使用telnet hostname port连接上主机后会提示Escape character is '^]' 这个提示的意思是按Ctrl + ] 会呼出telnet的命令行, ...