2026-07-28 17:57:28 +08:00
|
|
|
import json
|
2026-07-28 22:28:14 +08:00
|
|
|
import re
|
2026-07-28 17:57:28 +08:00
|
|
|
import os
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ParamsManager:
|
|
|
|
|
|
|
|
|
|
def __init__(self, params_dir='params'):
|
|
|
|
|
self.params_dir = params_dir
|
|
|
|
|
self._ensure_dir_exists()
|
|
|
|
|
|
|
|
|
|
def _ensure_dir_exists(self):
|
|
|
|
|
Path(self.params_dir).mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
def save(self, params, filename):
|
|
|
|
|
if not filename.endswith('.json'):
|
|
|
|
|
filename = filename + '.json'
|
2026-07-28 22:28:14 +08:00
|
|
|
|
2026-07-28 17:57:28 +08:00
|
|
|
filepath = os.path.join(self.params_dir, filename)
|
2026-07-28 22:28:14 +08:00
|
|
|
|
2026-07-28 17:57:28 +08:00
|
|
|
with open(filepath, 'w', encoding='utf-8') as f:
|
2026-07-28 22:28:14 +08:00
|
|
|
json.dump(params, f, indent=None, separators=(',', ':'), ensure_ascii=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-07-28 17:57:28 +08:00
|
|
|
|
|
|
|
|
def load(self, filename):
|
|
|
|
|
if not filename.endswith('.json'):
|
|
|
|
|
filename = filename + '.json'
|
|
|
|
|
|
|
|
|
|
filepath = os.path.join(self.params_dir, filename)
|
|
|
|
|
|
|
|
|
|
with open(filepath, 'r', encoding='utf-8') as f:
|
|
|
|
|
params = json.load(f)
|
|
|
|
|
return params
|
2026-07-28 22:28:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-07-28 17:57:28 +08:00
|
|
|
|
|
|
|
|
# from ParamsManager import ParamsManager
|
|
|
|
|
|
|
|
|
|
# # 创建参数管理器实例
|
|
|
|
|
# pm = ParamsManager('params') # 参数保存在 params 文件夹中
|
|
|
|
|
|
|
|
|
|
# # 保存单个参数
|
|
|
|
|
# pm.save(params, 'AWG_NCO') # 自动添加 .json 后缀
|
|
|
|
|
|
|
|
|
|
# # 加载单个参数
|
|
|
|
|
# params = pm.load('AWG_NCO')
|