欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)帶百分比的進度條

 更新時間:2016年06月28日 08:46:51   投稿:hebedich  
本文給大家匯總介紹了3種使用Python實現(xiàn)帶百分比進度條的代碼,非常的簡單實用,有需要的小伙伴可以參考下

大家在安裝程序或下載文件時,通常都能看到進度條,提示你當前任務(wù)的進度。其實,在python中實現(xiàn)這個功能很簡單,下面是具體代碼。在實際應(yīng)用中,你完全可以根據(jù)自己的要求進行修改!比如,示例中是通過time.sleep()方法進行時間延遲,你完全可以根據(jù)實際的程序運行耗時進行控制;同樣,在進度百分比處,你也可以顯示實際的進度比,而不是例子中機械的自增百分比。

import sys
import time

def view_bar(num, total):
  rate = num / total
  rate_num = int(rate * 100)
  r = '\r[%s%s]%d%%' % ("="*num, " "*(100-num), rate_num, )
  sys.stdout.write(r)
  sys.stdout.flush()

if __name__ == '__main__':
  for i in range(0, 101):
    time.sleep(0.1)
    view_bar(i, 100)

再給大家分享一個方法

import hashlib
 
a = "a test string"
print hashlib.md5(a).hexdigest()
print hashlib.sha1(a).hexdigest()
print hashlib.sha224(a).hexdigest()
print hashlib.sha256(a).hexdigest()
print hashlib.sha384(a).hexdigest()
print hashlib.sha512(a).hexdigest()

再來一個復雜點的函數(shù)吧

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import threading
import time
'''
class Demo:
  def __init__(self,thread_num=5):
    self.thread_num=thread_num
  def productor(self,i):
    print "thread-%d start" %i
  def start(self):
    threads=[]
    for x in xrange(self.thread_num):
      t=threading.Thread(target=self.productor,args=(x,))
      threads.append(t)
    for t in threads:
      t.start()
    for t in threads:
      t.join()
    print 'all thread end'
 
demo=Demo()
demo.start()
'''
thread_num=10
def productor(i):
    print "thread-%d start" %i
    time.sleep(2)
def start():
    threads=[]
    for x in range(thread_num):
      t=threading.Thread(target=productor,args=(x,))
      threads.append(t)
    for t in threads:
      t.start()
    for t in threads:
      t.join()
    print 'all thread end'

start()

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import paramiko
import sys

private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
# 創(chuàng)建SSH對象
ssh = paramiko.SSHClient()
# 允許連接不在know_hosts文件中的主機
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
t = paramiko.Transport(('vm_135',22))
# 連接服務(wù)器
t.connect(username='root',pkey=private_key)
ssh.connect(hostname='vm_135', port=22, username='root',pkey=private_key)
# 執(zhí)行命令
sftp = paramiko.SFTPClient.from_transport(t)
stdin, stdout, stderr = ssh.exec_command('df')
# 獲取命令結(jié)果
result = stdout.read()
print result

def progress_bar(transferred, toBeTransferred, suffix=''):
    # print "Transferred: {0}\tOut of: {1}".format(transferred, toBeTransferred)
    bar_len = 60
    filled_len = int(round(bar_len * transferred/float(toBeTransferred)))
    percents = round(100.0 * transferred/float(toBeTransferred), 1)
    bar = '=' * filled_len + '-' * (bar_len - filled_len)
    sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', suffix))
    sys.stdout.flush()
sftp.put("/tmp/134","/tmp/134",callback=progress_bar)

#for filename in filenames:
#  sftp.put(os.path.join(dirpath, filename),
#       os.path.join(remote_path, filename),
#       callback=self.progress_bar)
#  print
#  print "upload %s/%s" % (remote_path, filename) + '\t' + '[' + green("success") + ']'
ssh.close()

以上就是本文的全部內(nèi)容了,大家是否對使用Python實現(xiàn)帶百分比進度條有了新的認識了呢,希望大家能夠喜歡。

相關(guān)文章

  • python單向循環(huán)鏈表原理與實現(xiàn)方法示例

    python單向循環(huán)鏈表原理與實現(xiàn)方法示例

    這篇文章主要介紹了python單向循環(huán)鏈表原理與實現(xiàn)方法,結(jié)合實例形式詳細分析了Python單向循環(huán)鏈表概念、原理、定義及使用方法,需要的朋友可以參考下
    2019-12-12
  • python scipy卷積運算的實現(xiàn)方法

    python scipy卷積運算的實現(xiàn)方法

    這篇文章主要介紹了python scipy卷積運算的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • Python中實現(xiàn)結(jié)構(gòu)相似的函數(shù)調(diào)用方法

    Python中實現(xiàn)結(jié)構(gòu)相似的函數(shù)調(diào)用方法

    這篇文章主要介紹了Python中實現(xiàn)結(jié)構(gòu)相似的函數(shù)調(diào)用方法,本文講解使用dict和lambda結(jié)合實現(xiàn)結(jié)構(gòu)相似的函數(shù)調(diào)用,給出了不帶參數(shù)和帶參數(shù)的實例,需要的朋友可以參考下
    2015-03-03
  • Python 刪除連續(xù)出現(xiàn)的指定字符的實例

    Python 刪除連續(xù)出現(xiàn)的指定字符的實例

    今天小編就為大家分享一篇Python 刪除連續(xù)出現(xiàn)的指定字符的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • 用Python登錄Gmail并發(fā)送Gmail郵件的教程

    用Python登錄Gmail并發(fā)送Gmail郵件的教程

    這篇文章主要介紹了用Python登錄Gmail并發(fā)送Gmail郵件的教程,利用了Python的SMTP庫,代碼非常簡單,需要的朋友可以參考下
    2015-04-04
  • GDAL 矢量屬性數(shù)據(jù)修改方式(python)

    GDAL 矢量屬性數(shù)據(jù)修改方式(python)

    這篇文章主要介紹了GDAL 矢量屬性數(shù)據(jù)修改方式(python),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Python @property原理解析和用法實例

    Python @property原理解析和用法實例

    這篇文章主要介紹了Python @property原理解析和用法實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • Python抽象類應(yīng)用詳情

    Python抽象類應(yīng)用詳情

    這篇文章主要介紹了Python抽象類應(yīng)用詳情,抽象類就是控制子類的方法的名稱,要求子類必須按照父類的要求的實現(xiàn)指定的方法,且方法名要和父類保持一致,下文更多相關(guān)介紹需要的小伙伴可以參考一下
    2022-04-04
  • 使用Python+Appuim 清理微信的方法

    使用Python+Appuim 清理微信的方法

    這篇文章主要介紹了使用Python+Appuim 清理微信,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Python編程使用matplotlib繪制動態(tài)圓錐曲線示例

    Python編程使用matplotlib繪制動態(tài)圓錐曲線示例

    這篇文章主要介紹了Python使用matplotlib繪制動態(tài)的圓錐曲線示例實現(xiàn)代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-10-10

最新評論