Created
November 12, 2024 00:20
-
-
Save knwng/e24e569344ff5f1b964d27d592a31a06 to your computer and use it in GitHub Desktop.
VSCode Python Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
// 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" | |
// } | |
"Logger setup": { | |
"prefix": "setup_logger", | |
"body": [ | |
"def setup_logger(logname=None, verbose=False):", | |
" FORMAT = '[%(asctime)s] p%(process)d {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s'", | |
" logFormatter = logging.Formatter(FORMAT, datefmt='%m-%d %H:%M:%S')", | |
" rootLogger = logging.getLogger()", | |
" if verbose:", | |
" rootLogger.setLevel(logging.DEBUG)", | |
" else:", | |
" rootLogger.setLevel(logging.INFO)", | |
"", | |
" if logname is not None:", | |
" fileHandler = logging.FileHandler(logname)", | |
" fileHandler.setFormatter(logFormatter)", | |
" rootLogger.addHandler(fileHandler)", | |
"", | |
" consoleHandler = logging.StreamHandler()", | |
" consoleHandler.setFormatter(logFormatter)", | |
" rootLogger.addHandler(consoleHandler)" | |
], | |
"description": "setup logger use logging" | |
}, | |
"Run Command and get results": { | |
"prefix": "run_cmd", | |
"body": [ | |
"def run_cmd(cmd):", | |
" ret = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).stdout.decode('utf-8').split('\n')", | |
" ret = [x.strip() for x in ret]", | |
" return ret" | |
], | |
"description": "run command in cli and get results" | |
}, | |
"Directory Walk": { | |
"prefix": "os_walk", | |
"body": [ | |
], | |
"description": "" | |
}, | |
"ArgParser": { | |
"prefix": "argparser", | |
"body": [ | |
"parser = argparse.ArgumentParser()", | |
"parser.add_argument($1)", | |
"args = parser.parse_args()" | |
], | |
"description": "add argument parser" | |
}, | |
"ConfigParser": { | |
"prefix": "configparser", | |
"body": [ | |
"import configparser", | |
"config = configparser.ConfigParser()", | |
"config.read($1)" | |
], | |
"description": "add config parser" | |
}, | |
"Current Time": { | |
"prefix": "curr_time", | |
"body": [ | |
"curr_time = datetime.datetime.today().strftime('%Y%m%d%H%M%S')" | |
], | |
"description": "get current time, in format of YYYYMMDDhhmmss" | |
}, | |
"Parse Time": { | |
"prefix": "parse_time", | |
"body": [ | |
"date = datetime.datetime.strptime($2, '%Y-%m-%d %H:%M:%S')" | |
], | |
"description": "parse datetime" | |
}, | |
"Setup main function": { | |
"prefix": "main", | |
"body": [ | |
"def main():", | |
" pass", | |
"", | |
"if __name__ == '__main__':", | |
" main()" | |
], | |
"description": "setup main func and invoke in __main__" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment