mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-28 12:54:15 +02:00
Suggest column types ignores nulls, closes #94
This commit is contained in:
parent
755580e2f3
commit
1c745df923
2 changed files with 8 additions and 1 deletions
|
|
@ -13,7 +13,8 @@ def suggest_column_types(records):
|
||||||
all_column_types = {}
|
all_column_types = {}
|
||||||
for record in records:
|
for record in records:
|
||||||
for key, value in record.items():
|
for key, value in record.items():
|
||||||
all_column_types.setdefault(key, set()).add(type(value))
|
if value is not None:
|
||||||
|
all_column_types.setdefault(key, set()).add(type(value))
|
||||||
column_types = {}
|
column_types = {}
|
||||||
for key, types in all_column_types.items():
|
for key, types in all_column_types.items():
|
||||||
if len(types) == 1:
|
if len(types) == 1:
|
||||||
|
|
|
||||||
|
|
@ -7,14 +7,20 @@ from sqlite_utils.utils import suggest_column_types
|
||||||
"records,types",
|
"records,types",
|
||||||
[
|
[
|
||||||
([{"a": 1}], {"a": int}),
|
([{"a": 1}], {"a": int}),
|
||||||
|
([{"a": 1}, {"a": None}], {"a": int}),
|
||||||
([{"a": "baz"}], {"a": str}),
|
([{"a": "baz"}], {"a": str}),
|
||||||
|
([{"a": "baz"}, {"a": None}], {"a": str}),
|
||||||
([{"a": 1.2}], {"a": float}),
|
([{"a": 1.2}], {"a": float}),
|
||||||
|
([{"a": 1.2}, {"a": None}], {"a": float}),
|
||||||
([{"a": [1]}], {"a": str}),
|
([{"a": [1]}], {"a": str}),
|
||||||
|
([{"a": [1]}, {"a": None}], {"a": str}),
|
||||||
([{"a": (1,)}], {"a": str}),
|
([{"a": (1,)}], {"a": str}),
|
||||||
([{"a": {"b": 1}}], {"a": str}),
|
([{"a": {"b": 1}}], {"a": str}),
|
||||||
|
([{"a": {"b": 1}}, {"a": None}], {"a": str}),
|
||||||
([{"a": OrderedDict({"b": 1})}], {"a": str}),
|
([{"a": OrderedDict({"b": 1})}], {"a": str}),
|
||||||
([{"a": 1}, {"a": 1.1}], {"a": float}),
|
([{"a": 1}, {"a": 1.1}], {"a": float}),
|
||||||
([{"a": b"b"}], {"a": bytes}),
|
([{"a": b"b"}], {"a": bytes}),
|
||||||
|
([{"a": b"b"}, {"a": None}], {"a": bytes}),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_suggest_column_types(records, types):
|
def test_suggest_column_types(records, types):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue