Path: csiph.com!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: 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; 'subject:number': 0.07; 'wrapped': 0.07; 'format?': 0.09; 'received:internal': 0.09; 'subject:both': 0.09; 'output': 0.13; 'def': 0.13; 'file,': 0.15; 'things.': 0.15; 'googled': 0.16; 'len(value)': 0.16; 'message-id:@webmail.messagingengine.com': 0.16; 're-write': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:10.202.2.212': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:messagingengine.com': 0.16; 'subject:)?': 0.16; 'value:': 0.16; 'wrote:': 0.16; 'circular': 0.18; 'input': 0.18; 'aug': 0.20; 'to:2**1': 0.21; 'meant': 0.22; 'function,': 0.22; 'references': 0.23; 'represents': 0.23; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'fri,': 0.27; 'possibility': 0.27; "skip:' 10": 0.28; 'looks': 0.29; 'dictionary': 0.29; 'key,': 0.29; 'preserve': 0.29; "i'm": 0.30; "i'd": 0.31; 'probably': 0.31; 'values.': 0.33; 'handle': 0.34; 'add': 0.34; 'could': 0.35; 'should': 0.36; 'there': 0.36; 'skip:{ 10': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'skip:v 20': 0.38; 'received:66': 0.38; "didn't": 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'from:no real name:2**0': 0.60; 'ever': 0.60; "you'll": 0.61; 'header:Message-Id:1': 0.61; 'making': 0.62; 'more': 0.63; 'walk': 0.72; 'dict,': 0.84; 'messed': 0.84; 'subject:Casting': 0.84; 'victor': 0.84 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.us; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=4zBRlwjGr9llwuVE96oo0i8ue9U=; b=AH87IO tabqhHMogB0PJFIshHKFHP/fFEutBjGa7Roqr0Bjwi4Reh11tgvHmfjJ6/3MWQt8 fYLvoUmv8DRl7MA3XUQ8zHOLaeTd2zqA5QslzWcD6AIRqKLln7acKgHXVZK0bKSv jA9SsX+XDQ4/of9yDSixthtac3eoSBzXwL4K4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=4zBRlwjGr9llwuV E96oo0i8ue9U=; b=LMF0aEck4GyLDBJiNARExoC08H0sAQ5ksg/26vgnlp+i5ev fUrOKC8DUgub2dhV3ZBugzo8fl7KPPDRqS9qVxhInYXd/n0MJesNPqvu5qz4DVpC Y2ZZFE6DFhdNFGFl6F4kgUqWV1RnZSPMd/oJrDTnUaVkf24dl/5LR5Z8fOQk= X-Sasl-Enc: +fww9EMPOXgk/C5X/ijg2sEXMmieelvDb2KypVdAshkQ 1440741272 From: random832@fastmail.us To: Victor Hooi , python-list@python.org MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain X-Mailer: MessagingEngine.com Webmail Interface - html Subject: Re: Casting to a "number" (both int and float)? Date: Fri, 28 Aug 2015 01:54:32 -0400 In-Reply-To: <0bdda01a-de29-4742-9851-0617dad602ae@googlegroups.com> References: <0bdda01a-de29-4742-9851-0617dad602ae@googlegroups.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1440741274 news.xs4all.nl 23782 [2001:888:2000:d::a6]:47970 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95721 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_floatApprox_wrapping to handle both > ints/floats, and preserve the original format? You could do simply isinstance(field, (int, float)). > Or is there a more elegant way to deal with the arbitrary nesting with > floatApprox? I'd probably just recursively walk through the dictionary finding these and removing them. def clean_floatapprox(obj): if type(obj) is dict: for key, value in obj.items(): if type(value) is dict: if len(value) == 1 and 'floatApprox' in value: obj[key] = value['floatApprox'] else clean_floatapprox(obj) Since it's a JSON result I didn't bother with making a copy or dealing with the possibility of circular references for this example function, so you'll need to add to this if you have to deal with those things. Incidentally, do you ever get one that represents a very large value and has more fields than just floatApprox? I googled this and it looks like it's a MongoDB thing. The example given is {"floatApprox" : 9223372036854776000, "top" : 2147483647, "bottom" : 4294967295} Actually representing the value 9223372036854775807. If top and bottom are found the result should be top << 32 | bottom. I don't know how large negative numbers are represented.