view numerics/write.py @ 172:dc0a620a0368

add another example
author Jeff Hammel <k0scist@gmail.com>
date Fri, 03 Jul 2015 11:11:01 -0700
parents 8bfa28ff74ce
children
line wrap: on
line source

# -*- 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(self, *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