未提供项目描述
项目描述
此库提供了一个简单的接口,用于注册不同的工作负载,并根据当前环境使用纯文本进度条或jupyter小部件来可视化它们的进度。
它可选地依赖于Jupyter小部件,在交互式的Jupyter笔记本环境中绘制漂亮的进度条。
安装
使用pip
pip install progress_reporter
如果您使用IPython/Jupyter,强烈建议您也安装jupyter小部件
pip install ipywidgets
示例
假设您有一个执行一些复杂计算的类,这些计算被分成几个作业/任务/线程等。
为了可视化进度,只需要从progress_reporter.ProgressReporter派生工作类,并调用_progress_register方法来告诉报告者需要完成多少项工作。然后报告者通过调用_progress_update(n)来告诉报告者已经派发了多少项工作。
请注意,这些是“私有”的,用作混合类,而不是污染公共接口。
from progress_reporter import ProgressReporter
import time
class ExampleWorker(ProgressReporter):
def __init__(self, n_jobs=100):
self.n_jobs = n_jobs
""" register the amount of work with the given description """
self._progress_register(n_jobs, description='Dispatching jobs')
def work(self):
""" do some fake work (sleep) and update the progress via the reporter
"""
for job in (lambda: time.sleep(0.1) for _ in range(self.n_jobs)):
job()
# indicate we've finished one job, to update the progress bar
self._progress_update(1)
它还支持通过设置参数阶段的多阶段顺序工作负载。这只是对底层进程的字典键
class MultiStageWorker(ProgressReporter):
def __init__(self, n_jobs_init, n_jobs):
self.n_jobs_init = n_jobs_init
self.n_jobs = n_jobs
""" register an expensive initialization routine """
self._progress_register(self.n_jobs_init, description='initializing', stage=0)
""" register the main computation """
self._progress_register(self.n_jobs, description='main computation', stage=1)
def work(self):
""" do the initialization """
for job in (lambda: time.sleep(0.1) for _ in range(self.n_jobs_init)):
job()
self._progress_update(1, stage=0)
""" perform the next stage of the algorithm """
for job in (lambda: time.sleep(0.2) for _ in range(self.n_jobs)):
job()
self._progress_update(1, stage=1)
从2.0版本开始,还有一个适用于组合的此类的版本。
from progress_reporter import ProgressReporter_
class Estimator(object):
def fit(self, X, y=None):
pg = ProgressReporter_()
pg.register(100, description='work')
with pg.context(): # ensure progress bars are closed if an exception occurs.
pg.update(50)
# ...
项目详情
关闭
progress-reporter-2.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 3268b3281f06894643073dae5205e41e2a3b3811b7a3c34e39ece8b4107e9a7b |
|
MD5 | 12a9206f61d36ec473956590d3a375ee |
|
BLAKE2b-256 | aa1fe2b16e98bc70a5a15d7df9ea2559d74ca037bf1c572387308111eaaad20b |