Skip to main content

一个轻量级 Python 工具包,支持 并发执行(带进度条)、延迟调用、链式绑定参数、函数信息获取、模块/函数动态加载。

Project description

ygo

一个轻量级 Python 工具包,底层基于 joblib 和 tqdm 、loguru 实现,支持

  • 并发执行(带进度条)
  • 延迟调用
  • 链式绑定参数
  • 函数信息获取
  • 模块/函数动态加载...
  • 并结合 ylog 提供日志记录能力

安装

pip install -U ygo

🧰 功能概览

模块 功能
ygo 支持并发执行(带进度条)、延迟调用、函数信息获取以及模块/函数动态加载等功能
ylog 日志模块,提供统一的日志输出接口

示例

├── a
│   ├── __init__.py
│   └── b
│       ├── __init__.py
│       └── c.py
└── test.py

c.py 中定义了目标函数
def test_fn(a, b=2):
    return a+b

场景1: 并发执行

import ygo
import ylog
from a.b.c import test_fn

with ygo.pool(n_jobs=5, show_progress=True) as go:
    for i in range(10):
        go.submit(test_fn)(a=i, b=2*i)
    for res in go.do():
        ylog.info(res)

ygo.pool 支持的参数

参数名 类型 描述
n_jobs int 并行任务数(<=1 表示串行)
show_progress bool 是否显示进度条
backend str 执行后端(默认 'threading',可选 'multiprocessing' 或 'loky')

场景2: 延迟调用

>>> fn = delay(test_fn)(a=1, b=2)
>>> fn()
3
>>> # 逐步传递参数
>>> fn1 = delay(lambda a, b, c: a+b+c)(a=1)
>>> fn2 = delay(fn1)(b=2)
>>> fn2(c=3)
6
>>> # 参数更改
>>> fn1 = delay(lambda a, b, c: a+b+c)(a=1, b=2)
>>> fn2 = delay(fn1)(c=3, b=5)
>>> fn2()
9

场景3: 获取目标函数信息

>>> ygo.fn_info(test_fn)
=============================================================
    a.b.c.test_fn(a, b=2)
=============================================================
    def test_fn(a, b=2):
    return a+b

🔍 其他函数信息工具

方法名 描述
fn_params(fn) 获取函数实参
fn_signature_params(fn) 获取函数定义的所有参数名
fn_code(fn) 获取函数源码字符串
fn_path(fn) 获取函数所属模块路径
fn_from_str(s) 根据字符串导入函数(如 "a.b.c.test_fn")
module_from_str(s) 根据字符串导入模块

场景4: 通过字符串解析函数并执行

>>> ygo.fn_from_str("a.b.c.test_fn")(a=1, b=5)
6

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ygo-1.2.16.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ygo-1.2.16-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file ygo-1.2.16.tar.gz.

File metadata

  • Download URL: ygo-1.2.16.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.8

File hashes

Hashes for ygo-1.2.16.tar.gz
Algorithm Hash digest
SHA256 a9162274508ff2166596e3fa1fe009b09345b4677e08b1e8f1961316d8cb566f
MD5 acb5bb376444ba9c7a06c065d4eebad9
BLAKE2b-256 dae427feba271359cd3a8e76f5f5fe239a0faa1f9b6a3939faa0a4a9b70d2462

See more details on using hashes here.

File details

Details for the file ygo-1.2.16-py3-none-any.whl.

File metadata

  • Download URL: ygo-1.2.16-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.8

File hashes

Hashes for ygo-1.2.16-py3-none-any.whl
Algorithm Hash digest
SHA256 d43c55bda48d68af51fa8f1b62bead0148f0b95f566aa0992191a1d685e0e57b
MD5 d99781cf4a149788630e15cf62ae7b58
BLAKE2b-256 1ad689a5c6bb0aae8592395f905e68ed877f60a3892761a85f2b46c3c5fa31e2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page