Python中的JSON同步字典:处理临时引用

我正在编写一个充当字典的类,但每次进行修改以确保同步状态时,都会将其内容保存到 json 文件中。

但是,我偶然发现了一个中断同步的特殊情况:将值附加到字典中的列表时。由于这使用__getitem__,我如何确保如果返回的项目被修改,我将其保存到 JSON 文件?

这是一个功能齐全的代码片段(Python 3.9.2)来说明我的意思

import json


class SyncDictJSON(dict):
    __instances: dict = {}

    @classmethod
    def create(cls, filepath: str, **kwargs):
        if filepath not in SyncDictJSON.__instances:
            SyncDictJSON.__instances[filepath] = cls(filepath, **kwargs)
        return SyncDictJSON.__instances[filepath]

    def __init__(self, filepath: str, **kwargs):
        super().__init__(**kwargs)
        self.filepath = filepath
        self.update(SyncDictJSON.read_data_from_filename(self.filepath))

    def __getitem__(self, item):
        print(f"getitem {item}")
        return super(SyncDictJSON, self).__getitem__(item)

    def __setitem__(self, key, value):
        print(f"set item {key},{value}")
        super().__setitem__(key, value)
        SyncDictJSON.write_data_to_filename(self, self.filepath)

    def __delitem__(self, key):
        super().__delitem__(key)
        SyncDictJSON.write_data_to_filename(self, self.filepath)

    @staticmethod
    def write_data_to_filename(data, filepath: str):
        with open(filepath, "w", encoding="utf-8") as file:
            json.dump(data, file, indent=2, ensure_ascii=False)

    @staticmethod
    def read_data_from_filename(filename: str):
        with open(filename, "r", encoding="utf-8") as file:
            return json.load(file)

    @classmethod
    def from_file(cls, filepath):
        return cls(filepath)


if __name__ == '__main__':
    with open("testing.json", "w") as file:
        file.write("{}")
    dico = SyncDictJSON.create("testing.json")

    dico["a_list"] = []
    dico["a_list"].append(5)

    print(dico)  # {'a_list': [5]} but testing.json will be empty

以上是Python中的JSON同步字典:处理临时引用的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>