News for hackers. Stuff that matters.
Re:Box: Python dictionaries with recursive dot notation access by smoe on Saturday May 06, 2017 @02:06PM (#14281512)
I never thought much about best implementation because I only used it i think once in ten years. But afaik I used something like this, that does the job and is easy to read

  _empty = object()
  def deep_get(dct, dotted_path, default=_empty):
      for key in dotted_path.split('.'):
        try:
          dct = dct[key]
        except KeyError:
          if default is _empty:
            raise
          return default
      return dct

There are no comments on this comment yet.