title: Packing / unpacking complex data in Python
body: <p>I'm trying to send/receive data with an embedded device using a custom protocol. struct.pack() and unpack() seem like they'd do the job, but the fmt argument doesn't seem to be flexible enough to handle variable length arrays of data.</p>
  
  <p>Example: device sends [event report] [number of events] ([event type] [event value], [event type] [event value], ...)</p>
  
  <p>Where:</p>
  
  <ul>
  <li>event report (unsigned 8-bit)</li>
  <li>number of events (unsigned 16-bit)</li>
  <li>event type (unsigned 8-bit)</li>
  <li>event value (signed 16-bit)</li>
  </ul>
  
  <p>What would be the best way to decode this data into Python? Also, how would I encode similar data structures to send to the device?</p>