Changeset: 03c2163daa08 for MonetDB URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=03c2163daa08 Added Files: clients/python3/test/test_pythonize.py Modified Files: clients/python3/monetdb/sql/pythonize.py clients/python3/test/runtests.py Branch: Oct2012 Log Message:
sync python3 with python2 diffs (202 lines): diff --git a/clients/python3/monetdb/sql/pythonize.py b/clients/python3/monetdb/sql/pythonize.py --- a/clients/python3/monetdb/sql/pythonize.py +++ b/clients/python3/monetdb/sql/pythonize.py @@ -19,39 +19,43 @@ functions for converting monetdb SQL fields to Python objects """ -import logging import time import datetime from decimal import Decimal from monetdb.sql import types -import monetdb.exceptions +from monetdb.exceptions import ProgrammingError import re -logger = logging.getLogger("monetdb") - def strip(data): - """ returns a python string, chops off quotes, - replaces escape characters""" + """ returns a python string, with chopped off quotes, + and replaced escape characters""" return ''.join([w.encode('utf-8').decode('unicode_escape') if '\\' in w else w for w in re.split('([\000-\200]+)', data[1:-1])]) - def py_bool(data): """ return python boolean """ return (data == "true") def py_time(data): + """ returns a python Time + """ return Time(*[int(float(x)) for x in data.split(':')]) def py_date(data): + """ Returns a python Date + """ return Date(*[int(float(x)) for x in data.split('-')]) def py_timestamp(data): + """ Returns a python Timestamp + """ splitted = data.split(" ") date = [int(float(x)) for x in splitted[0].split('-')] time = [int(float(x)) for x in splitted[1].split(':')] return Timestamp(*date+time) def py_timestamptz(data): + """ Returns a python Timestamp where data contains a tz code + """ if data.find('+')!= -1: (dt, tz) = data.split("+") (tzhour, tzmin) = [int(x) for x in tz.split(':')] @@ -70,15 +74,6 @@ def py_timestamptz(data): hour, minute, second = time return Timestamp(year, month, day, hour+tzhour, minute+tzmin, second) -def py_blob(x): - """ Converts a monetdb blob in string representation to a python string. - The input is a string in the format: '(length: char char char char ... )' - w/ char in hex representation. The output is the string of chars. """ - #TODO: need to check if blob datatype should use this? otherwise - # this can be removed - x = x[x.find(":")+2:-1] - return ''.join(map(lambda x: chr(int(x, 16)), x.split(" "))) - mapping = { types.CHAR: strip, types.VARCHAR: strip, @@ -108,6 +103,10 @@ mapping = { } def convert(data, type_code): + """ + Calls the appropriate convertion function based upon the python type + """ + # null values should always be replaced by None type if data == "NULL": return None @@ -119,46 +118,29 @@ def convert(data, type_code): # below us stuff required by the DBAPI +def Binary(data): + """returns binary encoding of data""" + return ''.join(["%02X" % ord(i) for i in data]) + +def DateFromTicks(ticks): + """Convert ticks to python Date""" + return Date(*time.localtime(ticks)[:3]) + +def TimeFromTicks(ticks): + """Convert ticks to python Time""" + return Time(*time.localtime(ticks)[3:6]) + +def TimestampFromTicks(ticks): + """Convert ticks to python Timestamp""" + return Timestamp(*time.localtime(ticks)[:6]) + Date = datetime.date Time = datetime.time Timestamp = datetime.datetime - -def Binary(x): - #return x.encode("hex").upper() - return ''.join([hex(ord(i))[2:] for i in x]).upper() - -def DateFromTicks(ticks): - return Date(*time.localtime(ticks)[:3]) - -def TimeFromTicks(ticks): - return Time(*time.localtime(ticks)[3:6]) - -def TimestampFromTicks(ticks): - return Timestamp(*time.localtime(ticks)[:6]) - -class DBAPISet(frozenset): - """A special type of set for which A == x is true if A is a - DBAPISet and x is a member of that set.""" - - def __ne__(self, other): - from sets import BaseSet - if isinstance(other, BaseSet): - return super(DBAPISet.self).__ne__(self, other) - else: - return other not in self - - def __eq__(self, other): - if isinstance(other, set): - return super(DBAPISet, self).__eq__(self, other) - else: - return other in self - -STRING = DBAPISet([types.VARCHAR]) -BINARY = DBAPISet([types.BLOB]) -NUMBER = DBAPISet([types.DECIMAL, types.DOUBLE, types.REAL, - types.BIGINT, types.SMALLINT]) -DATE = DBAPISet([types.DATE]) -TIME = DBAPISet([types.TIME]) -TIMESTAMP = DBAPISet([types.TIMESTAMP]) -DATETIME = TIMESTAMP -ROWID = DBAPISet() +STRING = types.VARCHAR +BINARY = types.BLOB +NUMBER = types.DECIMAL +DATE = types.DATE +TIME = types.TIME +DATETIME = types.TIMESTAMP +ROWID = types.INT diff --git a/clients/python3/test/runtests.py b/clients/python3/test/runtests.py --- a/clients/python3/test/runtests.py +++ b/clients/python3/test/runtests.py @@ -25,6 +25,7 @@ import logging import capabilities import dbapi20 +import test_pythonize warnings.filterwarnings('error') @@ -102,10 +103,15 @@ class Test_DBAPI20(dbapi20.DatabaseAPI20 self.assertTrue(issubclass(self.driver.NotSupportedError, self.driver.Error)) if __name__ == '__main__': - suite1 = unittest.TestLoader().loadTestsFromTestCase(Test_Capabilities) - TextTestRunnerNoTime(verbosity=3).run(suite1) - suite2 = unittest.TestLoader().loadTestsFromTestCase(Test_DBAPI20) - TextTestRunnerNoTime(verbosity=3).run(suite2) + suites = [ + Test_Capabilities, + Test_DBAPI20, + test_pythonize.TestPythonize, + ] + for suite in suites: + tests = unittest.TestLoader().loadTestsFromTestCase(suite) + TextTestRunnerNoTime(verbosity=3).run(tests) + diff --git a/clients/python3/test/test_pythonize.py b/clients/python3/test/test_pythonize.py new file mode 100644 --- /dev/null +++ b/clients/python3/test/test_pythonize.py @@ -0,0 +1,15 @@ +import unittest +import monetdb.sql.pythonize + +class TestPythonize(unittest.TestCase): + def test_Binary(self): + input1 = ''.join([chr(i) for i in range(256)]) + output1 = ''.join(["%02X" % i for i in range(256)]) + result1 = monetdb.sql.pythonize.Binary(input1) + self.assertEqual(output1, result1) + + input2 = '\tdharma' + output2 = '09646861726D61' + result2 = monetdb.sql.pythonize.Binary(input2) + self.assertEqual(output2, result2) + _______________________________________________ checkin-list mailing list checkin-list@monetdb.org http://mail.monetdb.org/mailman/listinfo/checkin-list