Mercurial > hg > numerics
changeset 127:c9ae21955ca5
how to make this really small
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 16 Mar 2015 20:00:43 -0700 |
parents | 575c8e44227c |
children | 63bf582841a3 |
files | numerics/mean.py numerics/reduce.py |
diffstat | 2 files changed, 11 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/numerics/mean.py Mon Mar 16 19:55:41 2015 -0700 +++ b/numerics/mean.py Mon Mar 16 20:00:43 2015 -0700 @@ -8,30 +8,16 @@ # imports import sys from .filters import mean -from .manipulate import FloatParser -from .write import CSVWriter +from .reduce import ReduceParser + __all__ = ['main'] def main(args=sys.argv[1:]): """CLI""" - - # parse command line options - parser = FloatParser() - options = parser.parse_args(args) + ReduceParser(mean)(*args) - # read data - columns = parser.typed_data() - if not columns: - parser.error("No data given") - - # calculate means - means = [mean(column) for column in columns] - - # write CSV - writer = CSVWriter(options.output) - writer.write([means]) if __name__ == '__main__': main()
--- a/numerics/reduce.py Mon Mar 16 19:55:41 2015 -0700 +++ b/numerics/reduce.py Mon Mar 16 20:00:43 2015 -0700 @@ -18,13 +18,14 @@ class ReduceParser(FloatParser): """CLI option parser""" - # reducing function - function = lambda x: raise NotImplemented("Abstract base class") + def __init__(self, function, **kwargs): + """ + function -- reducing function + """ - def __init__(self, **kwargs): + self.function = function kwargs.setdefault('description', __doc__) - argparse.ArgumentParser.__init__(self, **kwargs) - self.options = None + FloatParser.__init__(self, **kwargs) def __call__(self, *args): @@ -37,8 +38,8 @@ self.error("No data given") # calculate scalars - data = [self.function(column) for column in column] + data = [self.function(column) for column in columns] # write CSV - writer = CSVWriter(options.output) + writer = CSVWriter(self.options.output) writer.write([data])