Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27177
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Subject | Re: Dynamically determine base classes on instantiation |
| Date | 2012-08-16 12:29 -0400 |
| Organization | > Bestiaria Support Staff < |
| References | <mailman.3324.1345065532.4697.python-list@python.org> <502c3bc2$0$29978$c3e8da3$5496439d@news.astraweb.com> <20120816125230.GA6195@roku> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3378.1345134562.4697.python-list@python.org> (permalink) |
On Thu, 16 Aug 2012 14:52:30 +0200, Thomas Bach
<thbach@students.uni-mainz.de> 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)
<type 'list'>
<type 'unicode'>
foo
<type 'dict'>
bar
<type 'list'>
<type 'unicode'>
baz
<type 'NoneType'>
None
<type 'float'>
1.0
<type 'int'>
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/ "<type
'NoneType'>", 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/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Dynamically determine base classes on instantiation Thomas Bach <thbach@students.uni-mainz.de> - 2012-08-15 23:17 +0200
Re: Dynamically determine base classes on instantiation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-16 00:16 +0000
Re: Dynamically determine base classes on instantiation Thomas Bach <thbach@students.uni-mainz.de> - 2012-08-16 14:52 +0200
Re: Dynamically determine base classes on instantiation Hans Mulder <hansmu@xs4all.nl> - 2012-08-16 17:10 +0200
Re: Dynamically determine base classes on instantiation Thomas Bach <thbach@students.uni-mainz.de> - 2012-08-16 18:54 +0200
Re: Dynamically determine base classes on instantiation Richard Thomas <chardster@gmail.com> - 2012-08-16 10:03 -0700
Re: Dynamically determine base classes on instantiation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-16 17:49 +0000
Re: Dynamically determine base classes on instantiation Richard Thomas <chardster@gmail.com> - 2012-08-17 04:50 -0700
Re: Dynamically determine base classes on instantiation Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-17 14:53 -0400
Re: Dynamically determine base classes on instantiation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-18 01:44 +0000
Re: Dynamically determine base classes on instantiation Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-18 11:36 +0100
Re: Dynamically determine base classes on instantiation Richard Thomas <chardster@gmail.com> - 2012-08-16 10:03 -0700
Re: Dynamically determine base classes on instantiation Thomas Bach <thbach@students.uni-mainz.de> - 2012-08-16 19:27 +0200
Re: Dynamically determine base classes on instantiation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-16 18:04 +0000
Re: Dynamically determine base classes on instantiation Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-16 12:29 -0400
Re: Dynamically determine base classes on instantiation Richard Thomas <chardster@gmail.com> - 2012-08-16 10:09 -0700
Re: Dynamically determine base classes on instantiation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-16 17:45 +0000
Re: Dynamically determine base classes on instantiation Thomas Bach <thbach@students.uni-mainz.de> - 2012-08-16 19:18 +0200
Re: Dynamically determine base classes on instantiation Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-16 18:08 +0000
csiph-web