****2022-11-10 22:38:27 星期四

目的

重新写一套新的代码,一些基本框架都比较类似,如加载模块、构建函数、参数读取、创建日志甚至是一些好用的自建装饰器,不想每次创建新脚本时都重新写一遍,或者找其他文件复制粘贴再删除不需要的代码块时,那么自建所需的代码块(snippet)就很好的解决这个问题。

方法

步骤一

打开设置,找到用户自定义代码块设置
image

步骤二

从弹窗中选择需要创建代码块的编程语言
image

步骤三

创建所需的代码块,以 python 为例:

点击查看代码
{
	// Place your snippets for python here. Each snippet is defined under a snippet name and has a prefix, body and 
	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
	// same ids are connected.
	// Example:
	// "Print to console": {
	// 	"prefix": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
	"python 模板(新脚本)": {
		"prefix": "pyformat",
		"body": [
			"\"\"\"",
			"$CURRENT_YEAR.$CURRENT_MONTH.$CURRENT_DATE Liukb",
			"    ",
			"\"\"\"",
			"",
			"import re",
			"import os",
			"import sys",
			"import time",
			"import logging",
			"import argparse",
			"import pandas as pd",
			"from glob import glob",
			"import subprocess as sp",
			"from collections import defaultdict",
			"",
			"logging.basicConfig(",
			"    level = logging.DEBUG,",
			"    filename = \"${1:./log}\",",
			"    filemode = \"a\",",
			"    format = \"[%(asctime)s - %(levelname)-4s - %(module)s : line %(lineno)s] %(message)s\",",
			"    datefmt = \"%Y-%m-%d %H:%M:%S\"",
			")",
			"",
			"class ${2:Core}:",
			"    def __init__(self):",
			"        self.name = 'posion'",
			"",
			"",
			"if __name__ == '__main__':",
			"    parse = argparse.ArgumentParser()",
			"    parse.add_argument('-b', '--batchs', default='')",
			"    opts = parse.parse_args()",
		],
		"description": "python 模板"
	},
}

使用实例

新建一个 python 格式的文件,输入配置中 prefix 定义的字段,不用完全输入完就会有提示。(vscode 不卡,或者模块安装正常,提示非常迅速)
image