簡體   English   中英

AWS Lambda 錯誤:AttributeError 'list' 對象沒有屬性 'get'

[英]AWS Lambda ERROR: AttributeError 'list' object has no attribute '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:

@JakePearse的幫助下,我解決了這個問題。

這是我的(全功能)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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM