"refreshing" 不断更新的配置文件的正确方法是什么?

What is the correct way of "refreshing" a config file which is constantly being updated?

我对自己遇到的问题有点不知所措。

我有一个 python 程序,它通过 GPIO 接口 (Pi 4) 发送信号。 信号取决于 config.json

json 的布局如下所示:

{
   "key1" : val1
   "key2" : val2
   "key3" : val3
}

配置数据作为列表传递给调用方,并在设备上保存为 dict/json 配置文件,以便在没有新配置到达时重复使用。 该程序使用以下代码读取、编辑和保存现有或新配置:

def check_json(self, source: str, write=False, val1=940, val2=5, val3=10):
    """check_json checks whether there's an existing config on the machine and in the same folder/location
    if it does exist, it gets returned to the caller. If it doesn't exist, a new file will be created
    with preset data"""
    if os.path.isfile(source):
        if write:
            try:
                with open(source, "r+") as json_data:  # update the existing values and save them
                    try:
                        config_data = js.load(json_data)
                        config_data["key1"] = val1
                        config_data["key2"] = val2
                        config_data["key3"] = val3
                        print(config_data)
                        json_data.seek(0)
                        json_data.truncate()
                        js.dump(config_data, json_data, indent=2)
                        json_data.flush()
                    except TypeError:
                        pass
            except TypeError:
                pass
        else:
            json_data = open(source, "r")
            dict_data = js.load(json_data)
            config_data = [dict_data["light_lvl"],
                           dict_data["on_time"], dict_data["off_time"]]
            json_data.close()
            return config_data
    # create new with presets if json config does not exist
    else:
        json_data = open(source, "w")
        dict_data = {"key1": val1,
                     "key2": on_time, "key3": val}
        js.dump(dict_data, json_data, indent=2)
        json_data.flush()
        json_data.close()
        return self.check_json(source)

新配置到达后,程序崩溃并出现以下错误:

"key1" = config_data[0]
TypeError: 'NoneType' object is not subscriptable

即使 json 到达时其内容完好无损,也会发生错误。我尝试使用可见的多次尝试和异常,希望它能在新的迭代中继续读取新的配置数据。 try 和 except 块没有一点帮助,我不知道我还能如何解决这个问题,

任何输入,tips/tricks 非常感谢

看来我的做法不对。我对解决方案进行编程的方式是我可以毫无顾虑地简单地访问我的值。但情况并非如此,因为文件有些不稳定,从某种意义上说,它可能仍处于“接收”过程中。

尝试获取仍未完全接收的文件的值只会导致问题。解决了这个问题,我回到绘图板并稍微修改了我的概念。

class 文件操作:

def __init__(self, file_name: str):
    self.file_name = file_name
    self.config_name = "config.json"
    self.preset_config = {"key1" : val1, "key2" : val2, "key3" : val3}


def save_config(self):
    "saves the newer config file and deletes it in order to avoid clutter."
    if isfile(self.file_name):
        with open(self.config_name, "w") as file:
            js.dump(self.pass_new(), file, indent=2)
            remove(self.file_name) # delete the newly arrived config to prevent clutter


def read_config(self):
    """pass the existing config over to the caller if present. 
    Creates a preset config using the field "preset_config" if config.json doesn't exist """
    if not isfile(self.config_name):
        with open(self.config_name,"w") as file:
            js.dump(self.preset_config, file, indent=2)
            return self.preset_config # return the preset config for now
    with open(self.config_name, "r") as file:
        json_string = ""
        for line in file:
            json_string += line
        return js.loads(json_string)


def pass_new(self): 
    """checks if a new config is present and passes the data through to the caller"""
    if isfile(self.file_name):
        with open(self.file_name, "r") as file:
            json_string = ""
            for line in file:
                json_string += line
            return js.loads(json_string)

我将文件读取操作分离到不同的文件和 class 以保持一切具体和简洁,我只是对其进行编程以便逐行读取文件而不是立即尝试解析它.这解决了我的 TypeError: 'NoneType' object is not subscriptable 问题并正确传递了信息。

class FIleOps 作为组合关系提供给主要class,并在需要时通过简单对象调用。