文件目录
mysetting
|
|--conf
| |
| ---__init__.py
| |
| ---globalsetting.py (系统默认配置)
|
---usersetting
| |
| ---setting.py(用户的配置)
|
---- test.py
init.py ,实现功能的代码
# init.py ,
import os
import importlib
from conf import globalsetting
class Setting:
def __init__(self):
user_setting = os.environ.get('user_setting')
for setting in dir(globalsetting):
if setting.isupper():
setattr(self, setting, getattr(globalsetting, setting))
mod = importlib.import_module(user_setting)
for setting in dir(mod):
if setting.isupper():
setattr(self, setting, getattr(mod, setting))
settings = Setting()
globalsetting.py与setting.py存放配置信息, 如DEBUG=True
test.py为程序的入口
# test.py
import os
#为环境变量添加一个user_setting:'usersetting.setting' ,
os.environ.setdefault('user_setting', 'usersetting.setting')
from conf import settings
print(settings.DEBUG)
网友评论