views:

46

answers:

1

I am new to database programming with Google App Engine and am programming in Python. I was wondering if I am allowed to have one Python file with several request handler classes, each of which has get and post methods. I know that the yaml file allows me to specify which scripts are run with specific urls, like the example below:

handlers:
- url: /.*
  script: helloworld.py

How would I tell it to run a specific method that is in one of the classes in the .py file? Is that even possible/allowed? Do I need to separate the different request handler classes into different python files? My understanding of databases is rather shallow at the moment, so I could be making no sense.

Thanks.

+2  A: 

I was wondering if I am allowed to have one Python file with several request handler classes, each of which has get and post methods.

Sure! That app.yaml just transfers control to helloworld.py, which will run the main function defined in that file -- and that function typically sets up a WSGI app which dispatches appropriately, depending on the URL, to the right handler class. For example, look at the sample code here, very early on in the tutorial:

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/sign', Guestbook)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

I'm not copying the import statements and class definitions, because they don't matter: this is an example of how a single .py file dispatches to various handler classes (two in this case).

This doesn't mean the yaml file lets you call any method whatsoever, of course: rather, it hands control to a .py file, whose main is responsible for all that follows (and e.g. with the webapp mini-framework that comes with App Engine, it will always be get or post method [[or put, delete, ..., etc, if you also support those -- few do unless they're being especially RESTful;-)]] being called depending on the exact HTTP method and URL in the incoming request.

Alex Martelli
Thank you for your thorough and helpful answer! I think I'm slowly starting to get a grip of databases and all. Very interesting stuff :)
ayanonagon
By the way, what does debug=True do?
ayanonagon
@Ayanonagon, you're welcome! `debug=True` means to show the tracebacks from exceptions (if any) on the visiting browser, otherwise by default only a very opaque "internal server error" (HTTP status 500) is given.
Alex Martelli
Aha, that makes sense. Thanks again!
ayanonagon