Skip to content

Latest commit

 

History

History
109 lines (87 loc) · 3.71 KB

File metadata and controls

109 lines (87 loc) · 3.71 KB

Parameterized Python Script Snippet

This snippet generates boilerplate code for creating a parameterized Python3 script. The script leverages the argparse library for parameterization and comes with a preconfigured logger, and an associated argparse argument for overriding the default log level of INFO.



Prerequisites

This snippet's dependencies are an up-to-date version of VSCode and the Python3 runtime:



Usage

Using the snippet requires copying the contents of the snippet.json file and pasting them into either the python.json snippet configuration file, or the global snippets configuration file in VSCode. The user snippets configuration files are accessible via VSCode Preferences under the Configure User Snippets option.




Paste the contents of the snippet.json file into the configuration file and save.

Example:

{
	"Python3 Parameterized Script": {
		"prefix": "pyscript",
		"body": [
		  "#!/usr/bin/env python3",
		  "\"\"\"${1:script_name}",
		  "",
		  "${2:script_description}",
		  "",
		  "Functions",
		  "---------",
		  "__get_arguments() - returns argparse.ArgumentParser object with script args",
		  "",
		  "main() - the script's main function",
		  "\"\"\"",
		  "import argparse",
		  "import logging",
		  "import sys",
		  "",
		  "logger = logging.getLogger(__name__)",
		  "for h in logger.handlers:",
		  "    logger.removeHandler(h)",
		  "h = logging.StreamHandler(sys.stdout)",
		  "FORMAT = '%(asctime)s [%(levelname)s] <%(name)s> - %(message)s'",
		  "h.setFormatter(logging.Formatter(FORMAT))",
		  "logger.addHandler(h)",
		  "",
		  "",
		  "def __get_arguments() -> argparse.ArgumentParser:",
		  "    \"\"\"Creates an argparse parser for simplifying parameterization of",
		  "    the script and argument management",
		  "",
		  "    Returns",
		  "    -------",
		  "    arguments: argparse.ArgumentParser",
		  "        an argparse parser object with a set of parsed arguments",
		  "    \"\"\"",
		  "    try:",
		  "        parser = argparse.ArgumentParser(usage='%(prog)s --log_level DEBUG')",
		  "        parser.add_argument(",
		  "            \"-l\",",
		  "            \"--log_level\",",
		  "            default=\"INFO\",",
		  "            help=\"Used to explicitly set the Log Level (Default: INFO)\"",
		  "        )",
		  "        arguments = parser.parse_args()",
		  "        return arguments",
		  "    except Exception:",
		  "        logger.exception(\"Error accessing arguments!\")",
		  "",
		  "",
		  "def main():",
		  "    arguments = __get_arguments()",
		  "    logger.setLevel(arguments.log_level)",
		  ""
		],
		"description": "Python3 Parameterized Script"
	  }
}

You can now test the snippet by creating any file in VSCode with the .py extension, typing the pyscript prefix trigger, and hitting <TAB> to insert your snippet's code at the current position of the cursor. Once the snippet has populated code into your file it will immediately reposition your cursor at the snippet's first placeholder position. Advance through each placeholder by entering content and/or hitting the <TAB> key.




Reference Documentation