[Python-ideas] struct.unpack should support open files

Dan Sommers 2QdxY4RzWzUUiLuE at potatochowder.com
Mon Dec 24 09:17:20 EST 2018


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


More information about the Python-ideas mailing list