这可能不是您要查找的内容,但此类允许存储程序设置。您使用代表您需要的变量的默认设置加载它并嵌入字典以生成命名空间。它并不完美,但试图允许多个数据库定义相互共存,同时确保数据类型不会改变。它的使用范围有限,但可以作为您需要的解决方案。
import copy
import pprint
import pickle
################################################################################
class _Interface:
def __init__(self, default, database=None):
self.__default = default
if database is None:
self.__database = copy.deepcopy(default)
else:
self.__database = database
def __repr__(self):
return pprint.pformat(self.__database)
def __getattr__(self, name):
attr = self.__default[name]
if isinstance(attr, dict):
return _Interface(attr, self.__database[name])
raise AttributeError(name)
def __setattr__(self, name, value):
if name in {'_Interface__default', '_Interface__database'}:
super().__setattr__(name, value)
else:
raise AttributeError(name)
def __getitem__(self, name):
item = self.__default[name]
if isinstance(item, dict):
raise KeyError(name)
return self.__database[name]
def __setitem__(self, name, value):
item = self.__default[name]
if isinstance(value, type(item)):
self.__database[name] = value
else:
raise TypeError(type(value))
################################################################################
class Registry(_Interface):
@property
def __database(self):
return self._Interface__database
def load(self, path):
with open(path, 'rb') as file:
data = pickle.load(file)
self.__merge(data, self.__database)
@classmethod
def __merge(cls, source, sink):
for key, value in source.items():
if key not in sink:
sink[key] = value
elif isinstance(value, type(sink[key])):
if isinstance(value, dict):
cls.__merge(value, sink[key])
else:
sink[key] = value
def save(self, path):
with open(path, 'wb') as file:
pickle.dump(self.__database, file)
################################################################################
file1 = Registry({'hello': ['a', 'b', 'c'],
'world': ['d', 'e', 'f'],
'foo': ['g', 'h', 'i'],
'namespace': {'j': 0,
'k': 1,
'm': '2',
'n': '3'}})
file2 = Registry({'hello': ['a', 'b', 'c'],
'world': ['d', 'e', 'f'],
'foo': ['g', 'h', 'i'],
'namespace': {'j': '4',
'k': '5',
'm': '6',
'n': '7'},
'bar': []})
file1['hello'][0] = 'z'
file1.save('mini.db')
file2.load('mini.db')
file2['bar'].extend(['j', 'k', 'l'])
print(file2)
print(file2.namespace['k'])
print(file2.namespace['m'])