about recursive load

Discussion in 'Python' started by Michel Perez, Oct 30, 2008.

  1. Michel Perez

    Michel Perez Guest

    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.
    This it's what i've done,

    class Settings:
    def __getattr__( self, attr):
    return self.__dict__['flags'][attr]

    def __setattr__(self, attr, value):
    self.__dict__['flags'][attr]=value

    def __init__(self, package = None, parent = None):
    self.__package = package
    self.__parent = None
    self.__dict__['flags']={}

    def get_parent ( self):
    return self.__parent

    def create_config_structure( self, pkg, parent = None ):
    # ... assuming no error and all that
    if pkg.count(".") > 0:
    if parent is None:
    if not self.__dict__['flags'].has_key(pkg[:pkg.find(".")]):
    father=self.__dict__['flags'][pkg]=Settings( \
    pkg[:pkg.find(".")],self)
    else:
    father = parent
    else:
    if not parent.__dict__['flags'].has_key(pkg[:pkg.find(".")]):
    father=parent.__dict__['flags'][pkg[:pkg.find(".")]]= \
    Settings(pkg[:pkg.find(".")], parent)
    else:
    father = parent
    self.create_config_structure( pkg [pkg.find(".")+1:],father)
    else:
    if not parent.__dict__['flags'].has_key:
    parent.__dict__['flags'][pkg]=Settings(pkg,parent)
    return parent.__dict__['flags'][pkg]

    def load_config ( self, pkg= None ):
    config_module_object = self.create_config_structure( pkg )

    # the loading configuration part
    try:
    if pkg is not None:
    mod = __import__( pkg + ".settings", {},{},[''])
    else:
    mod = __import__( "settings", {},{},[''])
    except:
    raise ImportError("Settings not found")

    data={}
    for setting in dir(mod):
    if setting == setting.upper():
    data[setting]=getattr(mod, setting)

    for key in data:
    if pkg is not None:
    setattr( config_module_object.__dict__['flags'], key, data[key])
    else:
    setattr(self.__dict__['flags'], key, data[key])

    Any idea it's welcome


    ---------------------------------------
    Red Telematica de Salud - Cuba
    CNICM - Infomed
    Michel Perez, Oct 30, 2008
    #1
    1. Advertising

  2. Michel Perez

    alex23 Guest

    On Oct 30, 2:22 pm, Michel Perez <> 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'
    alex23, Oct 31, 2008
    #2
    1. Advertising

  3. Michel Perez

    alex23 Guest

    On Fri, Oct 31, 2008 at 3:50 PM, Abah Joseph <>
    wrote:
    > what will module_name looks like?. i mean the new loaded
    > module. i am very new to this also. thank you.


    I'm not sure if I'm following what you want... I thought you were
    asking for a way to load modules containing configuration information
    into a single object, in order to be able to refer to them all through
    one instance:

    > 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


    Is that correct?

    If so, in my example 'module_name' will be arguments like
    "project.test.app", which the load_config module imports and then
    injects into its attribute dictionary, to allow for dot notation
    lookups...

    If you're after something different, could you try explaining it
    another way?

    (You should also reply to the group/mailing list rather than to me
    personally, it increases the potential sources of help...)
    alex23, Oct 31, 2008
    #3
  4. Michel Perez

    alex23 Guest

    On Oct 31, 4:58 pm, alex23 <> wrote:
    > "project.test.app", which the load_config module imports and then


    Sorry, that should be "load_config *method*".
    alex23, Oct 31, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mad Scientist Jr
    Replies:
    7
    Views:
    1,162
    Emma Gumbdough
    Jun 30, 2004
  2. venky

    Page Load and webcontrol page load

    venky, Jul 6, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    3,097
    venky
    Jul 11, 2005
  3. roni
    Replies:
    1
    Views:
    647
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tp?=
    Aug 30, 2005
  4. n00m
    Replies:
    12
    Views:
    1,112
  5. vamsi
    Replies:
    21
    Views:
    2,073
    Keith Thompson
    Mar 9, 2009
Loading...

Share This Page