Skip to content

Instantly share code, notes, and snippets.

@Kuzmenko-Pavel
Created May 9, 2022 13:15
Show Gist options
  • Select an option

  • Save Kuzmenko-Pavel/ed401b283727cc677c191f8a5c89bebb to your computer and use it in GitHub Desktop.

Select an option

Save Kuzmenko-Pavel/ed401b283727cc677c191f8a5c89bebb to your computer and use it in GitHub Desktop.
import operator
from functools import reduce
def find(data, element, default=...):
try:
return reduce(operator.getitem, element.split('.'), data)
except KeyError:
if default is not ...:
return default
raise
print(find({'a': {'b': 1}}, 'a.b', 1))
print(find({'a': {'b': 1}}, 'a.c', 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment