I have a list of strings, and would like to pass this to an api that accepts only a file
-like object, without having to concatenate/flatten the list to use the likes of StringIO
.
The strings are utf-8, don't necessarily end in newlines, and if naively concatenated could be used directly in StringIO
.
Preferred solution would be within the standard library (python3.8) (Given the shape of the data is naturally similar to a file (~identical to readlines()
obviously), and memory access pattern would be efficient, I have a feeling I'm just failing to DuckDuckGo correctly) - but if that doesn't exist any "streaming" (no data concatenation) solution would suffice.
[Update, based on @JonSG's links]
Both RawIOBase
and TextIOBase
look provide an api that decouples arbitrarily sized "chunks"/fragments (in my case: strings in a list) from a file-like
read which can specify its own read chunk size, while streaming the data itself (memory cost increases by only some window at any given time [dependent of course on behavior of your source & sink])
RawIOBase.readinto
looks especially promising because it provides the buffer returned to client reads directly, allowing much simpler code - but this appears to come at the cost of one full copy (into that buffer).
TextIOBase.read()
has its own cost for its operation solving the same subproblem, which is concatenating k
(k
much smaller than N
) chunks together.
I'll investigate both of these.