# HG changeset patch # User Jeff Hammel # Date 1425230365 28800 # Node ID c70ef55b61412b35bcc35f302bce486720d2fffa # Parent 9b9bfbcdd7491b44daac66d72416647bb1da1aa6 add some silly serialization thing diff -r 9b9bfbcdd749 -r c70ef55b6141 numerics/write.py --- /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