Python byte buffer object?
Question: Is there a byte buffer object in Python to which I can append values of
specific types? _(preferably with specifiable endianess)_

For example:

    
    
    buf.add_int(4)    # should add a 4 byte integer
    buf.add_short(10) # should add a 2 byte short
    buf.add_byte(24)  # should add a byte
    

I know that I could just use `struct.pack` but this approach just seems
easier. Ideally it should be like the `DataOutputStream` and `DataInputStream`
objects in **Java** which do this exact task.


Answer: You can always use [bitstring](http://code.google.com/p/python-bitstring/). It
is capable of doing all the things you ask and more.

    
    
    >>> import bitstring
    >>> stream=bitstring.BitStream()
    >>> stream.append("int:32=4")
    >>> stream.append("int:16=10")
    >>> stream.append("int:8=24")
    >>> stream
    BitStream('0x00000004000a18')
    >>> stream.bytes
    '\x00\x00\x00\x04\x00\n\x18'
    

Here is a link to the
[documentation](http://packages.python.org/bitstring/#documentation).

