上下文管理器 - context

class Context:
borrow_user = 1851xxxxx2
borrow_pwd = 123456
borrow_id = 89xx09

admin_user = 177xxxxx40
admin_pwd = 123456
admin_id = 29x627

normal_user = 18xxxxxx864
normal_pwd = 123456
normal_id = 32x025

def replace_new(s):
# 读取出来的数据用context的数据去替换,loan_id 动态替换
p = '\$\{(.*?)}'
while re.search(p, s):
m = re.search(p, s)
key = m.group(1)
if hasattr(Context, key):
value = str(getattr(Context, key))
s = re.sub(p, value, s, count=1)
else:
return None
return s
s = '{"mobilephone": "${borrow_user}", "pwd": "${borrow_pwd}"}'
s = replace_new(s)
print(s)



posted @ 2020-04-06 21:06  测试媛S  阅读(163)  评论(0)    收藏  举报