Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'binary': 0.05; 'chunk': 0.07; 'subject:help': 0.07; 'python': 0.09; 'bytes,': 0.09; 'curve': 0.09; 'formatted': 0.09; 'generators': 0.09; 'read()': 0.09; 'library': 0.15; 'adjacent': 0.16; 'chunk)': 0.16; 'chunk:': 0.16; 'invocations': 0.16; 'iteration,': 0.16; 'next:': 0.16; 'stream.': 0.16; 'travis': 0.16; 'byte': 0.17; 'bytes': 0.17; 'detect': 0.17; 'code,': 0.18; 'module': 0.19; 'file.': 0.20; 'trying': 0.21; 'interpret': 0.22; 'split': 0.23; 'idea': 0.24; 'script': 0.24; 'looks': 0.26; 'values': 0.26; 'bugs': 0.27; 'received:209.85.210.46': 0.27; "doesn't": 0.28; 'lines': 0.28; 'attempting': 0.29; 'signatures': 0.29; 'styles': 0.29; "i'm": 0.29; "we're": 0.30; 'that.': 0.30; 'expect': 0.31; 'code': 0.31; 'skip:s 30': 0.33; 'like:': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; "can't": 0.34; 'skip:b 20': 0.34; 'received:google.com': 0.34; 'received:172.16': 0.34; 'self': 0.34; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'next': 0.35; 'but': 0.36; 'message-id:@gmail.com': 0.36; '(i.e.': 0.36; 'charset:us-ascii': 0.36; 'does': 0.37; 'two': 0.37; 'skip:v 20': 0.37; 'received:209': 0.37; 'some': 0.38; 'to:addr:python.org': 0.39; 'subject:-': 0.40; 'header:Received:5': 0.40; 'help': 0.40; 'end': 0.40; 'map': 0.61; 'first': 0.61; 'header:Message-Id:1': 0.62; 'different': 0.63; 'success': 0.63; 'more': 0.63; 'other.': 0.64; 'sit': 0.65; 'vital': 0.75; 'add:': 0.84; 'belt,': 0.84; 'done."': 0.84; 'gap': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:content-type:content-transfer-encoding:subject:message-id:date :to:mime-version:x-mailer; bh=rbWkCWrZbMJTRDq5iXI8Is4n4AqA+nDlmZ2kxlFL31g=; b=NMEjq0QpvAbO5agtDDVXUdHPFuuEvztIae9EWoNUs/LZAFBdrMzgVZDyY7HlTSph1D ym9LSbYkqmSCdpIFTCJZEFdf+4iG/BfbUTd9Jfdh+36xKl/Y2zyZxqe7iSlCB5RVGquw 8WJsvy90Hkf3rDV2STijYNj8ma6dfF0P4MfKHtj/yUgmsqWEBlivTDJsDdoz68SaSNtI UuNenVaDF6ab6wwdv6CwznFUU5nVvHfXh0/UKxCkCGaHrBzeKleY8Eu8NGXdLLUtaAeH 8WAHQQIMVdCw590j+FCRj32+Qlu20CWJXPCH/ibvP6/A1VYP32D6VrvwpmppTgobcDhb rCOw== From: Travis Griggs Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: Style help for a Smalltalk-hack Date: Mon, 22 Oct 2012 17:43:00 -0700 To: python-list@python.org Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) X-Mailer: Apple Mail (2.1499) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350952985 news.xs4all.nl 6844 [2001:888:2000:d::a6]:54566 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31911 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 =3D byteStream.read(4) while chunk: # interpret the gap bytes gap, =3D struct.unpack('>I', chunk) # suck off the valveCount valveCount, =3D struct.unpack('>I', byteStream.read(4)) # collect the next valveCount signatures signatures =3D [struct.unpack('>I', byteStream.read(4))[0] for _ in = range(valveCount)] self.script.append(ScriptSpan(gap=3Dgap, valveSet=3Dsignatures)) # now get the next 4 bytes for the gap of the next iteration, it'll = be empty if we're at end chunk =3D 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 :=3D (byteStream next: 4) asInteger. valveCount :=3D (byteStream next: 4) asInteger. signatures :=3D (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