[Tutor] Problem with logic while extracting data from binary file

Michael Langford mlangford.cs03 at gtalumni.org
Tue Apr 1 19:51:18 CEST 2008


I tried to extract methods, and invert your logic of your if/else statements
as to put shorter code blocks higher, and tried to roll many of your if
statements into the checks of the while loops. This is algorithm I ended up
with. Is this what you were trying to do? The last while loop is pointless,
as you unconditionally return from it.

    --Michael

def basicFields(data,start):
    group_num = data[start:start+2]
    element_num = data[start+2:start+4]
    vl_field = data[start+4:start+8]
    length = struct.unpack('hh', vl_field)[0]
    value = data[start+8:(start+8+length)]
    start = start+8+length
    element = group_num+element_num

    return (group_num,element_num,vl_field,length,value,start,element)

def parseSequence(data, start):
    group_num,element_num,vl_field,length,value,pos,element =
basicFields(data,start)

    MY_SEQ = '\xfe\xff\x00\xe0'

    while start < 536: #length:  # 536
        group_num,element_num,vl_field,length,value,start,element =
basicFields(data,start)
        if element != MY_SEQ:
            return element, start, value
        else:
            data = value
            while start < 112: #length:  # 112, 112, 116, 116
                group_num,element_num,vl_field,length,value,start,element =
basicFields(data,start)
                return element, start, value


On Fri, Mar 28, 2008 at 4:24 PM, Bryan Fodness <bryan.fodness at gmail.com>
wrote:

>
> Thanks again,
>
> Still lost, even with watching the variables.  I see that it kicks out of
> the loop, but don't understand what I have done to cause this.  I'm sorry
> for repeated emails, but I have spent multiple days on this.  I have added
> another while loop that I think should work, but I can't seem to keep it in
> the while loop.  I feel like I am getting close.
>
> It seems like it gets everything at the first level , but not the way I
> expected.  It seems to get the first value inside the first while loop, and
> then goes outside the loop to get the next three.  I would have expected it
> to return the values as it goes through the first while loop (since they are
> at the same level), then when it sees the nested identifier, go into the
> second while loop and return values.
>
> Any insight would be wonderful.
>
> def parseSequence(data, start):
>     group_num = data[start:start+2]
>     element_num = data[start+2:start+4]
>     vl_field = data[start+4:start+8]
>     length = struct.unpack('hh', vl_field)[0]
>     value = data[start+8:(start+8+length)]
>     pos = start+8+length
>     element = (group_num+element_num)
>     if element == '\xfe\xff\x00\xe0':
>         data = value
>         while start < 536: #length:  # 536
>             group_num = data[start:start+2]
>             element_num = data[start+2:start+4]
>             vl_field = data[start+4:start+8]
>             length = struct.unpack('hh', vl_field)[0]
>             value = data[start+8:(start+8+length)]
>             start = start+8+length
>             element = (group_num+element_num)
>             if element == '\xfe\xff\x00\xe0':
>                 data = value
>                 while start < 112: #length:  # 112, 112, 116, 116
>                     group_num = data[start:start+2]
>                     element_num = data[start+2:start+4]
>                     vl_field = data[start+4:start+8]
>                     length = struct.unpack('hh', vl_field)[0]
>                     value = data[start+8:(start+8+length)]
>                     start = start+8+length
>                     element = (group_num+element_num)
>                     return element, start, value
>             else:
>                 return element, start, value
>     else:
>         return  element, pos, value
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.RowdyLabs.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080401/30f48b01/attachment-0001.htm 


More information about the Tutor mailing list