Hi Mark,
> Comments welcome.
> Mark
>
>
> class ProtocolBufferFileReader:
> def __init__(self, input_filename, message_constructor):
> self.file = open(input_filename, 'rb')
> self.message_constructor = message_constructor
It may also be useful to modify this constructor to accept a "file" argument:
class ProtocolBufferFileReader:
def __init__(self, file, message_constructor):
self.file = file
self.message_constructor = message_constructor
So that you can call the class in two ways:
reader = ProtocolBufferFileReader(open(input_filename, 'rb'))
... or ...
from cStringIO import StringIO
reader = ProtocolBufferFileReader(StringIO(binary_data))
... where binary_data is a Python binary string created by
myMessage.SerializeToString()
This would be useful for receiving several protobuf messages over the
network at once (for example). It also means that you don't need to
modify the existing code in ProtocolBufferFileReader, since StringIO
also has the same read, write, seek, and tell functions as the file
class. It's a bit like using a stream in C++ I suppose.
Nick