In Python, can I build a variable that acts like a string but is internally iterating through a sequence of strings?
For instance
def function_a():
for i in xrange(100000000):
yield str(i)
This, will iterate over a list of strings and it will do it efficiently - keeping only one string in memory at a time. But what I want is something like this:
''.join([s for s in function_a()])
But I bet this just does the naïve thing and iterates through the entire set and concatenates them all into one big string in memory. The other problem with this, is that I want a variable, I don't want to have to expose the user to the ugly work of actually doing the join. So maybe the user would do something like:
magic_str = get_long_but_memory_efficient_str()
And then use it to efficiently print to the screen (and free up memory as it goes):
print magic_str
Or my real use for it is to HTTP stream to a server:
request = urllib2.Request(url, magic_str)
Apparently something like this exists. Check out the code below for efficiently streaming a file to a server (from this question).
f = open('somelargefile.zip','rb')
mmapped_file_as_string = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
request = urllib2.Request(url, mmapped_file_as_string)
request.add_header("Content-Type", "application/zip")
response = urllib2.urlopen(request)
But my case is different because I'm constructing the string that I'm streaming to the server.