comparison numerics/convert.py @ 111:c4d26ef63d8e

fix error in convert and i think this works sorta well enouigh for now
author Jeff Hammel <k0scist@gmail.com>
date Sun, 15 Mar 2015 15:38:12 -0700
parents fae24f57dcb1
children 7578313b9fbf
comparison
equal deleted inserted replaced
110:5790bcb30bd8 111:c4d26ef63d8e
36 def cast_or_discard(to_type, *values): 36 def cast_or_discard(to_type, *values):
37 """ 37 """
38 cast to `to_type` if possible; 38 cast to `to_type` if possible;
39 otherwise just throw away 39 otherwise just throw away
40 """ 40 """
41 raise NotImplementedError('TODO') # -> record TODO items 41 retval = []
42 for value in values:
43 try:
44 retval.append(to_type(value))
45 except ValueError:
46 continue
47 return retval
48
42 49
43 def float_or_orig(*values): 50 def float_or_orig(*values):
44 return cast(float, *values) 51 return cast(float, *values)
45 52 # convenience function ; do we need this?
46 53
47 def column_type(values, types=default_cast): 54 def column_type(values, types=default_cast):
48 """determine the type of a column""" 55 """determine the type of a column"""
49 for t in types: 56 for t in types:
50 for value in values: 57 for value in values:
53 except ValueError, TypeError: 60 except ValueError, TypeError:
54 break 61 break
55 else: 62 else:
56 return t 63 return t
57 64
58 raise NotImplementedError('TODO') # -> record TODO items
59 65
60 def cast_columns(columns, types=default_cast): 66 def cast_columns(columns, types=default_cast):
61 """ 67 """
62 cast a column of data 68 cast a column of data
63 """ 69 """
64 column_types = [column_type(column, types=types) 70 column_types = [column_type(column, types=types)
65 for column in columns] 71 for column in columns]
66 return [[column_type(row) for row in column] 72 return [[_type(row) for row in column]
67 for column_type, column in zip(column_types, columns)] 73 for _type, column in zip(column_types, columns)]
68 74
69 75
70 def main(args=sys.argv[1:]): 76 def main(args=sys.argv[1:]):
71 """CLI""" 77 """CLI"""
72 78