在软件设计上尽量减少硬编码,这是我的实现,用了一个字典
def performance(unit):
c = {'s' : 1000, 'ms' : 1}
def fnn(f):
def fn(n):
start = time.time()
result = f(n)
end = time.time()
print 'call %s() in %d%s' % (f.__name__, (end - start) / c[unit], unit)
return result
return fn
return fnn
def performance(unit):
c = {'s' : 1000, 'ms' : 1}
def fnn(f):
def fn(n):
start = time.time()
result = f(n)
end = time.time()
print 'call %s() in %d%s' % (f.__name__, (end - start) / c[unit], unit)
return result
return fn
return fnn
2019-07-16
sorted_ignore_case = functools.partial(sorted, key=str.lower)
2019-07-10
def gci(filepath):
files = os.listdir(filepath)
for fi in files:
fi_d = os.path.join(filepath,fi)
if os.path.isdir(fi_d):
gci(fi_d)
else:
print(os.path.join(filepath,fi_d))
gci(r'/data')
files = os.listdir(filepath)
for fi in files:
fi_d = os.path.join(filepath,fi)
if os.path.isdir(fi_d):
gci(fi_d)
else:
print(os.path.join(filepath,fi_d))
gci(r'/data')
2019-07-06
print sorted(['bob', 'about', 'Zoo', 'Credit'], key=lambda x: x.upper())
2019-07-03
import math
def is_sqr(x):
return math.sqrt(x).is_integer()
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x).is_integer()
print filter(is_sqr, range(1, 101))
2019-07-03
def cmp_ignore_case(s1, s2):
s1 = s1.lower()
s2 = s2.lower()
if s1 < s2:
return -1
if s1 == s2:
return 0
if s1 > s2:
return 1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
s1 = s1.lower()
s2 = s2.lower()
if s1 < s2:
return -1
if s1 == s2:
return 0
if s1 > s2:
return 1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2019-07-01