gpt4 book ai didi

python-3.x - python : Fetching nested dictionary values GCP Recommendations

转载 作者:行者123 更新时间:2023-12-04 13:26:58 24 4
gpt4 key购买 nike

我正在尝试使用 Python 从 GCP Recommendations API 的 API JSON 输出中非法获取某些关键值,并且我对使用 Python 比较陌生。我试图非法获取的大多数值我都可以毫无问题地获取,但是,当我尝试在 JSON 中更深层嵌套的代码块中非法获取某些值时,它会失败并显示错误:TypeError: 'OperationGroup' object is not subscriptableJSON 响应的完整输出在这里(某些值已更改以保护公司信息):

name: "projects/12345678910/locations/us-central1-a/recommenders/google.compute.instance.MachineTypeRecommender/recommendations/abcd-efg-hijk-lmnop-qrstuv-123456"
description: "Save cost by changing machine type from e2-medium to e2-small."
last_refresh_time {
seconds: 1623222401
}
primary_impact {
category: COST
cost_projection {
cost {
currency_code: "USD"
units: -12
nanos: -98539964
}
duration {
seconds: 2592000
}
}
}
content {
operation_groups {
operations {
action: "test"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/xyz/zones/us-central1-a/instances/abcname123"
path: "/machineType"
value_matcher {
matches_pattern: ".*zones/us-central1-a/machineTypes/e2-medium"
}
}
operations {
action: "replace"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/xyz/zones/us-central1-a/instances/abcname123"
path: "/machineType"
value {
string_value: "zones/us-central1-a/machineTypes/e2-small"
}
}
}
}
state_info {
state: ACTIVE
}
etag: "\"abc-123-def-456\""
recommender_subtype: "CHANGE_MACHINE_TYPE"

name: "projects/12345678910/locations/us-central1-a/recommenders/google.compute.instance.MachineTypeRecommender/recommendations/abcdefg-hijklmnop-123-456"
description: "Save cost by changing machine type from e2-medium to e2-small."
last_refresh_time {
seconds: 1623222401
}
primary_impact {
category: COST
cost_projection {
cost {
currency_code: "USD"
units: -12
nanos: -99648292
}
duration {
seconds: 2592000
}
}
}
content {
operation_groups {
operations {
action: "test"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/xyz/zones/us-central1-a/instances/instance-example1"
path: "/machineType"
value_matcher {
matches_pattern: ".*zones/us-central1-a/machineTypes/e2-medium"
}
}
operations {
action: "replace"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/xyz/zones/us-central1-a/instances/instance-example1"
path: "/machineType"
value {
string_value: "zones/us-central1-a/machineTypes/e2-small"
}
}
}
}
state_info {
state: ACTIVE
}
etag: "\"abcdefg12345\""
recommender_subtype: "CHANGE_MACHINE_TYPE"

name: "projects/12345678910/locations/us-central1-a/recommenders/google.compute.instance.MachineTypeRecommender/recommendations/abcd1234"
description: "Save cost by changing machine type from e2-medium to e2-small."
last_refresh_time {
seconds: 1623222401
}
primary_impact {
category: COST
cost_projection {
cost {
currency_code: "USD"
units: -11
nanos: -568971875
}
duration {
seconds: 2592000
}
}
}
content {
operation_groups {
operations {
action: "test"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/abcd/zones/us-central1-a/instances/instance-example2"
path: "/machineType"
value_matcher {
matches_pattern: ".*zones/us-central1-a/machineTypes/e2-medium"
}
}
operations {
action: "replace"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/abcd/zones/us-central1-a/instances/instance-example2"
path: "/machineType"
value {
string_value: "zones/us-central1-a/machineTypes/e2-small"
}
}
}
}
state_info {
state: ACTIVE
}
etag: "\"abcd1234\""
recommender_subtype: "CHANGE_MACHINE_TYPE"
这是我在 Python 中的代码:
from google.cloud import recommender
import os

client = recommender.RecommenderClient()

def main():
name = client.list_recommendations(parent='projects/xyzproject/locations/us-central1-a/recommenders/google.compute.instance.MachineTypeRecommender')
for element in name:
# print(element)
print(element.description)
print(element.primary_impact.category)
print(element.primary_impact.cost_projection.cost.currency_code)
print(element.primary_impact.cost_projection.cost.units)
print(element.state_info.state)
print(element.content.operation_groups)
for item in element.content.operation_groups:
print(item['resource_type'])
main()
上述工作的以下部分:
print(element.description)
print(element.primary_impact.category)
print(element.primary_impact.cost_projection.cost.currency_code)
print(element.primary_impact.cost_projection.cost.units)
print(element.state_info.state)
print(element.content.operation_groups)
但我遇到的错误和麻烦是:
for item in element.content.operation_groups:
print(item['resource_type'])
每当我尝试使用 python 脚本的那部分时,它都会失败并显示错误:
TypeError: 'OperationGroup' object is not subscriptable
那么,有人可以帮助我了解如何正确利用 JSON 响应并非法使用以下块中的信息(例如“resource_type”)?
content {
operation_groups {
operations {
action: "test"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/abcd/zones/us-central1-a/instances/instance-example2"
path: "/machineType"
value_matcher {
matches_pattern: ".*zones/us-central1-a/machineTypes/e2-medium"
}
}
operations {
action: "replace"
resource_type: "compute.googleapis.com/Instance"
resource: "//compute.googleapis.com/projects/abcd/zones/us-central1-a/instances/instance-example2"
path: "/machineType"
value {
string_value: "zones/us-central1-a/machineTypes/e2-small"
}
}
}
}

最佳答案

我采用了您问题中的代码并重新编写了它以匹配 Python Recommender API。主要问题是您正在访问内部成员和字段名称。我已更改代码以使用公共(public)接口(interface)。
查看 API 的类型定义:
Types for Recommender API Client
GitHub Gist: Python example demonstrating the Google Cloud Compute Recommender list recommendations api

# pip install google-cloud-recommender

from google.cloud import recommender
import os

# Enter values for your Project ID and Zone
PROJECT=
LOCATION=

RECOMMENDER = 'google.compute.instance.MachineTypeRecommender'

def print_recommendations():
client = recommender.RecommenderClient()

parent = client.recommender_path(PROJECT, LOCATION, RECOMMENDER)

recommendations = client.list_recommendations(parent=parent)

for recommendation in recommendations:
print('Recommendation:')
print(' Description: ', recommendation.description)
print(' Impact Category: ', recommendation.primary_impact.category)
print(' Currency Code: ', recommendation.primary_impact.cost_projection.cost.currency_code)
print(' Units: ', recommendation.primary_impact.cost_projection.cost.units)
print(' State: ', recommendation.state_info.state)
print('')

for op_group in recommendation.content._pb.operation_groups:
for operation in op_group.operations:
print(' Operation:')
print(' Action: ', operation.action)
print(' Resource Type: ', operation.resource_type)
print(' Path: ', operation.path)
print(' Value: ', operation.value.string_value)
print('')

print_recommendations()
示例输出:
Recommendation:
Description: Save cost by changing machine type from e2-standard-4 to e2-highmem-2.
Impact Category: Category.COST
Currency Code: USD
Units: -31
State: State.ACTIVE

Operation:
Action: test
Resource Type: compute.googleapis.com/Instance
Path: /machineType
Value:

Operation:
Action: replace
Resource Type: compute.googleapis.com/Instance
Path: /machineType
Value: zones/us-east1-b/machineTypes/e2-highmem-2

关于python-3.x - python : Fetching nested dictionary values GCP Recommendations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67928039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com