[Csv] CSV interface question

Skip Montanaro skip at pobox.com
Thu Jan 30 03:07:37 CET 2003


    Skip> I was thinking of dialects as dicts....

    Dave> Note the spelling error in "linetermintor" - user constructed
    Dave> dictionaries are not good.

Yeah, but Cliff's dialect validator would have caught that. ;-)

    Dave> Whenever I find myself using dictionaries for storing values as
    Dave> opposed to indexing data I can't escape the feeling that my past
    Dave> as a Perl programmer is coming back to haunt me.  At least with
    Dave> Perl there is some syntactic sugar to make this type of thing less
    Dave> ugly:

    Dave> excel_dialect = { quotechar => '"',
    Dave>                   delimiter => ',',
    Dave>                   linetermintor => '\r\n' }

Other than losing a couple quote marks and substituting => for : I don't see
how the Perl syntax is any better.

Note also that with dicts you can simply pass them as keyword args:

    return _csv.reader(..., **kwdargs)

You'll have to do a little more work with classes to make that work (a
subclass's __dict__ attribute does not include the parent class's __dict__
contents) and with the possibility of new-style classes you will have to
work even harder.

    Dave> Maybe we could include a name attribute which allowed us to use
    Dave> 'excel-tsv' as a dialect identifier.

As I mentioned in my last post, I think name attributes will be necessary,
at least for human consumption.

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

Not quite.  You need to traverse the bases to pick up everything.

Skip
_______________________________________________
Csv mailing list
Csv at mail.mojam.com
http://manatee.mojam.com/mailman/listinfo/csv



More information about the Csv mailing list