I was disappointed that there was no good way to chunk up data for string formatting or packetzing UDP data. After a bit of thought, and a desire to avoid using python code to do loops, I came up with a nice simple regex-based solution.
import re
buff = #some data buffer
m = re.compile(r'.{##}|.+',re.S)
chunks = m.findall(buff)
example:
>>> import re
>>> m = re.compile(r'.{5}|.*',re.S)
>>> data = "HELLO_WORLD_HOW_ARE_YOU"
>>> m.findall(data)
['HELLO', '_WORL', 'D_HOW', '_ARE_', 'YOU']
I would assume that this is the most efficient way to do this, as it uses underlying c functions, and there’s nothing better than zippy python : )