烧瓶app.config.from_object如何工作?

时间:2016-03-23 13:05:16

标签: python

目前!我写了一个python项目,我指的是flask。当我编写配置模块时,我遇到了问题。下面的示例代码将无法正常工作!

class ConfigSample:
    PREFIX = ''
    MAIN_SPLIT = []
    MESSAGE_ID = 'message_id'
    DEVICE_ID = 'device_id'
    HOST = '127.0.0.1'
    PORT = 5555
    DEBUG = True
    ESCAPE_LIST = {}

class Config(dict):
   def from_object(self, obj):

       for key in dir(obj):
            if key.isupper():
                self[key] = getattr(obj, key)

class SampleForTestConfigDict:
    config = Config()

    def __init__(self):
        print self.config.get('HOST', 'default host')


if __name__ == '__main__':
   p = SampleForTestConfigDict()
   #: This config method can be easy load!
   p.config.from_object(ConfigSample)
   #: There also another config method
   p.config['ak'] = 'kkkk'
   print p.config

我想在SampleForTestConfigDict init方法中使用config。但是 init 方法在我通过'p.config.from_object'分配之前已经运行了,我该如何解决这个问题?

0 个答案:

没有答案