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

Uploaded Python 3

File details

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

File metadata

  • Download URL: ygo-1.2.15.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.15.tar.gz
Algorithm Hash digest
SHA256 46d3378101b0903eb547905b433e0c8b7bdccbf976b33b37d3ddf7a5745bfc99
MD5 e2573e20b0d21581830303a081d834fd
BLAKE2b-256 b41c5aa68451bc883f73ae0b9d674a455bbef2c22c574e6e6c8759e1991ff90a

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ygo-1.2.15-py3-none-any.whl
Algorithm Hash digest
SHA256 e053e5b9241b1858ca80956d9f56dabbe386e7b993b375caa561ec66de71fe79
MD5 361d690e08578b1a32abd221154c0b08
BLAKE2b-256 c63289739e2b62a0281b01b29e7c999751b3f2a9aafd3f6b42832d1c68cb88a4

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