Python 3.1 beta 1

pruebauno at latinmail.com pruebauno at latinmail.com
Thu May 7 16:39:25 EDT 2009


On May 7, 11:57 am, bearophileH... at lycos.com wrote:
> >Equality tests between OrderedDict objects are order-sensitive and are implemented as list(od1.items())==list(od2.items()). Equality tests between OrderedDict objects and other Mapping objects are order-insensitive<
>
> very nice idea.
>

I don't know if somebody else is interested but I wouldn't mind
support to OrderedDict and namedtuple in the csv module:

Have csv.DictReader return an OrderedDict instead of a regular one
(based on the order of the column names in the header line).

Have csv.DictWriter accept an OrderedDict. That would take care of
this:

"Note that unlike the DictReader class, the fieldnames parameter of
the DictWriter is not optional. Since Python’s dict objects are not
ordered, there is not enough information available to deduce the order
in which the row should be written to the csvfile."
http://docs.python.org/dev/py3k/library/csv.html#csv.DictWriter

Add a new csv.NamedTupleReader and csv.NamedTupleWriter that work
similar to the DictReader and DictWriter then this snippet (in
examples http://docs.python.org/dev/py3k/library/collections.html#collections.namedtuple
):

EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title,
department, paygrade')
import csv
for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv",
"rb"))):
    print(emp.name, emp.title)

Could be rewritten as:

import csv
for emp in csv.NamedTupleReader("employees.csv"):
    print(emp.name, emp.title)


Also, is there a way to convert a OrderedDict into a namedtuple? It is
possible to go the other direction using namedtuple._asdict, but I
don't see a _fromdict method.

And thanks Steven and Raymond for the Counter class. Several people
had started to see the common pattern and now we have it in the
stdlib.



More information about the Python-list mailing list