Skip to content

Instantly share code, notes, and snippets.

@andreberg
Last active June 5, 2024 19:15
Show Gist options
  • Save andreberg/d3876b82f9f33343862534df96ed2906 to your computer and use it in GitHub Desktop.
Save andreberg/d3876b82f9f33343862534df96ed2906 to your computer and use it in GitHub Desktop.
[VSCode: Python Snippets] Python snippet collection. #vscode #python #snippets #template #json
{
// 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"
// }
"Open file for reading": {
"prefix": "readfile",
"body": [
"with open($1) as f:",
"\ttext = f.read()"
],
"description": "Open file for reading in a with statement"
},
"Open file for writing": {
"prefix": "writefile",
"body": [
"with open($1, w) as f:",
"\tf.write($2)"
],
"description": "Open file for writing using a with statement"
},
"Encoding statement": {
"prefix": "cod",
"body": "# -*- coding: utf-8 -*-",
"description": "File encoding statement"
},
"File header comment": {
"prefix": "fileheader",
"body": [
"# -*- coding: utf-8 -*-",
"#",
"# $TM_FILENAME",
"# ${2:project}",
"#",
"# Created by André Berg on $CURRENT_YEAR-$CURRENT_MONTH-$CURRENT_DATE.",
"# Copyright $CURRENT_YEAR Iris VFX. All rights reserved.",
"#",
"# andre@irisvfx.com",
"#",
"# pylint: disable-msg=F0401"
]
},
"Abstract method": {
"prefix": "abstractmethod",
"body": [
"def ${1:name}(self):",
"\traise NotImplementedError(\"This method is abstract. To provide a concrete implementation override this method in a subclass.\")"
],
"description": "Abstract method body"
},
"Print function variable": {
"prefix": "pvar",
"body": [
"print('${1:func}: ${2:var} = {}'.format($2))"
],
"description": "Print a function local variable"
},
"Main entry point": {
"prefix": "main",
"body": [
"def ${1:main()}:",
"\t$0",
"",
"",
"if __name__ == '__main__':",
"\t${1:main()}"
],
"description": "Call entry point function if this script is run directly"
},
"New class with init": {
"prefix": "class1",
"body": [
"class ${1:name}${2:(object)}:",
"\tdef __init__(self, ${3:var}):",
"\t\tself.${3:var} = ${3:var}",
"\tdef __str__(self):",
"\t\treturn '${1:name}@{:#x}: {}'.format(id(self), self.${3:var})"
],
"description": "Simple class skeleton with init and str magic methods"
},
"New class with init and docstring": {
"prefix": "class2",
"body": [
"class ${1:name}${2:(object)}:",
"\t\"\"\" ${3:docstring}",
"\t\"\"\"",
"\tdef __init__(self, ${4:var}):",
"\t\tself.${4:var} = ${4:var}",
"\tdef __str__(self):",
"\t\treturn '${1:name}@{:#x}: {}'.format(id(self), self.${4:var})",
],
"description": "Class definition with doctsring"
},
"Function decorator": {
"prefix": "decor",
"body": [
"def ${1:name}(fn):",
"\tdef ${1:name}_wrapper(*args, **kwargs):",
"\t\tprint('{}: args={} kwargs={}'.format(fn.__name__, args, kwargs))",
"\t\treturn fn(*args, **kwargs)",
"\treturn ${1:name}_wrapper"
],
"description": "Function decorator"
},
"Decorated property with encapsulation": {
"prefix": "eprop",
"body": [
"@property",
"def ${1:name}(self):",
"\treturn self.__${1:name}",
"@${1:name}.setter",
"def ${1:name}(self, value):",
"\tself.__${1:name} = value",
"@${1:name}.deleter",
"def ${1:name}(self, value):",
"\tdel self.__${1:name}"
],
"description": "Encapsulated property getter, setter and deleter"
},
"Decorated property": {
"prefix": "prop1",
"body": [
"@property",
"def ${1:name}(self):",
"\treturn self.${1:name}",
"@${1:name}.setter",
"def ${1:name}(self, value):",
"\tself.${1:name} = value",
"@${1:name}.deleter",
"def ${1:name}(self, value):",
"\tdel self.${1:name}"
],
"description": "Property getter, setter and deleter"
},
"Decorated property (without deleter)": {
"prefix": "prop2",
"body": [
"@property",
"def ${1:name}(self):",
"\treturn self.${1:name}",
"@${1:name}.setter",
"def ${1:name}(self, value):",
"\tself.${1:name} = value",
],
"description": "Property getter and setter"
}
}
@pachecolanzziano
Copy link

thank's your contribution

@godKillerDagerott
Copy link

great work man, saved a lot of time

@kostakazakoff
Copy link

Thanks a lot :)

@Lewis-Lu
Copy link

Cheers mate, brilliant file!

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