-
-
Save jim-my/a9b2309e1c3edff471a8629f33662fd3 to your computer and use it in GitHub Desktop.
Convert any dictionary to a named tuple
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
| from collections import namedtuple | |
| def convert(dictionary): | |
| return namedtuple('GenericDict', dictionary.keys())(**dictionary) | |
| """ | |
| >>> d = dictionary(a=1, b='b', c=[3]) | |
| >>> named = convert(d) | |
| >>> named.a == d.a | |
| True | |
| >>> named.b == d.b | |
| True | |
| >>> named.c == d.c | |
| True | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment