记录实用的python库

记录实用的python库

  本文专门用来记录一下python中一些好用方法/库,可以在日常使用中提高效率。

进度条

  在爬虫和机器学习等工作中,可能需要有一个进度条能够反馈当前程序运行速度或者进度,可以考虑用以下方法实现:

tqdm

  老朋友tqdm,就是丑了点。

安装

1
pip install tqdm

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 最基本的用法
import time
from tqdm import tqdm
for i in tqdm(range(9)):
time.sleep(0.1)
# 效果如下
>>> 100%|██████████| 10/10 [00:01<00:00, 9.79it/s]

# trange类似于tqdm
import time
from tqdm import trange
for i in trange(10):
time.sleep(0.1)
# 效果如下
>>> 100%|██████████| 10/10 [00:01<00:00, 9.79it/s]

# 传入list
import time
from tqdm import tqdm
pbar = tqdm([1,2,3,4,5,6,7,8,9,10])
for char in pbar:
pbar.set_description("Processing %s" % char)
time.sleep(0.1)
# 效果如下
>>> Processing 10: 100%|██████████| 10/10 [00:01<00:00, 9.49it/s]

# 手动控制更新
import time
from tqdm import tqdm
with tqdm(total=10) as pbar:
for i in range(10):
pbar.update(1)
time.sleep(0.1)
# 效果如下
>>> 100%|██████████| 10/10 [00:00<00:00, 10.10it/s]

# 也可以这样
import time
from tqdm import tqdm
pbar = tqdm(total=10)
for i in range(10):
pbar.update(1)
time.sleep(0.1)
pbar.close()
# 效果如下
>>> 100%|██████████| 10/10 [00:00<00:00, 10.10it/s]

  在Spyder下正常了,然而在命令窗口有问题。

Rich

  非常炫酷的包,当然这里仅仅用它的进度条。(由@kotori-y投稿)

安装

1
pip install rich

使用

  基本用法:

1
2
3
4
from rich.progress import track

for step in track(range(100)):
do_step(step)

  按步更新:

1
2
3
4
5
6
from rich.progress import Progress

with Progress() as progress:
task = progress.add_task("[red]Downloading...", total=total)
#do sth
progress.update(task, advance=1)

  存在问题在于,不能在cmder或者spyder下正常显示,有请Windows Terminal(逃)。

ShowProcess类

  在网上找到别人写的一个方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 建立一个ShowProcess类
class ShowProcess():
i = 0
max_steps = 0
max_arrow = 50

def __init__(self, max_steps):
self.max_steps = max_steps
self.i = 0

def show_process(self, i=None):
if i is not None:
self.i = i
else:
self.i += 1
num_arrow = int(self.i * self.max_arrow / self.max_steps)
num_line = self.max_arrow - num_arrow
percent = self.i * 100.0 / self.max_steps
process_bar = '[' + '>' * num_arrow + '-' * num_line + ']'\
+ '%.2f' % percent + '%'
print('\r',process_bar,end='',flush=True)

def close(self, words='done'):
print('')
print(words)
self.i = 0

  使用示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
if __name__=='__main__':
max_steps = 100

process_bar = ShowProcess(max_steps)

for i in range(max_steps):
process_bar.show_process()
time.sleep(0.05)
process_bar.close()

# 效果如下
>>> [>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]100.00%
>>> done

  在命令窗口下使用正常,但是在IDLE和Spyder中显示存在问题,考虑使用其他方法。

重试

  进行爬虫的时候,很容易因为网络问题导致失败,这里有2个库可以很轻松地实现这个功能。

retry

安装

1
pip install retry

使用

  只需要在函数定义前加上@retry就行了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from retry import retry

@retry()
def make_trouble():
'''重试直到成功'''
@retry(ZeroDivisionError, tries=3, delay=2)
def make_trouble():
'''出现ZeroDivisionError时重试, 重试3次,每次间隔2秒'''
@retry((ValueError, TypeError), delay=1, backoff=2)
def make_trouble():
'''出现ValueError或TypeError时重试, 每次间隔1, 2, 4, 8, ...秒'''
@retry((ValueError, TypeError), delay=1, backoff=2, max_delay=4)
def make_trouble():
'''出现ValueError或TypeError时重试, 每次间隔1, 2, 4, 4, ...秒,最高间隔为4秒'''
@retry(ValueError, delay=1, jitter=1)
def make_trouble():
'''出现ValueError时重试,每次间隔1, 2, 3, 4, ... 秒'''

Tenacity

  使用类似于retry。同样只需要在函数定义前加上@retry就行了。

安装

1
pip install tenacity

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from tenacity import retry, retry_if_exception_type, wait_fixed, stop_after_attempt, stop_after_delay,

@retry()
def make_trouble():
'''重试直到成功'''
@retry(retry=retry_if_exception_type(ZeroDivisionError), wait=wait_fixed(2), stop=stop_after_attempt(3))
def make_trouble():
'''出现ZeroDivisionError时重试, 重试3次,每次间隔2秒'''
@retry(stop=(stop_after_delay(10) | stop_after_attempt(5)))
def make_trouble():
'''重试10秒或者5次'''
@retry(wait=wait_random(min=1, max=2))
def make_trouble():
'''重试间隔在随机1-2秒'''
@retry(wait=wait_chain(*[wait_fixed(3) for i in range(3)] +
[wait_fixed(7) for i in range(2)] +
[wait_fixed(9)]))
def make_trouble():
'''前三次重试每次间隔3秒,接下来2次间隔7秒,之后重试间隔9秒'''

超时

  很多任务特别是多线程时,为了防止程序卡死,需要设定一个超时。

func_timeout

  由于windows下signal的支持问题,选择使用第三方包,func_timeout就是一个给函数添加超时的包。

安装

1
pip install func_timeout

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import time
from func_timeout import func_set_timeout,FunctionTimedOut

# 基本用法
try:
doitReturnValue = func_timeout(5, doit, args=('arg1', 'arg2'))
except FunctionTimedOut:
print ( "doit('arg1', 'arg2') could not complete within 5 seconds and was terminated.\n")
except Exception as e:
# 其他Exception

# 装饰器用法
@func_set_timeout(2)
def task():
time.sleep(5)
# 效果如下
FunctionTimedOut: Function task (args=()) (kwargs={}) timed out after 2.000000 seconds.

# 捕获异常
from func_timeout.exceptions import FunctionTimedOut
try:
task()
except FunctionTimedOut:
print('task func_timeout')

  此外它还有一个重试的函数FunctionTimedOut,就不赘述了。

异步

  待填坑……

评论

3D cell culture 3D cell culturing 3D cell microarrays 3D culture 3D culture model 3D printing 3D spheroid 3D tumor culture 3D tumors 3D vascular mapping ACT ADV AUTODESK Abdominal wall defects Acoustofluidics Adipocyte Adipogenesis Adoptive cell therapy AirPods Alginate Anticancer Anticancer agents Anticancer drugs Apple Apriori Association Analysis August AutoCAD Autodock Vina Bio-inspired systems Biochannels Bioengineering Bioinspired Biological physics Biomarkers Biomaterial Biomaterials Biomimetic materials Biomimetics Bioprinting Blood purification Blood-brain barrier Bone regeneration Breast cancer Breast cancer cells Breast neoplasms CM1860 CRISPR/Cas9 system CSS CTC isolation CTCs Cancer Cancer angiogenesis Cancer cell invasion Cancer immunity Cancer immunotherapy Cancer metabolism Cancer metastasis Cancer models Cancer screening Cancer stem cells Cell adhesion Cell arrays Cell assembly Cell clusters Cell culture Cell culture techniques Cell mechanical stimulation Cell morphology Cell trapping Cell-bead pairing Cell-cell interaction Cell-laden gelatin methacrylate Cellular uptake Cell−cell interaction Cervical cancer Cheminformatics Chemotherapy Chimeric antigen receptor-T cells Chip interface Circulating tumor cells Clinical diagnostics Cmder Co-culture Coculture Colon Colorectal cancer Combinatorial drug screening Combinatorial drug testing Compartmentalized devices Confined migration Continuous flow Convolutional neural network Cooking Crawler Cryostat Curved geometry Cytokine detection Cytometry Cytotoxicity Cytotoxicity assay DESeq DNA tensioners Data Mining Deep learning Deformability Delaunay triangulation Detective story Diabetic wound healing Diagnostics Dielectrophoresis Differentiation Digital microfluidics Direct reprogramming Discrimination of heterogenic CTCs Django Double emulsion microfluidics Droplet Droplet microfluidics Droplets generation Droplet‐based microfluidics Drug combination Drug efficacy evaluation Drug evaluation Drug metabolism Drug resistance Drug resistance screening Drug screening Drug testing Dual isolation and profiling Dynamic culture Earphone Efficiency Efficiency of encapsulation Elastomers Embedded 3D bioprinting Encapsulation Endothelial cell Endothelial cells English Environmental hazard assessment Epithelial–mesenchymal transition Euclidean distance Exosome biogenesis Exosomes Experiment Extracellular vesicles FC40 FP-growth Fabrication Fast prototyping Fibroblasts Fibrous strands Fiddler Flask Flow rates Fluorescence‐activated cell sorting Functional drug testing GEO Galgame Game Gene Expression Profiling Gene delivery Gene expression profiling Gene targetic Genetic association Gene‐editing Gigabyte Glypican-1 GoldenDict Google Translate Gradient generator Growth factor G‐CSF HBEXO-Chip HTML Hanging drop Head and neck cancer Hectorite nanoclay Hepatic models Hepatocytes Heterotypic tumor HiPSCs High throughput analyses High-throughput High-throughput drug screening High-throughput screening assays High‐throughput methods Histopathology Human neural stem cells Human skin equivalent Hydrogel Hydrogel hypoxia Hydrogels ImageJ Immune checkpoint blockade Immune-cell infiltration Immunoassay Immunological surveillance Immunotherapy In vitro tests In vivo mimicking Induced hepatocytes Innervation Insulin resistance Insulin signaling Interferon‐gamma Intestinal stem cells Intracellular delivery Intratumoral heterogeneity JRPG Jaccard coefficient JavaScript July June KNN Kidney-on-a-chip Lab-on-a-chip Laptop Large scale Lattice resoning Leica Leukapheresis Link Lipid metabolism Liquid biopsy Literature Liver Liver microenvironment Liver spheroid Luminal mechanics Lung cells MOE Machine Learning Machine learning Macro Macromolecule delivery Macroporous microgel scaffolds Magnetic field Magnetic sorting Malignant potential Mammary tumor organoids Manhattan distance Manual Materials science May Mechanical forces Melanoma Mesenchymal stem cells Mesoporous silica particles (MSNs) Metastasis Microassembly Microcapsule Microcontact printing Microdroplets Microenvironment Microfluidic array Microfluidic chips Microfluidic device Microfluidic droplet Microfluidic organ-on-a chip Microfluidic organ-on-a-chip Microfluidic patterning Microfluidic screening Microfluidic tumor models Microfluidic-blow-spinning Microfluidics Microneedles Micropatterning Microtexture Microvascular Microvascular networks Microvasculatures Microwells Mini-guts Mirco-droplets Molecular docking Molecular imprinting Monolith Monthly Multi-Size 3D tumors Multi-organoid-on-chip Multicellular spheroids Multicellular systems Multicellular tumor aggregates Multi‐step cascade reactions Myeloid-derived suppressor cells NK cell NanoZoomer Nanomaterials Nanoparticle delivery Nanoparticle drug delivery Nanoparticles Nanowell Natural killer cells Neural progenitor cell Neuroblastoma Neuronal cell Neurons Nintendo Nissl body Node.js On-Chip orthogonal Analysis OpenBabel Organ-on-a-chip Organ-on-a-chip devices Organically modified ceramics Organoids Organ‐on‐a‐chip Osteochondral interface Oxygen control Oxygen gradients Oxygen microenvironments PDA-modified lung scaffolds PDMS PTX‐loaded liposomes Pain relief Pancreatic cancer Pancreatic ductal adenocarcinoma Pancreatic islet Pathology Patient-derived organoid Patient-derived tumor model Patterning Pearl powder Pearson coefficient Penetralium Perfusable Personalized medicine Photocytotoxicity Photodynamic therapy (PDT) Physiological geometry Pluronic F127 Pneumatic valve Poetry Polymer giant unilamellar vesicles Polystyrene PowerShell Precision medicine Preclinical models Premetastatic niche Primary cell transfection Printing Protein patterning Protein secretion Pubmed PyMOL Pybel Pytesseract Python Quasi-static hydrodynamic capture R RDKit RNAi nanomedicine RPG Reactive oxygen species Reagents preparation Resistance Review Rod-shaped microgels STRING Selective isolation Self-assembly Self-healing hydrogel September Signal transduction Silk-collagen biomaterial composite Similarity Single cell Single cells Single molecule Single-cell Single-cell RNA sequencing Single‐cell analysis Single‐cell printing Size exclusion Skin regeneration Soft lithography Softstar Spheroids Spheroids-on-chips Staining StarBase Stem cells Sub-Poisson distribution Supramolecular chemistry Surface chemistry Surface modification Switch T cell function TCGA Tanimoto coefficient The Millennium Destiny The Wind Road Thin gel Tissue engineering Transcriptome Transfection Transient receptor potential channel modulators Tropism Tubulogenesis Tumor environmental Tumor exosomes Tumor growth and invasion Tumor immunotherapy Tumor metastasis Tumor microenvironment Tumor response Tumor sizes Tumor spheroid Tumor-on-a-chip Tumorsphere Tumors‐on‐a‐chip Type 2 diabetes mellitus Ultrasensitive detection Unboxing Underlying mechanism Vascularization Vascularized model Vasculature Visual novel Wettability Windows Terminal Word Writing Wuxia Xenoblade Chronicles Xin dynasty XuanYuan Sword Youdao cnpm fsevents miR-125b-5p miR-214-3p miRNA signature miRanda npm
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×