def perf_decorator(f):
@functools.wraps(f)
def wrapper(*args, **kw):
return f(*args,**kw)
return wrapper
return perf_decorator
@functools.wraps(f)
def wrapper(*args, **kw):
return f(*args,**kw)
return wrapper
return perf_decorator
2016-03-30
def performance(unit):
def p_fn(f):
def fn(*args,**kw):
if unit == 'ms':
t=str(datetime.datetime.now().microsecond)+'ms'
else :
t=str(time.time())+'s'
print 'call %s() in %s' % (f.__name__,t)
太长发不了
def p_fn(f):
def fn(*args,**kw):
if unit == 'ms':
t=str(datetime.datetime.now().microsecond)+'ms'
else :
t=str(time.time())+'s'
print 'call %s() in %s' % (f.__name__,t)
太长发不了
2016-03-30
def performance(f):
def fn (*args,**kw):
print 'call ' + f.__name__ + '() in',time.time()
return f(*args,**kw)
return fn
def fn (*args,**kw):
print 'call ' + f.__name__ + '() in',time.time()
return f(*args,**kw)
return fn
2016-03-30
print filter(lambda s : s and len(s.strip())>0, ['test', None, '', 'str', ' ', 'END'])
把 s 改写成 x ,输出结果一样,可是就是不能通过,真是醉了
把 s 改写成 x ,输出结果一样,可是就是不能通过,真是醉了
2016-03-30
def cmp_ignore_case(s1, s2):
if s1.upper() > s2.upper():
return 1
elif s1.upper() < s2.upper():
return -1
return 0
if s1.upper() > s2.upper():
return 1
elif s1.upper() < s2.upper():
return -1
return 0
2016-03-30
已采纳回答 / 小丫嘛肖儿郎
对于类方法,第一个参数必须是类对象,一般以"cls"作为第一个参数(当然可以用其他名称的变量作为其第一个参数,但是大部分人都习惯以'cls'作为第一个参数的名字,就最好用'cls'了)
2016-03-30
import time
def performance(unit):
def log_decorator(f):
def fn(*args,**kw):
t1=time.time()
r=f(*args,**kw)
t2=time.time()
print 'call'+f.__name__+'()','in',str(t2-t1)+unit
return r
return fn
return log_decorator
def performance(unit):
def log_decorator(f):
def fn(*args,**kw):
t1=time.time()
r=f(*args,**kw)
t2=time.time()
print 'call'+f.__name__+'()','in',str(t2-t1)+unit
return r
return fn
return log_decorator
2016-03-30
没有纠错功能,第一遍竟然把name写成mane~~~~
import time
def performance(f):
def fn(*args,**kw):
t1=time.time()
r=f(*args,**kw)
t2=time.time()
print 'call',f.__name__+'() in',str(t2-t1)+'s'
return r
return fn
import time
def performance(f):
def fn(*args,**kw):
t1=time.time()
r=f(*args,**kw)
t2=time.time()
print 'call',f.__name__+'() in',str(t2-t1)+'s'
return r
return fn
2016-03-30
def calc_prod(lst):
def chengji():
b = 1
for a in lst:
b = b * a
return b
return chengji
f = calc_prod([1, 2, 3, 4])
print f()
可以用最新学习的reduce()。开始我是忘记了,,,这记性
def chengji():
b = 1
for a in lst:
b = b * a
return b
return chengji
f = calc_prod([1, 2, 3, 4])
print f()
可以用最新学习的reduce()。开始我是忘记了,,,这记性
2016-03-30
def cmp_ignore_case(s1, s2):
t1=s1.lower()
t2=s2.lower()
if t1>t2:
return 1
if t1<t2:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
t1=s1.lower()
t2=s2.lower()
if t1>t2:
return 1
if t1<t2:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-03-29