Question

I want to use webpy to build an app, but don't want all my source code to be in one large .py file. However, splitting the classes (index, login etc.) across multiple files results in problems with access to session and db objects.

What is the best layout for this kind of thing? Ideally I would want one file per page, so each file describes a single class, but how does one go about sharing dbs and sessions?

Was it helpful?

Solution

im my projects I use a utils.py file, in there is the code for many utilities methods used across my code, so i use this module to hold the session object. allowing me to access the session wherever its need to.

The code stays with something like this:

 
import utils
class Login(object):
    def GET(self):
        if utils.session.login:
            return "OK"
        else:
            return "Please login."

OTHER TIPS

I don't know about web.py specific, but in my projects I define a db.py (or similiar module), which defines how to access db connections. From there I import e.g. a Session() factory, which gives me the session object(s), and do that in all modules where I need it I do:

from db import Session

def some_function():
    session = Session()
    some_obj = session.get(some_id)

In db.py would be the encapsulation of the database access portion of the ORM framework of your choice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top