怎么回事app.config.from_对象工作?

2024-04-23 08:52:08 发布

您现在位置: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_对象'如何解决这个问题?在


Tags: keyfromselfidconfigobjhostobject