Let's say I have a list of tuples in a form of:
data = [(1,2), (1,2), ..., (1,2)]
and a method data_to_bytes
that accepts tuple does something to it and returns bytes. Now I want to call this method on every element from data
and save output to the file. This is something I already have:
def create_data_file(data_file, data):
with data_file.open('wb') as _file:
for i in data:
_file.write(data_to_bytes(i))
return data_file
But this is terribly slow and I would like to improve it. Maybe it is possible to get rid of the loop inside with. I was thinking about using numpy somehow and maybe call data_to_bytes()
on whole array instead of every element. Is this possible somehow?