Mercurial > hg > numerics
annotate numerics/convert.py @ 60:e3c3ee7b5ccf
stubbing manipulate data
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Thu, 22 Jan 2015 13:33:56 -0800 |
parents | 3781174542bb |
children | 202ab51601b4 |
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 |
33 | 14 __all__ = ['cast', 'float_or_orig', 'main'] |
15 | |
45
ef915968d104
put this in the parser so that i can use this in convert
Jeff Hammel <k0scist@gmail.com>
parents:
34
diff
changeset
|
16 default_cast = (int, float, str) |
6 | 17 |
5
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
18 def cast(to_type, *values): |
33 | 19 |
5
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
20 retval = [] |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
21 for value in values: |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
22 try: |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
23 retval.append(to_type(value)) |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
24 except ValueError: |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
25 retval.append(value) |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
26 return retval |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
27 |
33 | 28 |
5
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
29 def float_or_orig(*values): |
33 | 30 return cast([float], *values) |
31 | |
32 | |
34 | 33 def column_type(values, types=default_cast): |
34 """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
|
35 for t in types: |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
36 for value in values: |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
37 try: |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
38 t(value) |
56 | 39 except ValueError, TypeError: |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
40 break |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
41 else: |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
42 return t |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
43 |
34 | 44 raise NotImplementedError('TODO') # -> record TODO items |
45 | |
59 | 46 def cast_columns(columns, types=default_cast): |
33 | 47 """ |
48 cast a column of data | |
49 """ | |
59 | 50 column_types = [column_type(column) for column in columns] |
60 | 51 return [[column_type(row) for row in column] |
52 for column_type, column in zip(column_types, columns)] | |
27 | 53 |
59 | 54 |
27 | 55 def main(args=sys.argv[1:]): |
56 """CLI""" | |
57 | |
31 | 58 # parse command line |
47
6d34c02f7c9c
inherit from the right thing
Jeff Hammel <k0scist@gmail.com>
parents:
46
diff
changeset
|
59 parser = CSVParser(description="interpolate types from file") |
31 | 60 options = parser.parse_args(args) |
61 | |
32 | 62 # read CSV file |
46 | 63 data = parser.read() |
64 | |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
65 # transpose |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
66 columns = transpose(data) |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
67 |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
68 # get types |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
69 types = [column_type(column) for column in columns] |
46 | 70 |
71 # print type information | |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
72 writer = csv.writer(sys.stdout) |
56 | 73 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
|
74 |
32 | 75 |
27 | 76 if __name__ == '__main__': |
77 main() |