Here's a snippet of semi-production code we use: def read_and_unpack(handle, fmt): size = struct.calcsize(fmt) data = handle.read(size) if len(data) < size: return None return struct.unpack(fmt, data)
which was originally something like: def read_and_unpack(handle, fmt, offset=None): if offset is not None: handle.seek(*offset) size = struct.calcsize(fmt) data = handle.read(size) if len(data) < size: return None return struct.unpack(fmt, data) until we pulled file seeking up out of the function. Having struct.unpack and struct.unpack_from support files would seem straightforward and be a nice quality of life change, imo. On Mon, Dec 24, 2018 at 9:36 AM Dan Sommers < 2qdxy4rzwzuui...@potatochowder.com> wrote: > On 12/24/18 7:33 AM, Steven D'Aprano wrote: > > On Mon, Dec 24, 2018 at 03:01:07PM +0200, Andrew Svetlov wrote: > > >> Handling files overcomplicates both implementation and mental space > >> for API saving. > > > I haven't thought about this very deeply, but at first glance, I like > > Drew's idea of being able to just pass an open file to unpack and have > > it read from the file. > > The json module has load for files, and loads for bytes and strings, > That said, JSON is usually read and decoded all at once, but I can see > lots of use cases for ingesting "unpackable" data in little chunks. > > Similarly (but not really), print takes an optional destination that > overrides the default destination of stdout. > > Ironically, StringIO adapts strings so that they can be used in places > that expect open files. > > What about something like gzip.GzipFile (call it struct.StructFile?), > which is basically a specialized file-like class that packs data on > writes and unpacks data on reads? > > Dan > _______________________________________________ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/