Created
May 12, 2020 08:28
-
-
Save ficapy/b4e646e7438355e66277f766bdcd472a to your computer and use it in GitHub Desktop.
临时处理ElasticSearch返回的上十级嵌套
This file contains 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
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