[Tutor] global variables

Guillermo Fernandez guillermo.fernandez at epfl.ch
Sat Oct 4 16:15:45 EDT 2003


Hi,

Appended is a module I've written to interface to a sqlite database. My problem 
is I would like to make 'module variables' to address the database and cursor 
without passing them as arguments to the functions. I've done it with 'global', 
but:
1) I don't really get the global concept.
2) I feel there must be a cleaner way of doing it.

Furthermore, I don't understand why I can address clientmacs, apnames and ipaps 
  inside a function without making them global while I can't with the database, 
cursor and id variable.

If you have any explanation, advice or document I could read, I would be 
grateful to know it.

Thanks!

Guille


************************************************************
CODE:


#--- Imports
import sqlite       #Sqlite database interface, Python DB API 2.0 compatible


#--- Database

global database
global cursor
global id

# TODO: This is immediate operations, must implement security checks!
def open(databasename):
     """Open or create the database and prepares a cursor to operate"""
     global database
     global cursor
     global id
     database=sqlite.connect(databasename)
     cursor=database.cursor()

def create():
     """Create the database tables"""
     global database
     global cursor
     global id
     id=1
     cursor.execute('CREATE TABLE logs(id VARCHAR(10) PRIMARY KEY,\
                                         log VARCHAR(400),\
                                         mmonth VARCHAR(20),\
                                         mday VARCHAR(10),\
                                         mhour VARCHAR(10),\
                                         apname VARCHAR(50),\
                                         ipap VARCHAR(20),\
                                         messagenumber VARCHAR(20),\
                                         apmessage VARCHAR(300),\
                                         year VARCHAR(10),\
                                         apmonth VARCHAR(20),\
                                         apday VARCHAR(10),\
                                         aphour VARCHAR(10),\
                                         messagecode VARCHAR(20),\
                                         message VARCHAR(160),\
                                         clientname VARCHAR(50),\
                                         clientmac VARCHAR(20),\
                                         messageexplanation VARCHAR(200))')
     cursor.execute('CREATE TABLE clientmacs(clientmac VARCHAR(20))')
     cursor.execute('CREATE TABLE apnames(apname VARCHAR(200))')
     cursor.execute('CREATE TABLE ipaps(ipap VARCHAR(20))')


def execute(query):
     """Executes a query that *do not need* a commit over the database"""
     global database
     global cursor
     global id

     cursor.execute(query)
     return cursor.fetchall()

variables=['mmonth', 'mday', 'mhour', 'apname',
             'ipap', 'messagenumber', 'apmessage', 'year',
             'apmonth', 'apday', 'aphour', 'messagecode', 'message',
             'clientname', 'clientmac', 'messageexplanation']

clientmacs=[]
apnames=[]
ipaps=[]
def addentry(parser):
     """Inserts an entry to the database"""

     global database
     global cursor
     global id
     SQLbegin="INSERT INTO logs (id, log, mmonth, mday, mhour, apname, ipap,\
                                 messagenumber, apmessage, year, apmonth,\
                                 apday, aphour, messagecode, message,\
                                 clientname, clientmac, messageexplanation)\
                 VALUES ( '"+str(id)+"', '"
     id+=1
     SQLend="')"
     query=SQLbegin+parser.group(0)+"', '"
     for i in variables:
         query=query+str(parser.group(i))+"', '"
     query=query[:-4]+SQLend
     cursor.execute(query)
    # We make lists with all relevant data
     if parser.group('clientmac') not in clientmacs:
         query="INSERT INTO clientmacs (clientmac) VALUES ('"+\
             parser.group('clientmac')+SQLend
         cursor.execute(query)
         clientmacs.append(parser.group('clientmac'))
     if parser.group('apname') not in apnames:
         query="INSERT INTO apnames (apname) VALUES ('"+\
             parser.group('apname')+SQLend
         cursor.execute(query)
         clientmacs.append(parser.group('apname'))
     if parser.group('ipap') not in ipaps:
         query="INSERT INTO ipaps (ipap) VALUES ('"+\
             parser.group('ipap')+SQLend
         cursor.execute(query)
         clientmacs.append(parser.group('ipap'))


def close():
     """Closes the database"""

     global database
     global cursor
     global id
     id=1
     cursor.close()
     database.commit()
     database.close()





More information about the Tutor mailing list