简体   繁体   English

AWS Lambda 错误:AttributeError 'list' 对象没有属性 'get'

[英]AWS Lambda ERROR: AttributeError 'list' object has no attribute 'get'

I have a Lambda function that is designed to turn ON/OFF my Philip HUE lightbulbs.我有一个 Lambda 函数,用于打开/关闭我的 Philip HUE 灯泡。 I am able to execute the python script & it runs (error-free) on my local machine.我能够执行 python 脚本并在我的本地机器上运行(无错误)。 However, when I trigger the Lambda function (using an IoT Button) I get the following error message.但是,当我触发 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)

I think the error relates the the following line of code:我认为该错误与以下代码行有关:

internal_dict_value = internal_dict_value.get(k, None)

However, I am almost certain that the "internal_dict_value" variable is a dictionary, NOT a list.但是,我几乎可以肯定“internal_dict_value”变量是字典,而不是列表。 To verify, I inserted the following line of code into my script:为了验证,我将以下代码行插入到我的脚本中:

internal_dict_value = input_dict
print (internal_dict_value)

Here is the output that I received:这是我收到的输出:

{
    "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)

The last line in my script calls the lambda_handler() function.我脚本中的最后一行调用了 lambda_handler() 函数。 I'm told that I do not need this line because my Lambda calls the function when the Lambda Function is triggered.我被告知我不需要这一行,因为我的 Lambda 在 Lambda 函数被触发时调用该函数。 However I (believe) that I do need to manually call the function, when executing the script on my local machine.但是,我(相信)在本地计算机上执行脚本时确实需要手动调用该函数。

The issue with changing the value of internal_dict_value during the loop is you dont know what type it's going to hold.在循环期间更改 internal_dict_value 的值的问题是您不知道它将保存什么类型。 try something like if type(internal_dict_value) is dict:尝试类似if type(internal_dict_value) is dict:

With the help of @JakePearse I was able to resolve the issue.@JakePearse的帮助下,我解决了这个问题。

Here is the final version of my (fully-functional) python script.这是我的(全功能)python 脚本的最终版本。

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!')
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError'list'对象没有属性'get' - AttributeError 'list' object has no attribute 'get' "AttributeError: 'list' 对象没有属性 'get' odoo" - AttributeError: 'list' object has no attribute 'get' odoo “ AttributeError:'list'对象没有属性'get'” - “AttributeError: 'list' object has no attribute 'get' ” Python:AttributeError:'str'对象没有属性'get':Lambda + Map - Python: AttributeError: 'str' object has no attribute 'get': Lambda + Map AttributeError: 'list' object 没有属性 'get_attribute' - AttributeError: 'list' object has no attribute 'get_attribute' AWS sagemaker 错误 - AttributeError: 'NoneType' object 没有属性 'startswith' - AWS sagemaker error - AttributeError: 'NoneType' object has no attribute 'startswith' AttributeError: 'list' object 没有属性 - AttributeError: 'list' object has no attribute 为什么会出现此错误? AttributeError: 'list' object 没有属性 'get_cards' - Why do I get this error? AttributeError: 'list' object has no attribute 'get_cards' AttributeError: object 没有属性 'get' - AttributeError: object has no attribute 'get' 尝试将 append 加入列表时,出现错误“AttributeError: 'NoneType' object has no attribute 'append'” - When trying to append to a list, I get the error "AttributeError: 'NoneType' object has no attribute 'append'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM