-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
from types import SimpleNamespace
def dict_to_namespace(d):
if isinstance(d, dict):
return SimpleNamespace(**{k: dict_to_namespace(v) for k, v in d.items()})
elif isinstance(d, list):
return [dict_to_namespace(item) for item in d]
else:
return d
nested_dict = {
"user": {
"name": "Alice",
"address": {
"city": "Wonderland",
"zip": "12345"
},
"hobbies": ["reading", {"type": "adventure"}]
}
}
ns = dict_to_namespace(nested_dict)
print(ns.user.name) # Alice
print(ns.user.address.city) # Wonderland
print(ns.user.hobbies[1].type) # adventure
Metadata
Metadata
Assignees
Labels
No labels