Skip to content

Instantly share code, notes, and snippets.

@egrachev
Created November 26, 2018 15:09
Show Gist options
  • Save egrachev/6932c53ec456d4ba739da23c8eaf0a1f to your computer and use it in GitHub Desktop.
Save egrachev/6932c53ec456d4ba739da23c8eaf0a1f to your computer and use it in GitHub Desktop.
Search key in structure by yield
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