changeset 73:16b3a11db8d2

add stub read functionality
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Dec 2017 13:27:24 -0800
parents 479b7c598a0e
children e21021ca3907
files tvii/read.py
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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]