Changeset: e53718f5c891 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=e53718f5c891
Added Files:
        clients/python2/test/test_monetize.py
        clients/python3/test/test_monetize.py
Modified Files:
        clients/python2/monetdb/sql/monetize.py
        clients/python2/test/capabilities.py
        clients/python2/test/runtests.py
        clients/python3/monetdb/sql/monetize.py
        clients/python3/test/capabilities.py
        clients/python3/test/runtests.py
Branch: default
Log Message:

add type conversion for sub classes


diffs (233 lines):

diff --git a/clients/python2/monetdb/sql/monetize.py 
b/clients/python2/monetdb/sql/monetize.py
--- a/clients/python2/monetdb/sql/monetize.py
+++ b/clients/python2/monetdb/sql/monetize.py
@@ -60,28 +60,36 @@ def monet_bytes(data):
 def monet_unicode(data):
     return monet_escape(data.encode('utf-8'))
 
-mapping = {
-    type(None): monet_none,
-    bool: monet_bool,
-    float: str,
-    complex: str,
-    int: str,
-    str: monet_escape,
-    datetime.datetime: monet_escape,
-    datetime.time: monet_escape,
-    decimal.Decimal: str,
-    datetime.timedelta: monet_escape,
-    datetime.date: monet_escape,
-    bytes: monet_bytes,
-    unicode: monet_unicode,
-}
+mapping = (
+    (unicode, monet_unicode),
+    (str, monet_escape),
+    (bytes, monet_bytes),
+    (int, str),
+    (complex, str),
+    (float, str),
+    (decimal.Decimal, str),
+    (datetime.datetime, monet_escape),
+    (datetime.time, monet_escape),
+    (datetime.date, monet_escape),
+    (datetime.timedelta, monet_escape),
+    (bool, monet_bool),
+    (type(None), monet_none),
+)
+
+mapping_dict = dict(mapping)
 
 
 def convert(data):
     """
-    Calls the appropriate convertion function based upon the python type
+    Return the appropriate convertion function based upon the python type.
     """
-    try:
-        return mapping[type(data)](data)
-    except KeyError:
-        raise ProgrammingError("type %s not supported as value" % type(data))
+    if type(data) in mapping_dict:
+        return mapping_dict[type(data)](data)
+    else:
+        for type_, func in mapping:
+            if issubclass(type(data), type_):
+                return func
+        #if hasattr(data, '__str__'):
+        #    return monet_escape
+    raise ProgrammingError("type %s not supported as value" % type(data))
+
diff --git a/clients/python2/test/capabilities.py 
b/clients/python2/test/capabilities.py
--- a/clients/python2/test/capabilities.py
+++ b/clients/python2/test/capabilities.py
@@ -373,7 +373,7 @@ class DatabaseTest(unittest.TestCase):
     def test_customtype(self):
         t = ["list", "test"]
         self.assertRaises(ProgrammingError, self.db_module.monetize.convert, t)
-        self.db_module.monetize.mapping[list] = str
+        self.db_module.monetize.mapping_dict[list] = str
         self.assertEqual(self.db_module.monetize.convert(t), "['list', 
'test']")
 
     def test_multiple_queries(self):
diff --git a/clients/python2/test/runtests.py b/clients/python2/test/runtests.py
--- a/clients/python2/test/runtests.py
+++ b/clients/python2/test/runtests.py
@@ -37,6 +37,7 @@ except ImportError:
 import capabilities
 import dbapi20
 import test_pythonize
+import test_monetize
 
 warnings.filterwarnings('error')
 
@@ -97,13 +98,9 @@ if __name__ == '__main__':
         Test_Capabilities,
         Test_DBAPI20,
         test_pythonize.TestPythonize,
+        test_monetize.TestMonetize,
     ]
 
     for suite in suites:
         tests = unittest.TestLoader().loadTestsFromTestCase(suite)
         TextTestRunnerNoTime(verbosity=3).run(tests)
-
-
-
-
-
diff --git a/clients/python2/test/test_monetize.py 
b/clients/python2/test/test_monetize.py
new file mode 100644
--- /dev/null
+++ b/clients/python2/test/test_monetize.py
@@ -0,0 +1,18 @@
+import unittest
+from monetdb.sql.monetize import convert, monet_escape
+from monetdb.exceptions import ProgrammingError
+
+
+class TestMonetize(unittest.TestCase):
+    def test_str_subclass(self):
+        class StrSubClass(str):
+            pass
+        x = StrSubClass('test')
+        func = convert(x)
+        self.assertEqual(func, monet_escape)
+
+    def test_unknown_type(self):
+        class Unknown:
+            pass
+        x = Unknown()
+        self.assertRaises(ProgrammingError, convert, x)
diff --git a/clients/python3/monetdb/sql/monetize.py 
b/clients/python3/monetdb/sql/monetize.py
--- a/clients/python3/monetdb/sql/monetize.py
+++ b/clients/python3/monetdb/sql/monetize.py
@@ -57,27 +57,35 @@ def monet_bytes(data):
     return monet_escape(data)
 
 
-mapping = {
-    type(None): monet_none,
-    bool: monet_bool,
-    float: str,
-    complex: str,
-    int: str,
-    str: monet_escape,
-    datetime.datetime: monet_escape,
-    datetime.time: monet_escape,
-    decimal.Decimal: str,
-    datetime.timedelta: monet_escape,
-    datetime.date: monet_escape,
-    bytes: monet_bytes,
-}
+mapping = (
+    (str, monet_escape),
+    (bytes, monet_bytes),
+    (int, str),
+    (complex, str),
+    (float, str),
+    (decimal.Decimal, str),
+    (datetime.datetime, monet_escape),
+    (datetime.time, monet_escape),
+    (datetime.date, monet_escape),
+    (datetime.timedelta, monet_escape),
+    (bool, monet_bool),
+    (type(None), monet_none),
+)
+
+mapping_dict = dict(mapping)
 
 
 def convert(data):
     """
-    Calls the appropriate convertion function based upon the python type
+    Return the appropriate convertion function based upon the python type.
     """
-    try:
-        return mapping[type(data)](data)
-    except KeyError:
-        raise ProgrammingError("type %s not supported as value" % type(data))
+    if type(data) in mapping_dict:
+        return mapping_dict[type(data)](data)
+    else:
+        for type_, func in mapping:
+            if issubclass(type(data), type_):
+                return func
+        #if hasattr(data, '__str__'):
+        #    return monet_escape
+    raise ProgrammingError("type %s not supported as value" % type(data))
+
diff --git a/clients/python3/test/capabilities.py 
b/clients/python3/test/capabilities.py
--- a/clients/python3/test/capabilities.py
+++ b/clients/python3/test/capabilities.py
@@ -373,7 +373,7 @@ class DatabaseTest(unittest.TestCase):
     def test_customtype(self):
         t = ["list", "test"]
         self.assertRaises(ProgrammingError, self.db_module.monetize.convert, t)
-        self.db_module.monetize.mapping[list] = str
+        self.db_module.monetize.mapping_dict[list] = str
         self.assertEqual(self.db_module.monetize.convert(t), "['list', 
'test']")
 
     def test_multiple_queries(self):
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
@@ -37,6 +37,7 @@ except ImportError:
 import capabilities
 import dbapi20
 import test_pythonize
+import test_monetize
 
 warnings.filterwarnings('error')
 
@@ -97,6 +98,7 @@ if __name__ == '__main__':
         Test_Capabilities,
         Test_DBAPI20,
         test_pythonize.TestPythonize,
+        test_monetize.TestMonetize,
     ]
 
     for suite in suites:
diff --git a/clients/python3/test/test_monetize.py 
b/clients/python3/test/test_monetize.py
new file mode 100644
--- /dev/null
+++ b/clients/python3/test/test_monetize.py
@@ -0,0 +1,18 @@
+import unittest
+from monetdb.sql.monetize import convert, monet_escape
+from monetdb.exceptions import ProgrammingError
+
+
+class TestMonetize(unittest.TestCase):
+    def test_str_subclass(self):
+        class StrSubClass(str):
+            pass
+        x = StrSubClass('test')
+        func = convert(x)
+        self.assertEqual(func, monet_escape)
+
+    def test_unknown_type(self):
+        class Unknown:
+            pass
+        x = Unknown()
+        self.assertRaises(ProgrammingError, convert, x)
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to