# HG changeset patch # User Jeff Hammel # Date 1513546044 28800 # Node ID 16b3a11db8d25153510871f34ecfdc693a984ab3 # Parent 479b7c598a0e99a27d9010faa895ab2d564eb059 add stub read functionality diff -r 479b7c598a0e -r 16b3a11db8d2 tvii/read.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tvii/read.py Sun Dec 17 13:27:24 2017 -0800 @@ -0,0 +1,33 @@ +""" +deserialization +""" + +# TODO: use `numerics`'s functionality + +import csv +from .types import string + + +def read(fp, type=float, separator=None): + """ + read CSV or space-separated value file `fp` + """ + + if isinstance(fp, string): + with open(fp) as f: + return read(f, type=type, separator=separator) + + try: + # whitespace separated values + return [[type(i) + for i in line.strip().split(separator)] + for line in fp.read().strip().splitlines()] + except ValueError: + # CSV + fp.seek(0) + kwargs = {} + if separator is not None: + kwargs['delimeter'] = separator + rows = [row for row in csv.reader(fp, **kwargs)] + return [[type(i) for i in row] + for row in rows]