动态生成带有GraphQl响应值作为关键名称的字典

发布于 2025-02-08 06:02:15 字数 3991 浏览 1 评论 0原文

以下代码的目的是创建一个词典,该字典填充了从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浴红衣 2025-02-15 06:02:15

您需要首先将字典分配给结果[Manager]。另外,您需要将操作分配给结果[Manager] [雇员]

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文