Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98885

Re: Problems using struct pack/unpack in files, and reading them.

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Problems using struct pack/unpack in files, and reading them.
Date Tue, 17 Nov 2015 00:27:55 +1100
Lines 58
Message-ID <mailman.365.1447680484.16136.python-list@python.org> (permalink)
References <20151113192045.GA9913@z-sverige.nu> <CALwzidknLk3w8NWJqhYXjCFy4uBm79dxhBn=oaXCtxfJu-d-_g@mail.gmail.com> <mailman.310.1447454532.16136.python-list@python.org> <56469f14$0$1612$c3e8da3$5496439d@news.astraweb.com> <mailman.314.1447470111.16136.python-list@python.org> <5646c95a$0$1597$c3e8da3$5496439d@news.astraweb.com> <mailman.329.1447515842.16136.python-list@python.org> <5647e90c$0$1588$c3e8da3$5496439d@news.astraweb.com> <mailman.342.1447554248.16136.python-list@python.org> <5649d77a$0$1585$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de nP6MB8aDFNYgW00UYn+OEA2OaOb0Hm/OYb9Dp6Rj80Cw==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'valueerror:': 0.07; 'cc:addr:python-list': 0.09; 'assumed': 0.09; 'check.': 0.09; 'skip:0 70': 0.09; 'skip:1 70': 0.09; 'subject:files': 0.09; 'subject:using': 0.09; '1:08': 0.16; 'ah!': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'inf': 0.16; 'reason.': 0.16; 'received:209.85.213.176': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Problems': 0.16; 'wrote:': 0.16; 'obviously': 0.16; 'string': 0.17; 'try:': 0.18; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'first,': 0.20; '"",': 0.22; 'assumes': 0.22; 'am,': 0.23; 'unlike': 0.23; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'sense': 0.26; 'chris': 0.26; 'right.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'change,': 0.27; 'then.': 0.27; 'this.': 0.28; 'fine': 0.28; 'convert': 0.29; 'subject:/': 0.30; 'code': 0.30; '15,': 0.30; "i'd": 0.31; "d'aprano": 0.33; 'int': 0.33; 'steven': 0.33; 'traceback': 0.33; 'tue,': 0.34; 'file': 0.34; 'except': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'text': 0.35; 'nov': 0.35; 'sometimes': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'some': 0.40; 'ever': 0.60; 'your': 0.60; 'more': 0.63; 'different': 0.63; 'between': 0.65; 'number:': 0.69; 'chrisa': 0.84; 'confusion.': 0.84; 'distinguish': 0.84; 'subject:pack': 0.84; 'this...': 0.84; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=abv1k/gqGAqWor5MsL9HFeehXhgpOAizvt+gXnx2JtQ=; b=mLglIKNcNGblNncN4MTPJs7ogJNURFteVMM/xcWOT6I6bNGxjT2+8G/kxDiM/9JBNi ibYdYxmjCzW5VccZQExmJ0bagWjEioOUIw5pE4HSVP2/8ZKl/U/THXYnkHRzNlrlbpLC yXxywK7eubmwvTBavnPwOvq76jkTkRqDmDDQeQUzPu1g2zjS777hSwrLw6+xYfjxKqBW ac3aCOcC4NuWNoH8I/6YYpXebXTBqdEvqmUzRuu5k5fCOZz5sFO37BvSdJsOoEz42kWV 5btiMzaW/KxJXxN7YevBsUypUhc8ee/d8qTSLEQFZtvzzhQ28ucnQ5xpYgJne+ifEim/ afhQ==
X-Received by 10.50.87.71 with SMTP id v7mr11321171igz.92.1447680475846; Mon, 16 Nov 2015 05:27:55 -0800 (PST)
In-Reply-To <5649d77a$0$1585$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:98885

Show key headers only | View raw


On Tue, Nov 17, 2015 at 12:17 AM, Steven D'Aprano <steve@pearwood.info> wrote:
> On Sun, 15 Nov 2015 01:23 pm, Chris Angelico wrote:
>
>> On Sun, Nov 15, 2015 at 1:08 PM, Steven D'Aprano <steve@pearwood.info>
>> wrote:
>>> number = +raw_input("enter a number: ")
>>>
>>> versus:
>>>
>>> text = raw_input("enter a number: ")
>>> try:
>>>     number = float(text)
>>> except ValueError:
>>>     number = int(text)
>>
>> What kinds of strings can float() not handle but int() can,
>
> Heh, I think I got the order of them backwards. You should try to convert to
> int first, and if that fails, try float.

Ah! Yes, that makes sense then. I assumed this...

>> and in a
>> program that's going to group floats and ints together as "numbers",
>> will they ever be useful? I'd be more likely to write this as simply:

... on the basis of the "float first" check.

> Obviously this code assumes you want to distinguish between ints and floats
> for some reason. In Python, unlike Lua, Javascript and a few others, we do
> distinguish between ints and floats. Since they have different
> capabilities, that may sometimes be useful:
>
>
> py> 10.0 ** 400
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> OverflowError: (34, 'Numerical result out of range')
> py> 10 ** 400
> 1000000000000000000000000000000000000000000000000000000000000000000000000
> 0000000000000000000000000000000000000000000000000000000000000000000000000
> 0000000000000000000000000000000000000000000000000000000000000000000000000
> 0000000000000000000000000000000000000000000000000000000000000000000000000
> 0000000000000000000000000000000000000000000000000000000000000000000000000
> 000000000000000000000000000000000000

Right. If you check int() first and then float(), you can make use of
this. But if the user enters that string and you try to float() it, it
will work:

>>> float(str(int(10**400)))
inf

Hence my confusion. :)

With that small change, your code makes fine sense.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Problems using struct pack/unpack in files, and reading them. Chris Angelico <rosuav@gmail.com> - 2015-11-14 09:42 +1100
  Re: Problems using struct pack/unpack in files, and reading them. Steven D'Aprano <steve@pearwood.info> - 2015-11-14 13:40 +1100
    Re: Problems using struct pack/unpack in files, and reading them. Chris Angelico <rosuav@gmail.com> - 2015-11-14 14:01 +1100
      Re: Problems using struct pack/unpack in files, and reading them. Steven D'Aprano <steve@pearwood.info> - 2015-11-14 16:40 +1100
        Re: Problems using struct pack/unpack in files, and reading them. Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-14 08:43 -0700
          Re: Problems using struct pack/unpack in files, and reading them. Marko Rauhamaa <marko@pacujo.net> - 2015-11-14 18:52 +0200
            Re: Problems using struct pack/unpack in files, and reading them. Chris Angelico <rosuav@gmail.com> - 2015-11-15 04:04 +1100
              Re: Problems using struct pack/unpack in files, and reading them. Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-11-16 19:19 +1300
            Re: Problems using struct pack/unpack in files, and reading them. Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-14 10:04 -0700
              Re: Problems using struct pack/unpack in files, and reading them. Marko Rauhamaa <marko@pacujo.net> - 2015-11-14 23:53 +0200
              Re: Problems using struct pack/unpack in files, and reading them. Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-11-16 19:15 +1300
                Re: Problems using struct pack/unpack in files, and reading them. Steven D'Aprano <steve@pearwood.info> - 2015-11-16 23:11 +1100
                Re: Problems using struct pack/unpack in files, and reading them. Dave Farrance <df@see.replyto.invalid> - 2015-11-17 09:54 +0000
            Re: Problems using struct pack/unpack in files, and reading them. Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-14 10:13 -0700
            Re: Problems using struct pack/unpack in files, and reading them. Random832 <random832@fastmail.com> - 2015-11-14 17:53 -0500
              Re: Problems using struct pack/unpack in files, and reading them. Marko Rauhamaa <marko@pacujo.net> - 2015-11-15 02:12 +0200
              Re: Problems using struct pack/unpack in files, and reading them. Steven D'Aprano <steve@pearwood.info> - 2015-11-15 12:14 +1100
                Re: Problems using struct pack/unpack in files, and reading them. Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-15 11:09 -0500
                Re: Problems using struct pack/unpack in files, and reading them. Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-11-16 19:11 +1300
            Re: Problems using struct pack/unpack in files, and reading them. MRAB <python@mrabarnett.plus.com> - 2015-11-15 00:49 +0000
          Re: Problems using struct pack/unpack in files, and reading them. Steven D'Aprano <steve@pearwood.info> - 2015-11-15 13:08 +1100
            Re: Problems using struct pack/unpack in files, and reading them. Chris Angelico <rosuav@gmail.com> - 2015-11-15 13:23 +1100
              Re: Problems using struct pack/unpack in files, and reading them. Steven D'Aprano <steve@pearwood.info> - 2015-11-17 00:17 +1100
                Re: Problems using struct pack/unpack in files, and reading them. Chris Angelico <rosuav@gmail.com> - 2015-11-17 00:27 +1100
        Re: Problems using struct pack/unpack in files, and reading them. Chris Angelico <rosuav@gmail.com> - 2015-11-15 03:59 +1100
    Re: Problems using struct pack/unpack in files, and reading them. Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-13 20:45 -0700
    Re: Problems using struct pack/unpack in files, and reading them. Ian Kelly <ian.g.kelly@gmail.com> - 2015-11-13 20:48 -0700
    Re: Problems using struct pack/unpack in files, and reading them. Chris Angelico <rosuav@gmail.com> - 2015-11-14 14:56 +1100
    Re: Problems using struct pack/unpack in files, and reading them. Chris Angelico <rosuav@gmail.com> - 2015-11-14 14:57 +1100

csiph-web