ewa.buffutil

Package ewa :: Module buffutil |
[hide private]
[frames] | no frames]

# Source Code for Module ewa.buffutil

     1 -def buff_chunk_string(s, bsize):
     2      idx=0
     3      length=len(s)
     4      while idx < length:
     5          yield s[idx:bsize+idx]
     6          idx+=bsize
     7
     8 -def buff_chunk_file(fp, end, bsize):
     9      idx=fp.tell()
    10      while 1:
    11          chunk=fp.read(min(bsize, end-idx))
    12          if chunk:
    13              idx+=len(chunk)
    14              yield chunk
    15          else:
    16              break
    17
    18 -def buff_chunk_iterator(iterator, bsize):
    19      """
    20      this adapts an iterator that yields chunks of
    21      one size to one that yields chunks of another
    22      """
    23      buff=[]
    24      buffsize=0
    25      stopped=False
    26      while 1:
    27          while buffsize < bsize:
    28              try:
    29                  chunkie=iterator.next()
    30              except StopIteration:
    31                  stopped=True
    32                  break
    33              else:
    34                  buff.append(chunkie)
    35                  buffsize+=len(chunkie)
    36          if stopped:
    37              if buffsize:
    38                  yield ''.join(buff)
    39              raise StopIteration
    40          else:
    41              flattened=''.join(buff)
    42              while len(flattened) >=bsize:
    43                  chunk=flattened[:bsize]
    44                  flattened=flattened[bsize:]
    45                  yield chunk
    46              buff=[flattened]
    47              buffsize=len(flattened)
    48