Changeset 357 for trunk/module/pg.py


Ignore:
Timestamp:
Nov 21, 2008, 6:18:32 AM (11 years ago)
Author:
cito
Message:

Modernize code in pg module a bit (we are requiring Py 2.3 now).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/module/pg.py

    r346 r357  
    66# Improved by Christoph Zwerschke
    77#
    8 # $Id: pg.py,v 1.58 2008-11-01 18:21:12 cito Exp $
     8# $Id: pg.py,v 1.59 2008-11-21 11:18:32 cito Exp $
    99#
    1010
     
    2020
    2121from _pg import *
    22 from types import *
    2322try:
    2423    from decimal import Decimal
     
    4342        return "'%.2f'" % float(d)
    4443    if t == 'bool':
    45         if type(d) == StringType:
     44        if isinstance(d, basestring):
    4645            if d == '':
    4746                return 'NULL'
    48             d = str(d).lower() in ('t', 'true', '1', 'y', 'yes', 'on')
    49         else:
    50             d = not not d
     47            d = d.lower() in ('t', 'true', '1', 'y', 'yes', 'on')
     48        else:
     49            d = bool(d)
    5150        return ("'f'", "'t'")[d]
    5251    if t in ('date', 'inet', 'cidr'):
     
    115114# The PostGreSQL database connection interface:
    116115
    117 class DB:
     116class DB(object):
    118117    """Wrapper class for the _pg connection type."""
    119118
     
    152151        self.debug = None # For debugging scripts, this can be set
    153152            # * to a string format specification (e.g. in CGI set to "%s<BR>"),
    154             # * to a function which takes a string argument or
    155             # * to a file object to write debug statements to.
     153            # * to a file object to write debug statements or
     154            # * to a callable object which takes a string argument.
    156155
    157156    def __getattr__(self, name):
     
    169168        """Print a debug message."""
    170169        if self.debug:
    171             if isinstance(self.debug, StringType):
     170            if isinstance(self.debug, basestring):
    172171                print self.debug % s
    173             elif isinstance(self.debug, FunctionType):
     172            elif isinstance(self.debug, file):
     173                print >> self.debug, s
     174            elif callable(self.debug):
    174175                self.debug(s)
    175             elif isinstance(self.debug, FileType):
    176                 print >> self.debug, s
    177176
    178177    def close(self):
     
    268267        """
    269268        # First see if the caller is supplying a dictionary
    270         if isinstance(newpkey, DictType):
     269        if isinstance(newpkey, dict):
    271270            # make sure that we have a namespace
    272271            self._pkeys = {}
     
    284283
    285284        # Get all the primary keys at once
    286         if self._pkeys == {} or not self._pkeys.has_key(qcl):
     285        if qcl not in self._pkeys:
    287286            # if not found, check again in case it was added after we started
    288287            self._pkeys = dict([
     
    338337
    339338        """
    340         if isinstance(newattnames, DictType):
     339        if isinstance(newattnames, dict):
    341340            self._attnames = newattnames
    342341            return
     
    347346        qcl = _join_parts(cl) # build qualified name
    348347        # May as well cache them:
    349         if self._attnames.has_key(qcl):
     348        if qcl in self._attnames:
    350349            return self._attnames[qcl]
    351350        if qcl not in self.get_relations('rv'):
     
    407406            keyname = self.pkey(qcl)
    408407        fnames = self.get_attnames(qcl)
    409         if isinstance(arg, DictType):
     408        if isinstance(arg, dict):
    410409            # XXX this code is for backwards compatibility and will be
    411410            # XXX removed eventually
    412             if not arg.has_key(foid):
     411            if foid not in arg:
    413412                ofoid = 'oid_' + self._split_schema(cl)[-1]
    414                 if arg.has_key(ofoid):
     413                if ofoid in arg:
    415414                    arg[foid] = arg[ofoid]
    416415
     
    464463        n = []
    465464        for f in fnames.keys():
    466             if f != 'oid' and a.has_key(f):
     465            if f != 'oid' and f in a:
    467466                t.append(_quote(a[f], fnames[f]))
    468467                n.append('"%s"' % f)
     
    494493
    495494        # Note that we only accept oid key from named args for safety
    496         if kw.has_key('oid'):
     495        if 'oid' in kw:
    497496            kw[foid] = kw['oid']
    498497            del kw['oid']
     
    506505        # XXX this code is for backwards compatibility and will be
    507506        # XXX removed eventually
    508         if not a.has_key(foid):
     507        if foid not in a:
    509508            ofoid = 'oid_' + self._split_schema(cl)[-1]
    510             if a.has_key(ofoid):
     509            if ofoid in a:
    511510                a[foid] = a[ofoid]
    512511
    513         if a.has_key(foid):
     512        if foid in a:
    514513            where = "oid=%s" % a[foid]
    515514        else:
     
    524523        fnames = self.get_attnames(qcl)
    525524        for ff in fnames.keys():
    526             if ff != 'oid' and a.has_key(ff):
     525            if ff != 'oid' and ff in a:
    527526                v.append('%s=%s' % (ff, _quote(a[ff], fnames[ff])))
    528527        if v == []:
     
    532531        self.db.query(q)
    533532        # Reload the dictionary to catch things modified by engine:
    534         if a.has_key(foid):
     533        if foid in a:
    535534            return self.get(qcl, a, 'oid')
    536535        else:
     
    577576
    578577        # Note that we only accept oid key from named args for safety
    579         if kw.has_key('oid'):
     578        if 'oid' in kw:
    580579            kw[foid] = kw['oid']
    581580            del kw['oid']
     
    589588        # XXX this code is for backwards compatibility and will be
    590589        # XXX removed eventually
    591         if not a.has_key(foid):
     590        if foid not in a:
    592591            ofoid = 'oid_' + self._split_schema(cl)[-1]
    593             if a.has_key(ofoid):
     592            if ofoid in a:
    594593                a[foid] = a[ofoid]
    595594
Note: See TracChangeset for help on using the changeset viewer.