Skip to main content

farspec

farspec 是一个纯 Python 的任务协议规范包:提供 Request/Response/Task 基类, 以及 dataclass ↔ dict(JSON 友好)的通用序列化工具,方便在不同服务/任务系统之间 约定统一的输入输出结构。不依赖网络、数据库或云服务。

特性

  • BaseRequest:请求基类,自动生成 request_id,meta 承载版本/追踪等横切信息
  • BaseResponse:响应基类,内置 status/events/耗时统计与成功/失败标记方法
  • BaseTask:模板方法基类,run() 统一处理开始/结束时间戳、事件记录与异常捕获
  • to_jsonable/from_jsonable/dataclass_to_dict/dataclass_from_dict:通用的 dataclass ↔ JSON 友好 dict 转换工具,支持 X | None、list、dict、Enum、 datetime、嵌套 dataclass

环境要求

  • Python 3.10 或更高版本

安装

pip install farspec

本地开发用可编辑安装:

git clone https://github.com/farfarfun/farspec.git
cd farspec
pip install -e .

快速开始

from farspec.task import BaseRequest, BaseResponse, BaseTask


class EchoTask(BaseTask[BaseRequest, BaseResponse]):
    def build_response(self) -> BaseResponse:
        return BaseResponse()

    def execute(self, response: BaseResponse, *args, **kwargs) -> BaseResponse:
        response.add_event("echo", "doing work")
        response.mark_succeeded(payload={"echoed": True})
        return response


task = EchoTask(BaseRequest(request_id="req-42"))
result = task.run()

print(result.status)  # TaskStatus.SUCCEEDED
print(result.payload)  # {'echoed': True}
print(result.to_dict())  # JSON 友好 dict,可直接 json.dumps

开发

uv sync
uv run ruff check .
uv run ruff format --check .
uv run pytest tests/ -v

许可证

本项目使用 MIT License。


关于 farfarfun

farfarfun 是一个专注于实用工具库的开源组织, 涵盖云存储、数据处理、AI、多媒体与开发工具链等方向。

本项目基于 MIT 协议开源。

Metadata

Release files for farspec 1.0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for farspec 1.0.11
File Interpreter ABI Platform
farspec-1.0.11-py3-none-any.whl Python 3 none any Details

Release files / farspec-1.0.11-py3-none-any.whl

Download URL farspec-1.0.11-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
765957f7a0fcade6f388ec5aa82bf989f70ffc63e3fb1ad0b406b2e05cf6c4b4
BLAKE2b-256 checksum
How to use checksums
81df26028af1806a863e964ff767f53e4e8fa2201d2629b77fb8af92d096b6c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.22 {"installer":{"name":"uv","version":"0.12.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

1.0.11 This release

1 release file

1.0.10

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page