Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '(at': 0.03; 'elif': 0.04; 'case.': 0.05; 'none,': 0.05; 'python)': 0.05; 'json': 0.07; 'level,': 0.07; 'null,': 0.07; 'parser': 0.07; 'parsing': 0.07; 'api': 0.09; 'python': 0.09; 'dict': 0.09; 'itself,': 0.09; 'none.': 0.09; 'objects.': 0.09; 'parsed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'structure,': 0.09; 'def': 0.10; 'aug': 0.13; '(str,': 0.16; "`str'": 0.16; 'arbitrarily': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'strs': 0.16; 'two.': 0.16; 'url:json': 0.16; "{'foo':": 0.16; 'string': 0.17; 'basically': 0.17; 'thu,': 0.17; '>>>': 0.18; 'import': 0.21; '"",': 0.22; 'int,': 0.22; 'latter': 0.22; 'parse': 0.22; 'subject:skip:i 10': 0.22; 'tuples': 0.22; "i'd": 0.22; 'pass': 0.25; 'least': 0.25; '???': 0.27; 'forgot': 0.27; 'merge': 0.27; 'header:X-Complaints- To:1': 0.28; 'dictionary': 0.29; 'far,': 0.29; 'obj': 0.29; 'so- called': 0.29; 'strings,': 0.29; 'thus,': 0.29; 'character': 0.29; 'this.': 0.29; "i'm": 0.29; 'classes': 0.30; 'query': 0.30; 'returned': 0.30; 'function': 0.30; 'point': 0.31; 'structure': 0.32; 'print': 0.32; '+0200,': 0.33; 'extract': 0.33; 'url:home': 0.33; 'to:addr:python-list': 0.33; 'equal': 0.33; 'another': 0.33; "can't": 0.34; 'thanks': 0.34; 'list': 0.35; 'nature': 0.35; 'so,': 0.35; 'sometimes': 0.35; 'received:org': 0.36; 'but': 0.36; 'url:org': 0.36; 'skip:{ 10': 0.36; 'should': 0.36; 'too': 0.36; 'charset:us-ascii': 0.36; 'itself': 0.37; 'option': 0.37; 'item': 0.37; 'quite': 0.37; 'rather': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'companies': 0.60; 'first': 0.61; 'thomas': 0.62; 'walk': 0.71; 'hand': 0.82; "'foo'": 0.84; 'dennis': 0.91; 'poorly': 0.93; 'examine': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Dynamically determine base classes on instantiation Date: Thu, 16 Aug 2012 12:29:21 -0400 Organization: > Bestiaria Support Staff < References: <502c3bc2$0$29978$c3e8da3$5496439d@news.astraweb.com> <20120816125230.GA6195@roku> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-76-253-107-45.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 91 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1345134562 news.xs4all.nl 6983 [2001:888:2000:d::a6]:39276 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27177 On Thu, 16 Aug 2012 14:52:30 +0200, Thomas Bach declaimed the following in gmane.comp.python.general: > Thanks for the clarification on that one. So, here's the use-case: I'm > querying the crunchbase API which returns JSON data and is rather > poorly documented. I want to create a data model for the companies > listed on Crunchbase in order to be able to put the queried data in a > data-base. As I am too lazy to examine all the data by hand I thought > I automatize this. I thought that it would be nice to be able to pass > a function a parsed JSON object (AFAIK these are lists, dicts, > strings, ints, floats, strs in Python) and it returns me the type of > these objects. For the simple classes (str, int, float) this is quite > trivial: F('foo') should return `str' and F(8) should return `int'. > I'm not familiar with JSON structure, but off-hand I'd say the point to determine the nature of the data is during the so-called parsing of the JSON data itself, not after... Based upon http://www.json.org/ the type of an item can basically be determined from the first character of the "value": { dictionary (JSON "object") [ list (JSON "array") " string t/f/n true/false/null - or 0..9 number I'd be looking for some way to have the parser itself return a structure of tuples of (type, parsedJSONitem) Of course, since the parse result (at least from my recent experiment) is a Python structure, it isn't difficult to walk that structure... >>> import simplejson as j >>> SAMPLE = '["foo", {"bar":["baz", null, 1.0, 2]}]' >>> parse = j.loads(SAMPLE) >>> parse [u'foo', {u'bar': [u'baz', None, 1.0, 2]}] >>> def typer(obj, level=0): ... otype = type(obj) ... print "\t" * level, otype ... if otype == type([]): ... for o in obj: ... typer(o, level+1) ... elif otype == type({}): ... for k,o in obj.items(): ... print "\t" * level, " ", k ... typer(o, level+1) ... else: ... print "\t" * level, " ", obj ... >>> print parse [u'foo', {u'bar': [u'baz', None, 1.0, 2]}] >>> typer(parse) foo bar baz None 1.0 2 >>> {Hmmm, forgot to print the type of "k" for dictionaries}. > For a compound object like dict I would like it to return the data > fields with their type. Hence, F({'foo': 8}) should return > {'foo': int}, and given that f = F({'foo': {'bar': 80}}) I would like > f to equal to {'foo': dict}, with the option to query the type of > 'foo' via f.foo, where the latter should equal to {'bar': int}. So > far, this is not a complicated case. But, sometimes a data field on > returned data set is simply None. Thus, I want to extract the types from > another data set and merge the two. > "But, sometimes a data field on returned data set is simply None. Thus, I want to extract the types from another data set and merge the two." ??? A "data field" /value/ of None has the /type/ "", so I don't quite understand what you intend to merge? You can't arbitrarily change the "type" without changing the "value". -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/