Skip to content

Instantly share code, notes, and snippets.

@ficapy
Created May 12, 2020 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ficapy/b4e646e7438355e66277f766bdcd472a to your computer and use it in GitHub Desktop.
Save ficapy/b4e646e7438355e66277f766bdcd472a to your computer and use it in GitHub Desktop.
临时处理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