Lambda 中使用 Python 3.6 从 DynamoDB 提取的语法无效

Invalid syntax within Lambda using Python 3.6 to pull from DynamoDB

我这辈子都搞不清楚下面这四行代码有什么问题。

def getAssetExistance(asset, element, table):
    dynamoTable = dynamo.Table(table)
    response = dynamoTable.query(KeyConditionExpression=Key(element).eq(asset)
    return bool(response)

我是 运行 通过 aws Lambda 和 cloudwatch 上的日志告诉我错误在 return 行。这是错误(第 24 行是 return 行):

Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 24)

如果这有任何帮助,下面是代码的其余部分:

################################
# Slack Lambda handler.
################################

import boto3
import os
import logging
import urllib

# Grab data from the environment.
BOT_TOKEN   = os.environ["BOT_TOKEN"]
ASSET_TABLE = os.environ["ASSET_TABLE"]
REGION_NAME = os.getenv('REGION_NAME', 'us-east-1')

dynamo = boto3.client('dynamodb', region_name=REGION_NAME)

# Define the URL of the targeted Slack API resource.
SLACK_URL = "https://slack.com/api/chat.postMessage"

def getAssetExistance(asset, element, table):
    dynamoTable = dynamo.Table(table)
    response = dynamoTable.query(KeyConditionExpression=Key(element).eq(asset)
    return bool(response)

def lambda_handler(data, context):
    # Slack challenge answer.
    if "challenge" in data:
        return data["challenge"]

    # Grab the Slack channel data.
    slack_event  = data['event']
    slack_user   = slack_event["user"]
    slack_text   = slack_event["text"]
    channel_id   = slack_event["channel"]
    slack_userID = slack_user["ID"]
    slack_reply  = ""

    # Ignore bot messages.
    if "bot_id" in slack_event:
        logging.warn("Ignore bot event")
    else:

        # Start data sift.
        if slack_text.startswith("!networth"):
            slack_reply = "Your networth is: "
        elif slack_text.startwith("!price"):
            command,asset = text.split()
            slack_reply = "The price of a(n) %s is: " % (asset)
        elif slack_text.startwith("!Addme"):
            if not getAssetExistance(slack_userID, userID, ASSET_TABLE):
                slack_reply = "Adding user: %s(%s)" % (slack_user, slack_userID)
                dynamo.update_item(TableName=ASSET_TABLE, 
                    Key={'userID':{'S':'slack_userID'},
                    AttributeUpdates= {
                        'resources':{
                            'Action': 'ADD',
                            'Value': {'N': '1000'}
                        }
                    }
                )
            else
                slack_reply = "User %s(%s) already exists" % (slack_user, slack_userID)

        # We need to send back three pieces of information:
        data = urllib.parse.urlencode(
            (
                ("token", BOT_TOKEN),
                ("channel", channel_id),
                ("text", slack_reply)
            )
        )
        data = data.encode("ascii")

        # Construct the HTTP request that will be sent to the Slack API.
        request = urllib.request.Request(
            SLACK_URL, 
            data=data, 
            method="POST"
        )
        # Add a header mentioning that the text is URL-encoded.
        request.add_header(
            "Content-Type", 
            "application/x-www-form-urlencoded"
        )

        # Fire off the request!
        urllib.request.urlopen(request).read()

    # Everything went fine.
    return "200 OK"

希望我在做一些蠢事;我对这一切都很陌生。非常感谢任何帮助。谢谢!

您跳过了这行中的右圆括号:

response = dynamoTable.query(KeyConditionExpression=Key(element).eq(asset)

将此行替换为:

response = dynamoTable.query(KeyConditionExpression=Key(element)).eq(asset)