Lambda 在匹配操作中引发语法错误
Lambda raise a syntax error on a match operation
我正在尝试创建一个可以在我的机器上运行的 Lambda 函数,但是当我测试它时,我收到以下错误:
Response
{
"errorMessage": "Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 64)",
"errorType": "Runtime.UserCodeSyntaxError",
"requestId": "ae5f2481-a836-4b4e-8c8d-a82ccd12c630",
"stackTrace": [
" File \"/var/task/lambda_function.py\" Line 64\n match operation:\n"
]
}
Function Logs
START RequestId: ae5f2481-a836-4b4e-8c8d-a82ccd12c630 Version: $LATEST
[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 64)
Traceback (most recent call last):
File "/var/task/lambda_function.py" Line 64
match operation:END RequestId: ae5f2481-a836-4b4e-8c8d-a82ccd12c630
REPORT RequestId: ae5f2481-a836-4b4e-8c8d-a82ccd12c630 Duration: 1.34 ms Billed Duration: 2 ms Memory Size: 128 MB Max Memory Used: 35 MB Init Duration: 117.69 ms
Request ID
ae5f2481-a836-4b4e-8c8d-a82ccd12c630
所以在浪费了几个小时之后:
- 我看不到任何语法错误。
- 我试过捆绑枚举包(不知道是否有必要)。
- 我还评论了任何使用外部参数的行。
- 我在 Lambda 中使用 Python 3.9,在本地使用 python 3.10。
- 日志始终指向函数
get_url
中的第 64 行 match operation:
这是错误函数之前的代码:
import requests
import json
from enum import Enum
import urllib.parse
import boto3
import base64
max_number_of_digit = 1
base_url = "https://somesite.io"
app_version = "version-test/"
bubble_api_token = "some api token"
pdf_creator_api_token = 'some other api token'
class APP_ENTITY(str, Enum):
PROJECT = "project"
COMPANY = "company"
PAYMENT = "payment"
TRANCHE = "tranche"
COST_ITEMS = "payment"
DOCUMENT = "document"
class OPERATIONS(int, Enum):
CREATE = 1
DELETE = 2
PATCH = 3
READ = 4
GET = 5
document = dict()
customer = dict()
base_assets = [
'bank.png',
'calculator.png',
'card.png',
'caret.png',
'jetbrains-mono-v11-latin-500.woff2',
'jetbrains-mono-v11-latin-regular.woff2',
'line.png',
'logo.svg',
'poppins-v19-latin-300.woff2',
'poppins-v19-latin-500.woff2',
'poppins-v19-latin-600.woff2',
'poppins-v19-latin-regular.woff2',
'style.css'
]
def set_app_version(event):
global app_version
result = "version-test/"
try:
result = event['queryStringParameters']['appversion']
result = "" if (result.lower() != 'test') else "version-test/"
except:
result = "version-test/"
app_version = result
return result
def get_url(operation: OPERATIONS, entity: APP_ENTITY, cursor: int = 0, record_id: str = "", project_id: str = ""):
match operation:
case OPERATIONS.CREATE:
result = base_url + \
"/{}api/1.1/obj/{}/bulk".format(app_version, entity)
case OPERATIONS.DELETE:
result = base_url + \
"/{}api/1.1/obj/{}/".format(app_version, entity)
case OPERATIONS.PATCH:
result = base_url + \
"/{}api/1.1/obj/{}/{}".format(app_version, entity, record_id)
case OPERATIONS.READ:
match entity:
case APP_ENTITY.COST_ITEMS:
result = base_url + \
"/{}api/1.1/obj/{}/?api_token={}&cursor={}&constraints=".format(
app_version, entity, bubble_api_token, cursor)
constraints = [
{
"key": "type",
"constraint_type": "equals",
"value": "development"
},
{
"key": "project",
"constraint_type": "equals",
"value": project_id
}]
constraints = urllib.parse.quote_plus(
json.dumps(constraints))
result = result+constraints
case OPERATIONS.GET:
result = base_url + \
"/{}api/1.1/obj/{}/{}".format(app_version, entity, record_id)
return result
下面是 lambda 控制台的屏幕截图。
Lambda console
这里是错误的屏幕截图:
Lambda error
match
和 case
是 Python 3.10 中新增的内容。 Lambda 不支持 Python 3.10, 因此您遇到了错误。您必须移植回您的代码才能使用 Python 3.9
我正在尝试创建一个可以在我的机器上运行的 Lambda 函数,但是当我测试它时,我收到以下错误:
Response
{
"errorMessage": "Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 64)",
"errorType": "Runtime.UserCodeSyntaxError",
"requestId": "ae5f2481-a836-4b4e-8c8d-a82ccd12c630",
"stackTrace": [
" File \"/var/task/lambda_function.py\" Line 64\n match operation:\n"
]
}
Function Logs
START RequestId: ae5f2481-a836-4b4e-8c8d-a82ccd12c630 Version: $LATEST
[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 64)
Traceback (most recent call last):
File "/var/task/lambda_function.py" Line 64
match operation:END RequestId: ae5f2481-a836-4b4e-8c8d-a82ccd12c630
REPORT RequestId: ae5f2481-a836-4b4e-8c8d-a82ccd12c630 Duration: 1.34 ms Billed Duration: 2 ms Memory Size: 128 MB Max Memory Used: 35 MB Init Duration: 117.69 ms
Request ID
ae5f2481-a836-4b4e-8c8d-a82ccd12c630
所以在浪费了几个小时之后:
- 我看不到任何语法错误。
- 我试过捆绑枚举包(不知道是否有必要)。
- 我还评论了任何使用外部参数的行。
- 我在 Lambda 中使用 Python 3.9,在本地使用 python 3.10。
- 日志始终指向函数
get_url
中的第 64 行
match operation:
这是错误函数之前的代码:
import requests
import json
from enum import Enum
import urllib.parse
import boto3
import base64
max_number_of_digit = 1
base_url = "https://somesite.io"
app_version = "version-test/"
bubble_api_token = "some api token"
pdf_creator_api_token = 'some other api token'
class APP_ENTITY(str, Enum):
PROJECT = "project"
COMPANY = "company"
PAYMENT = "payment"
TRANCHE = "tranche"
COST_ITEMS = "payment"
DOCUMENT = "document"
class OPERATIONS(int, Enum):
CREATE = 1
DELETE = 2
PATCH = 3
READ = 4
GET = 5
document = dict()
customer = dict()
base_assets = [
'bank.png',
'calculator.png',
'card.png',
'caret.png',
'jetbrains-mono-v11-latin-500.woff2',
'jetbrains-mono-v11-latin-regular.woff2',
'line.png',
'logo.svg',
'poppins-v19-latin-300.woff2',
'poppins-v19-latin-500.woff2',
'poppins-v19-latin-600.woff2',
'poppins-v19-latin-regular.woff2',
'style.css'
]
def set_app_version(event):
global app_version
result = "version-test/"
try:
result = event['queryStringParameters']['appversion']
result = "" if (result.lower() != 'test') else "version-test/"
except:
result = "version-test/"
app_version = result
return result
def get_url(operation: OPERATIONS, entity: APP_ENTITY, cursor: int = 0, record_id: str = "", project_id: str = ""):
match operation:
case OPERATIONS.CREATE:
result = base_url + \
"/{}api/1.1/obj/{}/bulk".format(app_version, entity)
case OPERATIONS.DELETE:
result = base_url + \
"/{}api/1.1/obj/{}/".format(app_version, entity)
case OPERATIONS.PATCH:
result = base_url + \
"/{}api/1.1/obj/{}/{}".format(app_version, entity, record_id)
case OPERATIONS.READ:
match entity:
case APP_ENTITY.COST_ITEMS:
result = base_url + \
"/{}api/1.1/obj/{}/?api_token={}&cursor={}&constraints=".format(
app_version, entity, bubble_api_token, cursor)
constraints = [
{
"key": "type",
"constraint_type": "equals",
"value": "development"
},
{
"key": "project",
"constraint_type": "equals",
"value": project_id
}]
constraints = urllib.parse.quote_plus(
json.dumps(constraints))
result = result+constraints
case OPERATIONS.GET:
result = base_url + \
"/{}api/1.1/obj/{}/{}".format(app_version, entity, record_id)
return result
下面是 lambda 控制台的屏幕截图。 Lambda console
这里是错误的屏幕截图: Lambda error
match
和 case
是 Python 3.10 中新增的内容。 Lambda 不支持 Python 3.10, 因此您遇到了错误。您必须移植回您的代码才能使用 Python 3.9