Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Problems using struct pack/unpack in files, and reading them. Date: Fri, 13 Nov 2015 12:45:42 -0700 Lines: 37 Message-ID: References: <20151113192045.GA9913@z-sverige.nu> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 67OJa7IxlLw5UUH5UwulfAnTnExT8A1v3xjuuZowywYA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'error:': 0.05; 'second.': 0.09; 'subject:files': 0.09; 'subject:using': 0.09; 'exception': 0.13; 'variables': 0.15; '#we': 0.16; 'better:': 0.16; 'operators,': 0.16; 'printing.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Problems': 0.16; 'unnecessary.': 0.16; 'wrote:': 0.16; 'bytes': 0.18; '2015': 0.20; 'first,': 0.20; 'meant': 0.22; 'pass': 0.22; 'bit': 0.23; 'sets': 0.23; "haven't": 0.24; 'header:In-Reply-To:1': 0.24; 'command': 0.26; '(which': 0.26; 'points': 0.27; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'said,': 0.27; '13,': 0.29; 'received:209.85.213.174': 0.29; 'spaces': 0.29; 'raise': 0.29; "i'm": 0.30; 'subject:/': 0.30; 'error.': 0.31; 'probably': 0.31; 'raised': 0.33; 'file': 0.34; 'received:google.com': 0.35; 'skip:c 30': 0.35; 'nov': 0.35; 'set.': 0.35; 'comment': 0.35; 'instead': 0.36; 'lines': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'skip:p 20': 0.38; 'why': 0.39; 'easily': 0.39; 'to:addr:python.org': 0.40; 'field': 0.60; 'ever': 0.60; 'caused': 0.61; 'replying': 0.61; 'per': 0.62; 'here.': 0.62; 'more': 0.63; '12:20': 0.84; 'kent': 0.84; 'subject:pack': 0.84; 'to:name:python': 0.84; 'write:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=w/YJBqmA+h4Rd3qWsRZKHxJ8HxpWiDgyzIDVr69tjMo=; b=UnkMZDLegTzvc35Ce929TD4yqTDb7Ri/tARgEqiyIyRknIlSLnyGKTGG+wYEy/0Pdg HDXHeFgpI1r49VPOlnXcOU/g2MbB18WMZwXCWvB0JcxIHyICSU3R42mN1/cSY3hiETcc YPfREBjTH5yCpv+NAeuuOijxiuBy/Vf9IxTGouhYwxwvkrxwZkXPpcgUthc7KSQQT5XQ KF+o+WVuW1oAuIwMwkTpLvjVnfQKfwbZtizB7VCAuejk11xXcOuGliXyMrTgRK5dq1dl cuDUtsPsBEAPXevvu0uOba9+gyR5yEuegyKfKFvsyyAbEsPH1FUx1ShOKJ5vD/aztDHH pM3A== X-Received: by 10.50.136.132 with SMTP id qa4mr5092668igb.68.1447443981684; Fri, 13 Nov 2015 11:46:21 -0800 (PST) In-Reply-To: <20151113192045.GA9913@z-sverige.nu> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98759 As long as I'm replying to this, I see a few more issues to comment on: On Fri, Nov 13, 2015 at 12:20 PM, kent nyberg wrote: > if place_to_read.closed: > print("Drive error. Drive closed.") You probably also want to break or return here. Even better: raise an exception instead of printing. That said, why would this ever be closed here, since you just read from it two lines prior (which would have raised an exception if the file were closed), and you haven't closed it in the meantime? > if checkfirstbit(klar[RegisterAX]): > print("First bit is set. Move Cmd.") > if (klar[0] & 0b0111111111111111): > print("Cmd ERROR: Multiple commands is set.") Why do you pass the commands in a bit field if you're not going to allow multiple commands to be set? Also, see above about break/return/exception. > pass Unnecessary. > #Change RegisterAX offset+2bytes, > RegisterAX=+2 #We read two bytes per cycle. Becaus command is first, and variables are second. This sets RegisterAX to 2. Specifically, positive 2. If you want to *add* 2, you probably meant to write: RegisterAX += 2 This also points out a good reason for using spaces around operators, as "RegisterAX =+ 2" would have caused a SyntaxError and been more easily detectable.