def make_chunk_iter(stream, separator, limit=None, buffer_size=(10 * 1024), cap_at_buffer=False):
 _iter = _make_chunk_iter(stream, limit, buffer_size)
 first_item = next(_iter, '')
 if (not first_item):
  return
 _iter = chain((first_item,), _iter)
 if isinstance(first_item, text_type):
  separator = to_unicode(separator)
  _split = re.compile(('(%s)' % re.escape(separator))).split
  _join = u''.join
 else:
  separator = to_bytes(separator)
  _split = re.compile((('(' + re.escape(separator)) + ')')).split
  _join = ''.join
 buffer = []
 while 1:
  new_data = next(_iter, '')
  if (not new_data):
   break
  chunks = _split(new_data)
  new_buf = []
  buf_size = 0
  for item in chain(buffer, chunks):
   if (item == separator):
    (yield _join(new_buf))
    new_buf = []
    buf_size = 0
   else:
    buf_size += len(item)
    new_buf.append(item)
    if (cap_at_buffer and (buf_size >= buffer_size)):
     rv = _join(new_buf)
     while (len(rv) >= buffer_size):
      (yield rv[:buffer_size])
      rv = rv[buffer_size:]
     new_buf = [rv]
     buf_size = len(rv)
  buffer = new_buf
 if buffer:
  (yield _join(buffer))

def make_chunk_iter ( stream , separator , limit = None , buffer_size = ( 10 * 1024 ) , cap_at_buffer = False ) : _iter = _make_chunk_iter ( stream , limit , buffer_size ) first_item = next ( _iter , '' ) if ( not first_item ) : return _iter = chain ( ( first_item , ) , _iter ) if isinstance ( first_item , text_type ) : separator = to_unicode ( separator ) _split = re . compile ( ( '(%s)' % re . escape ( separator ) ) ) . split _join = u'' . join else : separator = to_bytes ( separator ) _split = re . compile ( ( ( '(' + re . escape ( separator ) ) + ')' ) ) . split _join = '' . join buffer = [ ] while 1 : new_data = next ( _iter , '' ) if ( not new_data ) : break chunks = _split ( new_data ) new_buf = [ ] buf_size = 0 for item in chain ( buffer , chunks ) : if ( item == separator ) : ( yield _join ( new_buf ) ) new_buf = [ ] buf_size = 0 else : buf_size += len ( item ) new_buf . append ( item ) if ( cap_at_buffer and ( buf_size >= buffer_size ) ) : rv = _join ( new_buf ) while ( len ( rv ) >= buffer_size ) : ( yield rv [ : buffer_size ] ) rv = rv [ buffer_size : ] new_buf = [ rv ] buf_size = len ( rv ) buffer = new_buf if buffer : ( yield _join ( buffer ) )
works like :func:make_line_iter but accepts a separator which divides chunks .