refactor: 将装饰器 gobridge 重命名为 expose,并添加 pyproject.toml
This commit is contained in:
20
README.md
20
README.md
@@ -28,7 +28,7 @@ Python 端直接复制 `python/gobridge/` 目录到项目中,无需安装依
|
|||||||
from gobridge import gobridge, run
|
from gobridge import gobridge, run
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def add(a: int, b: int) -> int:
|
def add(a: int, b: int) -> int:
|
||||||
return a + b
|
return a + b
|
||||||
|
|
||||||
@@ -61,15 +61,15 @@ result, err := gobridge.Invoke[[]User](ctx, pool, "enrich_users", users)
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
# Python
|
# Python
|
||||||
@gobridge
|
@expose
|
||||||
def add(a: int, b: int) -> int:
|
def add(a: int, b: int) -> int:
|
||||||
return a + b
|
return a + b
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def get_user(uid: int) -> dict:
|
def get_user(uid: int) -> dict:
|
||||||
return {"id": uid, "name": f"user_{uid}", "score": uid * 1.5}
|
return {"id": uid, "name": f"user_{uid}", "score": uid * 1.5}
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def enrich_users(users: list) -> list:
|
def enrich_users(users: list) -> list:
|
||||||
for u in users:
|
for u in users:
|
||||||
u["level"] = "gold" if u["score"] >= 10 else "silver"
|
u["level"] = "gold" if u["score"] >= 10 else "silver"
|
||||||
@@ -95,12 +95,12 @@ for u := range userCh {
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
# Python
|
# Python
|
||||||
@gobridge
|
@expose
|
||||||
def range_gen(start: int, stop: int) -> Iterator[int]:
|
def range_gen(start: int, stop: int) -> Iterator[int]:
|
||||||
for i in range(start, stop):
|
for i in range(start, stop):
|
||||||
yield i
|
yield i
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def gen_users(count: int) -> Iterator[dict]:
|
def gen_users(count: int) -> Iterator[dict]:
|
||||||
for i in range(1, count + 1):
|
for i in range(1, count + 1):
|
||||||
yield {"id": i, "name": f"user_{i}", "score": float(i * 3)}
|
yield {"id": i, "name": f"user_{i}", "score": float(i * 3)}
|
||||||
@@ -125,7 +125,7 @@ fmt.Println(total) // 15
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
# Python
|
# Python
|
||||||
@gobridge
|
@expose
|
||||||
def sum_stream(numbers: Iterator[int]) -> int:
|
def sum_stream(numbers: Iterator[int]) -> int:
|
||||||
return sum(numbers)
|
return sum(numbers)
|
||||||
```
|
```
|
||||||
@@ -151,7 +151,7 @@ for u := range outCh {
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
# Python
|
# Python
|
||||||
@gobridge
|
@expose
|
||||||
def process_users(users: Iterator[dict]) -> Iterator[dict]:
|
def process_users(users: Iterator[dict]) -> Iterator[dict]:
|
||||||
for u in users:
|
for u in users:
|
||||||
yield {"id": u["id"], "name": u["name"].upper(), "score": u["score"] * 2}
|
yield {"id": u["id"], "name": u["name"].upper(), "score": u["score"] * 2}
|
||||||
@@ -170,7 +170,7 @@ result, err := gobridge.Invoke[int](ctx, pool, "slow_compute", 1000000)
|
|||||||
```
|
```
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@gobridge
|
@expose
|
||||||
def slow_compute(n: int) -> int:
|
def slow_compute(n: int) -> int:
|
||||||
total = 0
|
total = 0
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
@@ -360,7 +360,7 @@ gobridge = { git = "https://git.fsdpf.net/go/gobridge.git", subdirectory = "pyth
|
|||||||
│ │ _dispatch() │ → 执行线程抛 InterruptedError│ │
|
│ │ _dispatch() │ → 执行线程抛 InterruptedError│ │
|
||||||
│ │ │ │ │ │
|
│ │ │ │ │ │
|
||||||
│ │ ┌──────▼──────┐ │ │ │
|
│ │ ┌──────▼──────┐ │ │ │
|
||||||
│ │ │ @gobridge fn│ │ │ │
|
│ │ │ @expose fn│ │ │ │
|
||||||
│ │ │ │ │ │ │
|
│ │ │ │ │ │ │
|
||||||
│ │ │ 普通函数 │ │ │ │
|
│ │ │ 普通函数 │ │ │ │
|
||||||
│ │ │ return val ──────────────► result/error │ │
|
│ │ │ return val ──────────────► result/error │ │
|
||||||
|
|||||||
@@ -2,29 +2,29 @@ import sys
|
|||||||
import os
|
import os
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "python"))
|
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
|
from typing import Iterator
|
||||||
|
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def add(a: int, b: int) -> int:
|
def add(a: int, b: int) -> int:
|
||||||
return a + b
|
return a + b
|
||||||
|
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def range_gen(start: int, stop: int) -> Iterator[int]:
|
def range_gen(start: int, stop: int) -> Iterator[int]:
|
||||||
"""流式输出:对应 Go 侧 Invoke[chan int]"""
|
"""流式输出:对应 Go 侧 Invoke[chan int]"""
|
||||||
for i in range(start, stop):
|
for i in range(start, stop):
|
||||||
yield i
|
yield i
|
||||||
|
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def sum_stream(numbers: Iterator[int]) -> int:
|
def sum_stream(numbers: Iterator[int]) -> int:
|
||||||
"""流式输入:对应 Go 侧传入 chan int 参数"""
|
"""流式输入:对应 Go 侧传入 chan int 参数"""
|
||||||
return sum(numbers)
|
return sum(numbers)
|
||||||
|
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def double_stream(numbers: Iterator[int]) -> Iterator[int]:
|
def double_stream(numbers: Iterator[int]) -> Iterator[int]:
|
||||||
"""双向流:输入每个数,yield 其平方,对应 Go 侧 Invoke[chan int](c, ctx, "double_stream", inputChan)"""
|
"""双向流:输入每个数,yield 其平方,对应 Go 侧 Invoke[chan int](c, ctx, "double_stream", inputChan)"""
|
||||||
for n in numbers:
|
for n in numbers:
|
||||||
@@ -33,19 +33,19 @@ def double_stream(numbers: Iterator[int]) -> Iterator[int]:
|
|||||||
|
|
||||||
# ── struct(dict)类型 ──────────────────────────────────────────────────────
|
# ── struct(dict)类型 ──────────────────────────────────────────────────────
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def get_user(uid: int) -> dict:
|
def get_user(uid: int) -> dict:
|
||||||
"""普通调用:返回一个 struct(Go 对应 User)"""
|
"""普通调用:返回一个 struct(Go 对应 User)"""
|
||||||
return {"id": uid, "name": f"user_{uid}", "score": uid * 1.5}
|
return {"id": uid, "name": f"user_{uid}", "score": uid * 1.5}
|
||||||
|
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def total_score(users: list) -> float:
|
def total_score(users: list) -> float:
|
||||||
"""slice 输入:接收 []User,返回总分"""
|
"""slice 输入:接收 []User,返回总分"""
|
||||||
return sum(u["score"] for u in users)
|
return sum(u["score"] for u in users)
|
||||||
|
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def enrich_users(users: list) -> list:
|
def enrich_users(users: list) -> list:
|
||||||
"""slice 输入输出:为每个 user 追加 level 字段"""
|
"""slice 输入输出:为每个 user 追加 level 字段"""
|
||||||
result = []
|
result = []
|
||||||
@@ -58,14 +58,14 @@ def enrich_users(users: list) -> list:
|
|||||||
|
|
||||||
# ── struct/slice 流式组合 ────────────────────────────────────────────────────
|
# ── struct/slice 流式组合 ────────────────────────────────────────────────────
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def gen_users(count: int) -> Iterator[dict]:
|
def gen_users(count: int) -> Iterator[dict]:
|
||||||
"""流式输出 struct:yield 多个 User,对应 Go 侧 Invoke[chan User]"""
|
"""流式输出 struct:yield 多个 User,对应 Go 侧 Invoke[chan User]"""
|
||||||
for i in range(1, count + 1):
|
for i in range(1, count + 1):
|
||||||
yield {"id": i, "name": f"user_{i}", "score": float(i * 3)}
|
yield {"id": i, "name": f"user_{i}", "score": float(i * 3)}
|
||||||
|
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def process_users(users: Iterator[dict]) -> Iterator[dict]:
|
def process_users(users: Iterator[dict]) -> Iterator[dict]:
|
||||||
"""双向流 struct:输入流式 User,yield 处理后的 User"""
|
"""双向流 struct:输入流式 User,yield 处理后的 User"""
|
||||||
for u in users:
|
for u in users:
|
||||||
|
|||||||
@@ -3,24 +3,24 @@ gobridge - Python 端库,配合 Go 侧 gobridge 使用
|
|||||||
|
|
||||||
用法::
|
用法::
|
||||||
|
|
||||||
from gobridge import gobridge, run
|
from gobridge import expose, run
|
||||||
from typing import Iterator
|
from typing import Iterator
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def add(a: int, b: int) -> int:
|
def add(a: int, b: int) -> int:
|
||||||
return a + b
|
return a + b
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def range_gen(start: int, stop: int) -> Iterator[int]:
|
def range_gen(start: int, stop: int) -> Iterator[int]:
|
||||||
for i in range(start, stop):
|
for i in range(start, stop):
|
||||||
yield i # 对应 Go 侧 Invoke[chan int]
|
yield i # 对应 Go 侧 Invoke[chan int]
|
||||||
|
|
||||||
@gobridge
|
@expose
|
||||||
def sum_stream(numbers: Iterator[int]) -> int:
|
def sum_stream(numbers: Iterator[int]) -> int:
|
||||||
return sum(numbers) # 对应 Go 侧传入 chan int 参数
|
return sum(numbers) # 对应 Go 侧传入 chan int 参数
|
||||||
|
|
||||||
# ctx 取消时,框架自动向该线程注入 InterruptedError,无需在函数中检查
|
# ctx 取消时,框架自动向该线程注入 InterruptedError,无需在函数中检查
|
||||||
@gobridge
|
@expose
|
||||||
def slow_compute(n: int) -> int:
|
def slow_compute(n: int) -> int:
|
||||||
total = 0
|
total = 0
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
@@ -43,7 +43,7 @@ import threading
|
|||||||
_exposed: dict = {}
|
_exposed: dict = {}
|
||||||
|
|
||||||
|
|
||||||
def gobridge(fn):
|
def expose(fn):
|
||||||
"""装饰器:将函数暴露给 Go 侧调用"""
|
"""装饰器:将函数暴露给 Go 侧调用"""
|
||||||
_exposed[fn.__name__] = fn
|
_exposed[fn.__name__] = fn
|
||||||
return fn
|
return fn
|
||||||
|
|||||||
12
python/pyproject.toml
Normal file
12
python/pyproject.toml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
[build-system]
|
||||||
|
requires = ["hatchling"]
|
||||||
|
build-backend = "hatchling.build"
|
||||||
|
|
||||||
|
[project]
|
||||||
|
name = "gobridge"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = "Python 端库,配合 Go 侧 gobridge 使用"
|
||||||
|
requires-python = ">=3.10"
|
||||||
|
|
||||||
|
[tool.hatch.build.targets.wheel]
|
||||||
|
packages = ["gobridge"]
|
||||||
Reference in New Issue
Block a user