达观数据
需求函数f 0.8的几率成功,0.2的几率失败,当它失败重试count次,重试count次后还是失败则打报错信息,装饰器实现
# 有参装饰器
def retry(count):
def outter(func):
def inner(*args, **kwargs):
if isinstance(count, int):
for i in range(0, count):
try:
return func(*args, **kwargs)
except Exception as e:
if i == count - 1:
print(e)
else:
raise Exception('传入类型错误')
return inner
return outter
@retry(3)
def f():
print(1111)
qq
f()
字符串 'abc_button(hello_world)'.lstrip('abc_').rstrip('(hello_world)') 的结果
aa = 'abc_button(hello_world)'.lstrip('abc_').rstrip('(hello_world)')
print(aa) # utton
'abc_hello(hello_world)' 怎么拿到中间的hello?
ss = 'abc_button(hello_world)'
import re
l1=re.findall(r'_(.*?)\(',ss)[0]
print(l1) #button
将字典{'1':a, '2':b}中key的类型从str修改为int,即{1:a, 2:b}
dic1 = {'1': 'a', '2': 'b'}
new_dic = {}
for k, v in dic1.items():
k = int(k)
# new_dic[k] = v
# 或者
new_dic.update({k: v})
dic1 = new_dic
print(dic1)