about recursive load

alex23 wuwei23 at gmail.com
Fri Oct 31 04:06:11 CET 2008


On Oct 30, 2:22 pm, Michel Perez <ops... at infomed.sld.cu> wrote:
> Hi, am very newbie in Python, but as part of a project i need to load
> configuration -a settings.py file in the package dir- of my apps
> recursively, something like this:
>
> settings.load_config("project.test.app")
> settings.load_config("project.test.*")
> settings.load_config("project.test")
> settings.load_config("*")
>
> this allows me to load them as:
>
> settings.project.CONFIG_PARAMETER_1      # project configuration
> settings.project.test.CONFIG_PARAMETER_1 # sub project
>
> and so on.

I'm not sure if I follow your full requirements, but are you after
something like this?

  class Settings(object):
      def load_config(self, module_name):
          module = __import__(module_name)
          self.__dict__[module_name] = module

  >>> settings = Settings()
  >>> settings.load_config('project.test')
  >>> settings.load_config('project')
  >>> settings.project.CONFIG_PARAMETER_1
  'project.config_parameter_1'
  >>> settings.project.test.CONFIG_PARAMETER_1
  'project.test.config_parameter_1'




More information about the Python-list mailing list