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

2015-08-30 Thread Mark Lawrence
".esnes sekam ti wonk uoY .daer ot ysae os sgniht ekam seod yllaer ti ,niaga gnitsop potrof s'knahT" On 30/08/2015 12:38, Sven R. Kunze wrote: Well, it comes down to "can he switch from JSON to YAML" in the first place as he is going to parse "by-third-party" generated json. So, if he can pers

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

2015-08-30 Thread Steven D'Aprano
On Sat, 29 Aug 2015 02:15 am, Robin Koch wrote: > Am 28.08.2015 um 18:09 schrieb Sven R. Kunze: > > >> I'm reading JSON output from an input file, and extracting values. > > >> for proper parsing into native Python types, I would recommend YAML. > > "What's the best way to get from A to B?" >

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

2015-08-30 Thread Sven R. Kunze
Well, it comes down to "can he switch from JSON to YAML" in the first place as he is going to parse "by-third-party" generated json. So, if he can persuade MongoDB to output YAML instead, he would be all fine. Just my 2 cents. On 28.08.2015 18:15, Robin Koch wrote: Am 28.08.2015 um 18:09 schr

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

2015-08-28 Thread Robin Koch
Am 28.08.2015 um 18:09 schrieb Sven R. Kunze: >> I'm reading JSON output from an input file, and extracting values. > for proper parsing into native Python types, I would recommend YAML. "What's the best way to get from A to B?" "I recommend starting at C." - Every other usenet-discussion. -

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

2015-08-28 Thread Sven R. Kunze
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 bre

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

2015-08-28 Thread Chris Angelico
On Fri, Aug 28, 2015 at 5:56 PM, Victor Hooi wrote: > > Currently I'm using this: > > def strip_floatApprox_wrapping(field): > # Extracts a integer value from a field. Workaround for the float_approx > wrapping. > if isinstance(field, dict): > return field['floatApprox'] > els

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

2015-08-28 Thread Victor Hooi
Hi, Thanks heaps to everybody for their suggestions/advice =). Currently I'm using this: def strip_floatApprox_wrapping(field): # Extracts a integer value from a field. Workaround for the float_approx wrapping. if isinstance(field, dict): return field['floatApprox'] else:

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

2015-08-27 Thread Jussi Piitulainen
Ben Finney writes: > Victor Hooi writes: [- -] >> For example: >> >> { >> "hostname": "example.com", >> "version": "3.0.5", >> "pid": { >> "floatApprox": 18403 >> } >> "network": { >> "bytesIn": 123123, >> "bytesOut": { >> "floatApprox": 2131

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

2015-08-27 Thread Jussi Piitulainen
Ben Finney writes: > Victor Hooi 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. I took the dist

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

2015-08-27 Thread Laura Creighton
I suspect your code will have these 2 lines in it somewhere ... if isinstance(field, dict): return int(field['floatApprox']) using isinstance() or type() is generally frowned upon because it breaks duck typing, and makes it necessary for you to write more code every time somebody wants to feed

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

2015-08-27 Thread random832
On Fri, Aug 28, 2015, at 00:57, 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. > Is there a way to re-write strip_flo

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

2015-08-27 Thread Jussi Piitulainen
Victor Hooi writes: > 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", > "versio

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

2015-08-27 Thread Ben Finney
Victor Hooi 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": "exam

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

2015-08-27 Thread Victor Hooi
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 s

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

2015-08-27 Thread Victor Hooi
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": { "fl