装饰器参数加log
#有多个函数,需要计算他们的执行时间,加log
import time
def logger(flag):
def show_time(f): #foo对象
def inner(*x,**y):
starttime = time.time()
f(*x,**y)
endtime = time.time()
print("执行时间:%s"%(endtime - starttime))
if flag == "abc":
print("日志记录")
return inner
return show_time
# 这里要注意的是最先执行是的logger函数,@后面的取决于返回的show_time对象
@logger("abc") #show_time 返回给logger(”abc“) 相当于 @show_time 相当于 foo = show_time(foo) def foo(*a,**b):
fnum = 0
for i in a:
fnum += i
print(fnum)
print("this is foo")
time.sleep(2)
#foo = show_time(foo)
foo(1,2,3,4,5,6)
装饰器参数加log的更多相关文章
- 一个关于python装饰器参数的问题
看到廖雪峰python教程上,python装饰器一章 https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3 ...
- python_3 装饰器参数之谜
装饰器参数之谜 之前已经初步了解过装饰器了,知道了装饰器可以"偷梁换柱",在不改变函数的调用方式和函数内容的时候,而把函数的功能偷偷地修改. 那么问题来了,如果被修改的函数中有参数 ...
- Python装饰器探究——装饰器参数
Table of Contents 1. 探究装饰器参数 1.1. 编写传参的装饰器 1.2. 理解传参的装饰器 1.3. 传参和不传参的兼容 2. 参考资料 探究装饰器参数 编写传参的装饰器 通常我 ...
- python 装饰器 第八步:使用类来作为装饰器参数
#第八步:使用类作为装饰器参数 #装饰器使用的操作类 class Wish: #祈求方法 def before(): print('饭前洗洗手') #还愿方法 def after(): print(' ...
- python 使用装饰器并记录log
1.首先定义一个log文件 # -*- coding: utf-8 -*- import os import time import logging import sys log_dir1=os.pa ...
- python装饰器参数那些事_接受参数的装饰器
# -*- coding: utf-8 -*- #coding=utf-8 ''' @author: tomcat @license: (C) Copyright 2017-2019, Persona ...
- pytho装饰器参数那些事_inspect.getcallargs
''' Created on Jul 26, 2019 @author: tomcat ''' import inspect def chack_admin(func): def wrapper(*a ...
- python 装饰器(二): 加参数
接上篇python 闭包&装饰器(一) 一.功能函数加参数:实现一个可以接收任意数据的加法器 源代码如下: def show_time(f): def inner(*x, **y): # 形参 ...
- Python带参数的装饰器
在装饰器函数里传入参数 # -*- coding: utf-8 -*- # 2017/12/2 21:38 # 这不是什么黑魔法,你只需要让包装器传递参数: def a_decorator_passi ...
随机推荐
- css快速浏览
meta <meta charset="utf-8" /> <meta name="keywords" content="key1, ...
- Java IO流学习总结(转)
原文地址:http://www.cnblogs.com/oubo/archive/2012/01/06/2394638.html Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 ...
- JNI调用so动态库
1.编写native接口 package org.demo; public class JniDemo { public static native int bmp2fea(byte[] bmp, b ...
- springboot项目 线程消费队列注入报错误空指针
背景: 在调用阿里云隐私保护有一个通话记录的回执消息是一个消费线程队列,这个还别人告诉我的,因为我根本没有看出来哪里是个线程了,然后我就把它当成普通的代码拿到返回值以后然后插入数据库 可是我这边该加的 ...
- Git的http与ssh配置
http 进入git bash 直接clone所需项目 通过http方式 eg:git clone http://xxxxxxxxxx/bk_linux_inspect-master.git 会弹出提 ...
- Junit学习使用
InputStream in; SqlSessionFactory factory; SqlSession session; UserDao userDao; @BeforeEach public v ...
- VM虚拟机安装 常用Linux命令 网卡配置 (第二天)
VM虚拟机安装:(昨天已经安装好了VM了,按照提示安装就好,很简单) 1.安装centos7虚拟机,现在磁盘里面新建文件夹作为安装文件夹 2.找到centos7的iso文件,打开vm-新建虚拟机-按照 ...
- P1018 锤子剪刀布
转跳点:
- cf 261B.Maxim and Restaurant
什么什么期望的,不会! (题解http://blog.sina.com.cn/s/blog_140e100580102wj4e.html(看不懂)) #include<bits/stdc++.h ...
- 创建Oracle序列sequence
create sequence SEQ_ID minvalue 1 maxvalue 99999999 start with 1 increment by 1 nocache order; 建解发器代 ...