使用 pickle 从文件中加载、存储和删除配置参数
Load, Store and delete config parameters from file using pickle
我正在尝试保留我的应用程序的状态,目前已找到 pickle 库。
我从
中找到了如何将配置参数设置和获取到字典中
我已经设法将它保存到外部配置文件,但我认为我做的不对,而且感觉有点笨拙。
这里是演示的简化版本:
Config.py
#
import pickle
from pathlib import Path
filename = 'config'
class Config(dict):
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
def __init__(self):
# Load config from file
my_file = Path(filename)
if my_file.is_file():
infile = open(filename, 'rb')
self.update(pickle.load(infile))
infile.close()
def __getstate__(self):
return self
def __setstate__(self, state):
self.update(state)
def save(self):
# filename = 'config'
outfile = open(filename, 'wb')
pickle.dump(self, outfile)
outfile.close()
App.py
import tkinter as tk
import pickle
from Config import Config
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
# Init config
self.config = Config()
# initalise variables from config
param0 = tk.BooleanVar()
self.getConfig(param0)
param1 = tk.StringVar()
self.getConfig(param1)
param2 = tk.StringVar()
self.getConfig(param2, "one")
# Build config page
cb = tk.Checkbutton(self, text = "Param", variable=param0)
cb.pack()
e = tk.Entry(self, textvariable=param1)
e.pack()
om = tk.OptionMenu(self, param2, "one", "two", "three")
om.pack()
def getConfig(self, object, default=None):
if str(object) in self.config:
object.set(self.config[str(object)])
else:
if default:
object.set(default)
object.trace("w", lambda name, index, mode, object=object: self.setConfig(object))
def setConfig(self, object):
self.config[str(object)] = object.get()
self.config.save()
if __name__ == "__main__":
app=App()
app.mainloop()
这按照我期望的方式工作,但是我不知道如何保存变量对象名称,而是 python 生成的名称存储在配置文件中,这只有在我只永远追加更多参数,但如果我在现有参数之间插入一个新参数,一切都会变得混乱。
配置文件的示例输出:
{'PY_VAR0': False, 'PY_VAR1': 'test string', 'PY_VAR2': 'three'}
我想知道是否有更好的方法?
我认为最好通过给自己设置变量名称而不是使用默认名称来为参数赋予有意义的名称:
例如
param0 = tk.BooleanVar(name='boolean_param')
param1 = tk.StringVar(name='string_param')
param2 = tk.StringVar(name='user_choice')
会给你配置
{'string_param': 'test string', 'boolean_param': False, 'user_choice': 'three'}
因此,即使您更改变量的创建顺序,也不会更改它们的名称,您仍然可以在配置文件中检索到正确的值。
我正在尝试保留我的应用程序的状态,目前已找到 pickle 库。
我从
我已经设法将它保存到外部配置文件,但我认为我做的不对,而且感觉有点笨拙。
这里是演示的简化版本:
Config.py
#
import pickle
from pathlib import Path
filename = 'config'
class Config(dict):
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
def __init__(self):
# Load config from file
my_file = Path(filename)
if my_file.is_file():
infile = open(filename, 'rb')
self.update(pickle.load(infile))
infile.close()
def __getstate__(self):
return self
def __setstate__(self, state):
self.update(state)
def save(self):
# filename = 'config'
outfile = open(filename, 'wb')
pickle.dump(self, outfile)
outfile.close()
App.py
import tkinter as tk
import pickle
from Config import Config
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
# Init config
self.config = Config()
# initalise variables from config
param0 = tk.BooleanVar()
self.getConfig(param0)
param1 = tk.StringVar()
self.getConfig(param1)
param2 = tk.StringVar()
self.getConfig(param2, "one")
# Build config page
cb = tk.Checkbutton(self, text = "Param", variable=param0)
cb.pack()
e = tk.Entry(self, textvariable=param1)
e.pack()
om = tk.OptionMenu(self, param2, "one", "two", "three")
om.pack()
def getConfig(self, object, default=None):
if str(object) in self.config:
object.set(self.config[str(object)])
else:
if default:
object.set(default)
object.trace("w", lambda name, index, mode, object=object: self.setConfig(object))
def setConfig(self, object):
self.config[str(object)] = object.get()
self.config.save()
if __name__ == "__main__":
app=App()
app.mainloop()
这按照我期望的方式工作,但是我不知道如何保存变量对象名称,而是 python 生成的名称存储在配置文件中,这只有在我只永远追加更多参数,但如果我在现有参数之间插入一个新参数,一切都会变得混乱。
配置文件的示例输出:
{'PY_VAR0': False, 'PY_VAR1': 'test string', 'PY_VAR2': 'three'}
我想知道是否有更好的方法?
我认为最好通过给自己设置变量名称而不是使用默认名称来为参数赋予有意义的名称:
例如
param0 = tk.BooleanVar(name='boolean_param')
param1 = tk.StringVar(name='string_param')
param2 = tk.StringVar(name='user_choice')
会给你配置
{'string_param': 'test string', 'boolean_param': False, 'user_choice': 'three'}
因此,即使您更改变量的创建顺序,也不会更改它们的名称,您仍然可以在配置文件中检索到正确的值。