Mercurial > hg > numerics
annotate numerics/convert.py @ 150:8a1fe454c98a
STUB
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 13 Apr 2015 10:17:47 -0700 |
parents | 7578313b9fbf |
children | c16940bd2cee |
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 |
107 | 14 __all__ = ['default_cast', |
15 'cast', | |
81 | 16 'float_or_orig', |
17 'main'] | |
33 | 18 |
45
ef915968d104
put this in the parser so that i can use this in convert
Jeff Hammel <k0scist@gmail.com>
parents:
34
diff
changeset
|
19 default_cast = (int, float, str) |
6 | 20 |
5
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
21 def cast(to_type, *values): |
81 | 22 """ |
23 gently cast a thing to a thing; | |
24 if you can't, return the original value | |
25 """ | |
26 | |
33 | 27 |
5
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
28 retval = [] |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
29 for value in values: |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
30 try: |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
31 retval.append(to_type(value)) |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
32 except ValueError: |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
33 retval.append(value) |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
34 return retval |
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
35 |
81 | 36 def cast_or_discard(to_type, *values): |
37 """ | |
38 cast to `to_type` if possible; | |
39 otherwise just throw away | |
40 """ | |
111
c4d26ef63d8e
fix error in convert and i think this works sorta well enouigh for now
Jeff Hammel <k0scist@gmail.com>
parents:
109
diff
changeset
|
41 retval = [] |
c4d26ef63d8e
fix error in convert and i think this works sorta well enouigh for now
Jeff Hammel <k0scist@gmail.com>
parents:
109
diff
changeset
|
42 for value in values: |
c4d26ef63d8e
fix error in convert and i think this works sorta well enouigh for now
Jeff Hammel <k0scist@gmail.com>
parents:
109
diff
changeset
|
43 try: |
c4d26ef63d8e
fix error in convert and i think this works sorta well enouigh for now
Jeff Hammel <k0scist@gmail.com>
parents:
109
diff
changeset
|
44 retval.append(to_type(value)) |
c4d26ef63d8e
fix error in convert and i think this works sorta well enouigh for now
Jeff Hammel <k0scist@gmail.com>
parents:
109
diff
changeset
|
45 except ValueError: |
c4d26ef63d8e
fix error in convert and i think this works sorta well enouigh for now
Jeff Hammel <k0scist@gmail.com>
parents:
109
diff
changeset
|
46 continue |
c4d26ef63d8e
fix error in convert and i think this works sorta well enouigh for now
Jeff Hammel <k0scist@gmail.com>
parents:
109
diff
changeset
|
47 return retval |
c4d26ef63d8e
fix error in convert and i think this works sorta well enouigh for now
Jeff Hammel <k0scist@gmail.com>
parents:
109
diff
changeset
|
48 |
33 | 49 |
5
d5447d401c44
serializaion; pandas probably does this
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
50 def float_or_orig(*values): |
81 | 51 return cast(float, *values) |
111
c4d26ef63d8e
fix error in convert and i think this works sorta well enouigh for now
Jeff Hammel <k0scist@gmail.com>
parents:
109
diff
changeset
|
52 # convenience function ; do we need this? |
33 | 53 |
112 | 54 |
34 | 55 def column_type(values, types=default_cast): |
56 """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
|
57 for t in types: |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
58 for value in values: |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
59 try: |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
60 t(value) |
56 | 61 except ValueError, TypeError: |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
62 break |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
63 else: |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
64 return t |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
65 |
34 | 66 |
59 | 67 def cast_columns(columns, types=default_cast): |
33 | 68 """ |
69 cast a column of data | |
70 """ | |
109 | 71 column_types = [column_type(column, types=types) |
72 for column in columns] | |
111
c4d26ef63d8e
fix error in convert and i think this works sorta well enouigh for now
Jeff Hammel <k0scist@gmail.com>
parents:
109
diff
changeset
|
73 return [[_type(row) for row in column] |
c4d26ef63d8e
fix error in convert and i think this works sorta well enouigh for now
Jeff Hammel <k0scist@gmail.com>
parents:
109
diff
changeset
|
74 for _type, column in zip(column_types, columns)] |
27 | 75 |
59 | 76 |
27 | 77 def main(args=sys.argv[1:]): |
78 """CLI""" | |
79 | |
31 | 80 # parse command line |
47
6d34c02f7c9c
inherit from the right thing
Jeff Hammel <k0scist@gmail.com>
parents:
46
diff
changeset
|
81 parser = CSVParser(description="interpolate types from file") |
31 | 82 options = parser.parse_args(args) |
83 | |
109 | 84 # read CSV file columns |
85 columns = parser.columns() | |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
86 |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
87 # get types |
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
88 types = [column_type(column) for column in columns] |
46 | 89 |
90 # print type information | |
55
ecaf1d4b1c2c
this should work now; it doesnt, but it should
Jeff Hammel <k0scist@gmail.com>
parents:
52
diff
changeset
|
91 writer = csv.writer(sys.stdout) |
56 | 92 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
|
93 |
32 | 94 |
27 | 95 if __name__ == '__main__': |
96 main() |