refactor: 将装饰器 gobridge 重命名为 expose,并添加 pyproject.toml
This commit is contained in:
@@ -2,29 +2,29 @@ import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "python"))
|
||||
|
||||
from gobridge import gobridge, run
|
||||
from gobridge import expose, run
|
||||
from typing import Iterator
|
||||
|
||||
|
||||
@gobridge
|
||||
@expose
|
||||
def add(a: int, b: int) -> int:
|
||||
return a + b
|
||||
|
||||
|
||||
@gobridge
|
||||
@expose
|
||||
def range_gen(start: int, stop: int) -> Iterator[int]:
|
||||
"""流式输出:对应 Go 侧 Invoke[chan int]"""
|
||||
for i in range(start, stop):
|
||||
yield i
|
||||
|
||||
|
||||
@gobridge
|
||||
@expose
|
||||
def sum_stream(numbers: Iterator[int]) -> int:
|
||||
"""流式输入:对应 Go 侧传入 chan int 参数"""
|
||||
return sum(numbers)
|
||||
|
||||
|
||||
@gobridge
|
||||
@expose
|
||||
def double_stream(numbers: Iterator[int]) -> Iterator[int]:
|
||||
"""双向流:输入每个数,yield 其平方,对应 Go 侧 Invoke[chan int](c, ctx, "double_stream", inputChan)"""
|
||||
for n in numbers:
|
||||
@@ -33,19 +33,19 @@ def double_stream(numbers: Iterator[int]) -> Iterator[int]:
|
||||
|
||||
# ── struct(dict)类型 ──────────────────────────────────────────────────────
|
||||
|
||||
@gobridge
|
||||
@expose
|
||||
def get_user(uid: int) -> dict:
|
||||
"""普通调用:返回一个 struct(Go 对应 User)"""
|
||||
return {"id": uid, "name": f"user_{uid}", "score": uid * 1.5}
|
||||
|
||||
|
||||
@gobridge
|
||||
@expose
|
||||
def total_score(users: list) -> float:
|
||||
"""slice 输入:接收 []User,返回总分"""
|
||||
return sum(u["score"] for u in users)
|
||||
|
||||
|
||||
@gobridge
|
||||
@expose
|
||||
def enrich_users(users: list) -> list:
|
||||
"""slice 输入输出:为每个 user 追加 level 字段"""
|
||||
result = []
|
||||
@@ -58,14 +58,14 @@ def enrich_users(users: list) -> list:
|
||||
|
||||
# ── struct/slice 流式组合 ────────────────────────────────────────────────────
|
||||
|
||||
@gobridge
|
||||
@expose
|
||||
def gen_users(count: int) -> Iterator[dict]:
|
||||
"""流式输出 struct:yield 多个 User,对应 Go 侧 Invoke[chan User]"""
|
||||
for i in range(1, count + 1):
|
||||
yield {"id": i, "name": f"user_{i}", "score": float(i * 3)}
|
||||
|
||||
|
||||
@gobridge
|
||||
@expose
|
||||
def process_users(users: Iterator[dict]) -> Iterator[dict]:
|
||||
"""双向流 struct:输入流式 User,yield 处理后的 User"""
|
||||
for u in users:
|
||||
|
||||
Reference in New Issue
Block a user