Mercurial > hg > numerics
annotate numerics/convert.py @ 58:537fba7c9def
stubbing
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 20 Jan 2015 16:44:56 -0800 |
parents | 06e487f90e05 |
children | 3781174542bb |
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 | |
58 | 46 def cast_columns(values, to=default_cast): |
33 | 47 """ |
48 cast a column of data | |
49 """ | |
34 | 50 raise NotImplementedError('TODO') # -> record TODO items |
27 | 51 |
52 def main(args=sys.argv[1:]): | |
53 """CLI""" | |
54 | |
31 | 55 # parse command line |
47
6d34c02f7c9c
inherit from the right thing
Jeff Hammel <k0scist@gmail.com>
parents:
46
diff
changeset
|
56 parser = CSVParser(description="interpolate types from file") |
31 | 57 options = parser.parse_args(args) |
58 | |
32 | 59 # read CSV file |
46 | 60 data = parser.read() |
61 | |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
62 # transpose |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
63 columns = transpose(data) |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
64 |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
65 # get types |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
66 types = [column_type(column) for column in columns] |
46 | 67 |
68 # print type information | |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
69 writer = csv.writer(sys.stdout) |
56 | 70 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
|
71 |
32 | 72 |
27 | 73 if __name__ == '__main__': |
74 main() |