在python中保存配置文件的最佳实践

时间:2017-11-29 21:08:52

标签: python config

我有一个python脚本,其中有一个如下所示的配置文件:

PROD = 'production'
DEV = 'dev'
ENVIRONMENT = None

我有一个函数从命令参数中获取所需的环境并将其设置为:

if sys.argv[1] in [config.PROD, config.DEV]:
    config.ENVIRONMENT = sys.argv[1]

我理解当我开始在多个文件中导入配置文件并且ENV保持重置为无时,这不是一个好习惯。

所以,我的问题是:这种情况的最佳做法是什么

1 个答案:

答案 0 :(得分:1)

我不确定最佳做法是什么,但我喜欢使用JSON文件。我使用以下类作为抽象层来与config(属性)文件进行交互。您可以创建一个JSONPropertiesFile并将其传递给您的应用程序。

pragma Linker_Options

在您的情况下,您可以像这样使用它:

import json
from collections import OrderedDict
import os
from stat import * # ST_SIZE etc
from datetime import datetime
from copy import deepcopy


class JSONPropertiesFileError(Exception):
    pass


class JSONPropertiesFile(object):

    def __init__(self, file_path, default={}):
        self.file_path = file_path
        self._default_properties = default
        self._validate_file_path(file_path)

    def _validate_file_path(self, file_path):
        if not file_path.endswith(".json"):
            raise JSONPropertiesFileError(f"Must be a JSON file: {file_path}")
        if not os.path.exists(file_path):
            self.set(self._default_properties)

    def set(self, properties):
        new_properties = deepcopy(self._default_properties)
        new_properties.update(properties)
        with open(self.file_path, 'w') as file:
            json.dump(new_properties, file, indent=4)


    def get(self):
        properties = deepcopy(self._default_properties)
        with open(self.file_path) as file:
            properties.update(json.load(file, object_pairs_hook=OrderedDict))
        return properties

    def get_file_info(self):
        st = os.stat(self.file_path)
        res = {
            'size':st[ST_SIZE],
            'size_str':str(round(st[ST_SIZE]/1000,2)) + ' KB',
            'last_mod': datetime.fromtimestamp(st[ST_MTIME]).strftime("%Y-%m-%d")
         }
         return res
相关问题