[Python-ideas] Re: Pickle to/from filename or path
Frankly, I’ve often wanted this for other things that share a similar API, JSON comes to mind. Loading from/saving to a file is a pretty common thing— nice to make it easy. And JSON at least has *s versions for strings, so no confusion there. But it’s been this way a LONG time, and with a general trend of Python becoming more of a systems that scripting language, I don’t see it changing. -CHB PS: you don’t need the context manager — at least in cPython — you can put the open call right in the call to load() -- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@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/APUNKJZXVAMOGZKNS64Y7366YPCIUAI3/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Pickle to/from filename or path
07.02.20 20:41, Antoine Pitrou пише: What you call "quite a bit of boilerplate" is just an additional line of code. If you are frequently being bothered by this (I'm curious what the context is?), it's easy to add the desired helper function to your own library. In general, I don't think adding tons of trivial convenience helpers is good API design, it ends up making the API more difficult to discover and digest. Concur with Antoine. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@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/HQ54BHXFVBTWZ6ATVJVCUIXFRWBKASZX/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Pickle to/from filename or path
> On Feb 7, 2020, at 10:21, Todd wrote: > > I think it would be nice if pickle.dump and pickle.load would also accept a > filename or path. Pickle intentionally has the same API as marshal, and so do a number of other modules—not just in the stdlib, but popular third party modules. So, I don’t think you’d want to change pickle to be different from everything else, but it might be a major process to get everything changed. Also, while there is code in Python that does this “file object or path” thing, I think it’s mostly older code, and not something to encourage going forward. It can be a confusing API, since there’s _other_ code that does the “file object or string” API (not too much of a problem for dump, but think about load). Maybe a keyword-only parameter would be better? Or even a third function: load for file, loads for string, loadp or load_path for path? Or maybe it would be better to have a generic wrapper that can take any file-using function and give you a path-using function: def opener(func, mode='r') @wraps(func) def wrapper(*args, **kw): with open(args[-1], mode) as f: return func(*args[:-1], f, **kwargs) This seems a little magical to have in the stdlib (especially since obviously not every function that wants a file wants it as the last positional argument—in a different language I’d probably make this take the first argument and then wrap load in opener but dump in flip opener flip…), but might be worth putting in your own toolkit. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@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/C2ICRJLFJZNHQ6ZFBCHJEQGAQYHJUBZZ/ Code of Conduct: http://python.org/psf/codeofconduct/
[Python-ideas] Re: Pickle to/from filename or path
On Fri, 7 Feb 2020 13:03:52 -0500 Todd wrote: > Currently with pickle you have to "open" the file separately from the > pickle operation, generally using a "with" clause, then provide the file > object to one of the pickle.dump or pickle.load. This adds quite a bit of > boilerplate for simply saving a file What you call "quite a bit of boilerplate" is just an additional line of code. If you are frequently being bothered by this (I'm curious what the context is?), it's easy to add the desired helper function to your own library. In general, I don't think adding tons of trivial convenience helpers is good API design, it ends up making the API more difficult to discover and digest. Regards Antoine. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@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/4N2O3UMNV2AUXQ5TSZ4JSP7DUIRJDE3S/ Code of Conduct: http://python.org/psf/codeofconduct/