annotate tvii/read.py @ 86:b56d329c238d

workaround namespace foolishess
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Dec 2017 14:04:45 -0800
parents 16b3a11db8d2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
73
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 """
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 deserialization
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 # TODO: use `numerics`'s functionality
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 import csv
86
b56d329c238d workaround namespace foolishess
Jeff Hammel <k0scist@gmail.com>
parents: 73
diff changeset
8 from ._types import string
73
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 def read(fp, type=float, separator=None):
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 """
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 read CSV or space-separated value file `fp`
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 """
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 if isinstance(fp, string):
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 with open(fp) as f:
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 return read(f, type=type, separator=separator)
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 try:
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 # whitespace separated values
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 return [[type(i)
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 for i in line.strip().split(separator)]
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 for line in fp.read().strip().splitlines()]
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 except ValueError:
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 # CSV
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 fp.seek(0)
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 kwargs = {}
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 if separator is not None:
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 kwargs['delimeter'] = separator
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 rows = [row for row in csv.reader(fp, **kwargs)]
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32 return [[type(i) for i in row]
16b3a11db8d2 add stub read functionality
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 for row in rows]