Mercurial > hg > numerics
changeset 79:c70ef55b6141
add some silly serialization thing
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 01 Mar 2015 09:19:25 -0800 |
parents | 9b9bfbcdd749 |
children | 8bfa28ff74ce |
files | numerics/write.py |
diffstat | 1 files changed, 39 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/numerics/write.py Sun Mar 01 09:19:25 2015 -0800 @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- + +""" +serialization (CSV) +""" + +# imports +import csv + +# module globals +__all__ = ['CSVWriter'] +string = (str, unicode) + +class CSVWriter(object): + """CSV writer interface""" + + def __init__(self, f, mode='a'): + """ + f -- file path or object + """ + + if isinstance(f, string): + f = open(f, mode) + self.f = f + self.writer = csv.writer(f) + + def writerow(*row): + self.writer.writerow(row) + self.f.flush() + __call__ = writerow + + def write(self, rows): + for row in rows: + self(*row) + + def close(self): + if self.f is not None: + self.f.close() + __del__ = close