-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathscript-example.py
60 lines (53 loc) · 2.29 KB
/
script-example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 基础演示
# Basic Demo
# 使用装饰器为函数命名为'两数相加',并允许外部调用本函数
# 额外的配置包括:
# category='math'
# 指定分类为 "math"
# tags=['basic', 'simple']
# 指定 2 个标签:'basic', 'simple'
# cache_result=300
# 指定处理结果缓存 300 秒。
# 首次调用成功后,后续使用完全相同的参数进行 API 调用时,系统将直接返回结果而不需要重新运行
# timeout=10
# 指定函数执行超时时间为 10 秒
# 当函数执行超过10秒时,将强制中断并退出
#
# (更多额外配置请参考脚本开发手册 https://func.guance.com/doc/development-guide-builtin-features-dff-api/)
# Use a decorator to name the function 'add two numbers' and allow external calls to this function
# Additional configurations include:
# category='math'
# Specify the category as “math”
# tags=['basic', 'simple']
# Specify 2 tags: 'basic', 'simple'
# cache_result=300
# Specify to cache the result for 300 seconds.
# After the first successful call, subsequent API calls with the exact same parameters will return the results directly without rerunning the API
# timeout=10
# Specify a timeout of 10 seconds for function execution.
# When function execution exceeds 10 seconds, it will force a break and exit.
#
# (For more additional configurations please refer to the script development manual at https://func.guance.com/doc/development-guide-builtin-features-dff-api/)
@DFF.API('两数相加 / Adding two numbers', category='math', tags=['math', 'simple'], cache_result=300, timeout=10)
def plus(x, y):
'''
两数相加
Adding two numbers
输入参数 x, y 均为数字类型,返回结果为两者之和
The input parameters x, y are numeric and the return result is the sum of the two.
'''
print('INPUT: x = {}, y = {}'.format(x, y))
_x = float(x)
_y = float(y)
result = _x + _y
if isinstance(x, int) and isinstance(y, int):
result = int(result)
print('\tRESULT: {}'.format(result))
return result
# 测试函数不需要装饰器
# Test functions don't need a decorator
def test_plus():
assert plus(1, 1) == 2
assert plus(1, 1.1) == 2.1
assert plus(1.1, 1.2) == 2.3
return 'OK'