[Tutor] Design Question: File Object used everywhere

Jan Jansen knacktus at googlemail.com
Fri May 14 06:49:59 CEST 2010


Hi there,

I'm working on a code to read and write large amounts of binary data 
according to a given specification. In the specification there are a lot 
of "segments" defined. The segments in turn have defintions of datatypes 
and what they represent, how many of some of the data values are present 
in the file and sometimes the offset from the beginning of the file.

Now I wonder, what would be a good way to model the code.

Currently I have one class, that is the "FileReader". This class holds 
the file object, information about the endianess and also a method to 
read data (using the struct module). Then, I have more classes 
representing the segements. In those classes I define data-formats, call 
the read-method of the FileReader object and hold the data. Currently 
I'm passing the FileReader object as arguement.

Here some examples, first the "FileReader" class:

class JTFile():

     def __init__(self, file_obj):
         self.file_stream = file_obj
         self.version_string = ""
         self.endian_format_prefix = ""

     def read_data(self, fmt, pos = None):
         format_size = struct.calcsize(fmt)
         if pos is not None:
             self.file_stream.seek(pos)
         return struct.unpack_from(self.endian_format_prefix + fmt, 
self.file_stream.read(format_size))

and here an example for a segment class that uses a FileReader instance 
(file_stream):

class LSGSegement():

     def __init__(self, file_stream):
         self.file_stream = file_stream
         self.lsg_root_element = None
         self._read_lsg_root()

     def _read_lsg_root(self):
         fmt = "80Bi"
         raw_data = self.file_stream.read_data(fmt)
         self.lsg_root_element = LSGRootElement(raw_data[:79], raw_data[79])

So, now I wonder, what would be a good pythonic way to model the 
FileReader class. Maybe use a global functions to avoid passing the 
FileReader object around? Or something like "Singleton" I've heard about 
but never used it? Or keept it like that?

Cheers,

Jan



More information about the Tutor mailing list