import re import communication.convert as convert def expend_vals(write_vals): _int16_write_vals = convert.float_to_int16_n(write_vals) return _int16_write_vals def rebuild_name(p_name, p_idx): if p_idx == "": return f"{{{p_name}}}" else: return f"{{{p_name}[{p_idx}]}}" def extract_param_names(exp_string): pattern = r"\{([a-zA-Z0-9_]+)(?:\[(\d+)\])?\}" # 加括号捕获方括号内的内容 matches = re.findall(pattern, exp_string) return matches def extract_param_names_plain(exp_string): # 正则表达式,使用捕获组 pattern = r"(\w+)(\[\d+\])?" # 使用 re.findall 提取 matches = re.findall(pattern, exp_string) return matches def gen_table_config(param_names): _table_config = [] for idx, itm_name in enumerate(param_names): _table_config.append((idx, 0, 0, itm_name)) _table_config.append((idx, 1, 1, f'{{{itm_name}}}')) return _table_config from communication.param import Param if __name__ == "__main__": # mydict = {'values0_8': Param('values0_8', 1000, 1)} # mydict['values0_8'].update([1]) # # strings = "[values0_8] * 2" # eval_exp = super_expression(strings, 'mydict') # print('eval_exp', eval_exp) # print('eval_res', eval(eval_exp)) test_string = "{a[0]} + {b} * 2}" print(rebuild_name('test_string', 0))