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.17.tar.gz (12.0 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.17-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ygo-1.2.17.tar.gz
Algorithm Hash digest
SHA256 a26b305eabe66f8c9dc41bbe93efaa089608398ec2f5900d9e88925a908df474
MD5 5099712119f707ef5a61ee27b5955e34
BLAKE2b-256 034e5016ca9b98c90296a8a30a0fee02a2518219304008fa5e6ea9a98c1480c9

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ygo-1.2.17-py3-none-any.whl
Algorithm Hash digest
SHA256 d77db0620aae4222142167d3995d12adb0c4d9c48361369bf1dc362010171313
MD5 b50be337c9946497c891ecbd885bf575
BLAKE2b-256 8e88626367817ab0f8558e7def00ccde57c386ce151bcf897446c11247d2d462

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