Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多个模块使用的配置信息发生意外更改的更好方法_Python_Configuration_Import - Fatal编程技术网

Python 多个模块使用的配置信息发生意外更改的更好方法

Python 多个模块使用的配置信息发生意外更改的更好方法,python,configuration,import,Python,Configuration,Import,我正在努力确定向许多系统管理员脚本提供配置数据的“最佳”方式,这些脚本可以管理备份、归档等 我在测试过程中使用的两种方法是脚本中的常量(对于生产来说一点也不好,但对于测试来说很容易),以及使用configparser将信息保存在单个文件中 我正在寻找一种替代方法,将所有内容都放在一个模块中,如下所示: 配置.py import re # Regular expression object to use in matching with file names FILE_NAME_MATCH_PAT

我正在努力确定向许多系统管理员脚本提供配置数据的“最佳”方式,这些脚本可以管理备份、归档等

我在测试过程中使用的两种方法是脚本中的常量(对于生产来说一点也不好,但对于测试来说很容易),以及使用configparser将信息保存在单个文件中

我正在寻找一种替代方法,将所有内容都放在一个模块中,如下所示:

配置.py

import re
# Regular expression object to use in matching with file names
FILE_NAME_MATCH_PATTERN = re.compile(
    '''([A-Z@~1-9]{4})     # Group-1: 4 uppercase letters for the show code
    _                 # if they are followed by an underscore
    (\d{2}\.?\d{3})   # Group-2: either ##.### or ##### (remove the . to get a final file name!
    _                 # if they are followed by an underscore
    ([A-Z](?=[._ ]))   # Group-3: a SINGLE uppercase letter IF it is followed by a " ", "." or an "_" only
    _?                # but, since we may or may not have the _ we need to look for it
    (.*)              # Group-4: IF there is anything between A_ and .ext we capture it here
    \.(.+)$           # Group-5: The file extension, of any length
    ''', re.VERBOSE)  # DESIGNED TO WORK WITH RE.MATCH AT THE BEGINNING OF A STRING!
WATCHED_FOLDER_PATH = '/Users/ops/Desktop/first_drop'
OUTPUT_FOLDER_PATH = '/Users/ops/Desktop/slicer_watched'
SECONDS_TO_WAIT_FOR_IMAGES = 5  # 5 for testing, 60 for production
SECONDS_TO_WAIT_FOR_UPLOADS = 5  # 5 for testing, 20 for production
SECONDS_TO_WAIT_BETWEEN_FOLDER_CHECKS = 1  # 1 for testing, 120 for production
并使用从配置导入* 但是,我有点担心名字冲突。我知道我可以进行配置。输出文件夹路径(等等),只是看起来有点凌乱。也许,

from configurations import SECONDS_TO_WAIT_FOR_IMAGES
等等

理想情况下,有一个单独的配置文件可能会被许多不同的例程使用,但这会带来一个问题,即确保配置文件路径是每个脚本的一部分(另一个常量),而导入方法只需要我将configurations.py文件放入计算机上的pythonpath中


当然这是一个被讨论到了极点的问题,但我的谷歌浏览器让我失望了

我可以用字典吗?不确定
配置有什么问题。除了行长问题之外,输出文件夹路径也会让我烦恼。没有想到dict。主要问题是,如果您允许“用户”修改dict,那么dict更容易出错。这也是配置文件的优点之一——没有引号,没有混乱,很难搞乱。是的,很容易错过
。你可以制作更小的类,比如
SecondsToWait
,它有
用于图像
等,这使得我们更容易处理类似的事情,就像我们处理类似的工作一样