Mercurial > hg > numerics
changeset 164:c16940bd2cee
this works
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 15 May 2015 16:59:09 -0700 |
parents | 8508dbeb9759 |
children | d5f93606f402 |
files | numerics/convert.py numerics/normalize.py |
diffstat | 2 files changed, 20 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/numerics/convert.py Fri May 15 16:35:34 2015 -0700 +++ b/numerics/convert.py Fri May 15 16:59:09 2015 -0700 @@ -11,11 +11,13 @@ from .data import transpose from .read import read_csv, CSVParser -__all__ = ['default_cast', +__all__ = ['numeric', + 'default_cast', 'cast', 'float_or_orig', 'main'] +numeric = (int, float) default_cast = (int, float, str) def cast(to_type, *values):
--- a/numerics/normalize.py Fri May 15 16:35:34 2015 -0700 +++ b/numerics/normalize.py Fri May 15 16:59:09 2015 -0700 @@ -7,10 +7,17 @@ # imports import sys +from .convert import numeric +from .data import transpose from .manipulate import ManipulationParser # module globals -__all__ = ['main', 'NormalizationParser'] +__all__ = ['normalize', 'NormalizationParser'] + +def normalize(*data): + """normalize data""" + norm = float(sum(data)) + return [i/norm for i in data] class NormalizationParser(ManipulationParser): """CLI option parser""" @@ -23,6 +30,15 @@ parser = NormalizationParser() options = parser.parse_args(args) + # get columns + columns = parser.typed_data() + for index in range(len(columns)): + column = columns[index] + if column and isinstance(column[0], numeric): + columns[index] = normalize(*column) + + # output + parser.write(transpose(columns)) if __name__ == '__main__': main()