[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread Oscar Benjamin
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))

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread Greg Ewing
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: ... IMO this is preferable than going around adding context

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread Christopher Barker
On Sun, Sep 5, 2021 at 10:32 AM David Mertz, Ph.D. wrote: > 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

[Python-ideas] Re: Improve traceback for common `with` statement mistake

2021-09-05 Thread Chris Angelico
On Mon, Sep 6, 2021 at 9:37 AM Finn Mason wrote: > > Hello all, > > In Python 3.10 and 3.11, exception tracebacks are being greatly improved. I > noticed that there's nothing related to a fairly common (in my personal > experience) cryptic traceback relating to the `with` statement: > > >>>

[Python-ideas] Improve traceback for common `with` statement mistake

2021-09-05 Thread Finn Mason
Hello all, In Python 3.10 and 3.11, exception tracebacks are being greatly improved. I noticed that there's nothing related to a fairly common (in my personal experience) cryptic traceback relating to the `with` statement: >>> with ContextManager as ctx: ... # do something with `ctx` ...

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread David Mertz, Ph.D.
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

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread Christopher Barker
> This would only be helpful when the CSV is on the disk but csv.reader() > takes a file object so that it can used with anything like a socket for > example. json.load() does the same thing. > There has been discussion about adding loading from a “path like” to the JSON lib. See this list about

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread Rémi Lapeyre
> On 5 Sep 2021, at 17:07, C. Titus Brown via Python-ideas > wrote: > > Hi all, > > the product of Sunday morning idle curiosity... > > I’ve been using the csv module a lot, and I’m wondering if there would be > value in adding a standard mechanism for opening a CSV file (correctly) using

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread Thomas Grainger
Seems nice, tarfile has a similar shortcut too. I do tend to reach for pandas now whenever I can for csv processing On Sun, 5 Sep 2021, 16:10 C. Titus Brown via Python-ideas, < python-ideas@python.org> wrote: > Hi all, > > the product of Sunday morning idle curiosity... > > I’ve been using the

[Python-ideas] Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread C. Titus Brown via Python-ideas
Hi all, the product of Sunday morning idle curiosity... I’ve been using the csv module a lot, and I’m wondering if there would be value in adding a standard mechanism for opening a CSV file (correctly) using a context manager? So, instead of with open(filename, newline=“”) as fp: r =