Tuesday, February 13, 2018

reading of chunk of data from the binary file and unpacking the data using struct construct in python

import struct

class parser():
    def __init__(self):
        self.data = ""        self.offset = 0
    def add_data(self,data):
        self.data = self.data[self.offset:]
        # print (self.data)        
        self.data += data
        self.offset = 0

    def parse_header(self):
        l,n = struct.unpack('>hh',self.data[self.offset: self.offset+4])
        if len(self.data) - self.offset >l:
            status = True        
        else:
            status = False
        return l,n,status



    def parse_QuoteORTrade_data(self):
        val,type = struct.unpack('>hs',self.data[self.offset:self.offset+3])
        # print(val,type)        
        self.update_offset(3)
        if(type == b'Q'):

            # sym,price,size = struct.unpack('>5shh',self.data[self.offset:self.offset+9])            self.update_offset(17)
            # print (sym,price,size)            
            self.update_offset(val-20)
        if(type == b'T'):

            sym,price,size = struct.unpack('>5shq',self.data[self.offset:self.offset+15])
            self.update_offset(15)
            print (sym,price,size)
            self.update_offset(val-18)




    def update_offset(self,offset):
        self.offset = self.offset+ offset

def ReadInChunk(fileobj,ChunkSize= 50):
    while True:
        data = fileobj.read(ChunkSize)
        if not data:
            break
        yield data



def read_file():
    f = open("input.dat","rb")
    return  f



if __name__ == "__main__" :
    offset_chunk = 0    obj=parser()
    fp = read_file()

    for chunk in ReadInChunk(fp):

        obj.add_data(chunk)
        # print (obj.data)       
        while True:
            try:
                length,num,status = obj.parse_header()


            except Exception,e:
                break            
            if(status == False):
                break
            obj.update_offset(4)
            for data in range(num):

                obj.parse_QuoteORTrade_data()





    fp.close()

No comments:

Post a Comment