[issue16297] make csv.DictReader.__init__ use self.fieldnames
tb added the comment: This is my use case: I am creating a custom DictReader class for reading and accessing the columns white space and case insensitive. For this I created a dict subclass (DictInsensitive) with a custom __getitem__ method. DictReaderInsensitive.__next__ overrides the original DictReader.__next__ by returning a DictInsensitive. I also override the DictReader.fieldnames property to lower/strip all fieldnames. See http://stackoverflow.com/a/12970460/1251007 for an example. This is all functional with the current csv.DictReader. All entries in DictReaderInsensitive.fieldnames are in lower case and without white space and by iterating over the lines I get dictionaries where I can access the keys white space/case insensitive. Now, the problem is: If I want the check wether a specific fieldname is present in the input file, I have strip()/lower() by hand: csv_in = DictReaderInsensitive(open(input_file, 'rU')) if Fieldname to check.strip().lower() in csv_in.fieldnames To do this automatically, I created a ListInsensitive as a subclass of list: class ListInsensitive(list): def __contains__(self, item): return list.__contains__(self, item.strip().lower()) Now I want csv.DictReader to use that new class. I thought of overwriting the setter method, but here lies the current problem. I can overwrite the setter method, but csv.DictReader does not use the setter method internally. My setter method looks like this: @fieldnames.setter def fieldnames(self, value): if value == None: self._fieldnames = None else: self._fieldnames = ListInsensitive() for name in value: self._fieldnames.append(name.strip().lower()) -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16297 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16297] make csv.DictReader.__init__ use self.fieldnames
Serhiy Storchaka added the comment: You can overwrite fieldnames getter. @property def fieldnames(self): if self._fieldnames is None: try: self._fieldnames = next(self.reader) except StopIteration: pass self.line_num = self.reader.line_num if not isinstance(self._fieldnames, ListInsensitive): self._fieldnames = ListInsensitive(name.strip().lower() for name in self._fieldnames) return self._fieldnames -- nosy: +serhiy.storchaka ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16297 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16297] make csv.DictReader.__init__ use self.fieldnames
tb added the comment: Okay, that makes my use case obsolete. Overwriting the getter method like this seems obvious ... Somehow this hasn't come to my mind. Sorry for bothering. From my point of view, we can close this issue. Or is there another use case? -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16297 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16297] make csv.DictReader.__init__ use self.fieldnames
R. David Murray added the comment: It does seem more logical to implement this in the getter, now that Serhiy has pointed it out :) So let's close this. (Aside: if it were me, I'd move the mangling of the fieldnames into the ListInsensitive __init__ method.) -- resolution: - invalid status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16297 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16297] make csv.DictReader.__init__ use self.fieldnames
Changes by Ramchandra Apte maniandra...@gmail.com: -- title: small fix to csv.DictReader.__init__ - make csv.DictReader.__init__ use self.fieldnames ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16297 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16297] make csv.DictReader.__init__ use self.fieldnames
Berker Peksag added the comment: I think this is an invalid request. See the usage of property decorator: http://docs.python.org/py3k/library/functions.html#property -- nosy: +berker.peksag ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16297 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue16297] make csv.DictReader.__init__ use self.fieldnames
R. David Murray added the comment: I think it is not an invalid request. However, what is the use case? Normally a class will manipulate the real variable, and the getter/setter is the public API. Without a good use case it doesn't seem worth changing that bit of the init method. -- nosy: +r.david.murray ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16297 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com