57
|
1 #!/usr/bin/env python
|
|
2 # -*- coding: utf-8 -*-
|
|
3
|
|
4 """
|
|
5 manipulate CSV data
|
|
6 """
|
|
7
|
|
8 # imports
|
|
9 import argparse
|
|
10 import os
|
|
11 import sys
|
108
|
12 from .convert import default_cast, cast_columns
|
110
|
13 from .data import transpose
|
60
|
14 from .read import CSVParser
|
57
|
15
|
|
16 # module globals
|
106
|
17 __all__ = ['ManipulationParser', 'main']
|
110
|
18
|
57
|
19
|
60
|
20 class ManipulationParser(CSVParser):
|
|
21 """CLI option parser for data manipulation"""
|
|
22
|
108
|
23 types = default_cast
|
|
24
|
57
|
25 def __init__(self, **kwargs):
|
|
26 kwargs.setdefault('description', __doc__)
|
60
|
27 CSVParser.__init__(self, **kwargs)
|
57
|
28 self.options = None
|
|
29
|
60
|
30 def typed_data(self):
|
108
|
31 """return parsed and casted data"""
|
110
|
32 return cast_columns(self.columns(), self.types)
|
|
33
|
|
34 def process(self):
|
|
35 return transpose(self.typed_data())
|
57
|
36
|
|
37 def main(args=sys.argv[1:]):
|
|
38 """CLI"""
|
|
39
|
|
40 # parse command line options
|
106
|
41 parser = ManipulationParser()
|
57
|
42 options = parser.parse_args(args)
|
|
43
|
110
|
44 # write manipulated data
|
|
45 parser.write(parser.process())
|
|
46
|
57
|
47 if __name__ == '__main__':
|
|
48 main()
|