Friday, February 6, 2009

Python packages

We tried to create packages for our controllers and there is code like below. But I don't want to have to write an import for every class I use, and I don't want to have to put all my classes in one file. I want it to be like Java - you can import a package (a directory) and you get access to all the classes within it.


from controllers.main_page import *
from controllers.book_owning_create import *
from controllers.book_yearning_create import *

application = webapp.WSGIApplication([
('/', MainPage),
('/bookowning/create', BookOwningCreate),
('/bookyearning/create', BookYearningCreate)


What I want to do is


from controllers import *

application = webapp.WSGIApplication([
('/', MainPage),
('/bookowning/create', BookOwningCreate),
('/bookyearning/create', BookYearningCreate)



It seems there is no really easy way to do it (that doesn't involve more typing). Also it seems to general consensus is to group classes in a module (ie. file).

The way I could do it would be to import all those classes in the __init__.py file of the controllers package. Like such:

(in __init__.py)

from book_owning_create import BookOwningCreate
from book_yearning_create import BookYearningCreate

No comments: