├── .vscodeignore ├── .gitignore ├── images └── python.png ├── snippets ├── debug.json ├── unittest.json ├── tkinter.json ├── comprehension.json └── base.json ├── CHANGELOG.md ├── package.json └── README.md /.vscodeignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | vsc-* 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ 2 | vsc-* 3 | .DS_Store/ 4 | flask-snippets-* -------------------------------------------------------------------------------- /images/python.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cstrap/python-snippets/HEAD/images/python.png -------------------------------------------------------------------------------- /snippets/debug.json: -------------------------------------------------------------------------------- 1 | { 2 | "PDB set trace": { 3 | "prefix": "pdb", 4 | "body": "import pdb; pdb.set_trace()$0" 5 | }, 6 | "iPDB set trace": { 7 | "prefix": "ipdb", 8 | "body": "import ipdb; ipdb.set_trace()$0" 9 | }, 10 | "rPDB set trace": { 11 | "prefix": "rpdb", 12 | "body": "import rpdb2; rpdb2.start_embedded_debugger('${1:debug_password}')$0" 13 | }, 14 | "PuDB set trace": { 15 | "prefix": "pudb", 16 | "body": "import pudb; pudb.set_trace()$0" 17 | }, 18 | "IPython set trace": { 19 | "prefix": "ipydb", 20 | "body": "from IPython import embed; embed()$0" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to the "python-snippets" extension will be documented in this file. 4 | 5 | ## [0.1.2] 2022-02-17 6 | 7 | - Merge PR #18, #19 8 | 9 | ## [0.1.1] 2021-10-14 10 | 11 | - Merge PR #14 12 | 13 | ## [0.1.0] 14 | 15 | - Merge PRs #15, #16, #17 16 | 17 | ## [0.0.9] 18 | 19 | - Merge PR #11 20 | 21 | ## [0.0.8] 22 | 23 | - Merge PR #9, #10 24 | - Add "New property", issue #2 25 | 26 | ## [0.0.7] 27 | 28 | - Merge PR #7 29 | 30 | ## [0.0.6] 31 | 32 | - Merge PR #1 #3 #5 fix typo, new snippets added 33 | 34 | ## [0.0.5] 35 | 36 | - Fix duplicate snippets, adding some doc 37 | 38 | ## [0.0.4] 39 | 40 | - Added abbreviations list with description on README file 41 | 42 | ## [0.0.3] 43 | 44 | - Initial support for Tkinter, ported from Atom snippets found on [GitHub](https://github.com/adiultra/python-snippets) 45 | - Splitting snippets into category files 46 | 47 | ## [0.0.2] 48 | 49 | - Public repo on [GitHub](https://github.com/cstrap/python-snippets), feel free to contribute! :-) 50 | 51 | ## [0.0.1] 52 | 53 | - Initial release 54 | - Porting Atom [language-python](https://github.com/atom/language-python) with [atomizr](https://www.npmjs.com/package/node-atomizr) 55 | - Added some snippets ported from PyCharm 56 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "python-snippets", 3 | "displayName": "python-snippets", 4 | "description": "Python Snippets", 5 | "version": "0.1.2", 6 | "publisher": "cstrap", 7 | "icon": "images/python.png", 8 | "engines": { 9 | "vscode": "^1.12.0" 10 | }, 11 | "categories": [ 12 | "Snippets" 13 | ], 14 | "keywords": [ 15 | "snippets", 16 | "python" 17 | ], 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/cstrap/python-snippets.git" 21 | }, 22 | "contributes": { 23 | "snippets": [{ 24 | "language": "python", 25 | "path": "./snippets/base.json" 26 | }, 27 | { 28 | "language": "python", 29 | "path": "./snippets/unittest.json" 30 | }, 31 | { 32 | "language": "python", 33 | "path": "./snippets/comprehension.json" 34 | }, 35 | { 36 | "language": "python", 37 | "path": "./snippets/debug.json" 38 | }, 39 | { 40 | "language": "python", 41 | "path": "./snippets/tkinter.json" 42 | } 43 | ] 44 | } 45 | } -------------------------------------------------------------------------------- /snippets/unittest.json: -------------------------------------------------------------------------------- 1 | { 2 | "Assert equal": { 3 | "prefix": "ase", 4 | "body": "self.assertEqual(${1:expected}, ${2:actual}${3:, '${4:message}'})$0" 5 | }, 6 | "Assert not equal": { 7 | "prefix": "asne", 8 | "body": "self.assertNotEqual(${1:expected}, ${2:actual}${3:, '${4:message}'})$0" 9 | }, 10 | "Assert raises": { 11 | "prefix": "asr", 12 | "body": "self.assertRaises(${1:exception}, ${2:callable}, ${3:args})$0" 13 | }, 14 | "Assert True": { 15 | "prefix": "ast", 16 | "body": "self.assertTrue(${1:actual}${2:, '${3:message}'})$0" 17 | }, 18 | "Assert False": { 19 | "prefix": "asf", 20 | "body": "self.assertFalse(${1:actual}${2:, '${3:message}'})$0" 21 | }, 22 | "Assert is": { 23 | "prefix": "asi", 24 | "body": "self.assertIs(${1:expected}, ${2:actual}${3:, '${4:message}'})$0" 25 | }, 26 | "Assert is not": { 27 | "prefix": "asint", 28 | "body": "self.assertIsNot(${1:expected}, ${2:actual}${3:, '${4:message}'})$0" 29 | }, 30 | "Assert is None": { 31 | "prefix": "asino", 32 | "body": "self.assertIsNone(${1:actual}${2:, '${3:message}'})$0" 33 | }, 34 | "Assert is not None": { 35 | "prefix": "asinno", 36 | "body": "self.assertIsNotNone(${1:actual}${2:, '${3:message}'})$0" 37 | }, 38 | "Assert in": { 39 | "prefix": "asin", 40 | "body": "self.assertIn(${1:needle}, ${2:haystack}${3:, '${4:message}'})$0" 41 | }, 42 | "Assert not in": { 43 | "prefix": "asni", 44 | "body": "self.assertNotIn(${1:needle}, ${2:haystack}${3:, '${4:message}'})$0" 45 | }, 46 | "Assert": { 47 | "prefix": "as", 48 | "body": "self.assert_(${1:boolean expression}${2:, '${3:message}'})$0" 49 | }, 50 | "Fail (a test)": { 51 | "prefix": "fail", 52 | "body": "self.fail('${1:message}')$0" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /snippets/tkinter.json: -------------------------------------------------------------------------------- 1 | { 2 | "Import Tkinter": { 3 | "prefix": "imtk", 4 | "body": "import Tkinter as tk$0" 5 | }, 6 | "Import tkinter": { 7 | "prefix": "imtk3", 8 | "body": "import tkinter as tk$0" 9 | }, 10 | "Root configuration": { 11 | "prefix": "config", 12 | "body": "config(menu=$1)$0" 13 | }, 14 | "Button": { 15 | "prefix": "button", 16 | "body": "tk.Button(${1:root}, text=\"$2\")$0" 17 | }, 18 | "Label": { 19 | "prefix": "label", 20 | "body": "tk.Label(${1:root}, text=\"$2\")$0" 21 | }, 22 | "Frame": { 23 | "prefix": "frame", 24 | "body": "tk.Frame(${1:root})$0" 25 | }, 26 | "Entry": { 27 | "prefix": "entry", 28 | "body": "tk.Entry(${1:root})$0" 29 | }, 30 | "Grid": { 31 | "prefix": "grid", 32 | "body": "grid(row=$1, colomn=$2)$0" 33 | }, 34 | "Sticky": { 35 | "prefix": "sticky", 36 | "body": "sticky=tk.$0" 37 | }, 38 | "Check button": { 39 | "prefix": "checkbutton", 40 | "body": "tk.Checkbutton(${1:root}, text=\"$2\")$0" 41 | }, 42 | "Main loop": { 43 | "prefix": "mainloop", 44 | "body": "mainloop()$0" 45 | }, 46 | "Pack": { 47 | "prefix": "pack", 48 | "body": "pack($1)$0" 49 | }, 50 | "Side": { 51 | "prefix": "side", 52 | "body": "side=tk.$0" 53 | }, 54 | "Bind": { 55 | "prefix": "bind", 56 | "body": "bind(\"\", ${2:fxn})$0" 57 | }, 58 | "Menu": { 59 | "prefix": "menu", 60 | "body": "tk.menu(${1:root})$0" 61 | }, 62 | "Add cascade": { 63 | "prefix": "addcascade", 64 | "body": "add_cascade(label=\"$1\", menu=$2)$0" 65 | }, 66 | "Add command": { 67 | "prefix": "addcommand", 68 | "body": "add_command(label=\"$1\", command=$2)$0" 69 | }, 70 | "Add seperator": { 71 | "prefix": "addseperator", 72 | "body": "add_separator()$0" 73 | } 74 | } -------------------------------------------------------------------------------- /snippets/comprehension.json: -------------------------------------------------------------------------------- 1 | { 2 | "List comprehension": { 3 | "prefix": "lc", 4 | "body": "[${1:value} for ${2:value} in ${3:iterable}]$0", 5 | "description" : "List comprehension for creating a list based on existing lists." 6 | }, 7 | "List comprehension if else": { 8 | "prefix": "lcie", 9 | "body": "[${1:value} if ${2:condition} else ${3:condition} for ${4:value} in ${5:iterable}]$0", 10 | "description" : "List comprehension for creating a list based on existing lists, with conditional if-else statement." 11 | }, 12 | "List comprehension if filter": { 13 | "prefix": "lci", 14 | "body": "[${1:value} for ${2:value} in ${3:iterable} if ${4:condition}$0]", 15 | "description" : "List comprehension for creating a list based on existing lists, with conditional if statement." 16 | }, 17 | "Dictionary comprehension": { 18 | "prefix": "dc", 19 | "body": "{${1:key}: ${2:value} for ${3:key}, ${4:value} in ${5:iterable}}$0", 20 | "description" : "Handy and faster way to create dictories based on existing dictionaries." 21 | }, 22 | "Dictionary comprehension if filter": { 23 | "prefix": "dci", 24 | "body": "{${1:key}: ${2:value} for ${3:key}, ${4:value} in ${5:iterable} if ${6:condition}}$0", 25 | "description" : "Handy and faster way to create dictories based on existing dictionaries, with conditional if statement." 26 | }, 27 | "Set comprehension": { 28 | "prefix": "sc", 29 | "body": "{${1:value} for ${2:value} in ${3:iterable}}$0", 30 | "description" : "Create a set based on existing iterables." 31 | }, 32 | "Set Comprehension if filter": { 33 | "prefix": "sci", 34 | "body": "{${1:value} for ${2:value} in ${3:iterable} if ${4:condition}}$0", 35 | "description" : "Create a set based on existing iterables, with condition if statement." 36 | }, 37 | "Generator comprehension": { 38 | "prefix": "gc", 39 | "body": "(${1:key} for ${2:value} in ${3:iterable})$0", 40 | "description" : "Create a generator based on existing iterables." 41 | }, 42 | "Generator comprehension if filter": { 43 | "prefix": "gci", 44 | "body": "(${1:key} for ${2:value} in ${3:iterable} if ${4:condition})$0", 45 | "description" : "Create a generator based on existing iterables, with condition if statement." 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python snippets 2 | 3 | Python snippets collections. 4 | 5 | Initially ported from PyCharm, TextMate, SublimeText and other editors/IDEs. 6 | 7 | If you'd like to contribute, feel free to [submit a pull request on github](https://github.com/cstrap/python-snippets) 8 | 9 | Thanks! 10 | 11 | ## Features 12 | 13 | ### Base snippets 14 | 15 | | Abbreviation | Description | 16 | |--------------|-----------------------------------| 17 | | env | #!/usr/bin/env python | 18 | | env3 | #!/usr/bin/env python3 | 19 | | enc | # -*- coding=utf-8 -*- | 20 | | enco | # coding=utf-8 | 21 | | fenc | from future import ... | 22 | | fenco | from future import ... (no `-*-`) | 23 | | im | import | 24 | | fim | from ... import ... | 25 | | class | New class | 26 | | classd | New dataclass | 27 | | defs | New method | 28 | | def | New function | 29 | | dowhile | Do while structure | 30 | | adef | Async function | 31 | | property | New property | 32 | | enum | New Enum | 33 | | if | if | 34 | | for | for | 35 | | lambda | lambda expression | 36 | | while | while | 37 | | try | try:except: | 38 | | tryef | try:except:else:finally: | 39 | | trye | try:except:else: | 40 | | tryf | try:except:finally: | 41 | | s | self | 42 | | __ | __magic__ | 43 | | ifmain | if __name__ == "__main__" | 44 | 45 | ### Comprehensions 46 | 47 | | Abbreviation | Description | 48 | |--------------|------------------------------------| 49 | | lc | List comprehension | 50 | | lcie | List comprehension if else | 51 | | lci | List comprehension if filter | 52 | | dc | Dictionary comprehension | 53 | | dci | Dictionary comprehension if filter | 54 | | sc | Set comprehension | 55 | | sci | Set Comprehension if filter | 56 | | gc | Generator comprehension | 57 | | gci | Generator comprehension if filter | 58 | 59 | ### Unittest 60 | 61 | | Abbreviation | Description | 62 | |--------------|--------------------| 63 | | ase | Assert equal | 64 | | asne | Assert not equal | 65 | | asr | Assert raises | 66 | | ast | Assert True | 67 | | asf | Assert False | 68 | | asi | Assert is | 69 | | asint | Assert is not | 70 | | asino | Assert is None | 71 | | asinno | Assert is not None | 72 | | asin | Assert in | 73 | | asni | Assert not in | 74 | | as | Assert | 75 | | fail | Fail (a test) | 76 | 77 | ### Debugging 78 | 79 | | Abbreviation | Description | 80 | |--------------|----------------| 81 | | pdb | PDB set trace | 82 | | ipdb | iPDB set trace | 83 | | rpdb | rPDB set trace | 84 | | pudb | PuDB set trace | 85 | 86 | ### Tkinter 87 | 88 | | Abbreviation | Description | 89 | |---------------|--------------------| 90 | | imtk | Import Tkinter py2 | 91 | | imtk3 | Import tkinter py3 | 92 | | config | Root configuration | 93 | | button | Button | 94 | | label | Label | 95 | | frame | Frame | 96 | | entry | Entry | 97 | | grid | Grid | 98 | | sticky | Sticky | 99 | | checkbutton | Check button | 100 | | mainloop | Main loop | 101 | | pack | Pack | 102 | | side | Side | 103 | | bind | Bind | 104 | | menu | Menu | 105 | | addcascade | Add cascade | 106 | | addcommand | Add command | 107 | | addseperator | Add seperator | 108 | 109 | ## Release Notes 110 | 111 | See [changelog](CHANGELOG.md) for all changes and releases. 112 | 113 | ## Troubleshooting 114 | 115 | If you experience problems with the auto-formatting of certain snippets, make sure you have the option `editor.tabCompletion` set on `onlySnippets` or `on`. -------------------------------------------------------------------------------- /snippets/base.json: -------------------------------------------------------------------------------- 1 | { 2 | "#!/usr/bin/env python": { 3 | "prefix": "env", 4 | "body": "#!/usr/bin/env python\n$0", 5 | "description" : "Adds shebang line for default python interpreter." 6 | }, 7 | "#!/usr/bin/env python3": { 8 | "prefix": "env3", 9 | "body": "#!/usr/bin/env python3\n$0", 10 | "description" : "Adds shebang line for default python 3 interpreter." 11 | }, 12 | "# -*- coding=utf-8 -*-": { 13 | "prefix": "enc", 14 | "body": "# -*- coding=utf-8 -*-\n$0", 15 | "description" : "set default python2.x encoding specification to utf-8 as it is mentioned in pep-0263." 16 | }, 17 | "# coding=utf-8": { 18 | "prefix": "enco", 19 | "body": "# coding=utf-8\n$0", 20 | "description" : "Set default python3 encoding specification to utf-8, by default this is the encoding for python3.x as it is mentioned in pep-3120." 21 | }, 22 | "from future import ...": { 23 | "prefix": "fenc", 24 | "body": [ 25 | "# -*- coding: utf-8 -*-", 26 | "from __future__ import absolute_import, division, print_function, unicode_literals" 27 | ], 28 | "description" : "Import future statement definitions for python2.x scripts using utf-8 as encoding." 29 | }, 30 | "from future import ... v1": { 31 | "prefix": "fenco", 32 | "body": [ 33 | "# coding: utf-8", 34 | "from __future__ import absolute_import, division, print_function, unicode_literals" 35 | ], 36 | "description" : "Import future statement definitions for python3.x scripts using utf-8 as encoding." 37 | }, 38 | "import": { 39 | "prefix": "im", 40 | "body": "import ${1:package/module}$0", 41 | "description" : "Import a package or module" 42 | }, 43 | "from ... import ...": { 44 | "prefix": "fim", 45 | "body": "from ${1:package/module} import ${2:names}$0", 46 | "description" : "Import statement that allows individual objects from the module to be imported directly into the caller’s symbol table." 47 | }, 48 | "New class": { 49 | "prefix": "class", 50 | "body": [ 51 | "class ${1:ClassName}(${2:object}):", 52 | "\t\"\"\"${3:docstring for $1.}\"\"\"", 53 | "\tdef __init__(self, ${4:arg}):", 54 | "\t\t${5:super($1, self).__init__()}", 55 | "\t${4/([^,=]+)(?:=[^,]+)?(,\\s*|)/\tself.$1 = $1${2:+\n\t}/g}", 56 | "\n\t$0" 57 | ], 58 | "description" : "Code snippet for a class definition." 59 | }, 60 | "New dataclass": { 61 | "prefix": "classd", 62 | "body": [ 63 | "from dataclasses import dataclass\n\n", 64 | "@dataclass", 65 | "class ${1:ClassName}(${2:object}):", 66 | "\t\"\"\"${3:Docstring for $1.}\"\"\"", 67 | "\t${4:property}: ${type}", 68 | "\t$0" 69 | ], 70 | "description": "Code snippet for a dataclass definition." 71 | }, 72 | "New method": { 73 | "prefix": "defs", 74 | "body": "def ${1:mname}(self, ${2:arg}):\n\t${3:pass}$0", 75 | "description" : "Code snippet for a class method definition." 76 | }, 77 | "New function": { 78 | "prefix": "def", 79 | "body": "def ${1:fname}(${2:arg}):\n\t${3:pass}$0", 80 | "description" : "Code snippet for function definition." 81 | }, 82 | "New async function": { 83 | "prefix": "adef", 84 | "body": "async def ${1:fname}(${2:arg}):\n\t${3:pass}$0", 85 | "description" : "Code snippet for async function definition." 86 | }, 87 | "New property": { 88 | "prefix": "property", 89 | "body": "@property\ndef ${1:foo}(self):\n \"\"\"${2:The $1 property.}\"\"\"\n ${3:return self._$1}\n@${4:$1}.setter\ndef ${5:$1}(self, value):\n ${6:self._$1} = value", 90 | "description": "New property: get and set via decorator" 91 | }, 92 | "New froperty": { 93 | "prefix": "property", 94 | "body": "def ${1:foo}():\n doc = \"${2:The $1 property.}\"\n def fget(self):\n ${3:return self._$1}\n def fset(self, value):\n ${4:self._$1 = value}\n def fdel(self):\n ${5:del self._$1}\n return locals()\n$1 = property(**$1())$0", 95 | "description" : "" 96 | }, 97 | "New enum": { 98 | "prefix": "enum", 99 | "body": [ 100 | "from enum import Enum\n\n", 101 | "class ${1:MyEnum}(Enum):", 102 | "\t\"\"\"${2:Docstring for $1.}\"\"\"", 103 | "\t${3:FIRST_ENUM} = \"some_value\"", 104 | "\t${4:SECOND_ENUM} = \"some_other_value\"", 105 | "\t$0" 106 | ], 107 | "description": "Code snippet for enum definition." 108 | }, 109 | "if": { 110 | "prefix": "if", 111 | "body": "if ${1:condition}:\n\t${2:pass}$0", 112 | "description" : "Code snippet for the if statement." 113 | }, 114 | "for": { 115 | "prefix": "for", 116 | "body": "for ${1:value} in ${2:iterable}:\n\t${3:pass}$0", 117 | "description" : "Code snippet to create a for loop structure." 118 | }, 119 | "while": { 120 | "prefix": "while", 121 | "body": "while ${1:condition}:\n\t${2:pass}$0", 122 | "description" : "Code snippet to create a while loop structure." 123 | }, 124 | "dowhile": { 125 | "prefix": "dowhile", 126 | "body": "do = True\nwhile do or ${2:condition}:\n\tdo = False\n\t${1:body}$0", 127 | "description" : "Code snippet to create a do-while loop structure." 128 | }, 129 | "try:except:": { 130 | "prefix": "try", 131 | "body": "try:\n\t${1:pass}\nexcept ${2:Exception} as ${3:e}:\n\t${4:raise $3}$0", 132 | "description" : "Code Snippet for a try and except blocks." 133 | }, 134 | "try:except:else:finally": { 135 | "prefix": "tryef", 136 | "body": "try:\n\t${1:pass}\nexcept${2: ${3:Exception} as ${4:e}}:\n\t${5:raise}\nelse:\n\t${6:pass}\nfinally:\n\t${7:pass}$0", 137 | "description" : "Code Snippet for a try/except/finally with else statement." 138 | }, 139 | "try:except:else": { 140 | "prefix": "trye", 141 | "body": "try:\n\t${1:pass}\nexcept ${2:Exception} as ${3:e}:\n\t${4:raise $3}\nelse:\n\t${5:pass}$0", 142 | "description" : "Code Snippet for a try/except with else statement." 143 | }, 144 | "try:except:finally": { 145 | "prefix": "tryf", 146 | "body": "try:\n\t${1:pass}\nexcept ${2:Exception} as ${3:e}:\n\t${4:raise $3}\nfinally:\n\t${5:pass}$0", 147 | "description" : "Code Snippet for a try/except/finally." 148 | }, 149 | "self": { 150 | "prefix": "s", 151 | "body": "self.$0", 152 | "description" : "Shortend snippet to reference the self property in an object." 153 | }, 154 | "__magic__": { 155 | "prefix": "__", 156 | "body": "__${1:init}__$0", 157 | "description" : "Code snippet to create magic methods." 158 | }, 159 | "if __name__ == \"__main__\"": { 160 | "prefix": "ifmain", 161 | "body": "if __name__ == \"__main__\":\n\t${1:main()}$0", 162 | "description" : "Create implicitly all the code at the top level using the __name__ special variable." 163 | }, 164 | "lambda": { 165 | "prefix": "lam", 166 | "body": "lambda ${1:args}: ${2:expr}", 167 | "description": "Create template for lambda function" 168 | } 169 | } 170 | --------------------------------------------------------------------------------