Use the new by column type detection

master
Salvo 'LtWorf' Tomaselli 2020-08-19 08:11:05 +07:00
parent 5259921bb1
commit f410b112af
No known key found for this signature in database
GPG Key ID: B3A7CF0C801886CF
1 changed files with 17 additions and 6 deletions

@ -58,7 +58,7 @@ class Relation(NamedTuple):
method. method.
''' '''
header: 'Header' header: 'Header'
content: FrozenSet[Tuple[Rstring, ...]] content: FrozenSet[Tuple[CastValue, ...]]
@staticmethod @staticmethod
def load(filename: Union[str, Path]) -> 'Relation': def load(filename: Union[str, Path]) -> 'Relation':
@ -78,13 +78,24 @@ class Relation(NamedTuple):
Iterator for the header, and iterator for the content. Iterator for the header, and iterator for the content.
''' '''
header = Header(header) 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: for row in content:
content_row: Tuple[Rstring, ...] = tuple(Rstring(i) for i in row) if len(row) != len(header):
if len(content_row) != len(header):
raise ValueError(f'Line {row} contains an incorrect amount of values') raise ValueError(f'Line {row} contains an incorrect amount of values')
r_content.append(content_row) r_content.append(row)
return Relation(header, frozenset(r_content))
# 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): def __iter__(self):