[SQL-CVS] r2968 - in SQLObject/trunk/sqlobject: . tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2007-09-20 15:28:51
|
Author: phd Date: 2007-09-20 09:28:44 -0600 (Thu, 20 Sep 2007) New Revision: 2968 Modified: SQLObject/trunk/sqlobject/col.py SQLObject/trunk/sqlobject/constraints.py SQLObject/trunk/sqlobject/converters.py SQLObject/trunk/sqlobject/main.py SQLObject/trunk/sqlobject/sqlbuilder.py SQLObject/trunk/sqlobject/sresults.py SQLObject/trunk/sqlobject/tests/test_converters.py Log: Use isinstance() instead of type(). Modified: SQLObject/trunk/sqlobject/col.py =================================================================== --- SQLObject/trunk/sqlobject/col.py 2007-09-20 14:40:33 UTC (rev 2967) +++ SQLObject/trunk/sqlobject/col.py 2007-09-20 15:28:44 UTC (rev 2968) @@ -19,6 +19,7 @@ """ import re, time +from array import array try: import cPickle as pickle except ImportError: @@ -30,7 +31,6 @@ from formencode import compound from formencode import validators from classregistry import findClass -from converters import array_type, buffer_type from util.backports import count NoDefault = sqlbuilder.NoDefault @@ -150,7 +150,7 @@ "The only string value allowed for cascade is 'null' (you gave: %r)" % cascade) self.cascade = cascade - if type(constraints) not in (type([]), type(())): + if not isinstance(constraints, (list, tuple)): constraints = [constraints] self.constraints = self.autoConstraints() + constraints @@ -537,7 +537,7 @@ return None if isinstance(value, unicode): return value - if isinstance(value, array_type): # MySQL + if isinstance(value, array): # MySQL return unicode(value.tostring(), self.db_encoding) return unicode(value, self.db_encoding) @@ -651,17 +651,17 @@ if value is None: return None elif not value: - return sqlbuilder.FALSE + return False else: - return sqlbuilder.TRUE + return True def from_python(self, value, state): if value is None: return None elif value: - return sqlbuilder.TRUE + return True else: - return sqlbuilder.FALSE + return False class SOBoolCol(SOCol): def autoConstraints(self): @@ -1361,11 +1361,11 @@ if connection.dbName == "sqlite": value = connection.module.decode(value) return value - if isinstance(value, (buffer_type, state.soObject._connection._binaryType)): + if isinstance(value, (buffer, state.soObject._connection._binaryType)): cachedValue = self._cachedValue if cachedValue and cachedValue[1] == value: return cachedValue[0] - if isinstance(value, array_type): # MySQL + if isinstance(value, array): # MySQL return value.tostring() return str(value) # buffer => string raise validators.Invalid("expected a string in the BLOBCol '%s', got %s %r instead" % \ Modified: SQLObject/trunk/sqlobject/constraints.py =================================================================== --- SQLObject/trunk/sqlobject/constraints.py 2007-09-20 14:40:33 UTC (rev 2967) +++ SQLObject/trunk/sqlobject/constraints.py 2007-09-20 15:28:44 UTC (rev 2968) @@ -27,15 +27,15 @@ raise BadValue("is defined NOT NULL", obj, col, value) def isInt(obj, col, value): - if type(value) not in (type(1), type(1L)): + if not isinstance(value, (int, long)): raise BadValue("only allows integers", obj, col, value) def isFloat(obj, col, value): - if type(value) not in (type(1), type(1L), type(1.1)): + if not isinstance(value, (int, long, float)): raise BadValue("only allows floating point numbers", obj, col, value) def isBool(obj, col, value): - if type(value) not in (type(True),): + if not isinstance(value, bool): raise BadValue("only allows booleans", obj, col, value) class InList: Modified: SQLObject/trunk/sqlobject/converters.py =================================================================== --- SQLObject/trunk/sqlobject/converters.py 2007-09-20 14:40:33 UTC (rev 2967) +++ SQLObject/trunk/sqlobject/converters.py 2007-09-20 15:28:44 UTC (rev 2968) @@ -1,4 +1,4 @@ -import array +from array import array try: import mx.DateTime.ISO @@ -23,25 +23,8 @@ except ImportError: NumericType = None -if type(1==1) == type(1): - class BOOL(object): - def __init__(self, value): - self.value = not not value - def __nonzero__(self): - return self.value - def __repr__(self): - if self: - return 'TRUE' - else: - return 'FALSE' - TRUE = BOOL(1) - FALSE = BOOL(0) -else: - TRUE = 1==1 - FALSE = 0==1 +from types import ClassType, InstanceType, NoneType -from types import InstanceType, ClassType, TypeType - try: from decimal import Decimal except ImportError: @@ -85,7 +68,7 @@ self.basic[typ] = func def lookupConverter(self, value, default=None): - if type(value) == InstanceType: + if type(value) is InstanceType: # lookup on klasses dict return self.klass.get(value.__class__, default) return self.basic.get(type(value), default) @@ -94,16 +77,13 @@ registerConverter = converters.registerConverter lookupConverter = converters.lookupConverter -array_type = type(array.array('c', '')) # In Python 2.2 array.array and buffer -buffer_type = type(buffer('')) # are functions, not classes - def StringLikeConverter(value, db): - if isinstance(value, array_type): + if isinstance(value, array): try: value = value.tounicode() except ValueError: value = value.tostring() - elif isinstance(value, buffer_type): + elif isinstance(value, buffer): value = str(value) if db in ('mysql', 'postgres'): @@ -117,18 +97,18 @@ registerConverter(str, StringLikeConverter) registerConverter(unicode, StringLikeConverter) -registerConverter(array_type, StringLikeConverter) -registerConverter(buffer_type, StringLikeConverter) +registerConverter(array, StringLikeConverter) +registerConverter(buffer, StringLikeConverter) def IntConverter(value, db): return repr(int(value)) -registerConverter(type(1), IntConverter) +registerConverter(int, IntConverter) def LongConverter(value, db): return str(value) -registerConverter(type(0L), LongConverter) +registerConverter(long, LongConverter) if NumericType: registerConverter(NumericType, IntConverter) @@ -145,16 +125,12 @@ else: return '0' -if type(TRUE) == InstanceType: - # Python 2.2 compatibility: - registerConverter(BOOL, BoolConverter) -else: - registerConverter(type(TRUE), BoolConverter) +registerConverter(bool, BoolConverter) def FloatConverter(value, db): return repr(value) -registerConverter(type(1.0), FloatConverter) +registerConverter(float, FloatConverter) if DateTimeType: def DateTimeConverter(value, db): @@ -170,14 +146,14 @@ def NoneConverter(value, db): return "NULL" -registerConverter(type(None), NoneConverter) +registerConverter(NoneType, NoneConverter) def SequenceConverter(value, db): return "(%s)" % ", ".join([sqlrepr(v, db) for v in value]) -registerConverter(type(()), SequenceConverter) -registerConverter(type([]), SequenceConverter) -registerConverter(type({}), SequenceConverter) +registerConverter(tuple, SequenceConverter) +registerConverter(list, SequenceConverter) +registerConverter(dict, SequenceConverter) try: set, frozenset except NameError: Modified: SQLObject/trunk/sqlobject/main.py =================================================================== --- SQLObject/trunk/sqlobject/main.py 2007-09-20 14:40:33 UTC (rev 2967) +++ SQLObject/trunk/sqlobject/main.py 2007-09-20 15:28:44 UTC (rev 2968) @@ -841,8 +841,7 @@ if (not new_attrs.has_key('sqlmeta') and not is_base): # We have to create our own subclass, usually. - # type(className, bases_tuple, attr_dict) creates a new - # subclass: + # type(className, bases_tuple, attr_dict) creates a new subclass. cls.sqlmeta = type('sqlmeta', (cls.sqlmeta,), {}) if not issubclass(cls.sqlmeta, sqlmeta): # We allow no superclass and an object superclass, instead @@ -1659,9 +1658,9 @@ def getID(obj): if isinstance(obj, SQLObject): return obj.id - elif type(obj) is type(1): + elif isinstance(obj, int): return obj - elif type(obj) is type(1L): + elif isinstance(obj, long): return int(obj) elif isinstance(obj, str): try: @@ -1672,9 +1671,9 @@ return None def getObject(obj, klass): - if type(obj) is type(1): + if isinstance(obj, int): return klass(obj) - elif type(obj) is type(1L): + elif isinstance(obj, long): return klass(int(obj)) elif isinstance(obj, str): return klass(int(obj)) Modified: SQLObject/trunk/sqlobject/sqlbuilder.py =================================================================== --- SQLObject/trunk/sqlobject/sqlbuilder.py 2007-09-20 14:40:33 UTC (rev 2967) +++ SQLObject/trunk/sqlobject/sqlbuilder.py 2007-09-20 15:28:44 UTC (rev 2968) @@ -75,7 +75,7 @@ import types import classregistry -from converters import sqlrepr, registerConverter, TRUE, FALSE +from converters import sqlrepr, registerConverter safeSQLRE = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_\.]*$') def sqlIdentifier(obj): @@ -528,7 +528,7 @@ start=0, end=None, reversed=False, forUpdate=False, clause=NoDefault, staticTables=NoDefault, distinctOn=NoDefault): self.ops = {} - if not isinstance(items, (type([]), type(()), types.GeneratorType)): + if not isinstance(items, (list, tuple, types.GeneratorType)): items = [items] if clause is NoDefault and where is not NoDefault: clause = where @@ -694,7 +694,7 @@ insert = "INSERT INTO %s" % self.table allowNonDict = True template = self.template - if template is NoDefault and type(self.valueList[0]) is type({}): + if (template is NoDefault) and isinstance(self.valueList[0], dict): template = self.valueList[0].keys() allowNonDict = False if template is not NoDefault: @@ -703,7 +703,7 @@ listToJoin = [] listToJoin_app = listToJoin.append for value in self.valueList: - if type(value) is type({}): + if isinstance(value, dict): if template is NoDefault: raise TypeError, "You can't mix non-dictionaries with dictionaries in an INSERT if you don't provide a template (%s)" % repr(value) value = dictToList(template, value) Modified: SQLObject/trunk/sqlobject/sresults.py =================================================================== --- SQLObject/trunk/sqlobject/sresults.py 2007-09-20 14:40:33 UTC (rev 2967) +++ SQLObject/trunk/sqlobject/sresults.py 2007-09-20 15:28:44 UTC (rev 2968) @@ -128,7 +128,7 @@ return self.newClause(sqlbuilder.AND(clause, filter_clause)) def __getitem__(self, value): - if type(value) is type(slice(1)): + if isinstance(value, slice): assert not value.step, "Slices do not support steps" if not value.start and not value.stop: # No need to copy, I'm immutable Modified: SQLObject/trunk/sqlobject/tests/test_converters.py =================================================================== --- SQLObject/trunk/sqlobject/tests/test_converters.py 2007-09-20 14:40:33 UTC (rev 2967) +++ SQLObject/trunk/sqlobject/tests/test_converters.py 2007-09-20 15:28:44 UTC (rev 2968) @@ -1,4 +1,4 @@ -from sqlobject.sqlbuilder import sqlrepr, TRUE, FALSE +from sqlobject.sqlbuilder import sqlrepr from sqlobject.sqlbuilder import SQLExpression, SQLObjectField, \ Select, Insert, Update, Delete, Replace, \ SQLTrueClauseClass, SQLConstant, SQLPrefix, SQLCall, SQLOp @@ -77,10 +77,10 @@ assert sqlrepr(('one','two','three'), 'postgres') == "('one', 'two', 'three')" def test_bool(): - assert sqlrepr(TRUE, 'postgres') == "'t'" - assert sqlrepr(FALSE, 'postgres') == "'f'" - assert sqlrepr(TRUE, 'mysql') == "1" - assert sqlrepr(FALSE, 'mysql') == "0" + assert sqlrepr(True, 'postgres') == "'t'" + assert sqlrepr(False, 'postgres') == "'f'" + assert sqlrepr(True, 'mysql') == "1" + assert sqlrepr(False, 'mysql') == "0" def test_datetime(): from datetime import datetime, date, time |