Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
临时处理ElasticSearch返回的上十级嵌套
class Nested:
def __init__(self, data):
self.data = data
def __getitem__(self, item):
if self.data is None:
return Nested(None)
if isinstance(item, int) and isinstance(self.data, list):
if len(self.data) >= item:
return Nested(self.data[item])
return Nested(None)
if isinstance(item, str) and isinstance(self.data, dict):
if item in self.data:
return Nested(self.data[item])
return Nested(None)
return Nested(None)
def __nonzero__(self):
return bool(self.data)
def __str__(self):
return str(self.data)
a = {'b': {'c': [123]}}
a = Nested(a)
c = a['b']['c'][0]['123']['5']
print(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.