python - python脚本中的ListRecommendationsRequest构造函数错误
问题描述
我正在尝试使用 python3.7 脚本准备 VM Resizing Recommendations Report 我的代码如下:
导入日期时间
import logging
from google.cloud import bigquery
from google.cloud import recommender
from google.cloud.exceptions import NotFound
from googleapiclient import discovery
def main(event, context):
client = recommender.RecommenderClient()
recommender_type = "google.compute.instance.MachineTypeRecommender"
projects=list_projects() #This gives list of projects
# I hard-code the zones below:
zones = ["us-east1-b","us-east1-c","us-east1-d","us-east4-c","us-east4-b","us-east4-a"
,"us-central1-c","us-central1-a","us-central1-f","us-central1-b"
,"us-west1-b","us-west1-c","us-west1-a"]
for zone in zones:
parent = client.recommender_path("my-project", zone, recommender_type)
for element in client.list_recommendations(parent): #In this line I am getting this error and these are logs
****Parent**** projects/my-project/locations/us-east1-b/recommenders/google.compute.instance.MachineTypeRecommender
Traceback (most recent call last):
File "main.py", line 187, in main
x=list(client.list_recommendations(parent))
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/google/cloud/recommender_v1/services/recommender/client.py", line 734, in list_recommendations
request = recommender_service.ListRecommendationsRequest(request)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/proto/message.py", line 441, in __init__
raise TypeError(
TypeError: Invalid constructor input for ListRecommendationsRequest: 'projects/my-project/locations/us-east1-b/recommenders/google.compute.instance.MachineTypeRecommender'
During handling of the above exception, another exception occurred:
projects/my-project/locations/us-east1-b/recommenders/google.compute.instance.MachineTypeRecommender是传递给 list_recommendations 函数的参数。我不确定构造函数有什么问题,因为我得到这个:ListRecommendationsRequest 的构造函数输入无效
我是这个 google api 的新手。有人可以帮忙吗?谢谢。
解决方案
我已经检查了我的代码,调用该方法时似乎是语法错误。根据库文档,第一个参数应该是请求对象或None并且父参数必须按名称传递。如下更改行我没有错误:
client.list_recommendations(parent=parent)
推荐阅读
- osgi - 使 AEM MemoryUsage 参数成为代码库的一部分
- jupyter-notebook - EMR Notebook 出现“未找到内核”错误
- javascript - 在 vue 中为创建表单处理数据层次结构的最佳方法
- python - 如何在 python 线性模型中进行部分 F 测试?
- google-cloud-platform - GCP 事件无法解决
- jquery-select2 - 如何使这些字段在 select2 上填满
- r - 如何在 R 中循环遍历带有列表的数据框列?
- python - 用未知列表大小替换索引
- angular - Angular 如何使用其官方的第三个库绑定 DefinedlyTyped 类型
- python - Python 请求下载页面 html 内容而不是 .xlsx 文件