Workflow Custom Code Writing Guidelines
Background: For security reasons, the code writing for 【Python custom skill code】 && 【Advanced Orchestration - Code Node】 is subject to standardized specifications.
Supported Python Packages
1. Built-in Functions and Types
dictsortedfiltermapenumerate
2. Standard Library Modules
Data Processing
jsondecimaluuidbase64hashlib
String Processing
re(regular expressions)stringtextwrapdifflib(difference comparison)
Data Structures and Algorithms
copybisect(binary search)heapq(heap queue)statistics(statistical calculations)
Mathematical Computation
mathoperator(operator functions)
Date and Time
datetimetimecalendar
Other Tools
random(random number generation)requests(HTTP requests)
3. Type Annotations (typing module)
typing(the module itself)AnyUnionOptionalLiteralFinalClassVarTypeVarGenericProtocolruntime_checkableoverloadcastTYPE_CHECKINGNoReturnListDictSetTupleFrozenSetDequeCounterChainMapOrderedDictDefaultDictMutableMappingMutableSequenceMutableSetMappingSequenceAbstractSetCollectionContainerIterableIteratorReversibleSizedHashableCallableAwaitableCoroutineAsyncIterableAsyncIteratorAsyncGeneratorGeneratorContextManagerAsyncContextManager
Syntax Specifications
Due to restrictions such as secure environment isolation, code authors are required to follow the relevant specifications when writing code. Prerequisite: only built-in syntax, built-in libraries, and a limited set of standard libraries are supported.
- Using the math module
import math
def calculate(a, b):
result = math.fsum([a, b])
return result
Incorrect usage:
from math import fsum
def calculate(a, b):
result = fsum([a, b])
return result
Explanation: Due to the import restrictions in the current code block, only parent package imports are supported when importing packages; the from a import b syntax is not supported.
- Using datetime
Correct usage:
import typing
from datetime import datetime
def get_weather(location: str, date: typing.Optional[str] = None) -> dict:
# 处理默认日期
if date is None:
formatted_date = datetime.datetime.now().strftime("%Y-%m-%d")
else:
try:
# 尝试解析常见日期格式
if len(date) == 8 and date.isdigit():
# 处理 YYYYMMDD 格式
date_obj = datetime.datetime.strptime(date, "%Y%m%d")
else:
# 尝试标准格式 YYYY-MM-DD
date_obj = datetime.datetime.strptime(date, "%Y-%m-%d")
formatted_date = date_obj.strftime("%Y-%m-%d")
except ValueError:
# 如果格式不匹配,使用原始输入(可能导致API调用失败)
formatted_date = date
# 这里是模拟数据,实际应用中可能会调用天气API
weather_data = {
"location": location,
"date": formatted_date,
"forecast": "sunny",
"temperature": "25°C",
"wind": "light breeze"
}
return weather_data
Incorrect usage:
from typing import Optional, List, Dict, Any, Union, Callable
from datetime import datetime
...omitted...