Skip to content
Advertisement

Adapt an iterator to behave like a file-like object in Python

I have a generator producing a list of strings. Is there a utility/adapter in Python that could make it look like a file?

For example,

JavaScript

Because data may be big and needs to be streamable (each fragment is a few kilobytes, the entire stream is tens of megabytes), I do not want to eagerly evaluate the whole generator before passing it to stream adaptor.

Advertisement

Answer

Here’s a solution that should read from your iterator in chunks.

JavaScript
Advertisement