Last active
April 30, 2019 19:20
-
-
Save kevinmmartins/050f4960b53742985621546c262bb785 to your computer and use it in GitHub Desktop.
Check keys into dict
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 TarefasFlat(): | |
def __init__(self, data): | |
if data is not None: | |
self.dados = self.convert_to_flat_object(data) | |
def convert_to_flat_object(self, data): | |
ativo_flat_list = [] | |
for tarefa in data: | |
ativo_flat_list.append(self.reduce_tarefa_fields(tarefa)) | |
return ativo_flat_list | |
def reduce_tarefa_fields(self, tarefa): | |
return { | |
"id": tarefa["id"] if self.keys_exists(tarefa, "id") else None, | |
"caso": { | |
"pasta": tarefa["caso"]["pasta"] if self.keys_exists(tarefa, "caso", "pasta") else None, | |
"carteira": { | |
"nome": tarefa["caso"]["carteira"]["nome"] if self.keys_exists(tarefa, "caso", "carteira", | |
"nome") else None | |
} | |
} | |
} | |
def keys_exists(self, element, *keys): | |
_element = element | |
for key in keys: | |
try: | |
if _element is None: | |
return False | |
_element = _element[key] | |
except KeyError: | |
return False | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment