refector: formular无需用$符号

This commit is contained in:
caoqianming 2024-04-30 11:01:56 +08:00
parent 5f67673c45
commit 1e8e4c6f70
1 changed files with 4 additions and 4 deletions

View File

@ -23,12 +23,12 @@ def translate_eval_formula(exp_str: str, year: int, month: int, day: int, hour:
""" """
传入 传入
""" """
pattern = r"\${(.*?)}" pattern = r"\{(.*?)}"
matches = re.findall(pattern, exp_str) matches = re.findall(pattern, exp_str)
for match in matches: for match in matches:
mpst = MpointStat.objects.filter(Q(mpoint__id=match) | Q(mpoint__name=match) | Q(mpoint__code=match), type="hour", year=year, month=month, day=day, hour=hour).first() mpst = MpointStat.objects.filter(Q(mpoint__id=match) | Q(mpoint__name=match) | Q(mpoint__code=match), type="hour", year=year, month=month, day=day, hour=hour).first()
if mpst: if mpst:
exp_str = exp_str.replace(f"${{{match}}}", str(mpst.val)) exp_str = exp_str.replace(f"{{{match}}}", str(mpst.val))
rval = eval(exp_str) rval = eval(exp_str)
return rval return rval
@ -38,7 +38,7 @@ def transfer_mpoint_val_to_ep_running_state(current_val, base_val: float, expr_s
base_expr: 三元表达式 base_expr: 三元表达式
""" """
if expr_str: # 优先使用表达式 if expr_str: # 优先使用表达式
pattern = r"\${(.*?)}" pattern = r"\{(.*?)}"
matches = re.findall(pattern, expr_str) matches = re.findall(pattern, expr_str)
for match in matches: for match in matches:
if match == 'self': if match == 'self':
@ -46,7 +46,7 @@ def transfer_mpoint_val_to_ep_running_state(current_val, base_val: float, expr_s
else: else:
mpoint_data = get_mpoint_cache(match) mpoint_data = get_mpoint_cache(match)
if mpoint_data: if mpoint_data:
expr_str = expr_str.replace(f"${{{match}}}", str(mpoint_data['last_data']['last_val'])) expr_str = expr_str.replace(f"{{{match}}}", str(mpoint_data['last_data']['last_val']))
rval = eval(expr_str) rval = eval(expr_str)
return rval return rval
if isinstance(current_val, bool): if isinstance(current_val, bool):