Mercurial > hg > numerics
changeset 45:ef915968d104
put this in the parser so that i can use this in convert
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 16 Jan 2015 17:08:43 -0800 (2015-01-17) |
parents | 7927d7127f9a |
children | 93850093eafd |
files | numerics/convert.py numerics/read.py setup.py |
diffstat | 3 files changed, 27 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/numerics/convert.py Fri Jan 09 14:17:33 2015 -0800 +++ b/numerics/convert.py Fri Jan 16 17:08:43 2015 -0800 @@ -10,7 +10,7 @@ __all__ = ['cast', 'float_or_orig', 'main'] -default_cast = (int, float, ) +default_cast = (int, float, str) def cast(to_type, *values):
--- a/numerics/read.py Fri Jan 09 14:17:33 2015 -0800 +++ b/numerics/read.py Fri Jan 16 17:08:43 2015 -0800 @@ -115,6 +115,30 @@ def validate(self, options): """validate options""" + def read(self): + """read and process CSV""" + + data = read_csv(*self.options.csv) + + if self.options.added_columns: + # add columns + for row in data: + row.extend(options.added_columns) + + if self.options.columns: + # filter by column + data = [[row[column] for column in self.options.columns] + for row in data] + + if self.options.index: + # prepend numeric index + for index, row in enumerate(data): + row.insert(0, index) + + # return processed data + return data + + def main(args=sys.argv[1:]): """CLI""" @@ -127,22 +151,7 @@ options.csv = [sys.stdin] # read CSV - data = read_csv(*options.csv) - - if options.added_columns: - # add columns - for row in data: - row.extend(options.added_columns) - - if options.columns: - # filter by column - data = [[row[column] for column in options.columns] - for row in data] - - if options.index: - # prepend numeric index - for index, row in enumerate(data): - row.insert(0, index) + data = parser.read() # write CSV writer = csv.writer(options.output)