(unpacked, items_per_byte)
return bytearray([
        sum(prepacked[_offs : _offs+items_per_byte])
        for _offs in xrange(0, len(prepacked), items_per_byte)
    ])
Pack into packed-bits representation.
Pack into packed-bits representation.
Pack into packed - bits representation .
def pack_bytes(unpacked, items_per_byte):
    """Pack into packed-bits representation."""
    bpp = 8 // items_per_byte
    mask = (1 << bpp) - 1
    shifts = [8 - bpp - _sh for _sh in range(0, 8, bpp)]
    # ceildiv(a,b) == -(floordiv(-a,b))
    packed_width = -(-len(unpacked) // items_per_byte)
    prepacked = [
        (_byte & mask) << _shift
        for _byte, _shift in zip(iterbytes(unpacked), shifts*packed_width)
    ]
    return bytearray([
        sum(prepacked[_offs : _offs+items_per_byte])
        for _offs in xrange(0, len(prepacked), items_per_byte)
    ])
def pack_bytes ( unpacked , items_per_byte ) : bpp = 8 // items_per_byte mask = ( 1 << bpp ) - 1 shifts = [ 8 - bpp - _sh for _sh in range ( 0 , 8 , bpp ) ] # ceildiv(a,b) == -(floordiv(-a,b)) packed_width = - ( - len ( unpacked ) // items_per_byte ) prepacked = [ ( _byte & mask ) << _shift for _byte , _shift in zip ( iterbytes ( unpacked ) , shifts * packed_width ) ] return bytearray ( [ sum ( prepacked [ _offs : _offs + items_per_byte ] ) for _offs in xrange ( 0 , len ( prepacked ) , items_per_byte ) ] )