On Mar 27, 2017, at 8:50 AM, Ram Rachum <r...@rachum.com> wrote:What do you think about adding methods pathlib.Path.write_json and pathlib.Path.read_json , similar to write_text, write_bytes, read_text, read_bytes?
On Mar 27, 2017, at 10:36 AM, Paul Moore <p.f....@gmail.com> wrote:On 27 March 2017 at 15:33, Donald Stufft <don...@stufft.io> wrote:What do you think about adding methods pathlib.Path.write_json and
pathlib.Path.read_json , similar to write_text, write_bytes, read_text,
read_bytes?
-1, I also think that write_* and read_* were mistakes to begin with.
Text is (much) more general-use than JSON.
Another idea: Maybe make json.load and json.dump support Path objects?
On 27 March 2017 at 15:40, Ram Rachum <r...@rachum.com> wrote:
> Another idea: Maybe make json.load and json.dump support Path objects?
If they currently supported filenames, I'd say that's a reasonable
extension. Given that they don't, it still seems like more effort than
it's worth to save a few characters
I'm not in favor of this idea for the reason mentioned by many of the other posters. BUT ... this does bring up something missing from json readers: the ability to read one json object from the input rather than reading the entire input and attempting to interpret it as one object.
On Mon, Mar 27, 2017 at 7:59 AM, Paul Moore <p.f....@gmail.com> wrote:On 27 March 2017 at 15:40, Ram Rachum <r...@rachum.com> wrote:
> Another idea: Maybe make json.load and json.dump support Path objects?
If they currently supported filenames, I'd say that's a reasonable
extension. Given that they don't, it still seems like more effort than
it's worth to save a few charactersSure, but they probably should -- it's a REALLY common (most common) use-case to read and write JSON from a file. And many APIs support "filename or open file-like object".I'd love to see that added, and, or course, support for Path objects as well.
-CHB
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris....@noaa.gov
#python tip: Set separators=(',', ':') to dump JSON more compactly.
>>> json.dumps({'a':1, 'b':2}, separators=(',',':'))
'{"a":1,"b":2}'
Another idea: Maybe make json.load and json.dump support Path objects?
On 27 Mar 2017, at 15:08, Markus Meskanen <markusm...@gmail.com> wrote:-1, should we also include write_ini, write_yaml, etc?
I attended a talk at PYCON UK that talked to the point of using object compositionrather then rich interfaces. I cannot recall the term that was used to cover this idea.
def read_json(self, **kwargs):object_pairs_hook = kwargs.pop('object_pairs_hook', collections.OrderedDict) # OrderedDefaultDictobject_hook = kwargs.pop('object_hook', as_pathlib_Path)encoding = kwargs.pop('encoding', 'utf8')with codecs.open(self, 'r ', encoding=encoding) as _file:return json.load(_file,object_pairs_hook=object_pairs_hook,object_hook=object_hook,**kwargs)def write_json(self, obj, **kwargs):kwargs['cls'] = kwargs.pop('cls', PathJSONEncoder)encoding = kwargs.pop('encoding', 'utf8')with codecs.open(self, 'w', encoding=encoding) as _file:return json.dump(obj, _file, **kwargs)def test_pathlib_json_encoder_decoder():p = pathlib.Path('./test.json')obj = dict(path=p, _path=str(unicode(p)))p.write_json(obj)obj2 = p.read_json()assert obj['path'] == obj2['path']assert isinstance(obj['path'], pathlib.Path)