Thanks for your comments, everyone! Right, I’m struggling to figure out Greg's example :). Maybe Greg missed that DictReader.open didn’t exist and was in fact what I was asking for! (contextlib.closing is great, thank you for that!) Anyway, just to re-up the original points and add a few - * opening a CSV file with the right newline setting and then applying csv.reader and csv.DictReader is super common. * newline=“” has important ramifications for Windows functionality, as we <ahem> recently discovered when we tried to extend sourmash with windows compat. * yes it’s very easy to write my own utility function to do this, and in fact I have done so …repeatedly. :) * no one is proposing to remove any functionality. * I like Chris Barker’s comment, “”” it ended with the idea that maybe there should be a PEP for a common interface for all “file” readers — eg JSON, CSV, etc.. And that interface could be supported by third party libs. That interface *maybe* would include a single step load from a path-like functionality. “”” I’m +0 on David Mertz’s suggestion, “”" Most Pandas read methods take either a path-like argument or a file-like argument, and figure out which it is by introspection when called. Actually, most of them even accept a URL-like argument as well I don't think this is a terrible approach. It doesn't make things quite as explicit as the standard library generally does. But it's convenient, and there's no real ambiguity. “”” mostly because when we’ve done this in our own packages, I've struggled to figure out the best method to figure out if something is file-like. For example, it looks like ‘csv.DictReader’ will take any iterable, which means passing in a string is problematic; perhaps we could be looking for read or readline instead? Codifying that in some standard way could be nice. best, —titus
On Sep 5, 2021, at 5:30 PM, Oscar Benjamin
wrote: On Mon, 6 Sept 2021 at 01:13, Greg Ewing
wrote: On 6/09/21 3:07 am, C. Titus Brown via Python-ideas wrote:
with csv.DictReader.open(filename) as r: for row in r: …
You can do this now:
from contextlib import closing with closing(csv.DictReader.open(filename)) as r: ...
What version of Python are you using?
import csv csv.DictReader.open Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: type object 'DictReader' has no attribute 'open'
IMO this is preferable than going around adding context manager methods to everything that has open-like functionality.
I disagree. It would be better if resource acquisition (e.g. opening a file) always took place in an __enter__ method so that it could always be under control of a with statement. Having closing as a separate function negates that because there has to be a separate function that acquires the resource before the closing function is called and hence before __enter__ is called.
-- Oscar _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/AR6IHZ... Code of Conduct: http://python.org/psf/codeofconduct/