Python Progress Bar由' tqdm'创建然后是每次迭代后的意外值/符号

发布于 2025-01-22 23:02:31 字数 981 浏览 2 评论 0原文

我正在使用“ TQDM”软件包来建立进度栏。但是,在每次奇数的迭代(1,3,5)发生后,发生意外的值/数字(在这种情况下,当参数'ascii'等于true时,将出现数字5)。

Fortschritt:  17%|####5                      | 1/6 [00:02<00:11,  2.20s/it]
Fortschritt:  33%|#########                  | 2/6 [00:07<00:14,  3.54s/it]
Fortschritt:  50%|#############5             | 3/6 [00:13<00:13,  4.65s/it]

该代码相对复杂,因此我不会在此处复制它们...如果有必要发现和解决问题,我将在此后减少并粘贴在这里。

任何帮助将不胜感激。 :)

----------代码补充--------------

from os import makedirs
from tqdm import tqdm
import time

Workflow = ['0','1', '2', '3', '4', '5', '6']
Probenliste = ['A','B']
Oligos= {'Oligo1':'Sequence1', 'Oligo2':'Sequence2'}

for schritt in tqdm(range(len(Workflow)-1), desc='Fortschritt', ascii=True, ncols=75):
    schritt_name = Workflow[schritt+1]
    makedirs(schritt_name)
    for n in range(len(Probenliste)):
        probe = Probenliste[n]
        for primer_name, sequence in Oligos.items():
            time.sleep(1)

I am using 'tqdm' package to establish a progress bar. However, after every iteration of odd numbers (1,3,5) an unexpected value/number occurs (in this case number 5 appears when argument 'ascii' is equal to True).

Fortschritt:  17%|####5                      | 1/6 [00:02<00:11,  2.20s/it]
Fortschritt:  33%|#########                  | 2/6 [00:07<00:14,  3.54s/it]
Fortschritt:  50%|#############5             | 3/6 [00:13<00:13,  4.65s/it]

The code is relative complex so I would not copy them here... if it is necessary for finding out and solving the problem, I will reduce it and paste here afterwards.

any help will be appreciated. :)

---------code supplemented----------

from os import makedirs
from tqdm import tqdm
import time

Workflow = ['0','1', '2', '3', '4', '5', '6']
Probenliste = ['A','B']
Oligos= {'Oligo1':'Sequence1', 'Oligo2':'Sequence2'}

for schritt in tqdm(range(len(Workflow)-1), desc='Fortschritt', ascii=True, ncols=75):
    schritt_name = Workflow[schritt+1]
    makedirs(schritt_name)
    for n in range(len(Probenliste)):
        probe = Probenliste[n]
        for primer_name, sequence in Oligos.items():
            time.sleep(1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒岛晴空 2025-01-29 23:02:31

根据 [github]:tqdm/tqdm- documentation 是我的):

  • ascii:bool或str,可选

    如果未指定或错误,请使用Unicode(平滑块)填充仪表。 后备是使用ASCII字符“ 123456789#”

我在各种参数中播放了一点,我从经验上发现了这一点(我没有严格的解释),因为我没有足够的研究:

  • “时髦”的值来自字符串: < strong>“ 123456789#” (当 ascii true ),
  • 它们依赖:
    1. 迭代(其长度)
    2. 栏的长度(称为)本身,取决于 desc 的长度和 ncols
    • 看起来像 #1中的元素索引之间的分区(其余)。 #2。


可以使用更简单的代码复制该行为。

code00.py

#!/usr/bin/env python

import sys
import time

from tqdm import tqdm


def main(*argv):
    data = " " * 6

    cols = (
        49,  # 8, 6, 5, 3, 1, ...
        48,  # 6, 3,  , ...
        47,  # 5,  , ...
        46,  # 3, 6,  , ...
        45,  # 1, 3, 5, 6, 8, ...
        44,  # 
    )

    sleep_time = 0.5

    print("Various lengths:")
    ascii = True
    #ascii = " #"  # !!! DECOMMENT THIS LINE for the problem to go away !!!
    for col in cols:
        for _ in tqdm(data, desc="X", ascii=ascii, ncols=col):
            time.sleep(sleep_time)

    print("\nA combination of parameters that displays all digits")
    for _ in tqdm(range(10), desc="X", ascii=True, ncols=50):
        time.sleep(sleep_time)


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.")
    sys.exit(rc)

output (尽管仅是最相关的最后一个,因为它不会捕获行为):

  [cfati@cfati-5510-0:e:\ work \ dev \ stackoverflow \ q071951854]&gt; ”
Python 3.9.9(标签/v3.9.9:CCB0E6A,2021,18:08:50)[MSC V.1929 64 BIT(AMD64)] 064BIT

各个长度:
X:100%| ########### | 6/6 [00:03&lt; 00:00,1.95it/s]
X:100%| ########## | 6/6 [00:03&lt; 00:00,1.96it/s]
X:100%| ######### | 6/6 [00:03&lt; 00:00,1.95it/s]
X:100%| ######## | 6/6 [00:03&lt; 00:00,1.95it/s]
X:100%| ####### | 6/6 [00:03&lt; 00:00,1.96it/s]
X:100%| ###### | 6/6 [00:03&lt; 00:00,1.95it/s]

显示所有数字的参数组合
X:100%| ########## | 10/10 [00:05&lt; 00:00,1.95it/s]

完毕。
 

fix

使用字符串 “#” (对于 ascii ),仅包含初始 char space )和最后一个( pound )。这样,就不会有任何可能的中间 char

According to [GitHub]: tqdm/tqdm - Documentation (emphasis is mine):

  • ascii : bool or str, optional

    If unspecified or False, use unicode (smooth blocks) to fill the meter. The fallback is to use ASCII characters " 123456789#".

I played a bit with the various parameters, and I discovered this empirically (I don't have a rigorous explanation), as I didn't dive enough into the code:

  • The "funky" values come from the string: " 123456789#" (when ascii is True)
  • They depend on:
    1. iterable (its length)
    2. The length of the bar (called meter) itself, which depends on the length of desc and the value of ncols
    • Looks like a division (remainder) between the element index in #1. and #2.

The behavior can be reproduced using much simpler code.

code00.py:

#!/usr/bin/env python

import sys
import time

from tqdm import tqdm


def main(*argv):
    data = " " * 6

    cols = (
        49,  # 8, 6, 5, 3, 1, ...
        48,  # 6, 3,  , ...
        47,  # 5,  , ...
        46,  # 3, 6,  , ...
        45,  # 1, 3, 5, 6, 8, ...
        44,  # 
    )

    sleep_time = 0.5

    print("Various lengths:")
    ascii = True
    #ascii = " #"  # !!! DECOMMENT THIS LINE for the problem to go away !!!
    for col in cols:
        for _ in tqdm(data, desc="X", ascii=ascii, ncols=col):
            time.sleep(sleep_time)

    print("\nA combination of parameters that displays all digits")
    for _ in tqdm(range(10), desc="X", ascii=True, ncols=50):
        time.sleep(sleep_time)


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.")
    sys.exit(rc)

Output (although only the final one which is not very relevant, as it doesn't capture the behavior):

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q071951854]> "e:\Work\Dev\VEnvs\py_pc064_03.09_test0\Scripts\python.exe" code00.py
Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] 064bit on win32

Various lengths:
X: 100%|###########| 6/6 [00:03<00:00,  1.95it/s]
X: 100%|##########| 6/6 [00:03<00:00,  1.96it/s]
X: 100%|#########| 6/6 [00:03<00:00,  1.95it/s]
X: 100%|########| 6/6 [00:03<00:00,  1.95it/s]
X: 100%|#######| 6/6 [00:03<00:00,  1.96it/s]
X: 100%|######| 6/6 [00:03<00:00,  1.95it/s]

A combination of parameters that displays all digits
X: 100%|##########| 10/10 [00:05<00:00,  1.95it/s]

Done.

The fix:

Use the string " #" (for ascii) which only contains the initial char (SPACE) and the final one (POUND, #). This way, there won't be any possible intermediate char.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文