AWS Lambda 错误:AttributeError 'list' 对象没有属性 'get' [英] AWS Lambda ERROR: AttributeError 'list' object has no attribute 'get'
问题描述
我有一个 Lambda 函数,用于打开/关闭我的 Philip HUE 灯泡.我能够执行 python 脚本 &它在我的本地机器上运行(无错误).但是,当我触发 Lambda 函数(使用 IoT 按钮)时,我收到以下错误消息.
I have a Lambda function that is designed to turn ON/OFF my Philip HUE lightbulbs. I am able to execute the python script & it runs (error-free) on my local machine. However, when I trigger the Lambda function (using an IoT Button) I get the following error message.
[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)
我认为错误与以下代码行有关:
I think the error relates the the following line of code:
internal_dict_value = internal_dict_value.get(k, None)
但是,我几乎可以肯定internal_dict_value"变量是一个字典,而不是一个列表.为了验证,我在脚本中插入了以下代码行:
However, I am almost certain that the "internal_dict_value" variable is a dictionary, NOT a list. To verify, I inserted the following line of code into my script:
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"
}
这是我正在使用的脚本.如果您有任何鼓舞人心的想法,请分享它们!谢谢.
Here is the script that I am working with. If you have any inspirational ideas, please share them! Thanks.
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 函数被触发时调用该函数.但是,我(相信)在本地计算机上执行脚本时确实需要手动调用该函数.
The last line in my script calls the lambda_handler() function. I'm told that I do not need this line because my Lambda calls the function when the Lambda Function is triggered. However I (believe) that I do need to manually call the function, when executing the script on my local machine.
推荐答案
在 @JakePearse 的帮助下,我能够来解决问题.
With the help of @JakePearse I was able to resolve the issue.
这是我的(全功能)python 脚本的最终版本.
Here is the final version of my (fully-functional) python script.
import requests,json
bridgeIP = "IP/FQDN_Here:port_here"
userID = "userHash_here"
lightID = "2"
def lambda_handler(event, 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:
if type(internal_dict_value) is dict:
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"])
if bulbStatus == False:
requests.put(f"{url}/state",json.dumps({"on":True}))
elif bulbStatus == True:
requests.put(f"{url}/state",json.dumps({"on":False}))
return {
'statusCode': 200,
'body': json.dumps('Mission accomplished!')
}
这篇关于AWS Lambda 错误:AttributeError 'list' 对象没有属性 'get'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!