Created
November 26, 2018 15:09
-
-
Save egrachev/6932c53ec456d4ba739da23c8eaf0a1f to your computer and use it in GitHub Desktop.
Search key in structure by yield
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
def is_flatted(data): | |
assert isinstance(data, dict) | |
def _inner(item): | |
for k, v in item.items(): | |
print("key: %r" % k) | |
if "." in k: | |
yield True | |
if isinstance(v, dict): | |
yield from _inner(v) | |
return any(i for i in _inner(data)) | |
d1 = { | |
"b": 2, | |
"c": { | |
"d": 3 | |
}, | |
"test": { | |
"path": { | |
"a": 1 | |
}, | |
}, | |
} | |
d2 = { | |
"test": { | |
"path.test": { | |
"a": 1 | |
}, | |
}, | |
"b": 2 | |
} | |
d3 = { | |
"test": { | |
"path": { | |
"a": 1 | |
}, | |
}, | |
"b": 2, | |
"c": { | |
"d": { | |
"f.e": 3 | |
} | |
} | |
} | |
assert not is_flatted(d1) | |
assert is_flatted(d2) | |
assert is_flatted(d3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment