Python pattern for sharing configuration throughout application

前端 未结 4 985
逝去的感伤
逝去的感伤 2020-12-14 07:36

I have an application consisting of a base app that brings in several modules. The base app reads a parameter file into a configuration hash, and I want to share it across a

相关标签:
4条回答
  • 2020-12-14 08:23

    Take a look at this. It could help you: https://gist.github.com/dgarana/c052a3287629dd7c0b0c9d7921081e9d

    0 讨论(0)
  • I think by 'module', you are actually referring to a 'class/object'. An object is an instance of a class, for example:

    class MyClass(object):
        def __init__(self, ...):
            ...
        ...
    
    myObject = MyClass()
    

    A module is a .py file you import, like so:

    import mymodule
    

    It seems unlikely that all the classes you instantiate would want to have access to a global configuration. However if you really need everything in your application to have access to some global parameters, you can put them in your own config module:

    myParam1 = 1
    myParam2 = 2
    

    and then from any module or any object or anywhere really, as long as you did import config, you could just say print(config.myParam1)

    Alternatively if you want a large hierarchy of objects to all share access to the same property, you don't need to refer to it via manually setting a self.parent. As long as you use inheritance, you can do stuff like:

    class Parent(object):
        def __init__(self, theConfig):
            self.theConfig = theConfig
    
    class Child(Parent):
        ...
        def method(self,...):
            print(self.theConfig)
    
    0 讨论(0)
  • 2020-12-14 08:34

    You could just:

    import config
    

    and have a global config module


    excerpts from my comments:

    You can always add special rules for odd situations by just saying oddValue if isOddSituation() else config.normalValue.

    If you want to have configuration modules be hierarchically subclassable (like my other answer describes), then you can represent a config as a class, or you can use the copy module and make a shallow copy and modify it, or you can use a "config dictionary", e.g.:

    import config as baseConfig
    config = dict(baseConfig, overriddenValue=etc)
    

    It doesn't really matter too much which scope you're in.

    0 讨论(0)
  • 2020-12-14 08:37

    Answering old question:

    Just use dependency injection as suggested by @Reed Copsey here. E.g.

    class MyClass:
        def __init__(myConfig):
            self.myConfig = myConfig
            ...
    
        def foo():
            self.myConfig.getConfig(key)
            ...
            self.myConfig.setConfig(key,val)
            ...
    
    ...
    # myConfig is your configuration management Module/Class
    obj = SomeClass(myConfig)
    
    0 讨论(0)
提交回复
热议问题