The goal of the following piece of code is to create a dictionary that is populated with data received from GraphQL query responses.
Manager list is constructed as the following: managers_list: ['[email protected]', '[email protected]']
The "get_employees_per_manager" function takes a list of managers and a token, then it returns a list of employees and their actions.
"execute_query" function executes the given query and extracts all the necessary data.
def get_employees_per_manager(managers_list, token):
result = dict()
for manager in managers_list:
query = """
query {
details(managersEmailAddress: "##manager_email##"){
employeeId
}
}
"""
print("Manager", manager)
# Get the current index of managers list element
index = managers_list.index(manager)
# Replace the query attribute with current manager value
query = gql(query_query.replace("##manager_email##", managers_list[index]))
# Execute the query
list = obj.execute_query(query, token)
for employee in employee_list:
query = """
query {
details(employeeId: ##employeeId##){
Name
Surname
category{
categoryName
rating
action
}
}
}
"""
print("Employee: ", employee)
# Get the current index of current employeeId list element
index = employee_list.index(employee)
# Replace the query attribute with current employee value
query = gql(query.replace("##employeeId##", str(employee_list[index])))
# Execute the query
actions = obj.execute_query(query, token)
# In here I intend to create a dictionary with dynamically appended data.
# The first two lines below do not work and they trigger a KeyValue error.
result[manager].append(employee)
result[employee].append(actions)
print(result)
return(employee_list, actions, result)
Here's the fragment of data that I currently have after extracting all everything I need from the response:
Manager: [email protected]
Employee: 122345
([{'category': 'Category1', 'rating': 1, 'action': 'Action 1 for this employee'}, {'category': 'Category1', 'rating': 1, 'action': 'Action 2 for this employee'}, ], 'NameSurname')
Employee: 126541
([{'category': 'Category1', 'rating': 1, 'action': 'Action 1 for this employee'}, {'category': 'Category1', 'rating': 1, 'action': 'Action 2 for this employee'}, ], 'NameSurname')
Manager: [email protected]
Employee: 122346
([{'category': 'Category1', 'rating': 1, 'action': 'Action 1 for this employee'}, {'category': 'Category1', 'rating': 1, 'action': 'Action 2 for this employee'}, ], 'NameSurname')
I imagine that the final structure of the dictionary should look like this:
result = {
"manager": {
"employee": {
"Name": "Name 1"
"Surname": "Surname 1"
"Action": "Action 1"
},
"employee": {
"Name": "Name 2"
"Surname": "Surname 2"
"Action": "Action 2"
}
},
"manager": {
"employee": {
"Name": "Name 1"
"Surname": "Surname 1"
"Action": "Action 1"
}
},
...
}
So far I've tried to assign received results as new keys by using the append() function but that did not do the job. I'm not sure what would be the most 'pythonic' way to handle that.
CodePudding user response:
You need to assign a dictionary to result[manager]
first. Also, you need to assign the actions to result[manager][employee]
:
def get_employees_per_manager(managers_list, token):
result = dict()
for manager in managers_list:
result[manager] = {} # <---------------
query = """
query {
details(managersEmailAddress: "##manager_email##"){
employeeId
}
}
"""
print("Manager", manager)
index = managers_list.index(manager)
query = gql(query_query.replace("##manager_email##", managers_list[index]))
list = obj.execute_query(query, token)
for employee in employee_list:
query = """
query {
details(employeeId: ##employeeId##){
Name
Surname
category{
categoryName
rating
action
}
}
}
"""
print("Employee: ", employee)
index = employee_list.index(employee)
query = gql(query.replace("##employeeId##", str(employee_list[index])))
actions = obj.execute_query(query, token)
result[manager][employee] = actions # <---------------
print(result)
return(employee_list, actions, result)