Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing python dict using nested key lookup string

I am looking to create a simple nested "lookup" mechanism in python, and wanted to make sure there wasn't already something somewhere hidden in the vast libraries in python that doesn't already do this before creating it.

I am looking to take a dict that is formatted something like this

my_dict = { 
  "root": { 
    "secondary": { 
      "user1": { 
          "name": "jim", 
          "age": 24 
      }, 
      "user2": { 
        "name": "fred", 
        "age": 25 
      } 
    } 
  } 
}

and I am trying to have a way to access the data by using a decimal notation that would be something similar to

root.secondary.user2

and return that resulting dict back as a response. I am thinking that there must be something that does this and I could write one without much difficulty but I want to make sure I am not recreating something I might be missing from the documentation. Thanks

like image 368
Tanerax Avatar asked Feb 16 '12 22:02

Tanerax


People also ask

How do you get an item from a nested dictionary Python?

Access Values using get() Another way to access value(s) in a nested dictionary ( employees ) is to use the dict. get() method. This method returns the value for a specified key. If the specified key does not exist, the get() method returns None (preventing a KeyError ).

How do you make a nested dictionary dynamically in Python?

To create a nested dictionary, simply pass dictionary key:value pair as keyword arguments to dict() Constructor. You can use dict() function along with the zip() function, to combine separate lists of keys and values obtained dynamically at runtime.


3 Answers

There's nothing in the standard library for this purpose, but it is rather easy to code this yourself:

>>> key = "root.secondary.user2"
>>> reduce(dict.get, key.split("."), my_dict)
{'age': 25, 'name': 'fred'}

This exploits the fact that the look-up for the key k in the dictionary d can be written as dict.get(d, k). Applying this iteratively using reduce() leads to the desired result.

Edit: For completeness three functions to get, set or delete dictionary keys using this method:

def get_key(my_dict, key):
    return reduce(dict.get, key.split("."), my_dict)

def set_key(my_dict, key, value):
    key = key.split(".")
    my_dict = reduce(dict.get, key[:-1], my_dict)
    my_dict[key[-1]] = value

def del_key(my_dict, key):
    key = key.split(".")
    my_dict = reduce(dict.get, key[:-1], my_dict)
    del my_dict[key[-1]]
like image 152
Sven Marnach Avatar answered Oct 17 '22 15:10

Sven Marnach


You can have that. You can subclass dict, add the key lookup (and even retain the name dict) by using code similar to the one below. The {...} form however will still use the builtin dict class (now called orig_dict), so you have to enclose it, like so: Dict({...}). This implementation recursively converts dictionaries to the new form, so you don't have to use the method above for any dictionary entries that are plain dictionaries themselves.

orig_dict = dict
class Dict(orig_dict):
    def __init__(self, *args, **kwargs):
        super(Dict, self).__init__(*args, **kwargs)
        for k, v in self.iteritems():
            if type(v) == orig_dict and not isinstance(v, Dict):
                super(Dict, self).__setitem__(k, Dict(v))
    def __getattribute__(self, k):
        try: return super(Dict, self).__getattribute__(k)
        except: return self.__getitem__(k)
    def __setattr__(self, k, v):
        if self.has_key(k): self.__setitem__(k, v)
        else: return super(Dict, self).__setattr__(k, v)
    def __delattr__(self, k):
        try: self.__delitem__(k)
        except: super(Dict, self).__delattr__(k)
    def __setitem__(self, k, v):
        toconvert = type(v) == orig_dict and not isinstance(v, Dict)
        super(Dict, self).__setitem__(k, Dict(v) if toconvert else v)

# dict = Dict  <-- you can even do this but I advise against it

# testing:
b = Dict(a=1, b=Dict(c=2, d=3))
c = Dict({'a': 1, 'b': {'c': 2, 'd': 3}})
d = Dict(a=1, b={'c': 2, 'd': {'e': 3, 'f': {'g': 4}}})

b.a = b.b
b.b = 1
d.b.d.f.g = 40
del d.b.d.e
d.b.c += d.b.d.f.g
c.b.c += c.a
del c.a
print b
print c
print d
like image 35
Frg Avatar answered Oct 17 '22 14:10

Frg


Recursion still works.

def walk_into( dict, key ):
    head, _, tail = key.partition('.')
    if tail:
        return walk_into( dict[head], tail )
    return dict, key
d, k = walk_into( my_dict, "root.secondary.user2" )

d[k] can be used for getting or putting a new value.

like image 32
S.Lott Avatar answered Oct 17 '22 15:10

S.Lott