python single configuration file

前端 未结 4 601
旧巷少年郎
旧巷少年郎 2021-01-31 18:50

I am developing a project that requires a single configuration file whose data is used by multiple modules.
My question is: what is the common approach to that? should i rea

4条回答
  •  粉色の甜心
    2021-01-31 19:34

    I like the approach of a single config.py module whose body (when first imported) parses one or more configuration-data files and sets its own "global variables" appropriately -- though I'd favor config.teamdata over the round-about config.data['teamdata'] approach.

    This assumes configuration settings are read-only once loaded (except maybe in unit-testing scenarios, where the test-code will be doing its own artificial setting of config variables to properly exercise the code-under-test) -- it basically exploits the nature of a module as the simplest Pythonic form of "singleton" (when you don't need subclassing or other features supported only by classes and not by modules, of course).

    "One or more" configuration files (e.g. first one somewhere in /etc for general default settings, then one under /usr/local for site-specific overrides thereof, then again possibly one in the user's home directory for user specific settings) is a common and useful pattern.

提交回复
热议问题