Use isinstance() instead of type() is ~

This commit is contained in:
Benjamin Renard 2019-03-11 14:35:49 +01:00 committed by root
parent 2ed8dea4e2
commit 15cb807c2f

18
PgDB.py
View file

@ -75,25 +75,25 @@ class PgDB(object):
# SQL helpers # SQL helpers
# #
def _quote_value(self, value): def _quote_value(self, value):
if type(value) is int or type(value) is float: if isinstance(value, int) or isinstance(value, float):
return unicode(value) return unicode(value)
if type(value) is str: if isinstance(value, str):
value = unicode(value) value = unicode(value)
elif type(value) is datetime.datetime: elif isinstance(value, datetime.datetime):
value = unicode(self._format_datetime(value)) value = unicode(self._format_datetime(value))
elif type(value) is datetime.date: elif isinstance(value, datetime.date):
value = unicode(self._format_date(value)) value = unicode(self._format_date(value))
return u"'%s'" % value.replace(u"'",u"''") return u"'%s'" % value.replace(u"'",u"''")
def _format_where_clauses(self, where_clauses, where_op=u'AND'): def _format_where_clauses(self, where_clauses, where_op=u'AND'):
if type(where_clauses) is str: if isinstance(where_clauses, str):
return where_clauses return where_clauses
elif type(where_clauses) is list: elif isinstance(where_clauses, list):
return (u" %s " % where_op).join(where_clauses) return (u" %s " % where_op).join(where_clauses)
elif type(where_clauses) is dict: elif isinstance(where_clauses, dict):
return (u" %s " % where_op).join(map(lambda x: "%s=%s" % (x, _quote_value(where_clauses[x])), where_clauses)) return (u" %s " % where_op).join(map(lambda x: "%s=%s" % (x, self._quote_value(where_clauses[x])), where_clauses))
logging.error('Unsupported where clauses type %s', type(where_clauses)) logging.error('Unsupported where clauses type %s', type(where_clauses))
return False return False
@ -160,7 +160,7 @@ class PgDB(object):
sql = u"SELECT " sql = u"SELECT "
if fields is None: if fields is None:
sql += "*" sql += "*"
elif type(fields) in (str, unicode): elif isinstance(fields, str) or isinstance(fields, unicode):
sql += fields sql += fields
else: else:
sql += u", ".join(fields) sql += u", ".join(fields)