[Csv] CSV interface question

Dave Cole djc at object-craft.com.au
Thu Jan 30 00:57:57 CET 2003


>>>>> "Andrew" == Andrew McNamara <andrewm at object-craft.com.au> writes:

>> With the class technique this would become:
>> 
>> def reader(fileobj, dialect=excel, **kwds):
>>     kwargs = {}
>>     for key, value in dialect.__dict__.iteritems():
>>         if not key.startswith('_'):
>>             kwargs[key] = value
>>     kwargs.update(kwds)
>>     return _csv.reader(fileobj, **kwargs)

Andrew> BTW, your method of extracting directly from the instance's
Andrew> __dict__ doesn't pick up class attributes. In my prototype
Andrew> implementation, I used getattr instead.

Ahhh...

So does this mean that we can go back to classes?

class dialect:
    quotechar = '"'
    delimiter = ','
    lineterminator = '\r\n'

dialect_opts = [attr for attr in dir(dialect) if not attr.startswith('_')]

excel = dialect

class excel_tsv(excel):
    delimiter = '\t'

def reader(fileobj, dialectobj=excel, **kwds):
    kwargs = {}
    for opt in dialect_opts:
        kwargs[opt] = getattr(dialectobj, opt)
    kwargs.update(kwds)
    return _csv.reader(fileobj, **kwargs)

-- 
http://www.object-craft.com.au




More information about the Csv mailing list