diff --git a/.gitignore b/.gitignore index 3d858e1..815b42a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vscode/ vsc-* -.DS_Store +.DS_Store/ +flask-snippets-* \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 34db1cf..5cb51aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,23 +1,55 @@ # Change Log + All notable changes to the "python-snippets" extension will be documented in this file. +## [0.1.2] 2022-02-17 + +- Merge PR #18, #19 + +## [0.1.1] 2021-10-14 + +- Merge PR #14 + +## [0.1.0] + +- Merge PRs #15, #16, #17 + +## [0.0.9] + +- Merge PR #11 + +## [0.0.8] + +- Merge PR #9, #10 +- Add "New property", issue #2 + +## [0.0.7] + +- Merge PR #7 + ## [0.0.6] + - Merge PR #1 #3 #5 fix typo, new snippets added ## [0.0.5] + - Fix duplicate snippets, adding some doc ## [0.0.4] + - Added abbreviations list with description on README file ## [0.0.3] + - Initial support for Tkinter, ported from Atom snippets found on [GitHub](https://github.com/adiultra/python-snippets) - Splitting snippets into category files ## [0.0.2] + - Public repo on [GitHub](https://github.com/cstrap/python-snippets), feel free to contribute! :-) ## [0.0.1] + - Initial release - Porting Atom [language-python](https://github.com/atom/language-python) with [atomizr](https://www.npmjs.com/package/node-atomizr) - Added some snippets ported from PyCharm diff --git a/README.md b/README.md index 74ecd29..23d82de 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Python snippets +# Python snippets Python snippets collections. @@ -23,17 +23,22 @@ Thanks! | im | import | | fim | from ... import ... | | class | New class | +| classd | New dataclass | | defs | New method | | def | New function | +| dowhile | Do while structure | +| adef | Async function | | property | New property | +| enum | New Enum | | if | if | | for | for | +| lambda | lambda expression | | while | while | | try | try:except: | | tryef | try:except:else:finally: | | trye | try:except:else: | | tryf | try:except:finally: | -| . | self | +| s | self | | __ | __magic__ | | ifmain | if __name__ == "__main__" | @@ -103,7 +108,8 @@ Thanks! ## Release Notes -## 0.0.5 -- Fix duplicate snippets, adding some doc - See [changelog](CHANGELOG.md) for all changes and releases. + +## Troubleshooting + +If you experience problems with the auto-formatting of certain snippets, make sure you have the option `editor.tabCompletion` set on `onlySnippets` or `on`. \ No newline at end of file diff --git a/package.json b/package.json index b79709d..0bf5373 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "python-snippets", "displayName": "python-snippets", "description": "Python Snippets", - "version": "0.2.0", + "version": "0.1.2", "publisher": "cstrap", "icon": "images/python.png", "engines": { @@ -20,8 +20,7 @@ "url": "https://github.com/cstrap/python-snippets.git" }, "contributes": { - "snippets": [ - { + "snippets": [{ "language": "python", "path": "./snippets/base.json" }, diff --git a/snippets/base.json b/snippets/base.json index 2203da3..dbfcdcd 100644 --- a/snippets/base.json +++ b/snippets/base.json @@ -1,96 +1,169 @@ { "#!/usr/bin/env python": { "prefix": "env", - "body": "#!/usr/bin/env python\n$0" + "body": "#!/usr/bin/env python\n$0", + "description" : "Adds shebang line for default python interpreter." }, "#!/usr/bin/env python3": { "prefix": "env3", - "body": "#!/usr/bin/env python3\n$0" + "body": "#!/usr/bin/env python3\n$0", + "description" : "Adds shebang line for default python 3 interpreter." }, "# -*- coding=utf-8 -*-": { "prefix": "enc", - "body": "# -*- coding=utf-8 -*-\n$0" + "body": "# -*- coding=utf-8 -*-\n$0", + "description" : "set default python2.x encoding specification to utf-8 as it is mentioned in pep-0263." }, "# coding=utf-8": { "prefix": "enco", - "body": "# coding=utf-8\n$0" + "body": "# coding=utf-8\n$0", + "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." }, "from future import ...": { "prefix": "fenc", "body": [ "# -*- coding: utf-8 -*-", "from __future__ import absolute_import, division, print_function, unicode_literals" - ] + ], + "description" : "Import future statement definitions for python2.x scripts using utf-8 as encoding." }, "from future import ... v1": { "prefix": "fenco", "body": [ "# coding: utf-8", "from __future__ import absolute_import, division, print_function, unicode_literals" - ] + ], + "description" : "Import future statement definitions for python3.x scripts using utf-8 as encoding." }, "import": { "prefix": "im", - "body": "import ${1:package/module}$0" + "body": "import ${1:package/module}$0", + "description" : "Import a package or module" }, "from ... import ...": { "prefix": "fim", - "body": "from ${1:package/module} import ${2:names}$0" + "body": "from ${1:package/module} import ${2:names}$0", + "description" : "Import statement that allows individual objects from the module to be imported directly into the caller’s symbol table." }, "New class": { "prefix": "class", - "body": "class ${1:ClassName}(${2:object}):\n\t\"\"\"${3:docstring for $1.}\"\"\"\n\tdef __init__(self, ${4:arg}):\n\t\t${5:super($1, self).__init__()}\n\t\tself.arg = arg\n\t\t$0" + "body": [ + "class ${1:ClassName}(${2:object}):", + "\t\"\"\"${3:docstring for $1.}\"\"\"", + "\tdef __init__(self, ${4:arg}):", + "\t\t${5:super($1, self).__init__()}", + "\t${4/([^,=]+)(?:=[^,]+)?(,\\s*|)/\tself.$1 = $1${2:+\n\t}/g}", + "\n\t$0" + ], + "description" : "Code snippet for a class definition." + }, + "New dataclass": { + "prefix": "classd", + "body": [ + "from dataclasses import dataclass\n\n", + "@dataclass", + "class ${1:ClassName}(${2:object}):", + "\t\"\"\"${3:Docstring for $1.}\"\"\"", + "\t${4:property}: ${type}", + "\t$0" + ], + "description": "Code snippet for a dataclass definition." }, "New method": { "prefix": "defs", - "body": "def ${1:mname}(self, ${2:arg}):\n\t${3:pass}$0" + "body": "def ${1:mname}(self, ${2:arg}):\n\t${3:pass}$0", + "description" : "Code snippet for a class method definition." }, "New function": { "prefix": "def", - "body": "def ${1:fname}(${2:arg}):\n\t${3:pass}$0" + "body": "def ${1:fname}(${2:arg}):\n\t${3:pass}$0", + "description" : "Code snippet for function definition." + }, + "New async function": { + "prefix": "adef", + "body": "async def ${1:fname}(${2:arg}):\n\t${3:pass}$0", + "description" : "Code snippet for async function definition." + }, + "New property": { + "prefix": "property", + "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", + "description": "New property: get and set via decorator" }, "New froperty": { "prefix": "property", - "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" + "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", + "description" : "" + }, + "New enum": { + "prefix": "enum", + "body": [ + "from enum import Enum\n\n", + "class ${1:MyEnum}(Enum):", + "\t\"\"\"${2:Docstring for $1.}\"\"\"", + "\t${3:FIRST_ENUM} = \"some_value\"", + "\t${4:SECOND_ENUM} = \"some_other_value\"", + "\t$0" + ], + "description": "Code snippet for enum definition." }, "if": { "prefix": "if", - "body": "if ${1:condition}:\n\t${2:pass}$0" + "body": "if ${1:condition}:\n\t${2:pass}$0", + "description" : "Code snippet for the if statement." }, "for": { "prefix": "for", - "body": "for ${1:value} in ${2:iterable}:\n\t${3:pass}$0" + "body": "for ${1:value} in ${2:iterable}:\n\t${3:pass}$0", + "description" : "Code snippet to create a for loop structure." }, "while": { "prefix": "while", - "body": "while ${1:condition}:\n\t${2:pass}$0" + "body": "while ${1:condition}:\n\t${2:pass}$0", + "description" : "Code snippet to create a while loop structure." + }, + "dowhile": { + "prefix": "dowhile", + "body": "do = True\nwhile do or ${2:condition}:\n\tdo = False\n\t${1:body}$0", + "description" : "Code snippet to create a do-while loop structure." }, "try:except:": { "prefix": "try", - "body": "try:\n\t${1:pass}\nexcept ${2:Exception} as ${3:e}:\n\t${4:raise $3}$0" + "body": "try:\n\t${1:pass}\nexcept ${2:Exception} as ${3:e}:\n\t${4:raise $3}$0", + "description" : "Code Snippet for a try and except blocks." }, "try:except:else:finally": { "prefix": "tryef", - "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" + "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", + "description" : "Code Snippet for a try/except/finally with else statement." }, "try:except:else": { "prefix": "trye", - "body": "try:\n\t${1:pass}\nexcept ${2:Exception} as ${3:e}:\n\t${4:raise $3}\nelse:\n\t${5:pass}$0" + "body": "try:\n\t${1:pass}\nexcept ${2:Exception} as ${3:e}:\n\t${4:raise $3}\nelse:\n\t${5:pass}$0", + "description" : "Code Snippet for a try/except with else statement." }, "try:except:finally": { "prefix": "tryf", - "body": "try:\n\t${1:pass}\nexcept ${2:Exception} as ${3:e}:\n\t${4:raise $3}\nfinally:\n\t${5:pass}$0" + "body": "try:\n\t${1:pass}\nexcept ${2:Exception} as ${3:e}:\n\t${4:raise $3}\nfinally:\n\t${5:pass}$0", + "description" : "Code Snippet for a try/except/finally." }, "self": { - "prefix": ".", - "body": "self.$0" + "prefix": "s", + "body": "self.$0", + "description" : "Shortend snippet to reference the self property in an object." }, "__magic__": { "prefix": "__", - "body": "__${1:init}__$0" + "body": "__${1:init}__$0", + "description" : "Code snippet to create magic methods." }, "if __name__ == \"__main__\"": { "prefix": "ifmain", - "body": "if __name__ == \"__main__\":\n\t${1:main()}$0" + "body": "if __name__ == \"__main__\":\n\t${1:main()}$0", + "description" : "Create implicitly all the code at the top level using the __name__ special variable." + }, + "lambda": { + "prefix": "lam", + "body": "lambda ${1:args}: ${2:expr}", + "description": "Create template for lambda function" } -} \ No newline at end of file +} diff --git a/snippets/comprehension.json b/snippets/comprehension.json index 8150dbf..0bc0db1 100644 --- a/snippets/comprehension.json +++ b/snippets/comprehension.json @@ -1,38 +1,47 @@ { "List comprehension": { "prefix": "lc", - "body": "[${1:value} for ${2:value} in ${3:iterable}]$0" + "body": "[${1:value} for ${2:value} in ${3:iterable}]$0", + "description" : "List comprehension for creating a list based on existing lists." }, "List comprehension if else": { "prefix": "lcie", - "body": "[${1:value} if ${2:condition} else ${3:condition} for ${4:value} in ${5:iterable}]$0" + "body": "[${1:value} if ${2:condition} else ${3:condition} for ${4:value} in ${5:iterable}]$0", + "description" : "List comprehension for creating a list based on existing lists, with conditional if-else statement." }, "List comprehension if filter": { "prefix": "lci", - "body": "[${1:value} for ${2:value} in ${3:iterable} if ${4:condition}$0]" + "body": "[${1:value} for ${2:value} in ${3:iterable} if ${4:condition}$0]", + "description" : "List comprehension for creating a list based on existing lists, with conditional if statement." }, "Dictionary comprehension": { "prefix": "dc", - "body": "{${1:key}: ${2:value} for ${3:key}, ${4:value} in ${5:iterable}}$0" + "body": "{${1:key}: ${2:value} for ${3:key}, ${4:value} in ${5:iterable}}$0", + "description" : "Handy and faster way to create dictories based on existing dictionaries." }, "Dictionary comprehension if filter": { "prefix": "dci", - "body": "{${1:key}: ${2:value} for ${3:key}, ${4:value} in ${5:iterable} if ${6:condition}}$0" + "body": "{${1:key}: ${2:value} for ${3:key}, ${4:value} in ${5:iterable} if ${6:condition}}$0", + "description" : "Handy and faster way to create dictories based on existing dictionaries, with conditional if statement." }, "Set comprehension": { "prefix": "sc", - "body": "{${1:value} for ${2:value} in ${3:iterable}}$0" + "body": "{${1:value} for ${2:value} in ${3:iterable}}$0", + "description" : "Create a set based on existing iterables." }, "Set Comprehension if filter": { "prefix": "sci", - "body": "{${1:value} for ${2:value} in ${3:iterable} if ${4:condition}}$0" + "body": "{${1:value} for ${2:value} in ${3:iterable} if ${4:condition}}$0", + "description" : "Create a set based on existing iterables, with condition if statement." }, - "Gerator comprehension": { + "Generator comprehension": { "prefix": "gc", - "body": "(${1:key} for ${2:value} in ${3:iterable})$0" + "body": "(${1:key} for ${2:value} in ${3:iterable})$0", + "description" : "Create a generator based on existing iterables." }, - "Gerator comprehension if filter": { + "Generator comprehension if filter": { "prefix": "gci", - "body": "(${1:key} for ${2:value} in ${3:iterable} if ${4:condition})$0" + "body": "(${1:key} for ${2:value} in ${3:iterable} if ${4:condition})$0", + "description" : "Create a generator based on existing iterables, with condition if statement." } -} \ No newline at end of file +}