Created
May 9, 2022 13:15
-
-
Save Kuzmenko-Pavel/ed401b283727cc677c191f8a5c89bebb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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