Thread: Loading classes

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Bay area
    Posts
    9
    Rep Power
    0

    Loading classes


    Hello forum -

    I'm fairly new to python, and the task I've set myself may be over my head, but let me describe what I want, and see if it makes sense...

    I have my project structured something like this:
    Code:
    main/
          __init__.py
          main.py
          modules/
                __init__.py
                base_class.py
                derived_class_1.py
                derived_class_2.py
                ....
    So - when I run my main.py, I would like it to load any 'derived_class' that is in the modules folder. I want to be able to just drop new class in that folder, and have them be added automatically.

    So I've been playing with the os library, and I can get a list of the files in the modules folder. Then I can do some string manipulation, and the eval() function to load the class (provided the class has the same name as the file).

    It seems rather sloppy though. I think I must be missing something. Do you have any suggestions on how to accomplish this more efficiently?
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I'm a little unsure on what your doing here, it seems like all you want is a package (a group of Python modules). In this case you can just do something like

    from main.module.fileNameWithoutSuffix import className

    assuming of course that the main folder is on Pythons search path; $PYTHONPATH/sys.path .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    This is a fairly common idiom. You can import a module dynamically using the built-in __import__ function. e.g.

    Code:
    for filename in glob.glob('path/to/modules/*.py'):
       moduleName = os.path.splitext(os.path.basename(filename))[0]
       module = __import__(moduleName)
       module.initialise()
    Notes:
    1) I use the glob module to get a list of all the *.py files. If you iterate over all the files in the directory then you will end up importing everything twice, since each .py file will also have a .pyc file with it.

    2) I assume that each module has an initialise function that registers the module somewhere, so that my code can use it later on. However, what you do with the module once you have it loaded is up to you.

    Dave - The Developers' Coach
    Last edited by DevCoach; January 17th, 2005 at 05:28 PM.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Bay area
    Posts
    9
    Rep Power
    0
    That's great, thanks for your help

IMN logo majestic logo threadwatch logo seochat tools logo