Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Style help for a Smalltalk-hack

Reply
Thread Tools

Style help for a Smalltalk-hack

 
 
Travis Griggs
Guest
Posts: n/a
 
      10-23-2012
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

--
Travis Griggs
"A vital ingredient of success is not knowing that what you're attempting can't be done." -Terry Pratchett

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid header style inconsistent with sortable column style cedoucette@alum.rpi.edu ASP .Net 0 10-14-2005 12:13 AM
All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer Rob Nicholson ASP .Net 3 05-28-2005 03:11 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Javascript Style Switcher that remebers current site style in use Hardeep Rakhra HTML 8 01-15-2004 08:00 PM
Style sheets, include one style within another (not inheritance) foldface@yahoo.co.uk HTML 1 11-24-2003 01:37 PM



Advertisments