Next / Previous / Contents / NM Tech homepage

6.14. CBCData.__init__(): Constructor

Here is the constructor for the CBCData class, which establishes a connection to the Postgresql engine.

pycbc.py
# - - -   C B C D a t a . _ _ i n i t _ _
    def __init__ ( self, password ):
        '''Constructor: connect to the database.
        '''
        #-- 1 --
        # [ password is a string  ->
        #     if the Postgresql server is available ->
        #         self.engine  :=  an sqlalchemy.engine.Engine
        #             instance connected to that engine
        #         CBCData.meta  :=  CBCData.meta bound to that engine
        #     else -> raise an sqlalchemy.exc.SQLAlchemyError ]
        url = ( URL_FORMAT %
                (PROTOCOL, DB_USER, password, DB_HOST, DB_NAME) )
        self.engine = engine.create_engine ( url )
        CBCData.meta.bind = self.engine

Then we create the Session constructor. The autoflush=True option forces a flush of operations to the database on a commit. The autocommit=False means no commit is done after a flush. The expire_on_commit=True causes cached values to be invalidated after an update so that subsequent operations go out to the database.

pycbc.py
        #-- 2 --
        # [ session  :=  a class constructor that creates a
        #       new session using self.engine
        #   s  :=  an instance of that class ]
        self.Session = orm.sessionmaker(bind=self.engine,
            autoflush=True, autocommit=False, expire_on_commit=True )
        self.s = self.Session()