前言:Python有很多好用的方法和工具类,但是经常很长时间不用就忘记了,于是记录下
语法
异常处理
1 2 3 4 5 6 try : except Exception as e: else :
文件写入
r:只读,不创建文件
r+:读写:不创建文件
w:只写,创建文件,存在文件则清空
w+:读写,创建文件,存在文件则清空
a:只写,创建文件,存在文件则附加内容
a+:读写,创建文件,存在文件则附加内容
1 2 3 with open ("file-name" , "a" ) as file: file.write(ncbi_id) file.write("\n" )
遍历
1 2 3 4 5 for item in list : print (item) for idx, item in enumerate (list ): print (idx)
字符串
1 2 normalized_score = format (normalized_score, "0.5f" )
OS
创建文件夹
遍历文件夹内容(递归)
1 2 3 for root, dirs, files in os.walk('dir-name' ): for dir_name in dirs: dir_path = os.path.join(root, dir_name)
1 2 3 for root, dirs, files in os.walk('val_genomic_signature' ): for file_name in files: file_path = os.path.join(root, file_name)
遍历文件夹内容(不递归)
1 2 3 4 for item in os.listdir(root_path): item_path = os.path.join(root_path, item) if os.path.isdir(item_path): print (item_path)
需要注意,listdir和walk函数在linux上的遍历顺序是乱序的!
复制文件
1 2 cmd = "copy {} {}" .format (src_path, dst_path) os.system(cmd)
执行命令行命令
示例:批量执行python文件
1 2 3 4 5 6 for root, dirs, files in os.walk('CGR' ): for input_dir in dirs: input_dir_path = os.path.join(root, input_dir) output_dir_path = os.path.join('out' , input_dir) os.system('python gene.py --input {} --output {}' .format (input_dir_path, output_dir_path))
查看系统信息
1 2 3 4 5 6 7 8 print ('操作系统名称:' , platform.system()) print ('操作系统名称及版本号:' , platform.platform()) print ('操作系统版本号:' , platform.version()) print ('操作系统的位数:' , platform.architecture()) print ('计算机类型:' , platform.machine()) print ('计算机的网络名称:' , platform.node()) print ('计算机处理器信息:' , platform.processor()) print ('包含上面所有的信息汇总:' , platform.uname())
sys
1 print ('sys version: ' , sys.version)
Random
Request
自动报错重试
1 2 3 4 5 6 7 8 9 10 11 import requestsfrom requests.adapters import HTTPAdapter, Retrysession = requests.Session() retries = Retry(total=3 , backoff_factor=1 ) session.mount('http://' , HTTPAdapter(max_retries=retries)) session.mount('https://' , HTTPAdapter(max_retries=retries)) session.get('http://httpbin.org/delay/5' , timeout=2 ) session.get('https://www.kingname.info' )
使用Postman测试Api后,可以点击右侧代码按钮,自动生成各个语言的爬虫代码
Conda
下载
官网
unbuntu安装
1 2 3 4 5 6 7 8 9 10 11 12 # Go to home directory cd ~ # You can change what anaconda version you want at # https://repo.continuum.io/archive/ wget https://repo.continuum.io/archive/Anaconda2-4.2.0-Linux-x86_64.sh bash Anaconda2-4.2.0-Linux-x86_64.sh -b -p ~/anaconda rm Anaconda2-4.2.0-Linux-x86_64.sh echo 'export PATH="~/anaconda/bin:$PATH"' >> ~/.bashrc # Reload default profile source ~/.bashrc
镜像源管理
删除之前原有的镜像源(所有的)
conda config --remove-key channels
查看镜像源
conda config --show-sources
配置清华镜像源
参考Anaconda 镜像使用帮助
执行conda config --set show_channel_urls yes
,在用户目录下创建.condarc
文件
修改.condarc
文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 channels: - defaults show_channel_urls: true default_channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2 custom_channels: conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud deepmodeling: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/
Conda环境
参考Conda使用教程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 conda -v conda env list conda create -n env-name python=x.x conda create -n env-name --clone clone-env-name conda create -n env-name --file requirements.txt source activate conda activate env-name activate env-name conda remove -n env-name --all
在虚拟环境中操作
1 2 3 4 5 6 7 8 conda deactivate conda list conda list package-name conda install package-name
在Pycharm的Terminal中无法使用conda环境
使用activate env-name后,terminal的conda环境没有改变
发现Pycharm的Terminal使用的是Powershell,修改成cmd即可
PyTorch
安装
参考官网
查看cuda版本:Nvidia控制面板-帮助-系统信息-组件-NVCUDA64.DLL
比如cuda版本=11.7,则在官网上找到对应的11.x版本cuda的安装命令
1 conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
测试是否安装成功:
1 2 3 import torchprint (torch.cuda.is_available())
自定义ImageFolder,添加权重信息
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 class CustomImageFolder (ImageFolder ): def __init__ (self, root, transform=None , target_transform=None , weights=None ): super (CustomImageFolder, self).__init__(root, transform=transform, target_transform=target_transform) self.weights = weights def __getitem__ (self, index ): img, target = super (CustomImageFolder, self).__getitem__(index) weight = self.weights[index] return img, target, weight with open ("./all_name_score.json" , "r" ) as file: all_name_score = json.load(file) weights = [v for k, v in all_name_score.items()] origin_image_datasets = CustomImageFolder(root='CGR_7classes' , transform=data_transform, weights=weights) for inputs, labels, weights in dataloaders[phase]: inputs = inputs.to(device) labels = labels.to(device) weights = weights.to(device) ... loss_tensor = loss * weights running_loss = torch.sum (loss_tensor)
Json
Json对象和字符串互相转换
dumps
和loads
函数
Json对象实际上就是dict
1 2 3 4 5 6 7 8 9 10 payload = json.dumps({ "taxon" : taxo_id, "page_size" : size, "page_token" : None , "query" : "" , "types" : [] }) data = json.loads(response.text)
读取Json文件
1 2 with open ("./target_species.json" , "r" ) as file: data = json.load(file)
写入Json文件
1 2 with open ("./test.json" , "w" ) as file: json.dump(data, file)
tqdm
tqdm 是一个用于在 Python 中添加进度条的库。它的名字是 “taqaddum” 的缩写,意为 “进步”。tqdm 的主要作用是在循环迭代等长时间运行的任务时,提供一个可视化的进度条,让用户能够清晰地看到任务的进度。
1 2 3 4 5 6 7 8 9 10 11 from tqdm import tqdmimport timemy_list = list (range (100 )) for item in tqdm(my_list, desc="Processing" ): time.sleep(0.1 )