Hi folks,

I am quite new to Python and SQLAlchemy and I have a question about to
setup the project structure.

I have my main python script and then a bunch of classes - each
defined in their own class file and stored in packages (directories)
like so

/root
myApp.py
   modules/
   class1.py
   class2.py
   modules/packageA
      Amodule1.py
      Amodule2.py
   modules/packageB
      Bmodule1.py
      BModule2.py


and so on. This app is for a long running daemon running on linux
which monitors the file system and updates the database depending on
certain things.

>From reading through theSQLAlchemy docs, I gather that I am supposed
to instantiate an engine and a session once, globally for the app and
use this throughout the app, opening and closing connections when
needed and configuring sessions etc.

My question is (I am very new to Python to please bear with me), how
do I do this?

What I have tried is putting all the setup instructions into a class
and instantiating this in myApp.py. This doesn't make it global to all
the modules though. Do I need to pass this object around to all
classes as arguments to __init__() in order to reference this class?
(this seems a bit bulky to me)

Here is my Bootstrap class

class Bootstrap(object):
    def __init__(self):
        self.mDb = MyDb() #my DB abstraction class
        self.mEngine = self.mDb.getEngine()
        self.mSession = self.mDb.getSession()
        self.mMeta = self.mDb.getMeta(self.mEngine)

        if self.mTable1 == None:
            self. mTable1 = self.mDb.getTable("table1", self.mMeta)
            self.mEdoMapper = mapper(Table1, self. mTable1)

        if self.mTable2 == None:
            self. mTable2 = self.mDb.getTable("table2", self.mMeta)
            self.mEdoMapper = mapper(Table2, self. mTable2)

        #<snip>

class MyDb(object):
    mDbHost = "localhost"
    mDbUser = "user"
    mDbPass = "pass"
    mDbSchema = "schema"
    mEngine = None
    mSession = None
    mConn = None
    mMeta = None

    def getEngine(self):
            self.mEngine = create_engine("mysql://%s:[EMAIL PROTECTED]/%s" %
(self.mDbUser, self.mDbPass, self.mDbHost, self.mDbSchema))
            return self.mEngine

    def getConnection(self):
        return self.mEngine.connect()

    def getMeta(self, engine):
        self.mMeta = MetaData()
        self.mMeta.bind = engine
        return self.mMeta

    def getTable(self, table, meta):
        t = Table(table, meta, autoload=True)
        Column('created', mysql.MSDateTime, PassiveDefault(text
("CURRENT_TIMESTAMP")), nullable=False)
        return t

    def getSession(self):
        if self.mSession == None:
            self.mSession = sessionmaker()
            self.mSession.configure(bind=self.mEngine)

        return self.mSession

    #<snip>


I need to talk to the database in various modules but I don't want to
open up new engines/sessions etc. Can anyone point me to how to go
about setting up a project which involves many classes/modules etc
(I'm also using threads but I'll leave that for another day :))

Any pointers on this would be much appreciated. (please excuse my lack
of understanding of Python, I'm only about a week old on it so far)

thanks


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to