From f410b112af71ca0ba569332fb38e3e9082d724ff Mon Sep 17 00:00:00 2001 From: Salvo 'LtWorf' Tomaselli Date: Wed, 19 Aug 2020 08:11:05 +0200 Subject: [PATCH] Use the new by column type detection --- relational/relation.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/relational/relation.py b/relational/relation.py index 0205f72..ee24f50 100644 --- a/relational/relation.py +++ b/relational/relation.py @@ -58,7 +58,7 @@ class Relation(NamedTuple): method. ''' header: 'Header' - content: FrozenSet[Tuple[Rstring, ...]] + content: FrozenSet[Tuple[CastValue, ...]] @staticmethod def load(filename: Union[str, Path]) -> 'Relation': @@ -78,13 +78,24 @@ class Relation(NamedTuple): Iterator for the header, and iterator for the content. ''' header = Header(header) - r_content: List[Tuple[Rstring, ...]] = [] + r_content: List[Tuple[CastValue, ...]] = [] + guessed_types = list(repeat({Rdate, float, int, str}, len(header))) + for row in content: - content_row: Tuple[Rstring, ...] = tuple(Rstring(i) for i in row) - if len(content_row) != len(header): + if len(row) != len(header): raise ValueError(f'Line {row} contains an incorrect amount of values') - r_content.append(content_row) - return Relation(header, frozenset(r_content)) + r_content.append(row) + + # Guess types + for i, value in enumerate(row): + guessed_types[i] = guessed_types[i].intersection(guess_type(value)) + + typed_content = [] + for r in r_content: + t = tuple(cast(v, guessed_types[i]) for i, v in enumerate(r)) + typed_content.append(t) + + return Relation(header, frozenset(typed_content)) def __iter__(self):