动态生成带有GraphQl响应值作为关键名称的字典
以下代码的目的是创建一个词典,该字典填充了从GraphQl查询响应中收到的数据。
管理器列表的构建如下:managers_list:[' email&&&&&&&&&&nbs ,'
', ' 经理列表和一个令牌,然后返回员工及其行为列表。
“ execute_query”函数执行给定查询并提取所有必要的数据。
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)
这是我从响应中提取所有需要的所有内容后目前拥有的数据片段:
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')
我想词典的最终结构应该看起来像这样:
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"
}
},
...
}
到目前为止,我尝试通过使用附录来将收到的结果分配为新键()功能,但这没有完成工作。我不确定最“ Pythonic”的方法是什么。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要首先将字典分配给
结果[Manager]
。另外,您需要将操作分配给结果[Manager] [雇员]
:You need to assign a dictionary to
result[manager]
first. Also, you need to assign the actions toresult[manager][employee]
: