Mercurial > hg > numerics
annotate numerics/convert.py @ 85:411420ddaa46
wip
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 01 Mar 2015 16:59:08 -0800 |
parents | 202ab51601b4 |
children | 19a5c2fb52bb |
rev | line source |
---|---|
27 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
4 convert between types | |
5 """ | |
6 | |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
7 # imports |
31 | 8 import argparse |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
9 import csv |
27 | 10 import sys |
52 | 11 from .data import transpose |
47
6d34c02f7c9c
inherit from the right thing
Jeff Hammel <k0scist@gmail.com>
parents:
46
diff
changeset
|
12 from .read import read_csv, CSVParser |
27 | 13 |
81 | 14 __all__ = ['cast', |
15 'float_or_orig', | |
16 'main'] | |
33 | 17 |
45
ef915968d104
put this in the parser so that i can use this in convert
Jeff Hammel <k0scist@gmail.com>
parents:
34
diff
changeset
|
18 default_cast = (int, float, str) |
6 | 19 |
5
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
20 def cast(to_type, *values): |
81 | 21 """ |
22 gently cast a thing to a thing; | |
23 if you can't, return the original value | |
24 """ | |
25 | |
33 | 26 |
5
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
27 retval = [] |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
28 for value in values: |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
29 try: |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
30 retval.append(to_type(value)) |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
31 except ValueError: |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
32 retval.append(value) |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
33 return retval |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
34 |
81 | 35 def cast_or_discard(to_type, *values): |
36 """ | |
37 cast to `to_type` if possible; | |
38 otherwise just throw away | |
39 """ | |
40 raise NotImplementedError('TODO') # -> record TODO items | |
33 | 41 |
5
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
42 def float_or_orig(*values): |
81 | 43 return cast(float, *values) |
33 | 44 |
45 | |
34 | 46 def column_type(values, types=default_cast): |
47 """determine the type of a column""" | |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
48 for t in types: |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
49 for value in values: |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
50 try: |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
51 t(value) |
56 | 52 except ValueError, TypeError: |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
53 break |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
54 else: |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
55 return t |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
56 |
34 | 57 raise NotImplementedError('TODO') # -> record TODO items |
58 | |
59 | 59 def cast_columns(columns, types=default_cast): |
33 | 60 """ |
61 cast a column of data | |
62 """ | |
59 | 63 column_types = [column_type(column) for column in columns] |
60 | 64 return [[column_type(row) for row in column] |
65 for column_type, column in zip(column_types, columns)] | |
27 | 66 |
59 | 67 |
27 | 68 def main(args=sys.argv[1:]): |
69 """CLI""" | |
70 | |
31 | 71 # parse command line |
47
6d34c02f7c9c
inherit from the right thing
Jeff Hammel <k0scist@gmail.com>
parents:
46
diff
changeset
|
72 parser = CSVParser(description="interpolate types from file") |
31 | 73 options = parser.parse_args(args) |
74 | |
32 | 75 # read CSV file |
46 | 76 data = parser.read() |
77 | |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
78 # transpose |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
79 columns = transpose(data) |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
80 |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
81 # get types |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
82 types = [column_type(column) for column in columns] |
46 | 83 |
84 # print type information | |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
85 writer = csv.writer(sys.stdout) |
56 | 86 writer.writerow([t.__name__ for t in types]) |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
87 |
32 | 88 |
27 | 89 if __name__ == '__main__': |
90 main() |