python 装饰器的使用
功能:1. 统计一个函数执行时间;2.判断某个函数是否出现异常;3.参数检查;4.代理,和上下文提供者
def timer(func):
def _timer(*args,**kwargs):
begin=time.time()
func(*args,**kwargs)
time.sleep(2)
print time.time()-begin
return _timer
@timer
def log(info):
print info
log('sleep few seconds')
sleep few seconds
2.0
def deal_other_error():
'''
打印出错的参数
'''
def _deco(func):
def __deco(*args):
try:
return func(*args)
except:
traceback.print_exc()
raise
return __deco
return _deco
@deal_other_error()
def log(info):
print "wiod d e",info
log('sleep few seconds')
wiod d e sleep few seconds
字符串的find 函数
find(substr), 如果在字符串中发现了子串,返回 子串在父串(开始位置的)所在的索引,没有发现返回 -1
url = "http://leep/few/seconds"
find_result = url.find("http://")
print find_result
find_result1 = url.find("https://")
print find_result1
print url.find('//')
#
0
-1
5