Update of /usr/cvs/Public/pygresql/module
In directory druid.net:/tmp/cvs-serv25731

Modified Files:
        TEST_PyGreSQL_dbapi20.py pgdb.py 
Log Message:
Some more code clean-up in the pgdb module.
To see the diffs for this commit:
   
http://www.druid.net/pygresql/viewcvs.cgi/cvs/pygresql/module/TEST_PyGreSQL_dbapi20.py.diff?r1=1.9&r2=1.10

Index: TEST_PyGreSQL_dbapi20.py
===================================================================
RCS file: /usr/cvs/Public/pygresql/module/TEST_PyGreSQL_dbapi20.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- TEST_PyGreSQL_dbapi20.py    1 Nov 2008 13:04:07 -0000       1.9
+++ TEST_PyGreSQL_dbapi20.py    1 Nov 2008 15:38:02 -0000       1.10
@@ -1,20 +1,25 @@
 #!/usr/bin/env python
-# $Id: TEST_PyGreSQL_dbapi20.py,v 1.9 2008/11/01 13:04:07 cito Exp $
+# $Id: TEST_PyGreSQL_dbapi20.py,v 1.10 2008/11/01 15:38:02 cito Exp $
 
 import dbapi20
 import unittest
 import pgdb
 import popen2
 
-# We need a database to test against.  If LOCAL_PyGreSQL.py exists we will
-# get our information from that.  Otherwise we use the defaults.
+# We need a database to test against.
+# If LOCAL_PyGreSQL.py exists we will get our information from that.
+# Otherwise we use the defaults.
 dbname = 'dbapi20_test'
 dbhost = None
 dbport = 5432
-try: from LOCAL_PyGreSQL import *
-except: pass
+try:
+    from LOCAL_PyGreSQL import *
+except ImportError:
+    pass
+
 
 class test_PyGreSQL(dbapi20.DatabaseAPI20Test):
+
     driver = pgdb
     connect_args = ()
     connect_kw_args = {'dsn': ':' + dbname}
@@ -22,16 +27,14 @@
     lower_func = 'lower' # For stored procedure test
 
     def setUp(self):
-        # Call superclass setUp In case this does something in the
-        # future
+        # Call superclass setUp in case this does something in the future
         dbapi20.DatabaseAPI20Test.setUp(self)
-
         try:
             con = self._connect()
             con.close()
-        except:
+        except Exception:
             cmd = "psql -c 'create database dbapi20_test'"
-            cout,cin = popen2.popen2(cmd)
+            cout, cin = popen2.popen2(cmd)
             cin.close()
             cout.read()
 

   
http://www.druid.net/pygresql/viewcvs.cgi/cvs/pygresql/module/pgdb.py.diff?r1=1.42&r2=1.43

Index: pgdb.py
===================================================================
RCS file: /usr/cvs/Public/pygresql/module/pgdb.py,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -r1.42 -r1.43
--- pgdb.py     1 Nov 2008 13:04:07 -0000       1.42
+++ pgdb.py     1 Nov 2008 15:38:02 -0000       1.43
@@ -4,7 +4,7 @@
 #
 # Written by D'Arcy J.M. Cain
 #
-# $Id: pgdb.py,v 1.42 2008/11/01 13:04:07 cito Exp $
+# $Id: pgdb.py,v 1.43 2008/11/01 15:38:02 cito Exp $
 #
 
 """pgdb - DB-API 2.0 compliant module for PygreSQL.
@@ -64,7 +64,6 @@
 """
 
 from _pg import *
-import types
 import time
 try:
        frozenset
@@ -118,6 +117,7 @@
 
        def __init__(self, cnx):
                """Initialize type cache for connection."""
+               super(pgdbTypeCache, self).__init__()
                self._src = cnx.source()
 
        def typecast(typ, value):
@@ -137,7 +137,7 @@
                """Get name of database type with given oid."""
                try:
                        return self[oid]
-               except:
+               except KeyError:
                        self._src.execute(
                                "SELECT typname, typlen "
                                "FROM pg_type WHERE oid=%s" % oid)
@@ -157,30 +157,30 @@
                return _quote(super(_quoteitem, self).__getitem__(key))
 
 
-def _quote(x):
+def _quote(val):
        """Quote value depending on its type."""
-       if isinstance(x, DateTimeType):
-               x = str(x)
-       elif isinstance(x, unicode):
-               x = x.encode( 'utf-8' )
-       if isinstance(x, str):
-               x = "'%s'" % str(x).replace("\\", "\\\\").replace("'", "''")
-       elif isinstance(x, (int, long, float)):
+       if isinstance(val, DateTimeType):
+               val = str(val)
+       elif isinstance(val, unicode):
+               val = val.encode( 'utf-8' )
+       if isinstance(val, str):
+               val = "'%s'" % str(val).replace("\\", "\\\\").replace("'", "''")
+       elif isinstance(val, (int, long, float)):
                pass
-       elif x is None:
-               x = 'NULL'
-       elif isinstance(x, (list, tuple)):
-               x = '(%s)' % ','.join(map(lambda x: str(_quote(x)), x))
-       elif Decimal is not float and isinstance(x, Decimal):
+       elif val is None:
+               val = 'NULL'
+       elif isinstance(val, (list, tuple)):
+               val = '(%s)' % ','.join(map(lambda v: str(_quote(v)), val))
+       elif Decimal is not float and isinstance(val, Decimal):
                pass
-       elif hasattr(x, '__pg_repr__'):
-               x = x.__pg_repr__()
+       elif hasattr(val, '__pg_repr__'):
+               val = val.__pg_repr__()
        else:
-               raise InterfaceError('do not know how to handle type %s' % 
type(x))
-       return x
+               raise InterfaceError('do not know how to handle type %s' % 
type(val))
+       return val
 
 
-def _quoteparams(s, params):
+def _quoteparams(string, params):
        """Quote parameters.
 
        This function works for both mappings and sequences.
@@ -190,7 +190,7 @@
                params = _quoteitem(params)
        else:
                params = tuple(map(_quote, params))
-       return s % params
+       return string % params
 
 
 ### Cursor Object
@@ -199,6 +199,7 @@
        """Cursor Object."""
 
        def __init__(self, dbcnx):
+               """Create a cursor object for the database connection."""
                self._dbcnx = dbcnx
                self._cnx = dbcnx._cnx
                self._type_cache = dbcnx._type_cache
@@ -209,29 +210,30 @@
                self.lastrowid = None
 
        def row_factory(row):
-               """You can overwrite this with a custom row factory
-                       e.g. a dict_factory
+               """Process rows before they are returned.
+
+               You can overwrite this with a custom row factory,
+               e.g. a dict factory:
+
+               class myCursor(pgdb.pgdbCursor):
+                       def cursor.row_factory(self, row):
+                               d = {}
+                               for idx, col in enumerate(self.description):
+                                       d[col[0]] = row[idx]
+                               return d
+               cursor = myCursor(cnx)
 
-                       class myCursor(pgdb.pgdbCursor):
-                               def cursor.row_factory(self, row):
-                                       d = {}
-                                       for idx, col in 
enumerate(self.description):
-                                               d[col[0]] = row[idx]
-                                       return d
-                       cursor = myCursor(cnx)
                """
                return row
        row_factory = staticmethod(row_factory)
 
        def close(self):
+               """Close the cursor object."""
                self._src.close()
                self.description = None
                self.rowcount = -1
                self.lastrowid = None
 
-       def arraysize(self, size):
-               self.arraysize = size
-
        def execute(self, operation, params=None):
                """Prepare and execute a database operation (query or 
command)."""
                # The parameters may also be specified as list of
@@ -260,7 +262,7 @@
                                        self._cnx.source().execute(sql)
                                except:
                                        raise OperationalError("can't start 
transaction")
-                               self._dbcnx._tnx = 1
+                               self._dbcnx._tnx = True
                        for params in param_seq:
                                if params:
                                        sql = _quoteparams(operation, params)
@@ -280,15 +282,9 @@
                # then initialize result raw count and description
                if self._src.resulttype == RESULT_DQL:
                        self.rowcount = self._src.ntuples
-                       d = []
-                       for typ in self._src.listinfo():
-                               # listinfo is a sequence of
-                               # (index, column_name, type_oid)
-                               # getdescr returns all items needed for a
-                               # description tuple except the column_name.
-                               desc = typ[1:2] + 
self._type_cache.getdescr(typ[2])
-                               d.append(desc)
-                       self.description = d
+                       getdescr = self._type_cache.getdescr
+                       coltypes = self._src.listinfo()
+                       self.description = [typ[1:2] + getdescr(typ[2]) for typ 
in coltypes]
                        self.lastrowid = self._src.oidstatus()
                else:
                        self.rowcount = totrows
@@ -296,24 +292,34 @@
                        self.lastrowid = self._src.oidstatus()
 
        def fetchone(self):
-               res = self.fetchmany(1, 0)
+               """Fetch the next row of a query result set."""
+               res = self.fetchmany(1, False)
                try:
                        return res[0]
-               except:
+               except IndexError:
                        return None
 
        def fetchall(self):
-               return self.fetchmany(-1, 0)
+               """Fetch all (remaining) rows of a query result."""
+               return self.fetchmany(-1, False)
 
-       def fetchmany(self, size=None, keep=0):
+       def fetchmany(self, size=None, keep=False):
+               """Fetch the next set of rows of a query result.
+
+               The number of rows to fetch per call is specified by the
+               size parameter. If it is not given, the cursor's arraysize
+               determines the number of rows to be fetched. If you set
+               the keep parameter to true, this is kept as new arraysize.
+
+               """
                if size is None:
                        size = self.arraysize
-               if keep == 1:
+               if keep:
                        self.arraysize = size
                try:
                        result = self._src.fetch(size)
-               except Error, e:
-                       raise DatabaseError(str(e))
+               except Error, err:
+                       raise DatabaseError(str(err))
                row_factory = self.row_factory
                typecast = self._type_cache.typecast
                coltypes = [desc[1] for desc in self.description]
@@ -321,14 +327,17 @@
                        for args in zip(coltypes, row)]) for row in result]
 
        def nextset():
+               """Not supported."""
                raise NotSupportedError("nextset() is not supported")
        nextset = staticmethod(nextset)
 
        def setinputsizes(sizes):
+               """Not supported."""
                pass
        setinputsizes = staticmethod(setinputsizes)
 
-       def setoutputsize(size, col=0):
+       def setoutputsize(size, column=0):
+               """Not supported."""
                pass
        setoutputsize = staticmethod(setoutputsize)
 
@@ -339,8 +348,9 @@
        """Connection Object."""
 
        def __init__(self, cnx):
+               """Create a database connection object."""
                self._cnx = cnx # connection
-               self._tnx = 0 # transaction state
+               self._tnx = False # transaction state
                self._type_cache = pgdbTypeCache(cnx)
                try:
                        self._cnx.source()
@@ -348,6 +358,7 @@
                        raise OperationalError("invalid connection")
 
        def close(self):
+               """Close the connection object."""
                if self._cnx:
                        self._cnx.close()
                        self._cnx = None
@@ -355,9 +366,10 @@
                        raise OperationalError("connection has been closed")
 
        def commit(self):
+               """Commit any pending transaction to the database."""
                if self._cnx:
                        if self._tnx:
-                               self._tnx = 0
+                               self._tnx = False
                                try:
                                        self._cnx.source().execute("COMMIT")
                                except:
@@ -366,9 +378,10 @@
                        raise OperationalError("connection has been closed")
 
        def rollback(self):
+               """Roll back to the start of any pending transaction."""
                if self._cnx:
                        if self._tnx:
-                               self._tnx = 0
+                               self._tnx = False
                                try:
                                        self._cnx.source().execute("ROLLBACK")
                                except:
@@ -377,6 +390,7 @@
                        raise OperationalError("connection has been closed")
 
        def cursor(self):
+               """Return a new Cursor Object using the connection."""
                if self._cnx:
                        try:
                                return pgdbCursor(self)
@@ -389,6 +403,7 @@
 ### Module Interface
 
 _connect_ = connect
+
 def connect(dsn=None,
                user=None, password=None,
                host=None, database=None):
@@ -409,7 +424,7 @@
                dbpasswd = params[3]
                dbopt = params[4]
                dbtty = params[5]
-       except:
+       except (IndexError, TypeError):
                pass
 
        # override if necessary
@@ -424,7 +439,7 @@
                        params = host.split(":")
                        dbhost = params[0]
                        dbport = int(params[1])
-               except:
+               except (IndexError, TypeError, ValueError):
                        pass
 
        # empty host is localhost
@@ -449,17 +464,16 @@
 
        """
 
-       def __new__(cls, values):
-               # for Python >= 2.4
-               if isinstance(values, basestring):
-                       values = values.split()
-               return super(pgdbType, cls).__new__(cls, values)
-
-       def __init__(self, values):
-               # for Python < 2.4
-               if isinstance(values, basestring):
-                       values = values.split()
-               super(pgdbType, self).__init__(values)
+       if frozenset.__module__ == '__builtin__':
+               def __new__(cls, values):
+                       if isinstance(values, basestring):
+                               values = values.split()
+                       return super(pgdbType, cls).__new__(cls, values)
+       else: # Python < 2.4
+               def __init__(self, values):
+                       if isinstance(values, basestring):
+                               values = values.split()
+                       super(pgdbType, self).__init__(values)
 
        def __eq__(self, other):
                if isinstance(other, basestring):
@@ -501,24 +515,31 @@
 # Mandatory type helpers defined by DB-API 2 specs:
 
 def Date(year, month, day):
+       """Construct an object holding a date value."""
        return DateTime(year, month, day)
 
 def Time(hour, minute, second):
+       """Construct an object holding a time value."""
        return TimeDelta(hour, minute, second)
 
 def Timestamp(year, month, day, hour, minute, second):
+       """construct an object holding a time stamp value."""
        return DateTime(year, month, day, hour, minute, second)
 
 def DateFromTicks(ticks):
+       """Construct an object holding a date value from the given ticks 
value."""
        return Date(*time.localtime(ticks)[:3])
 
 def TimeFromTicks(ticks):
+       """construct an object holding a time value from the given ticks 
value."""
        return Time(*time.localtime(ticks)[3:6])
 
 def TimestampFromTicks(ticks):
+       """construct an object holding a time stamp from the given ticks 
value."""
        return Timestamp(*time.localtime(ticks)[:6])
 
 def Binary(value):
+       """construct an object capable of holding a binary (long) string 
value."""
        return value
 
 

_______________________________________________
PyGreSQL mailing list
[email protected]
http://mailman.vex.net/mailman/listinfo/pygresql

Reply via email to