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.18.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.18-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ygo-1.2.18.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.18.tar.gz
Algorithm Hash digest
SHA256 d899bd548aec095a7a79132e6884b011870dbd08c4f9caa9ce3304c154f98387
MD5 d9a3040b789a3368d30d70524569eea8
BLAKE2b-256 ab503ce51ced620b99c78b31221cebf40119d0f55093cb0ee7e027810f5e2ef0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ygo-1.2.18-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.18-py3-none-any.whl
Algorithm Hash digest
SHA256 8ef44ff0f38379d7016070334b66ca3085dd6489e9dd0027deb686b45801caf3
MD5 db4de4f3dd5c5c5749f0fec4bd086589
BLAKE2b-256 2f17e16101f6743ca09492ed2dab21345aa67bdb5e4c3af51d312f7d80138ab8

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