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


Groups > comp.lang.python > #95737

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

Path csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!bcyclone03.am1.xlned.com!bcyclone03.am1.xlned.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <srkunze@mail.de>
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; 'json': 0.05; 'elegant': 0.07; 'friday,': 0.07; 'subject:number': 0.07; 'wrapped': 0.07; 'dict': 0.09; 'exception,': 0.09; 'format?': 0.09; 'subject:both': 0.09; 'typeerror:': 0.09; 'example:': 0.10; 'python': 0.10; 'output': 0.13; 'def': 0.13; 'file,': 0.15; '(besides': 0.16; 'input.': 0.16; 're-write': 0.16; 'subject:)?': 0.16; 'wrote:': 0.16; 'string': 0.17; 'integer': 0.18; 'try:': 0.18; 'input': 0.18; 'hey': 0.20; '2015': 0.20; 'to:2**1': 0.21; 'meant': 0.22; 'function:': 0.22; 'int,': 0.22; 'parsing': 0.22; 'cheers,': 0.22; 'appears': 0.23; 'bit': 0.23; 'seems': 0.23; 'header:In-Reply- To:1': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'to:no real name:2**1': 0.27; '(although': 0.29; 'preserve': 0.29; 'relies': 0.29; 'workaround': 0.29; "i'm": 0.30; 'field,': 0.30; "i'd": 0.31; 'skip:s 30': 0.31; 'int': 0.33; 'values.': 0.33; 'except': 0.34; 'handle': 0.34; 'received:10.0': 0.34; 'supports': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'there': 0.36; 'possible.': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'test': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'field': 0.60; 'charset:windows-1252': 0.62; 'more': 0.63; 'cast': 0.66; 'august': 0.75; 'actually,': 0.84; 'casting': 0.84; 'dict,': 0.84; 'fields,': 0.84; 'messed': 0.84; 'subject:Casting': 0.84; 'victor': 0.84
Date Fri, 28 Aug 2015 18:09:27 +0200
From "Sven R. Kunze" <srkunze@mail.de>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0
MIME-Version 1.0
To python-list@python.org, victorhooi@gmail.com
Subject Re: Casting to a "number" (both int and float)?
References <0bdda01a-de29-4742-9851-0617dad602ae@googlegroups.com> <661c74bf-d038-41be-9519-2260f24ab8e2@googlegroups.com>
In-Reply-To <661c74bf-d038-41be-9519-2260f24ab8e2@googlegroups.com>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-purgate clean
X-purgate This mail is considered clean (visit http://www.eleven.de for further information)
X-purgate-type clean
X-purgate-Ad Categorized by eleven eXpurgate (R) http://www.eleven.de
X-purgate This mail is considered clean (visit http://www.eleven.de for further information)
X-purgate clean
X-purgate-size 2554
X-purgate-ID 154282::1440778170-000007B2-F0E458B5/0/0
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.110.1440778178.11709.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1440778178 news.xs4all.nl 23764 [2001:888:2000:d::a6]:59054
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 6083
X-Received-Body-CRC 3296563241
Xref csiph.com comp.lang.python:95737

Show key headers only | View raw


Hey Victor,

for proper parsing into native Python types, I would recommend YAML.

Also also supports (besides int vs. float) dates and datetimes.

Cheers,
Sven

On 28.08.2015 07:04, Victor Hooi wrote:
> Actually, I've just realised, if I just test for numeric or try to cast to ints, this will break for string fields.
>
> As in, the intention is to call strip_floatAprox_wrapping on all the fields I'm parsing, and have it deal with the floatApprox dict wrapping, whether the contents are numbers or strings (although strings would not be wrapped in floatApprox).
>
> On Friday, 28 August 2015 14:58:01 UTC+10, Victor Hooi  wrote:
>> I'm reading JSON output from an input file, and extracting values.
>>
>> Many of the fields are meant to be numerical, however, some fields are wrapped in a "floatApprox" dict, which messed with my parsing.
>>
>> 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.
>>
>> I'd like to find a way to deal with this robustly.
>>
>> For example, I have the following function:
>>
>> def strip_floatApprox_wrapping(field):
>>      # Extracts a integer value from a field. Workaround for the float_approx wrapping.
>>      try:
>>          return int(field)
>>      except TypeError:
>>          return int(field['floatApprox'])
>>
>> which I can then call on each field I want to extract.
>>
>> 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.
>>
>> (I know there's a isnumber() field - but you can only call that on a string - so if I do hit a floatApprox field, it will trigger a AttributeError exception, which seems a bit clunky to handle).
>>
>> def strip_floatApprox_wrapping(field):
>>      # Extracts a integer value from a field. Workaround for the float_approx wrapping.
>>      try:
>>          if field.isnumeric():
>>              return field
>>      except AttributeError:
>>          return field['floatApprox']
>>
>> Is there a way to re-write strip_floatApprox_wrapping to handle both ints/floats, and preserve the original format?
>>
>> Or is there a more elegant way to deal with the arbitrary nesting with floatApprox?

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