Sign in to follow this  

python: listing all modules in a dir

This topic is 4379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay I have a hierarchical filesystem as this:
GameDir /
          editor.py
          data /
                    __init__.py
                    entities /
                              __init__.py
                              entity.py


And inside the entity.py I got a class 'Entity' Now what I want is to make editor.py import all modules inside entities and then import the contents of that into a new namespace OR the main namespace. But a new namespace would be best. Like 'ets' so I can do: e1 = ets.Entity(); e2 = ets.ChildEntity(); Any ideas ?

Share this post


Link to post
Share on other sites
Assuming your __init__.py files are there to make the directory into packages and that the entities directory is on the module path, and that your __init__.py file properly defines the __all__ variable, I think you could probably just use from entities import *.

Share this post


Link to post
Share on other sites
Okay I think that will work.
*not at home now so I cant check it, but I think its the __all__ variable that
I haven't set properly*


Also:
How to create someting in python based on a string?
example:


name_of_class = "entity.Entity";

# here I want to create a Entity variable named 'e'
e = name_of_class();


I know this is not the way to do it but I dunno to. I guess it would involve
using the dictornary in some way...

[Edited by - CoMaNdore on December 20, 2005 8:36:02 AM]

Share this post


Link to post
Share on other sites
temp.py:

class Cow:
def noise(self):
return "Moo";

class Dog:
def noise(self):
return "Bark";


in the interpreter (or where ever)

import temp

s = "Cow"
animal = temp.__dict__[s]
d = animal()
d.noise()
s = "Dog"
animal = temp.__dict__[s]
d = animal()
d.noise()

Share this post


Link to post
Share on other sites

This topic is 4379 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this