psycopg2 across python files
Question: I am writing a Python application (console based) that makes use of a
PostgreSQL database (via psycopg2) and R (via rpy). It is a large procedure-
based application and involves several steps and sometimes repeating of steps
and do not always involve all steps.

I have is the following:

    
    
    main_file.py
    
    modules/__init__.py
    modules/module1.py
    modules/module2.py
    
    functions/__init__.py
    functions/function1.py
    functions/function2.py
    

The init files just states import module1, module2 or function1, function2
depending which init file it is.

The content of the main_file.py looks something like this:

    
    
    import modules
    from functions import function1
    
    class myClass():
        def my_function(self):
             scripts = [
                        # modules.module1.function, 
                        modules.module2.function, 
                       ]
    
             print "Welcome to the program."
    
             function1.connect()
    
             for i in scripts:
                i
    
             cur.close()
    
             print "End of program"
    
    if __name__ == '__main__':
         myClass().my_function()
    

The reason for the loop is to comment out certain steps if I don't need them.
The connect() function I'm trying to call is the psycopg2 connection. It looks
like this (inside function1.py file):

    
    
    import sys
    import psycopg2
    
    def connect():
        try:
            con = psycopg2.connect(database=dbname, user=dbuser)
            cur = con.cursor()
            db = cur.execute
        except psycopg2.DatabaseError, e:
            if con:
                 con.rollback()
            print e
            sys.exit
    

In the main_file.py example I'm trying to run module2, which needs to connect
to the database, using something like the following:

    
    
    def function:
        db("SELECT * INTO new_table FROM old_table")
        con.commit()
    

How do I get Python (2.7) to recognise the global names db, cur and con? Thus
connecting once-off to the database and keeping the active connection through
all steps in the program?


Answer: You should add a function to the module that initialize the DB that will
return the created DB objects, and then have every module that wants to use
the DB call that function:

**function1.py**

    
    
    import sys
    import psycopg2
    
    con = cur = db = None
    
    def connect():
        global con, cur, db
        try:
            con = psycopg2.connect(database=dbname, user=dbuser)
            cur = con.cursor()
            db = cur.execute
        except psycopg2.DatabaseError, e:
            if con:
                 con.rollback()
            print e
            sys.exit
    
    
    def get_db():
        if not (con and cur and db):
            connect()
        return (con, cur, db)
    

**function2.py**

    
    
    import function1
    
    con, cur, db = function1.get_db()
    
    def function:
        db("SELECT * INTO new_table FROM old_table")
        con.commit()
    

There's no way to make certain variables global to every single module in
package. You have explicitly import them from whatever module they live in, or
return them from a function call.

