Title: [972] trunk/pg.py: Speed-up guessing simple types
Revision
972
Author
cito
Date
2019-04-19 15:37:34 -0400 (Fri, 19 Apr 2019)

Log Message

Speed-up guessing simple types

Modified Paths


Diff

Modified: trunk/pg.py (971 => 972)


--- trunk/pg.py	2019-04-19 18:27:41 UTC (rev 971)
+++ trunk/pg.py	2019-04-19 19:37:34 UTC (rev 972)
@@ -582,9 +582,28 @@
             return typ.attnames
         return {}
 
+    _simple_types = {
+        Bytea: 'bytea',
+        str: 'text',
+        bytes: 'text',
+        bool: 'bool',
+        int: 'int',
+        long: 'int',
+        float: 'float',
+        Decimal: 'num',
+        date: 'date',
+        time: 'date',
+        datetime: 'date',
+        timedelta: 'date'
+    }
+
     @classmethod
     def guess_simple_type(cls, value):
         """Try to guess which database type the given value has."""
+        # optimize for most frequent types
+        simple_type = cls._simple_types.get(type(value))
+        if simple_type:
+            return simple_type
         if isinstance(value, Bytea):
             return 'bytea'
         if isinstance(value, basestring):
@@ -603,11 +622,13 @@
             return '%s[]' % (cls.guess_simple_base_type(value) or 'text',)
         if isinstance(value, tuple):
             simple_type = cls.simple_type
-            typ = simple_type('record')
             guess = cls.guess_simple_type
+
             def get_attnames(self):
                 return AttrDict((str(n + 1), simple_type(guess(v)))
                     for n, v in enumerate(value))
+
+            typ = simple_type('record')
             typ._get_attnames = get_attnames
             return typ
 
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo/pygresql

Reply via email to