Extracted detect_column_types as suggest_column_types, refs #81

This commit is contained in:
Simon Willison 2020-02-01 13:38:26 -08:00
commit 5ecf3ffdea
3 changed files with 47 additions and 27 deletions

View file

@ -7,3 +7,28 @@ except ImportError:
import sqlite3
OperationalError = sqlite3.OperationalError
def suggest_column_types(records):
all_column_types = {}
for record in records:
for key, value in record.items():
all_column_types.setdefault(key, set()).add(type(value))
column_types = {}
for key, types in all_column_types.items():
if len(types) == 1:
t = list(types)[0]
# But if it's list / tuple / dict, use str instead as we
# will be storing it as JSON in the table
if t in (list, tuple, dict):
t = str
elif {int, bool}.issuperset(types):
t = int
elif {int, float, bool}.issuperset(types):
t = float
elif {bytes, str}.issuperset(types):
t = bytes
else:
t = str
column_types[key] = t
return column_types