import os
print(os.path.isdir(r'/data/webroot/resource/python'))
print (os.path.isfile(r'/data/webroot/resource/python/test.txt'))
print(os.path.isdir(r'/data/webroot/resource/python'))
print (os.path.isfile(r'/data/webroot/resource/python/test.txt'))
2019-03-01
def calc_prod(lst):
def lazy_prod():
def f(x,y):
return x*y
return reduce(f,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
def f(x,y):
return x*y
return reduce(f,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2019-02-28
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
2019-02-28
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2019-02-28
这里计算的时间差,其实是在高阶函数里的时间差。
1. 调用装饰器
2. 进入装饰器,调用高阶函数
3. 计时t1,执行常规函数,计时t2,执行print命令,返回常规函数
4. 调用常规函数
对于我来说,难点在于r = f(*args, **kw)这一步
1. 调用装饰器
2. 进入装饰器,调用高阶函数
3. 计时t1,执行常规函数,计时t2,执行print命令,返回常规函数
4. 调用常规函数
对于我来说,难点在于r = f(*args, **kw)这一步
2019-02-27
def count():
fs = []
for i in range(1, 4):
def f(j=i):
return j*j
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(j=i):
return j*j
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2019-02-25
def performance(unit):
def per(f):
def fn(*args, **kw):
t1 = datetime.datetime.now()
res = f(*args, **kw)
t2 = datetime.datetime.now()
t = (t2 - t1).total_seconds()
t = t * 1000 if (unit == 'ms') else t
print('call %s() in %f' % (f.__name__, t))
return res
return fn
return per
def per(f):
def fn(*args, **kw):
t1 = datetime.datetime.now()
res = f(*args, **kw)
t2 = datetime.datetime.now()
t = (t2 - t1).total_seconds()
t = t * 1000 if (unit == 'ms') else t
print('call %s() in %f' % (f.__name__, t))
return res
return fn
return per
2019-02-15
print(sorted(['bob', 'about', 'Zoo', 'Credit'], key=lambda x: x.lower()))
2019-02-15