python - AWS Lambda 错误:AttributeError 'list' 对象没有属性 'get'
问题描述
我有一个 Lambda 函数,旨在打开/关闭我的 Philip HUE 灯泡。我能够执行 python 脚本并在我的本地机器上运行(无错误)。但是,当我触发 Lambda 函数(使用 IoT 按钮)时,我收到以下错误消息。
[ERROR] AttributeError: 'list' object has no attribute 'get'
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 21, in lambda_handler
bulbStatus = nested_get(data,["state","on"])
File "/var/task/lambda_function.py", line 16, in nestedDictLookup
internal_dict_value = internal_dict_value.get(k, None)
我认为该错误与以下代码行有关:
internal_dict_value = internal_dict_value.get(k, None)
但是,我几乎可以肯定“internal_dict_value”变量是字典,而不是列表。为了验证,我将以下代码行插入到我的脚本中:
internal_dict_value = input_dict
print (internal_dict_value)
这是我收到的输出:
{
"state": {
"on": true,
"bri": 254,
"hue": 8597,
"sat": 121,
"effect": "none",
"xy": [
0.4452,
0.4068
],
"ct": 343,
"alert": "select",
"colormode": "xy",
"mode": "homeautomation",
"reachable": false
},
"swupdate": {
"state": "noupdates",
"lastinstall": "2019-07-26T19:09:58"
},
"type": "Extended color light",
"name": "Couch Light",
"modelid": "LCT016",
"manufacturername": "Philips",
"productname": "Hue color lamp",
"capabilities": {
"certified": true,
"control": {
"mindimlevel": 1000,
"maxlumen": 800,
"colorgamuttype": "C",
"colorgamut": [
[
0.6915,
0.3083
],
[
0.1700,
0.7000
],
[
0.1532,
0.0475
]
],
"ct": {
"min": 153,
"max": 500
}
},
"streaming": {
"renderer": true,
"proxy": true
}
},
"config": {
"archetype": "sultanbulb",
"function": "mixed",
"direction": "omnidirectional",
"startup": {
"mode": "custom",
"configured": true,
"customsettings": {
"bri": 254,
"ct": 346
}
}
},
"uniqueid": "00:00:88:08:03:fd:4a:e2-0a",
"swversion": "1.46.13_r26312",
"swconfigid": "9DC82D22",
"productid": "Philips-LCT316-1-A17ECLv5"
}
这是我正在使用的脚本。如果您有任何鼓舞人心的想法,请分享!谢谢。
import requests,json
bridgeIP = "IP_Address_Here"
userID = "userID_here"
lightID = "4" #Represents the ID assigned to lightbulb, in the living room.
def lambda_handler(lightID, lambda_context):
url = f"http://{bridgeIP}/api/{userID}/lights/{lightID}"
r = requests.get(url)
data = json.loads(r.text)
def nested_get(input_dict, nested_key):
internal_dict_value = input_dict
for k in nested_key:
internal_dict_value = internal_dict_value.get(k, None)
if internal_dict_value is None:
return None
return internal_dict_value
bulbStatus = nested_get(data,{"state","on"})
#the nested_get() function captures the dict value, assigned to the "on" key.
#{"state":{"on":{True}} or {"state":{"on":{False}}
if bulbStatus == False:
r = requests.put(f"{url}/state", json.dumps({"on":True}))
elif bulbStatus == True:
r = requests.put(f"{url}/state", json.dumps({"on":False}))
lambda_handler(lightID, 4)
我脚本的最后一行调用了 lambda_handler() 函数。我被告知我不需要此行,因为我的 Lambda 在触发 Lambda 函数时调用该函数。但是我(相信)在我的本地机器上执行脚本时,我确实需要手动调用该函数。
解决方案
在循环期间更改 internal_dict_value 的值的问题是您不知道它将保存什么类型。尝试类似的东西if type(internal_dict_value) is dict:
推荐阅读
- xamarin.forms - 如何从父 shell 选项卡式页面导航到 shell 选项卡式页面
- android - 清单合并失败如何解决
- javascript - 如何根据几个规则过滤数组数组
- amazon-s3 - S3 到红移 nifi
- python-3.x - Django-rest-framework:身份验证/权限错误:TypeError:'str'对象不可调用
- pine-script - 为什么我的策略没有显示任何数据?
- java - 从字符串中读取特定行
- c - 返回重复序列的下一个数字的函数
- java - 如何使用 Selenium 和 Java 验证搜索结果中的所有单词以特定字母开头
- php - 使用现有活动域的 wordpress 多站点安装