Chunk Data For Streaming

NOTE: I originally posted this on Snipplr.

Chunks data into block_size blocks for streaming, adds null padding.

import math

is_debug = False

def chunk(data, block_size, padding=b'\x00'):
    data_size       = len(data)
    padding_size    = block_size - (data_size % block_size) if (data_size > block_size) else (block_size - data_size)
    is_padded       = (padding_size > 0) and (padding_size != block_size)
    total_blocks    = math.ceil(data_size / block_size)
    current_block   = 0
    block_slice     = [(block_size * current_block), (block_size * (current_block + 1))]

    if is_debug:
        print("data_size: %d" % (data_size,))
        print("padding_size: %d" % (padding_size,))
        print("is_padded: %s" % (is_padded,))
        print("total_blocks: %d\n" % (total_blocks,))

    while current_block < total_blocks:
        a_block = slice(block_slice[0], block_slice[1])

        if (is_padded and (current_block == (total_blocks - 1))):
            yield [current_block, total_blocks, data_size, data[a_block] + (padding_size * padding)]
            return

        yield [current_block, total_blocks, data_size, data[a_block]]

        current_block   += 1
        block_slice[0]  += block_size
        block_slice[1]  += block_size

Sections