More Pythonic conversion to binary?
<p>Here is a contrived example of how a lot of our classes return binary representations (to be read by C++) of themselves.</p>

<pre><code>def to_binary(self):
    'Return the binary representation as a string.'
    data = []

    # Binary version number.
    data.append(struct.pack('&lt;I', [2]))

    # Image size.
    data.append(struct.pack('&lt;II', *self.image.size))

    # Attribute count.
    data.append(struct.pack('&lt;I', len(self.attributes)))

    # Attributes.
    for attribute in self.attributes:

        # Id.
        data.append(struct.pack('&lt;I', attribute.id))

        # Type.
        data.append(struct.pack('&lt;H', attribute.type))

        # Extra Type.        
        if attribute.type == 0:
            data.append(struct.pack('&lt;I', attribute.typeEx))

    return ''.join(data)
</code></pre>

<p>What I dislike:</p>

<ul>
<li>Every line starts with <code>data.append(struct.pack(</code>, distracting from the unique part of the line.</li>
<li>The byte order (<code>'&lt;'</code>) is repeated over and over again.</li>
<li>You have to remember to return the boilerplate <code>''.join(data)</code>.</li>
</ul>

<p>What I like:</p>

<ul>
<li>The format specifiers appear near the attribute name. E.g., it's easy to see that <code>self.image.size</code> is written out as two unsigned ints.</li>
<li>The lines are (mostly) independent. E.g., To remove the Id field from an 'attribute', you don't have to touch more than one line of code.</li>
</ul>

<p>Is there a more readable/pythonic way to do this?</p>
