Skip to main content

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

  • dict
  • sorted
  • filter
  • map
  • enumerate

2. Standard Library Modules

Data Processing

  • json
  • decimal
  • uuid
  • base64
  • hashlib

String Processing

  • re (regular expressions)
  • string
  • textwrap
  • difflib (difference comparison)

Data Structures and Algorithms

  • copy
  • bisect (binary search)
  • heapq (heap queue)
  • statistics (statistical calculations)

Mathematical Computation

  • math
  • operator (operator functions)

Date and Time

  • datetime
  • time
  • calendar

Other Tools

  • random (random number generation)
  • requests (HTTP requests)

3. Type Annotations (typing module)

  • typing (the module itself)
  • Any
  • Union
  • Optional
  • Literal
  • Final
  • ClassVar
  • TypeVar
  • Generic
  • Protocol
  • runtime_checkable
  • overload
  • cast
  • TYPE_CHECKING
  • NoReturn
  • List
  • Dict
  • Set
  • Tuple
  • FrozenSet
  • Deque
  • Counter
  • ChainMap
  • OrderedDict
  • DefaultDict
  • MutableMapping
  • MutableSequence
  • MutableSet
  • Mapping
  • Sequence
  • AbstractSet
  • Collection
  • Container
  • Iterable
  • Iterator
  • Reversible
  • Sized
  • Hashable
  • Callable
  • Awaitable
  • Coroutine
  • AsyncIterable
  • AsyncIterator
  • AsyncGenerator
  • Generator
  • ContextManager
  • AsyncContextManager

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.

  1. 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.

  1. 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...