17000 stars! A simple and practical Python progress bar library

Love to learn 2021-02-21 09:38:53
stars simple practical python progress


<pre style="letter-spacing: 0.544px; background-color: rgb(255, 255, 255);" data-mpa-powered-by="yiban.io"> <p style="letter-spacing: 0.544px; white-space: normal; color: rgb(0, 0, 0); font-size: 16px; font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; text-align: center;"> <br> </p> <section data-tool="mdnice Editor " data-website="https://www.mdnice.com" style="padding-right: 10px; padding-left: 10px; white-space: normal; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif; text-align: left; word-break: break-word; font-size: 14px; letter-spacing: 0.05em; color: rgb(89, 89, 89); line-height: 2;"> <p style="margin-right: 8px; margin-left: 8px; text-indent: 0em; letter-spacing: 0.544px; widows: 1; font-family: -apple-system-font, system-ui, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; text-align: center;"> <span style="color: rgb(136, 136, 136); font-family: -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif; font-size: 12px; font-weight: bolder; letter-spacing: 0.544px; text-indent: 0em; word-spacing: 2px;"><mpchecktext contenteditable="false" id="1613870292538_0.7095697924585018"> </mpchecktext></span> </p> </section> </pre>

<p style="white-space: normal; text-align: center;"> <img class="rich_pages" data-ratio="0.3333333333333333" data-s="300,640" src="https://mmbiz.qpic.cn/mmbiz_png/V7hBdY6BMtfUyxxT3YWWuJJg8kncWYICIBHUghXxZWjtCIFNiaeGq0X9icwtf33TCQLMYQib4DwK13g9ATRH3icQwg/640?wx_fmt=png" data-type="png" data-w="600"> </p> <p style="white-space: normal;"> <br> </p> <section data-tool="mdnice Editor " data-website="https://www.mdnice.com" style="padding-right: 10px; padding-left: 10px; white-space: normal; color: black; word-break: break-word; text-align: left; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif; font-size: 14px; letter-spacing: 0.05em; line-height: 2;"> <figure data-tool="mdnice Editor " style="margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"> <img data-ratio="0.45" src="https://mmbiz.qpic.cn/mmbiz_png/QFzRdz9libEbaz9yRwwTOb5osSbsPqkKg6JrLic1I77cO5PKSAv7YpibUNl603ObKoBtgibp3nJDoTMSn9l6ZZ7AVg/640?wx_fmt=png" data-type="png" data-w="700" style="margin-right: auto; margin-left: auto; display: block; box-shadow: rgb(51, 51, 51) 0px 0px 10px; border-radius: 5px;"> </figure> </section> <p data-tool="mdnice Editor " style="padding-top: 8px; padding-bottom: 8px; white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> <span style="color: rgb(89, 89, 89); font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif; font-size: 15px; letter-spacing: 0.7px;"> yes , we have Python The program is big , Execution takes a long time , Some users are “ Blind ” Waiting for the result of running , But it's not clear how far the program has progressed , It's very likely that the program will be terminated due to impatience .<mpchecktext contenteditable="false" id="1613870292539_0.990648515700687"> </mpchecktext></span> </p> <p data-tool="mdnice Editor " style="padding-top: 8px; padding-bottom: 8px; white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> <span style="color: rgb(89, 89, 89); font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif; font-size: 15px; letter-spacing: 0.7px;"> This one introduced in this article has 1.7 ten thousand Star Open source library , Can solve this trouble .<mpchecktext contenteditable="false" id="1613870292540_0.14276449973371141"> </mpchecktext></span> </p> <figure data-tool="mdnice Editor " style="margin-top: 10px; margin-bottom: 10px; white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); display: flex; flex-direction: column; justify-content: center; align-items: center;"> <img data-ratio="0.352549889135255" data-type="png" data-w="902" src="https://mmbiz.qpic.cn/mmbiz_png/fhujzoQe7TpWtD8oiaUsgSJFCLaoMq5cU3uVcjaAOebeZ8hvt8uMbwXXSP29WSK3cEO5DrUCiaBUA10WnqggPianw/640?wx_fmt=png" style="margin-right: auto; margin-left: auto; display: block; box-sizing: border-box !important; width: 463px !important; visibility: visible !important;"> </figure> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;">https://github.com/tqdm/tqdm <mpchecktext contenteditable="false" id="1613870292541_0.38013013837788656"> </mpchecktext> </p> <h3 data-tool="mdnice Editor " style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; font-size: 20px; white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; text-align: left; background-color: rgb(255, 255, 255);"> <span style="font-size: 18px; color: rgb(0, 82, 255);">1、Tqdm brief introduction <mpchecktext contenteditable="false" id="1613870292542_0.46720602469384587"> </mpchecktext></span> </h3> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> Use Python Tqdm Progress bar library , It can make python Visualization of program execution progress . <mpchecktext contenteditable="false" id="1613870292543_0.6752751740063665"> </mpchecktext> </p> <p style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; background-color: rgb(255, 255, 255); text-align: center;"> <br> </p> <p style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255);"> <iframe class="video_iframe rich_pages wx_video_iframe" data-vidtype="2" data-mpvid="wxv_1712376995663118337" data-cover="http%3A%2F%2Fmmbiz.qpic.cn%2Fmmbiz_jpg%2FfhujzoQe7TpWtD8oiaUsgSJFCLaoMq5cUD8ViaM1F4VZuNMKkrNGYk1lUsLYicWW3fDLiad4MHhncFAr48wqNmbvVA%2F0%3Fwx_fmt%3Djpeg" allowfullscreen="" frameborder="0" data-ratio="1.6632124352331605" data-w="642" scrolling="no" width="578" height="325" src="/cgi-bin/readtemplate?t=tmpl/video_tmpl&amp;vid=wxv_1712376995663118337"></iframe> </p> <p data-tool="mdnice Editor " style="padding-top: 8px; padding-bottom: 8px; white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; background-color: rgb(255, 255, 255); line-height: 26px; text-align: center;"> <span style="color: rgb(136, 136, 136); font-size: 14px; letter-spacing: 0px;"> Example <mpchecktext contenteditable="false" id="1613870292544_0.8978213363224574"> </mpchecktext></span> <br> </p> <blockquote data-tool="mdnice Editor " style="color: rgb(0, 0, 0); line-height: 26px; font-size: 16px; white-space: normal; font-family: Helvetica; text-align: left; background-color: rgb(255, 255, 255);"> <p style="line-height: 26px;"> <span style="font-size: 12px;"><strong><span style="color: rgb(89, 89, 89); font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, &quot;PingFang SC&quot;, Cambria, Cochin, Georgia, Times, &quot;Times New Roman&quot;, serif; letter-spacing: 0.7px;"> In Aramaic (Aramaic)、 Hebrew (Hebrew)、 And Arabic (Arabic) in , Semitic language (Semitic) Root q-d-m Usually associated with the meaning of going forward or making progress .<mpchecktext contenteditable="false" id="1613870292545_0.07453517341823357"> </mpchecktext> for example , Arabic words taqaddum (تقدّم) It means “ progress ”.<mpchecktext contenteditable="false" id="1613870292546_0.5906906584074918"> </mpchecktext> Progress is important , Just like every delightful movie will convey a philosophy to you —— Travel is as important as destination .<mpchecktext contenteditable="false" id="1613870292547_0.9155591047627538"> </mpchecktext></span></strong></span> </p> </blockquote> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> Most programs have a clear goal , That's the end of the program ( end ) state , Success or failure ! <mpchecktext contenteditable="false" id="1613870292548_0.42000533212708024"> </mpchecktext> But sometimes the program may take a long time to run . <mpchecktext contenteditable="false" id="1613870292549_0.10647566367482253"> </mpchecktext> Even though computers have no emotions , It doesn't matter how long the program runs , But people care . <mpchecktext contenteditable="false" id="1613870292550_0.3367709102788239"> </mpchecktext> </p> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> <br> </p> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> Over time , Questions from program operators or users will continue to spread ( Did the program crash ? <mpchecktext contenteditable="false" id="1613870292551_0.61109647947811"> </mpchecktext> Is the disk beating ? <mpchecktext contenteditable="false" id="1613870292552_0.4817770352012156"> </mpchecktext> Whether the operating system allocates all computing resources to other tasks ? <mpchecktext contenteditable="false" id="1613870292553_0.9052996639596194"> </mpchecktext>) When the program runs for a long time without any sign of progress , Program operators may have doubts about 、 Fidgety , Not willing to keep waiting . <mpchecktext contenteditable="false" id="1613870292554_0.944740823147667"> </mpchecktext> But this article talks about tqdm Libraries help to clearly show the progress of a program . <mpchecktext contenteditable="false" id="1613870292555_0.4787629156108948"> </mpchecktext> </p> <h3 data-tool="mdnice Editor " style="margin-top: 30px; margin-bottom: 15px; font-weight: bold; font-size: 20px; white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; text-align: left; background-color: rgb(255, 255, 255);"> <span style="font-size: 18px; color: rgb(0, 82, 255);">2、 Usage method <mpchecktext contenteditable="false" id="1613870292556_0.42231546684004995"> </mpchecktext></span> </h3> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;">tqdm Library modules can be used together in the command console , But it also supports one of my favorite development environments Jupyter notebook. <mpchecktext contenteditable="false" id="1613870292557_0.07867050295887368"> </mpchecktext> If you want to Jupyter notebook Use in tqdm,notebook Module and tqdm Interface compatibility , Import required notebook Sub module and install ipywidget. <mpchecktext contenteditable="false" id="1613870292558_0.629576426502882"> </mpchecktext> </p> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> <br> </p> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> This means that you can import tqdm Make a condition judgment before library operation , The way to judge is to check _main_ Is there any get_ipython Global variables . <mpchecktext contenteditable="false" id="1613870292559_0.9588033261176434"> </mpchecktext> Although the above method is heuristic , But it's a pretty accurate method : <mpchecktext contenteditable="false" id="1613870292560_0.1231187935251632"> </mpchecktext> </p> <pre data-tool="mdnice Editor " style="margin-top: 10px; margin-bottom: 10px; color: rgb(0, 0, 0); font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"> <span style="margin-bottom: -7px; display: block; background: url(&quot;https://mmbiz.qpic.cn/mmbiz_png/fhujzoQe7TpWtD8oiaUsgSJFCLaoMq5cUCymmS73sg4t2hWo7SZlUhKAKNoEqIvDZtVzXc41BFusBh2YZtCicQfQ/640?wx_fmt=png&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52); height: 30px; width: 657px; border-radius: 5px;"></span> <code style="padding: 15px 16px 16px; overflow-x: auto; color: rgb(171, 178, 191); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; font-size: 12px; background: rgb(40, 44, 52); border-radius: 5px;"><span style="color: rgb(198, 120, 221); line-height: 26px;">import</span>&nbsp;sys<br><span style="color: rgb(198, 120, 221); line-height: 26px;">if</span>&nbsp;hasattr(sys.modules[<span style="color: rgb(152, 195, 121); line-height: 26px;">"__main__"</span>],<span style="color: rgb(152, 195, 121); line-height: 26px;">"get_ipython"</span>):<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">from</span>&nbsp;tqdm&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">import</span>&nbsp;notebook&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">as</span>&nbsp;tqdm<br><span style="color: rgb(198, 120, 221); line-height: 26px;">else</span>:<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">import</span>&nbsp;tqdm<mpchecktext contenteditable="false" id="1613870292561_0.22677490906251863"> </mpchecktext><br></code> </pre> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> Take the simplest case to help understand , A program needs to perform a certain number of iterations ( The number of iterations is known in advance ), But each of these iterations takes about the same amount of time . <mpchecktext contenteditable="false" id="1613870292562_0.32743566779320377"> </mpchecktext> for example , An algorithm that can calculate the square root of any number , The algorithm is from 1 Start , Then calculate a schedule estimate . <mpchecktext contenteditable="false" id="1613870292563_0.7153118693997842"> </mpchecktext> </p> <pre data-tool="mdnice Editor " style="margin-top: 10px; margin-bottom: 10px; color: rgb(0, 0, 0); font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"> <span style="margin-bottom: -7px; display: block; background: url(&quot;https://mmbiz.qpic.cn/mmbiz_png/fhujzoQe7TpWtD8oiaUsgSJFCLaoMq5cUCymmS73sg4t2hWo7SZlUhKAKNoEqIvDZtVzXc41BFusBh2YZtCicQfQ/640?wx_fmt=png&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52); height: 30px; width: 657px; border-radius: 5px;"></span> <code style="padding: 15px 16px 16px; overflow-x: auto; color: rgb(171, 178, 191); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; font-size: 12px; background: rgb(40, 44, 52); border-radius: 5px;"><span style="line-height: 26px;"><span style="color: rgb(198, 120, 221); line-height: 26px;">def</span>&nbsp;<span style="color: rgb(97, 174, 238); line-height: 26px;">improve_guess</span><span style="line-height: 26px;">(rt,&nbsp;n)</span>:</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">return</span>&nbsp;(rt&nbsp;+&nbsp;n/rt)&nbsp;/&nbsp;<span style="color: rgb(209, 154, 102); line-height: 26px;">2<mpchecktext contenteditable="false" id="1613870292564_0.38068598775258566"> </mpchecktext></span><br></code> </pre> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> A few improvements to the above code will make it easier for you to understand : <mpchecktext contenteditable="false" id="1613870292565_0.334725918180969"> </mpchecktext> </p> <pre data-tool="mdnice Editor " style="margin-top: 10px; margin-bottom: 10px; color: rgb(0, 0, 0); font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"> <span style="margin-bottom: -7px; display: block; background: url(&quot;https://mmbiz.qpic.cn/mmbiz_png/fhujzoQe7TpWtD8oiaUsgSJFCLaoMq5cUCymmS73sg4t2hWo7SZlUhKAKNoEqIvDZtVzXc41BFusBh2YZtCicQfQ/640?wx_fmt=png&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52); height: 30px; width: 657px; border-radius: 5px;"></span> <code style="padding: 15px 16px 16px; overflow-x: auto; color: rgb(171, 178, 191); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; font-size: 12px; background: rgb(40, 44, 52); border-radius: 5px;">guess&nbsp;=&nbsp;<span style="color: rgb(209, 154, 102); line-height: 26px;">1</span><br>target&nbsp;=&nbsp;<span style="color: rgb(209, 154, 102); line-height: 26px;">2</span><br><span style="color: rgb(198, 120, 221); line-height: 26px;">for</span>&nbsp;i&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">in</span>&nbsp;tqdm.trange(<span style="color: rgb(209, 154, 102); line-height: 26px;">10</span>):<br>&nbsp;&nbsp;&nbsp;&nbsp;guess&nbsp;=&nbsp;improve_guess(guess,&nbsp;target)<mpchecktext contenteditable="false" id="1613870292566_0.658737136238182"> </mpchecktext><br></code> </pre> <figure data-tool="mdnice Editor " style="margin-top: 10px; margin-bottom: 10px; white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); display: flex; flex-direction: column; justify-content: center; align-items: center;"> <img data-ratio="0.06666666666666667" data-type="png" data-w="675" src="https://mmbiz.qpic.cn/mmbiz_png/fhujzoQe7TpWtD8oiaUsgSJFCLaoMq5cUURczkhycV4mwQxM43cG2EHX9V4rMcBANV2QJZCDoWtcNRmMOeTbHfQ/640?wx_fmt=png" style="margin-right: auto; margin-left: auto; display: block; box-sizing: border-box !important; width: 675px !important; visibility: visible !important;"> </figure> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> Ten decimal places : <mpchecktext contenteditable="false" id="1613870292567_0.14271185994376734"> </mpchecktext> </p> <pre data-tool="mdnice Editor " style="margin-top: 10px; margin-bottom: 10px; color: rgb(0, 0, 0); font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"> <span style="margin-bottom: -7px; display: block; background: url(&quot;https://mmbiz.qpic.cn/mmbiz_png/fhujzoQe7TpWtD8oiaUsgSJFCLaoMq5cUCymmS73sg4t2hWo7SZlUhKAKNoEqIvDZtVzXc41BFusBh2YZtCicQfQ/640?wx_fmt=png&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52); height: 30px; width: 657px; border-radius: 5px;"></span> <code style="padding: 15px 16px 16px; overflow-x: auto; color: rgb(171, 178, 191); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; font-size: 12px; background: rgb(40, 44, 52); border-radius: 5px;">round(<span style="color: rgb(209, 154, 102); line-height: 26px;">2</span>&nbsp;-&nbsp;guess*guess,&nbsp;<span style="color: rgb(209, 154, 102); line-height: 26px;">10</span>)<br><span style="color: rgb(209, 154, 102); line-height: 26px;">0.0<mpchecktext contenteditable="false" id="1613870292568_0.21110173786423925"> </mpchecktext></span><br></code> </pre> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> Let's take a slightly more complicated one tqdm Example , When the number of elements processed is known and the time to calculate each element is similar . <mpchecktext contenteditable="false" id="1613870292569_0.9689761300339252"> </mpchecktext> for example , Calculate the chance of some random numbers : <mpchecktext contenteditable="false" id="1613870292570_0.4353738582883355"> </mpchecktext> </p> <pre data-tool="mdnice Editor " style="margin-top: 10px; margin-bottom: 10px; color: rgb(0, 0, 0); font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"> <span style="margin-bottom: -7px; display: block; background: url(&quot;https://mmbiz.qpic.cn/mmbiz_png/fhujzoQe7TpWtD8oiaUsgSJFCLaoMq5cUCymmS73sg4t2hWo7SZlUhKAKNoEqIvDZtVzXc41BFusBh2YZtCicQfQ/640?wx_fmt=png&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52); height: 30px; width: 657px; border-radius: 5px;"></span> <code style="padding: 15px 16px 16px; overflow-x: auto; color: rgb(171, 178, 191); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; font-size: 12px; background: rgb(40, 44, 52); border-radius: 5px;"><span style="color: rgb(198, 120, 221); line-height: 26px;">import</span>&nbsp;random<br>numbers&nbsp;=&nbsp;[random.uniform(<span style="color: rgb(209, 154, 102); line-height: 26px;">0</span>,&nbsp;<span style="color: rgb(209, 154, 102); line-height: 26px;">2.8</span>)&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">for</span>&nbsp;i&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">in</span>&nbsp;range(<span style="color: rgb(209, 154, 102); line-height: 26px;">100</span>)]<br>numbers[:<span style="color: rgb(209, 154, 102); line-height: 26px;">5</span>]<br>[<span style="color: rgb(209, 154, 102); line-height: 26px;">2.6575636572230916</span>,<br><span style="color: rgb(209, 154, 102); line-height: 26px;">0.1286674965830302</span>,<br><span style="color: rgb(209, 154, 102); line-height: 26px;">1.0634250104041332</span>,<br><span style="color: rgb(209, 154, 102); line-height: 26px;">1.1760969844376505</span>,<br><span style="color: rgb(209, 154, 102); line-height: 26px;">0.45192978568125486</span>]<mpchecktext contenteditable="false" id="1613870292571_0.9879254603631429"> </mpchecktext><br></code> </pre> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> The numerical element has , Next use tqdm Show progress bar , Use tqdm The easiest way is to encapsulate Python Iteratable objects in . <mpchecktext contenteditable="false" id="1613870292572_0.2948801335315765"> </mpchecktext> </p> <pre data-tool="mdnice Editor " style="margin-top: 10px; margin-bottom: 10px; color: rgb(0, 0, 0); font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"> <span style="margin-bottom: -7px; display: block; background: url(&quot;https://mmbiz.qpic.cn/mmbiz_png/fhujzoQe7TpWtD8oiaUsgSJFCLaoMq5cUCymmS73sg4t2hWo7SZlUhKAKNoEqIvDZtVzXc41BFusBh2YZtCicQfQ/640?wx_fmt=png&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52); height: 30px; width: 657px; border-radius: 5px;"></span> <code style="padding: 15px 16px 16px; overflow-x: auto; color: rgb(171, 178, 191); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; font-size: 12px; background: rgb(40, 44, 52); border-radius: 5px;">result&nbsp;=&nbsp;<span style="color: rgb(209, 154, 102); line-height: 26px;">1</span><br><span style="color: rgb(198, 120, 221); line-height: 26px;">for</span>&nbsp;num&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">in</span>&nbsp;tqdm.tqdm(numbers):<br>&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;*=&nbsp;num<br>result<br><span style="color: rgb(209, 154, 102); line-height: 26px;">2.4081854901728303<mpchecktext contenteditable="false" id="1613870292573_0.08637817275547355"> </mpchecktext></span><br></code> </pre> <figure data-tool="mdnice Editor " style="margin-top: 10px; margin-bottom: 10px; white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); display: flex; flex-direction: column; justify-content: center; align-items: center;"> <img data-ratio="0.0725925925925926" data-type="png" data-w="675" src="https://mmbiz.qpic.cn/mmbiz_png/fhujzoQe7TpWtD8oiaUsgSJFCLaoMq5cUAoORwYcziahDpEuVDVv6epOBrgCEhxic9ViceicFwJWudY3xO6Sp2qnl1w/640?wx_fmt=png" style="margin-right: auto; margin-left: auto; display: block; box-sizing: border-box !important; width: 675px !important; visibility: visible !important;"> </figure> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> however , Not everything is predictable , One of the unpredictable things is network speed . <mpchecktext contenteditable="false" id="1613870292574_0.5501801752126871"> </mpchecktext> When downloading large files , The only way to measure progress is to show how many files have been downloaded : <mpchecktext contenteditable="false" id="1613870292575_0.8337753190253012"> </mpchecktext> </p> <pre data-tool="mdnice Editor " style="margin-top: 10px; margin-bottom: 10px; color: rgb(0, 0, 0); font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"> <span style="margin-bottom: -7px; display: block; background: url(&quot;https://mmbiz.qpic.cn/mmbiz_png/fhujzoQe7TpWtD8oiaUsgSJFCLaoMq5cUCymmS73sg4t2hWo7SZlUhKAKNoEqIvDZtVzXc41BFusBh2YZtCicQfQ/640?wx_fmt=png&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52); height: 30px; width: 657px; border-radius: 5px;"></span> <code style="padding: 15px 16px 16px; overflow-x: auto; color: rgb(171, 178, 191); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; font-size: 12px; background: rgb(40, 44, 52); border-radius: 5px;">url&nbsp;=&nbsp;<span style="color: rgb(152, 195, 121); line-height: 26px;">"https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz"</span><br><span style="color: rgb(198, 120, 221); line-height: 26px;">import</span>&nbsp;httpx<br><span style="color: rgb(198, 120, 221); line-height: 26px;">with</span>&nbsp;httpx.stream(<span style="color: rgb(152, 195, 121); line-height: 26px;">"GET"</span>,&nbsp;url)&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">as</span>&nbsp;response:<br>&nbsp;&nbsp;&nbsp;&nbsp;total&nbsp;=&nbsp;int(response.headers[<span style="color: rgb(152, 195, 121); line-height: 26px;">"Content-Length"</span>])<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">with</span>&nbsp;tqdm.tqdm(total=total)&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">as</span>&nbsp;progress:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">for</span>&nbsp;chunk&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">in</span>&nbsp;response.iter_bytes():<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;progress.update(len(chunk))<mpchecktext contenteditable="false" id="1613870292576_0.5813966754295894"> </mpchecktext><br></code> </pre> <figure data-tool="mdnice Editor " style="margin-top: 10px; margin-bottom: 10px; white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); display: flex; flex-direction: column; justify-content: center; align-items: center;"> <img data-ratio="0.047407407407407405" data-type="png" data-w="675" src="https://mmbiz.qpic.cn/mmbiz_png/fhujzoQe7TpWtD8oiaUsgSJFCLaoMq5cUyXJ4Ve4Ew5c1N68tdjUJxlofPicibmpcw4r2GQSYyHQo7mKtWvhbIgeQ/640?wx_fmt=png" style="margin-right: auto; margin-left: auto; display: block; box-sizing: border-box !important; width: 675px !important; visibility: visible !important;"> </figure> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> Sometimes ,“ nesting ” Progress bars make sense . <mpchecktext contenteditable="false" id="1613870292577_0.7870996635671821"> </mpchecktext> for example , If you are downloading a directory , You need a progress bar to track files and a progress bar for each file . <mpchecktext contenteditable="false" id="1613870292578_0.35637092896327527"> </mpchecktext> Here's an example ( But there's no actual download directory ): <mpchecktext contenteditable="false" id="1613870292579_0.2193111243561241"> </mpchecktext> </p> <pre data-tool="mdnice Editor " style="margin-top: 10px; margin-bottom: 10px; color: rgb(0, 0, 0); font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"> <span style="margin-bottom: -7px; display: block; background: url(&quot;https://mmbiz.qpic.cn/mmbiz_png/fhujzoQe7TpWtD8oiaUsgSJFCLaoMq5cUCymmS73sg4t2hWo7SZlUhKAKNoEqIvDZtVzXc41BFusBh2YZtCicQfQ/640?wx_fmt=png&quot;) 10px 10px / 40px no-repeat rgb(40, 44, 52); height: 30px; width: 657px; border-radius: 5px;"></span> <code style="padding: 15px 16px 16px; overflow-x: auto; color: rgb(171, 178, 191); display: -webkit-box; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; font-size: 12px; background: rgb(40, 44, 52); border-radius: 5px;">files&nbsp;=&nbsp;[<span style="color: rgb(152, 195, 121); line-height: 26px;">f"vid-<span style="color: rgb(224, 108, 117); line-height: 26px;">{i}</span>.mp4"</span>&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">for</span>&nbsp;i&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">in</span>&nbsp;range(<span style="color: rgb(209, 154, 102); line-height: 26px;">4</span>)]<br><span style="color: rgb(198, 120, 221); line-height: 26px;">for</span>&nbsp;fname&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">in</span>&nbsp;tqdm.tqdm(files,&nbsp;desc=<span style="color: rgb(152, 195, 121); line-height: 26px;">"files"</span>):<br>&nbsp;&nbsp;&nbsp;&nbsp;total&nbsp;=&nbsp;random.randrange(<span style="color: rgb(209, 154, 102); line-height: 26px;">10</span>**<span style="color: rgb(209, 154, 102); line-height: 26px;">9</span>,&nbsp;<span style="color: rgb(209, 154, 102); line-height: 26px;">2</span>&nbsp;*&nbsp;<span style="color: rgb(209, 154, 102); line-height: 26px;">10</span>**<span style="color: rgb(209, 154, 102); line-height: 26px;">9</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">with</span>&nbsp;tqdm.tqdm(total=total,&nbsp;desc=fname)&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">as</span>&nbsp;progress:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;=&nbsp;<span style="color: rgb(209, 154, 102); line-height: 26px;">0</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">while</span>&nbsp;current&nbsp;&lt;&nbsp;total:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chunk_size&nbsp;=&nbsp;min(random.randrange(<span style="color: rgb(209, 154, 102); line-height: 26px;">10</span>**<span style="color: rgb(209, 154, 102); line-height: 26px;">3</span>,&nbsp;<span style="color: rgb(209, 154, 102); line-height: 26px;">10</span>**<span style="color: rgb(209, 154, 102); line-height: 26px;">5</span>),&nbsp;total&nbsp;-&nbsp;current)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current&nbsp;+=&nbsp;chunk_size<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgb(198, 120, 221); line-height: 26px;">if</span>&nbsp;random.uniform(<span style="color: rgb(209, 154, 102); line-height: 26px;">0</span>,&nbsp;<span style="color: rgb(209, 154, 102); line-height: 26px;">1</span>)&nbsp;&lt;&nbsp;<span style="color: rgb(209, 154, 102); line-height: 26px;">0.01</span>:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(<span style="color: rgb(209, 154, 102); line-height: 26px;">0.1</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;progress.update(chunk_size)<mpchecktext contenteditable="false" id="1613870292580_0.3759443275999621"> </mpchecktext><br></code> </pre> <figure data-tool="mdnice Editor " style="margin-top: 10px; margin-bottom: 10px; white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); display: flex; flex-direction: column; justify-content: center; align-items: center;"> <img data-ratio="0.1511111111111111" data-type="png" data-w="675" src="https://mmbiz.qpic.cn/mmbiz_png/fhujzoQe7TpWtD8oiaUsgSJFCLaoMq5cUmOKztNQXVBv2fwTr3DFtLe5HelDxWHpmKlqsjUYHmHYpgcuYAEKkLA/640?wx_fmt=png" style="margin-right: auto; margin-left: auto; display: block; box-sizing: border-box !important; width: 675px !important; visibility: visible !important;"> </figure> <p data-tool="mdnice Editor " style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: left; background-color: rgb(255, 255, 255); line-height: 26px;"> therefore , If your program takes a while to show the final result , To avoid the frustration of users : <mpchecktext contenteditable="false" id="1613870292582_0.34837786137867366"> </mpchecktext> Show its progress ! <mpchecktext contenteditable="false" id="1613870292581_0.4128478996743705"> </mpchecktext> </p> <p style="white-space: normal; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 16px; text-align: right; background-color: rgb(255, 255, 255); line-height: 26px;"> <br> </p>

[ Take it with you !Python 3.9 Official Chinese documents , Time limited collection !] (http://dwz.date/dE6v)

[ Time limit ! Quick collar !14 Zhang HD Python Quick reference table , It is necessary to improve efficiency !] (http://dwz.date/dE6w)

[GitHub Star sign 3W+,80 individual Python Case study , Take you easy to play Python Study !] (http://dwz.date/dE64)

版权声明
本文为[Love to learn]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/02/20210221093829056t.html

  1. Python Tkinter inserts all the pictures in a directory into the docx file
  2. 解决忽略VScode中Python插件pylint报错的问题
  3. To solve the problem of ignoring the error of Python plug-in in vscode
  4. python 毫秒级时间,时间戳转换
  5. Python millisecond time, timestamp conversion
  6. python try except 出现异常时,except 中如何返回异常的信息字符串
  7. When an exception occurs in Python try except, how to return the exception information string in except
  8. 手机最强Python编程神器,在手机上运行Python
  9. The strongest Python Programming artifact on mobile phones, running Python on mobile phones
  10. 2021年Python程序员薪资待遇如何?
  11. 「python安装」Windows上安装和创建python开发环境
  12. What is the salary of Python programmers in 2021?
  13. "Python installation" to install and create a python development environment on Windows
  14. python解决组合问题
  15. Python to solve the problem of composition
  16. Python中的Lasso回归之最小角算法LARS
  17. Lars, the least angle algorithm of lasso regression in Python
  18. 利用python提取网站曲线图数据
  19. Using Python to extract website graph data
  20. Python3中urllib详细使用方法(header,代理,超时,认证,异常处理)
  21. Detailed usage of urllib in Python 3 (header, proxy, timeout, authentication, exception handling)
  22. python 第三方库paramiko
  23. python 第三方库paramiko
  24. Python third party library paramiko
  25. Python third party library paramiko
  26. 卸载 PyCharm!这才是 Python 小白的最理想的 IDE
  27. 卸载 PyCharm!这才是 Python 小白的最理想的 IDE
  28. Uninstall pycharm! This is the ideal IDE for Python Xiaobai
  29. django学习-27.admin管理后台里:对列表展示页面的数据展示进行相关优化
  30. Uninstall pycharm! This is the ideal IDE for Python Xiaobai
  31. Django learning - 27. Admin management background: optimize the data display of the list display page
  32. python day2
  33. python day2
  34. Python 内存泄漏问题排查
  35. Troubleshooting of Python memory leak
  36. Python 与 excel的简单应用
  37. Simple application of Python and excel
  38. Python 与 excel的简单应用
  39. Simple application of Python and excel
  40. 2.7万 Star!最全面的 Python 设计模式集合
  41. 27000 stars! The most comprehensive collection of Python design patterns
  42. python day3
  43. python day3
  44. Commonly used data operation functions of Python
  45. (数据科学学习手札108)Python+Dash快速web应用开发——静态部件篇(上)
  46. (learning notes of data science 108) Python + dash rapid web application development -- static components (I)
  47. (数据科学学习手札108)Python+Dash快速web应用开发——静态部件篇(上)
  48. (learning notes of data science 108) Python + dash rapid web application development -- static components (I)
  49. [Python] Matplotlib 图表的绘制和美化技巧
  50. Drawing and beautifying skills of [Python] Matplotlib chart
  51. [Python] Matplotlib 图表的绘制和美化技巧
  52. Drawing and beautifying skills of [Python] Matplotlib chart
  53. Virtual environment of Python project
  54. 翻译:《实用的Python编程》02_01_Datatypes
  55. Translation: practical Python Programming 02_ 01_ Datatypes
  56. 翻译:《实用的Python编程》02_01_Datatypes
  57. 翻译:《实用的Python编程》02_01_Datatypes
  58. Translation: practical Python Programming 02_ 01_ Datatypes
  59. Translation: practical Python Programming 02_ 01_ Datatypes
  60. Python 3 入门,看这篇就够了