Mercurial > hg > numerics
comparison numerics/read.py @ 107:19a5c2fb52bb
add transpose functionality
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 15 Mar 2015 10:02:48 -0700 |
parents | 32a849b8f1f0 |
children | 5790bcb30bd8 |
comparison
equal
deleted
inserted
replaced
106:895ad896023a | 107:19a5c2fb52bb |
---|---|
10 # imports | 10 # imports |
11 import argparse | 11 import argparse |
12 import csv | 12 import csv |
13 import os | 13 import os |
14 import sys | 14 import sys |
15 from .data import transpose | |
15 from .write import CSVWriter | 16 from .write import CSVWriter |
16 | 17 |
17 # module globals | 18 # module globals |
18 __all__ = ['CSVSchema', 'read_csv', 'CSVParser', 'main'] | 19 __all__ = ['CSVSchema', 'read_csv', 'CSVParser', 'main'] |
19 string = (str, unicode) | 20 string = (str, unicode) |
143 # return processed data | 144 # return processed data |
144 return data | 145 return data |
145 | 146 |
146 def columns(self): | 147 def columns(self): |
147 """return columns vs `data`'s rows""" | 148 """return columns vs `data`'s rows""" |
148 raise NotImplementedError('TODO') # -> record TODO items | 149 return transpose(self.read()) |
149 | 150 |
150 | 151 |
151 def main(args=sys.argv[1:]): | 152 def main(args=sys.argv[1:]): |
152 """CLI""" | 153 """CLI""" |
153 | 154 |
154 # parse command line options | 155 # parse command line options |
155 parser = CSVParser() | 156 parser = CSVParser() |
157 parser.add_argument('--transpose', dest='transpose', | |
158 action='store_true', default=False, | |
159 help="transpose columns and rows") | |
156 options = parser.parse_args(args) | 160 options = parser.parse_args(args) |
157 | 161 |
158 if not options.csv: | 162 if not options.csv: |
159 # read from stdin | 163 # read from stdin |
160 options.csv = [sys.stdin] | 164 options.csv = [sys.stdin] |
161 | 165 |
162 # read CSV | 166 # read CSV |
163 data = parser.read() | 167 if options.transpose: |
168 data = parser.columns() | |
169 else: | |
170 data = parser.read() | |
164 | 171 |
165 # write CSV | 172 # write CSV |
166 writer = CSVWriter(options.output) | 173 writer = CSVWriter(options.output) |
167 writer.write(data) | 174 writer.write(data) |
168 | 175 |