views:

149

answers:

2

Hi all, am wondering if I'm doing this right.

I want to split a huge py file into ten .py files ( or controllers, if you will). The reason being it's neat, and doesn't contain thousands of code lines in one single file.

Every .py file will have its own request handler.

Each .py file will serve a certain function.

(the questions come after the Code section)

Code:

# *****app.yaml***** 
handlers: 
url: /part1 
script: controllers/part1.py 
url: /part2 
script: controllers/part2.py 
# this goes on until part10.py 


# ***** part1.py (this is the entry point of the application)***** 
# all required imports ( about 20 imports, some from app engine, some from external libs ) 

class Part1(webapp.RequestHandler): 

  # a lot of code here 
  # Models are accessed, data retrieved and worked upon. Results of the work are    
  # sent directly to the template_values variable

  # point to template 
  path = os.path.join(os.path.dirname(__file__), 'templates/part1.html')

  template_values = { 'variable ':variable } 
  # render template 
  self.response.out.write(template.render(path, template_values)) 

  # the standard WSGI calls 
  application = webapp.WSGIApplication([('/part1', Part1)],debug=True) 

def main(): run_wsgi_app(application) 
if __name__ == "__main__": main() 

Additional notes: for part2.py until part10.py, it follows the exact same format as part1.py.

So far it's been working for me on both dev and production server.

My worries: 1. Am I taxing the app engine server bcos the imports in each .py file are repetitive?
2. Is it okay to have a request handler for each .py file? Am I consuming too much resource?

The big question is, can I keep doing this as my application grows ( as I add more code/functionality) ? Is this the way to expand all the way up to N numbers of .py files? What are the drawbacks?

Hope you guys can give some insights on this.

Been thinking about this for weeks.

+1  A: 

Why not just have one module with all request handlers, and then separate modules for actually handling the requests?

EDIT: set up your server in Main.py, and then have each handler class defined in different files. That's how I did it anyway.

application = webapp.WSGIApplication([('/part1', Part1),
                                     ('/part2', Part2)
                                     ],debug=True) 
run_wsgi_app(application)

And then define Part1 and Part2 classes in other files.

Sorry for not being clearer in the first place.

Tom R
hi I don't really get it. could you give me a brief example of the code?my head is going bonkers right now. sorry
fooyee
+1  A: 

If those are "parts" to the same application, you should separate them using URL mappings instead, this way maintenance is easier (since the url endpoints will be "centralized" and you'll be editing them in python) and everything gets more concise (app.yaml maps applications).

Read this for clarification

You'd basically add new mappings to several different handlers and organize those as python modules.

(...)
from myapp import Part1, Part2 # etcetera

(...)
application = webapp.WSGIApplication([('/part1', Part1), ('/part2', Part2])])

In case you're having trouble modularizing your python code, perhaps reading the official docs will help.

EDIT:

My answer above is basically the same as the first one, it wasn't there when I started answering, sorry.

A little more clarification on why this is better than having each part as a separate application:

  • Module loading will only happen once per application restart
  • If you check your appengine dashboard, some modules may take a long time to load (mostly when they're big, obviously)
  • Profiling your whole set would be just a matter of adding a Middleware to application.

Even with all that, I think the most important point would be: code organization will be way easier to accomplish since you can separate applications and logics in individual packages/applications.

Caio Romão