Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing JSON keys as attributes in nested JSON
    text
    copied!<p>I'm working with nested JSON-like data structures in python 2.7 that I exchange with some foreign perl code. I just want to 'work with' these nested structures of lists and dictionaries in amore pythonic way.</p> <p>So if I have a structure like this...</p> <pre><code>a = { 'x': 4, 'y': [2, 3, { 'a': 55, 'b': 66 }], } </code></pre> <p>...I want to be able to deal with it in a python script as if it was nested python classes/Structs, like this:</p> <pre><code>&gt;&gt;&gt; aa = j2p(a) # &lt;&lt;- this is what I'm after. &gt;&gt;&gt; print aa.x 4 &gt;&gt;&gt; aa.z = 99 &gt;&gt;&gt; print a { 'x': 4, 'y': [2, 3, { 'a': 55, 'b': 66 }], 'z': 99 } &gt;&gt;&gt; aa.y[2].b = 999 &gt;&gt;&gt; print a { 'x': 4, 'y': [2, 3, { 'a': 55, 'b': 999 }], 'z': 99 } </code></pre> <p>Thus aa is a proxy into the original structure. This is what I came up with so far, inspired by the excellent <a href="https://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python">What is a metaclass in Python?</a> question.</p> <pre><code>def j2p(x): """j2p creates a pythonic interface to nested arrays and dictionaries, as returned by json readers. >>> a = { 'x':[5,8], 'y':5} >>> aa = j2p(a) >>> aa.y=7 >>> print a {'x': [5, 8], 'y':7} >>> aa.x[1]=99 >>> print a {'x': [5, 99], 'y':7} >>> aa.x[0] = {'g':5, 'h':9} >>> print a {'x': [ {'g':5, 'h':9} , 99], 'y':7} >>> print aa.x[0].g 5 """ if isinstance(x, list): return _list_proxy(x) elif isinstance(x, dict): return _dict_proxy(x) else: return x class _list_proxy(object): def __init__(self, proxied_list): object.__setattr__(self, 'data', proxied_list) def __getitem__(self, a): return j2p(object.__getattribute__(self, 'data').__getitem__(a)) def __setitem__(self, a, v): return object.__getattribute__(self, 'data').__setitem__(a, v) class _dict_proxy(_list_proxy): def __init__(self, proxied_dict): _list_proxy.__init__(self, proxied_dict) def __getattribute__(self, a): return j2p(object.__getattribute__(self, 'data').__getitem__(a)) def __setattr__(self, a, v): return object.__getattribute__(self, 'data').__setitem__(a, v) def p2j(x): """p2j gives back the underlying json-ic json-ic nested dictionary/list structure of an object or attribute created with j2p. """ if isinstance(x, (_list_proxy, _dict_proxy)): return object.__getattribute__(x, 'data') else: return x </code></pre> <p>Now I wonder whether there is an elegant way of mapping a whole set of the <code>__*__</code> special functions, like <code>__iter__</code>, <code>__delitem__</code>? so I don't need to unwrap things using <code>p2j()</code> just to iterate or do other pythonic stuff.</p> <pre><code># today: for i in p2j(aa.y): print i # would like to... for i in aa.y: print i </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload