On Fri, Jul 9, 2021 at 6:21 PM Steven D'Aprano <steve@pearwood.info> wrote:
Has anything changed since the last time it was discussed? If nothing
has changed, and there are no new arguments in favour of the change, why
do you think the result will be any different?

Note: one thing that has not been rejected, and would likely be accepted, is adding the capability in the json module to pass in a filename, rather than an open file, to have a json file read. So your example would be:

(untested)

 `asyncio.to_thread(pathlib.Path(...).read_text, encoding="utf8")`

import json
the_path - pathlib.Path(....)

asyncio.to_thread(json.loadf, the_path)

which reads even better to me. 

And while I like json file reading to be more simple, I don't understand why any of this is needed to "interact with the event loop as few times as possible and use only the RAM absolutely required."

Honestly I'm still not totally clear on all things async, but what's wrong with:

with open(the_path, encoding="utf-8") as the_file:
    asyncio.to_thread(json.load, the_file)

It opens the file in the main thread, and not asynchronously, but doesn't the file itself get read in the other thead, asynchronously? And is there any extra RAM used?

Also -- and this may be completely my ignorance -- but don't you need to wrap the json reading in a function anyway so that you can capture the result somewhere?

In short -- no, I don't think JSON is not special enough to get a Path method, but a simple way to read JSON directly from a Path would be nice.

-CHB




 
Why not just subclass pathlib.Path and add the method yourself?

    class MyPath(pathlib.Path):
        def json(self):
            ...

Or for that matter, you can add an extension method to pathlib.Path:

    def json(self):
        ...

    pathlib.Path.json = json


_______________________________________________
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/MWTA3BGUWDT74LIFPT47TGUZZIDSB47H/
Code of Conduct: http://python.org/psf/codeofconduct/


--
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython