用于创建进度条的python库
interaction的Python项目详细描述
互动
interaction是一个python包,用于使用map()和apply()方法以及for循环创建漂亮而多彩的进度条。
进度条
使用进度条,您需要启动具有最大进度量(Potal)的PraseSbar对象,并调用对象的显示方法来更新进度条。
frominteractionimportProgressBarfromtimeimportsleepbar=ProgressBar(total=100)foriinrange(100):sleep(0.05)bar.show(amount=i+1)
您还可以将自己的文本添加到进度条中。
frominteractionimportProgressBarfromtimeimportsleepbar=ProgressBar(total=100)foriinrange(100):sleep(0.05)bar.show(amount=i+1,text=f'working on {i}')
地图
您还可以使用progressbar的map方法而不是python的map方法。输出是一个iterable。一旦将iterable对象转换为列表,将显示进度条。
frominteractionimportProgressBarfromtimeimportsleeplist1=list(range(100))defwait_and_double(x,wait_time):sleep(wait_time)returnx*2# this will not make the progress bar appeariterable2=ProgressBar.map(function=lambdax:wait_and_double(x=x,wait_time=0.05),iterable=list1)# this will make the progress bar appearlist2=list(iterable2)
应用
progressbar还可以处理panda的dataframe和series对象。
frominteractionimportProgressBarfromtimeimportsleepfrompandasimportDataFramedata=DataFrame({'name':['Arminius','Boudica','Alaric','Attila','Genseric'],'ethnicity':['German','Celt','Goth','Hun','Vandal']})data['name_and_ethnicity']=ProgressBar.apply(data=data,function=lambdax:x['name']+' the '+x['ethnicity'])data['name_lower']=ProgressBar.apply(series=data['name'],function=lambdax:x.lower())
迭代
您还可以使用progressbar对iterable对象进行迭代,特别是对循环使用。
frominteractionimportiterateforiiniterate(range(100)):# do somethingpass