You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.5 KiB

11 months ago
import json
import re
class FunctionUtil:
def __init__(self):
self.filename = "./function.json"
self.data = {}
self.load()
def load(self):
"""
加载文件数据
"""
try:
with open(self.filename, 'r', encoding="utf-8") as f:
self.data = json.load(f)
except FileNotFoundError:
pass
def save(self):
"""
将函数保存至json文件当中
"""
with open(self.filename, "w", encoding="utf-8") as f:
json.dump(self.data, f)
def check_function_exit(self, function):
"""
检查用户输入的函数是否包含用户自定义函数
"""
for key in self.data.keys():
if function.find(key) >=0:
return True
return False
def add_function(self, key, value):
"""
添加函数
"""
self.data[key.strip()] = value.strip()
self.save()
def get_function_by_iter(self, key):
result = key
for dict_key in self.data.keys():
if result.find(dict_key) >= 0:
value = result.replace(dict_key, self.data[dict_key])
result = self.replace_values_recursive(value)
return result
def replace_values_recursive(self, value):
for key in self.data:
if key in value:
replacement = self.data[key]
value = value.replace(key, replacement)
return value