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.13.tar.gz (12.5 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.13-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ygo-1.2.13.tar.gz
Algorithm Hash digest
SHA256 a099f487f89c7870962d0a804d29c571ea8817ecabd60788de6a4ac23cc04d5a
MD5 c3a8a4d55cdf5139fccd6a723306cad3
BLAKE2b-256 2361954794081707e5b2b3638b9df5b47f9775d080cfa2e007583ecca56cbd77

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ygo-1.2.13-py3-none-any.whl
Algorithm Hash digest
SHA256 c155ba5ff13ab2d6e44ffc9874cf6b0507f6a412c0732a8b75bf285600cf771a
MD5 0f1a83500b41d7aabf67fcfeb221d394
BLAKE2b-256 1fb7ea4c96009fa3b8249bcba83b1305c167ae4165d70e37d44c463dff252535

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