factory/apps/enm/services.py

18 lines
509 B
Python

from apps.enm.models import Mpoint, MpointStat
import re
def translate_eval_formula(exp_str: str, year: int, month: int, day: int, hour: int):
"""
传入
"""
pattern = r'\${(\d+)}'
matches = re.findall(pattern, exp_str)
for match in matches:
mpst = MpointStat.objects.filter(type='hour', mpoint__id=match, year=year, month=month, day=day).first()
if mpst:
exp_str = exp_str.replace(f"${{{match}}}", mpst.val)
rval = eval(exp_str)
return rval