Skip to content

Instantly share code, notes, and snippets.

@JV-conseil
Last active November 30, 2023 23:26
Show Gist options
  • Save JV-conseil/68a3b85e57049728b6bc4ad33bc0659e to your computer and use it in GitHub Desktop.
Save JV-conseil/68a3b85e57049728b6bc4ad33bc0659e to your computer and use it in GitHub Desktop.
Code snippets are templates that make it easier to enter repeating code patterns, such as loops or conditional-statements #vscode #snippets #productivity

Python 🐍 Code Snippets

Become a sponsor to JV-conseil Follow JV conseil on StackOverflow Follow JVconseil on Twitter Follow JVconseil on Mastodon Follow JV conseil on GitHub

Code snippets are templates that make it easier to enter repeating code patterns, such as loops or conditional-statements

Code snippets are templates that make it easier to enter repeating code patterns, such as loops or conditional-statements.

In your repository, create a file under .vscode/python.code-snippets

{
    // Place your workspace snippets here. 
    // Each snippet is defined under a snippet name and has a scope, prefix, body and description.
    // Add comma separated ids of the languages where the snippet is applicable in the scope field.
    // If scope is left empty or omitted, the snippet gets applied to all languages.
    // 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": {
    // 	"scope": "javascript,typescript",
    // 	"prefix": "log",
    // 	"body": [
    // 		"console.log('$1');",
    // 		"$2"
    // 	],
    // 	"description": "Log output to console"
    // }
    //
    // Documentation:
    // --------------
    // - https://code.visualstudio.com/docs/editor/userdefinedsnippets
    // - https://github.com/cstrap/python-snippets/blob/master/snippets/base.json
    // - https://snippet-generator.app
    //
    "New function: new_function(self, *args, **kwargs):try:except:": {
        "scope": "python",
        "prefix": "def",
        "body": [
            "def ${1:new_function}(self, *args, **kwargs) -> int:",
            "\t\" $1 \"",
            "\toutput = 0",
            "\ttry:",
            "\t\t\" Do something here \"",
            "\t\t$0",
            "\t\toutput = 1",
            "\texcept Exception as e:",
            "\t\tlogger.exception(e)",
            "\tlogger.debug(\"$1: %s\", output)",
            "\treturn output",
        ],
        "description": "Python code snippet to create a new function"
    },
    "New class: NewClass:": {
        "scope": "python",
        "prefix": "class",
        "body": [
          "class ${1:NewClass}:",
          "    \"\"\"${1}",
          "",
          "    My ${1} does this, this and this...",
          "",
          "    Usage:",
          "",
          "    ```py",
          "    from .new_class import ${1}",
          "",
          "    new_class = ${1}()",
          "    do = new_class()",
          "    ```",
          "    \"\"\"",
          "",
          "    def __init__(self, attribute: str = \"\", **kwargs):",
          "        try:",
          "            assert attribute, f\"${1} attribute={attribute} is not valid\"",
          "            self.attribute = attribute",
          "        except Exception as e:",
          "            logger.exception(e)",
          "        logger.debug(\"%s: %s\", self.__class__.__name__, logger_json(self.__dict__))",
          "",
          "    def new_function(self, *args, **kwargs) -> int:",
          "        \"new_function\"",
          "        output = 0",
          "        try:",
          "            \"Do something here\"",
          "",
          "            output = 1",
          "        except Exception as e:",
          "            logger.exception(e)",
          "        logger.debug(\"new_function: %s\", output)",
          "        return output",
          "",
          "    def __call__(self, *args, **kwargs) -> int:",
          "        return self.new_function(*args, **kwargs)"
        ],
        "description": "Python code snippet to create a new class"
      },
}

Documentation πŸ“š

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment