title: Encode an object with Avro to a byte array in Python
body: <p>In python 2.7, using Avro, I'd like to encode an object to a byte array.</p>
  
  <p>All examples I've found write to a file.</p>
  
  <p>I've tried using io.BytesIO() but this gives:</p>
  
  <pre><code>AttributeError: '_io.BytesIO' object has no attribute 'write_long'
  </code></pre>
  
  <p>Sample using io.BytesIO</p>
  
  <pre class="lang-python prettyprint-override"><code>def avro_encode(raw, schema):
      writer = DatumWriter(schema)
      avro_buffer = io.BytesIO()
      writer.write(raw, avro_buffer)
      return avro_buffer.getvalue()
  </code></pre>