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


Groups > comp.lang.python > #95719

Re: Casting to a "number" (both int and float)?

Path csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!border2.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'json': 0.05; 'subject:number': 0.07; 'type,': 0.07; 'wrapped': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:both': 0.09; 'example:': 0.10; 'python': 0.10; 'python.': 0.11; 'output': 0.13; 'distinction': 0.16; 'input.': 0.16; 'means.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:)?': 0.16; 'instance,': 0.18; 'input': 0.18; 'all,': 0.20; 'meant': 0.22; 'int,': 0.22; 'appears': 0.23; 'examples': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'question': 0.27; 'values': 0.28; 'preserve': 0.29; 'question:': 0.29; 'relies': 0.29; "i'm": 0.30; 'code': 0.30; "i'd": 0.31; "i'll": 0.33; 'quite': 0.35; 'something': 0.35; "isn't": 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'possible.': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'received:org': 0.37; 'mean': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'show': 0.62; 'different': 0.63; 'great': 0.63; 'information': 0.63; 'other.': 0.64; 'skip:\xe2 10': 0.70; '8bit%:40': 0.72; '8bit%:43': 0.72; '_o__)': 0.84; 'casting': 0.84; 'casting,': 0.84; 'dict,': 0.84; 'fields,': 0.84; 'messed': 0.84; 'received:125': 0.84; 'subject:Casting': 0.84; 'victor': 0.84; '\xe2\x80\x94emo': 0.84; 'philips': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Casting to a "number" (both int and float)?
Date Fri, 28 Aug 2015 15:44:06 +1000
References <0bdda01a-de29-4742-9851-0617dad602ae@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock sha1:RlMSr4maUb9YBe5P/2QwXtA2xHM=
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>
Newsgroups comp.lang.python
Message-ID <mailman.96.1440740667.11709.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1440740667 news.xs4all.nl 23780 [2001:888:2000:d::a6]:42516
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:95719

Show key headers only | View raw


Victor Hooi <victorhooi@gmail.com> writes:

> Many of the fields are meant to be numerical, however, some fields are
> wrapped in a "floatApprox" dict, which messed with my parsing.

The examples you give of ‘floatApprox’ are not dicts, so I'm not sure
quite what that means.

> For example:
>
> {
>     "hostname": "example.com",
>     "version": "3.0.5",
>     "pid": {
>         "floatApprox": 18403
>     }
>     "network": {
>         "bytesIn": 123123,
>         "bytesOut": {
>             "floatApprox": 213123123
>         }
> }
>
> The floatApprox wrapping appears to happen sporadically in the input.

What do you mean by “wrapping”?

Can you show exactly what output values you would expect for these
example inputs?

> However, this relies on casting to int, which will only work for ints
> - for some fields, they may actually be floats, and I'd like to
> preserve that if possible.

(A terminology question: Note that “cast to int” isn't something that
happens in Python. You never “cast” an object as a different type, the
object is exactly what type it is and no other.

The example code you showed is not casting, but creating. Calling the
‘int’ type makes a new object of that type. Python doesn't have “cast”
as a concept.)

The question that needs to be answered is, how do you know what values
are “actually” floats?

In JSON there is no distinction at all, the only numeric type is
‘float’. What information is there in the input that can be used to know
which values should result in an ‘int’ instance, versus values that
should result in a ‘float’ instance?

-- 
 \                         “I'm a great lover, I'll bet.” —Emo Philips |
  `\                                                                   |
_o__)                                                                  |
Ben Finney

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


Thread

Casting to a "number" (both int and float)? Victor Hooi <victorhooi@gmail.com> - 2015-08-27 21:57 -0700
  Re: Casting to a "number" (both int and float)? Victor Hooi <victorhooi@gmail.com> - 2015-08-27 22:04 -0700
    Re: Casting to a "number" (both int and float)? "Sven R. Kunze" <srkunze@mail.de> - 2015-08-28 18:09 +0200
      Re: Casting to a "number" (both int and float)? Robin Koch <robin.koch@t-online.de> - 2015-08-28 18:15 +0200
        Re: Casting to a "number" (both int and float)? "Sven R. Kunze" <srkunze@mail.de> - 2015-08-30 13:38 +0200
        Re: Casting to a "number" (both int and float)? Steven D'Aprano <steve@pearwood.info> - 2015-08-30 22:00 +1000
        Re: Casting to a "number" (both int and float)? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-30 13:55 +0100
  Re: Casting to a "number" (both int and float)? Ben Finney <ben+python@benfinney.id.au> - 2015-08-28 15:44 +1000
    Re: Casting to a "number" (both int and float)? Jussi Piitulainen <harvestingn@makes.address.invalid> - 2015-08-28 09:03 +0300
    Re: Casting to a "number" (both int and float)? Jussi Piitulainen <harvesting@makes.address.invalid> - 2015-08-28 09:15 +0300
      Re: Casting to a "number" (both int and float)? Victor Hooi <victorhooi@gmail.com> - 2015-08-28 00:56 -0700
        Re: Casting to a "number" (both int and float)? Chris Angelico <rosuav@gmail.com> - 2015-08-28 19:23 +1000
  Re: Casting to a "number" (both int and float)? Jussi Piitulainen <harvesting@makes.address.invalid> - 2015-08-28 08:46 +0300
  Re: Casting to a "number" (both int and float)? random832@fastmail.us - 2015-08-28 01:54 -0400
  Re: Casting to a "number" (both int and float)? Laura Creighton <lac@openend.se> - 2015-08-28 07:59 +0200

csiph-web