python-3.x - TypeError:“上下文”对象不可用于谷歌云功能
问题描述
有人可以帮助诊断和纠正问题所在,因为此错误一直引用“上下文”对象(我在理论上将 schema_df 解释为该对象)?
我一直在尝试部署一个在以下部分本地工作的云功能。
def convert_schema(results_df, schema_df):
"""Converts data types in dataframe to match BigQuery destination table"""
dict(schema_df)
print(schema_df)
for k in schema_df: #for each column name in the dictionary, convert the data type in the dataframe
results_df[k] = results_df[k].astype(schema_df.get(k))
results_df_transformed = results_df
print("Updated schema to match BigQuery destination table")
return results_df_transformed
schema_df = {'_comments': 'object',
'_direction': 'object',
'_fromst': 'object',
'_last_updt': 'datetime64',
'_length': 'float64',
'_lif_lat': 'float64',
'_lit_lat': 'float64',
'_lit_lon': 'float64',
'_strheading': 'object',
'_tost': 'object',
'_traffic': 'int64',
'segmentid': 'int64',
'start_lon': 'float64',
'street': 'object'
}
但是,它不起作用,也不会将传入的对象识别为可迭代字典。
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 349, in run_background_function
_function_handler.invoke_user_function(event_object)
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 215, in invoke_user_function
return call_user_function(request_or_event)
File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 212, in call_user_function
event_context.Context(**request_or_event.context))
File "/user_code/main.py", line 43, in handler
results_df_transformed = convert_schema(results_df, schema_df)
File "/user_code/lib/data_ingestion.py", line 84, in convert_schema
dict(schema_df)
TypeError: 'Context' object is not iterable
解决方案
响应事件的函数(即“后台函数”)需要具有签名:
def my_function(data, context):
...
Cloud Functions 运行时在每个新事件上提供的位置data
和位置。context
您可能希望convert_schema
在顶级后台函数中调用您的函数。有关详细信息,请参阅“编写后台函数”。
推荐阅读
- reflection - Threejs:glb文件反映环境但不反映自身
- kubernetes - 如何在不丢失集群数据的情况下重启 kube apiserver
- python - Python:当我们在特定列中有重复项时合并行
- javascript - How to convert an object with arrays as values to an array of objects
- java - cucumber-junit-platform-engine 中的@CucumberOptions
- javascript - React.js:数据更新导致在 iOS 上滚动到顶部
- docker - 我可以使用 keycloak 使用 traefik 验证具有 oauth 支持的 docker 服务吗?
- nginx - 为什么 Nginx 位置 /dir/ 不适用于 /dir/ 本身,但适用于任何 /dir/file?
- docker - 使 Docker 容器只能从特定 IP/IP 子网访问
- deep-learning - 反向传播是否使用优化函数来更新权重?