亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

Helenr 2022-07-12 17:45:38
我有一個 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_dictprint (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        }    },我腳本的最后一行調用了 lambda_handler() 函數。我被告知我不需要此行,因為我的 Lambda 在觸發 Lambda 函數時調用該函數。但是我(相信)在我的本地機器上執行腳本時,我確實需要手動調用該函數。
查看完整描述

2 回答

?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

在循環期間更改 internal_dict_value 的值的問題是您不知道它將保存什么類型。嘗試類似的東西if type(internal_dict_value) is dict:



查看完整回答
反對 回復 2022-07-12
?
達令說

TA貢獻1821條經驗 獲得超6個贊

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

    }


查看完整回答
反對 回復 2022-07-12
  • 2 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號