Re: Style help for a Smalltalk-hack

Discussion in 'Python' started by MRAB, Oct 23, 2012.

  1. MRAB

    MRAB Guest

    On 2012-10-23 01:43, Travis Griggs wrote:
    > I'm writing some code that does a structured read from formatted binary file. The code I came up with looks like:
    >
    > # get the first four bytes, the first gap field
    > chunk = byteStream.read(4)
    > while chunk:
    > # interpret the gap bytes
    > gap, = struct.unpack('>I', chunk)
    > # suck off the valveCount
    > valveCount, = struct.unpack('>I', byteStream.read(4))
    > # collect the next valveCount signatures
    > signatures = [struct.unpack('>I', byteStream.read(4))[0] for _ in range(valveCount)]
    > self.script.append(ScriptSpan(gap=gap, valveSet=signatures))
    > # now get the next 4 bytes for the gap of the next iteration, it'll be empty if we're at end
    > chunk = byteStream.read(4)
    >
    > I can't help but thinking that there's some better way (i.e. more pythonic) to do this that doesn't involve having to use another module (Construct) or exploring generators or something like that. What bugs me about it is that there is two different styles for reading/decoding values from the byte stream. valveCount and signatures are both paired invocations of unpack() and read(). But to detect the end of the stream (file), I have to split the read() and unpack() of the gap value across 3 different lines of the code, and they don't even sit adjacent to each other.
    >
    > I'm wandering up the Python curve with a passel of Smalltalk experience under my belt, so I expect I'm struggling with trying to map something like this across to python
    >
    > [byteStream atEnd] whileFalse: [
    > gap := (byteStream next: 4) asInteger.
    > valveCount := (byteStream next: 4) asInteger.
    > signatures := (1 to: valveCount) collect: [:_ | (byteStream next: 4) asInteger].
    > self script add: (ScriptSpan gap: gap valveSet: signatures).
    > ]
    >
    > The part that doesn't seem to be there in the standard python library is the idea of an atEnd message for streams, it's inferred as a byproduct of a read().
    >
    > Please be gentle/kind. I'm still learning. :) TIA
    >

    Another way you could do it is:

    while True:
    chunk = byteStream.read(4)
    if not chunk:
    break
    ...

    And you could fetch multiple signatures in one read:

    signatures = list(struct.unpack('>{}I'.format(valveCount),
    byteStream.read(4 * valueCount)))

    By the way, in Python the recommended style for variable names (well,
    what you'd call a 'variable' in other languages :)) is lowercase with
    underscores, e.g. "byte_stream".
     
    MRAB, Oct 23, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Travis Griggs

    Style help for a Smalltalk-hack

    Travis Griggs, Oct 23, 2012, in forum: Python
    Replies:
    0
    Views:
    131
    Travis Griggs
    Oct 23, 2012
  2. Dennis Lee Bieber

    Re: Style help for a Smalltalk-hack

    Dennis Lee Bieber, Oct 23, 2012, in forum: Python
    Replies:
    0
    Views:
    232
    Dennis Lee Bieber
    Oct 23, 2012
  3. Travis Griggs

    Re: Style help for a Smalltalk-hack

    Travis Griggs, Oct 23, 2012, in forum: Python
    Replies:
    0
    Views:
    151
    Travis Griggs
    Oct 23, 2012
  4. Travis Griggs

    Re: Style help for a Smalltalk-hack

    Travis Griggs, Oct 23, 2012, in forum: Python
    Replies:
    0
    Views:
    155
    Travis Griggs
    Oct 23, 2012
  5. Ian Kelly

    Re: Style help for a Smalltalk-hack

    Ian Kelly, Oct 23, 2012, in forum: Python
    Replies:
    0
    Views:
    163
    Ian Kelly
    Oct 23, 2012
Loading...

Share This Page