1
0
Fork 0
mirror of synced 2024-11-22 16:55:34 -05:00
ultimate-vim/sources_non_forked/vim-snippets/snippets/python.snippets

524 lines
9.5 KiB
Text
Raw Normal View History

2012-08-16 23:41:25 -04:00
snippet #!
2018-11-01 06:03:42 -04:00
#!/usr/bin/env python3
2018-02-04 06:35:08 -05:00
snippet #!2
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
2015-12-08 08:20:04 -05:00
snippet #!3
#!/usr/bin/env python3
2012-08-16 23:41:25 -04:00
snippet imp
import ${0:module}
2013-07-17 19:06:05 -04:00
snippet uni
def __unicode__(self):
${0:representation}
2012-08-16 23:41:25 -04:00
snippet from
from ${1:package} import ${0:module}
2022-08-08 09:45:56 -04:00
2012-08-16 23:41:25 -04:00
# Module Docstring
snippet docs
"""
2013-07-17 19:06:05 -04:00
File: ${1:`vim_snippets#Filename('$1.py', 'foo.py')`}
Author: `g:snips_author`
Email: `g:snips_email`
Github: `g:snips_github`
Description: ${0}
"""
2013-07-17 19:06:05 -04:00
2019-01-08 05:11:54 -05:00
# Unittest skip
snippet sk "skip unittests" b
2019-03-08 06:04:56 -05:00
@unittest.skip(${1:skip_reason})
2019-01-08 05:11:54 -05:00
2012-08-16 23:41:25 -04:00
snippet wh
while $1:
2017-02-11 08:01:38 -05:00
${0:${VISUAL}}
2022-08-08 09:45:56 -04:00
2012-08-16 23:41:25 -04:00
# dowh - does the same as do...while in other languages
snippet dowh
while True:
2013-07-17 19:06:05 -04:00
${1}
if $0:
2012-08-16 23:41:25 -04:00
break
2022-08-08 09:45:56 -04:00
2012-08-16 23:41:25 -04:00
snippet with
with ${1:expr} as ${2:var}:
2017-02-11 08:01:38 -05:00
${0:${VISUAL}}
2022-08-08 09:45:56 -04:00
snippet awith
async with ${1:expr} as ${2:var}:
${0:${VISUAL}}
2022-08-08 09:45:56 -04:00
2012-08-16 23:41:25 -04:00
# New Class
snippet cl
class ${1:ClassName}(${2:object}):
"""${3:docstring for $1}"""
def __init__(self, ${4:arg}):
${5:super($1, self).__init__()}
self.$4 = $4
${0}
2018-06-14 06:31:12 -04:00
snippet cla
class ${1:class_name}:
"""${0:description}"""
2022-08-08 09:45:56 -04:00
2018-06-14 06:31:12 -04:00
snippet clai
class ${1:class_name}:
"""${2:description}"""
def __init__(self, ${3:args}):
${0}
2022-08-08 09:45:56 -04:00
# Data class
snippet dcl dataclass
@dataclass
class ${1:ClassName}:
"""${2:description}"""
${3:var_1}: ${4:int}
${5:var_2}: ${6:float} = ${7:0}
def ${8:total}(self): -> $6:
return ${0:self.$3 * self.$5}
2012-08-16 23:41:25 -04:00
# New Function
snippet def
def ${1:fname}(${2:`indent('.') ? 'self' : ''`}):
"""${3:docstring for $1}"""
${0}
2012-08-16 23:41:25 -04:00
snippet deff
def ${1:fname}(${2:`indent('.') ? 'self' : ''`}):
${0}
snippet adef
async def ${1:fname}(${2:`indent('.') ? 'self' : ''`}):
"""${3:docstring for $1}"""
${0}
snippet adeff
async def ${1:fname}(${2:`indent('.') ? 'self' : ''`}):
${0}
2022-08-08 09:45:56 -04:00
2012-08-16 23:41:25 -04:00
# New Method
2018-06-14 06:31:12 -04:00
snippet defi
def __init__(self, ${1:args}):
${0}
2015-07-13 06:22:46 -04:00
snippet defm
2012-08-16 23:41:25 -04:00
def ${1:mname}(self, ${2:arg}):
${0}
snippet adefm
async def ${1:mname}(self, ${2:arg}):
${0}
2012-08-16 23:41:25 -04:00
# New Property
snippet property
2021-10-11 05:30:43 -04:00
@property
def ${1:foo}(self) -> ${2:type}:
"""${3:doc}"""
2022-08-08 09:45:56 -04:00
return self._$1
2021-10-11 05:30:43 -04:00
2022-08-08 09:45:56 -04:00
@$1.setter
def $1(self, value: $2):
self._$1 = value
2021-10-11 05:30:43 -04:00
2012-08-16 23:41:25 -04:00
# Ifs
snippet if
if $1:
2017-02-11 08:01:38 -05:00
${0:${VISUAL}}
2012-08-16 23:41:25 -04:00
snippet el
else:
2017-02-11 08:01:38 -05:00
${0:${VISUAL}}
2012-08-16 23:41:25 -04:00
snippet ei
elif $1:
2017-02-11 08:01:38 -05:00
${0:${VISUAL}}
2022-08-08 09:45:56 -04:00
# Match
snippet match Structural pattern matching
match ${1:expression}:
case ${2:pattern_1}:
${3:pass}
case ${4:pattern_2}:
${5:pass}
# Match with wildcard
snippet matchw Pattern matching with wildcard
match ${1:expression}:
case ${2:pattern_1}:
${3:pass}
case _:
${0:pass}
2012-08-16 23:41:25 -04:00
# For
snippet for
for ${1:item} in ${2:items}:
${0}
2022-08-08 09:45:56 -04:00
2012-08-16 23:41:25 -04:00
# Encodes
snippet cutf8
# -*- coding: utf-8 -*-
snippet clatin1
# -*- coding: latin-1 -*-
snippet cascii
# -*- coding: ascii -*-
2022-08-08 09:45:56 -04:00
2012-08-16 23:41:25 -04:00
# Lambda
2013-07-17 19:06:05 -04:00
snippet ld
${1:var} = lambda ${2:vars} : ${0:action}
2022-08-08 09:45:56 -04:00
snippet ret
return ${0}
2012-08-16 23:41:25 -04:00
snippet .
self.
2020-04-25 21:56:16 -04:00
snippet sa self.attribute = attribute
self.${1:attribute} = $1
2022-08-08 09:45:56 -04:00
2012-08-16 23:41:25 -04:00
snippet try Try/Except
try:
2017-02-11 08:01:38 -05:00
${1:${VISUAL}}
except ${2:Exception} as ${3:e}:
${0:raise $3}
2018-03-31 10:56:26 -04:00
snippet trye Try/Except/Else
2012-08-16 23:41:25 -04:00
try:
2017-02-11 08:01:38 -05:00
${1:${VISUAL}}
except ${2:Exception} as ${3:e}:
2012-08-16 23:41:25 -04:00
${4:raise $3}
else:
${0}
2018-03-31 10:56:26 -04:00
snippet tryf Try/Except/Finally
2012-08-16 23:41:25 -04:00
try:
2017-02-11 08:01:38 -05:00
${1:${VISUAL}}
except ${2:Exception} as ${3:e}:
2012-08-16 23:41:25 -04:00
${4:raise $3}
finally:
${0}
2018-03-31 10:56:26 -04:00
snippet tryef Try/Except/Else/Finally
2012-08-16 23:41:25 -04:00
try:
2017-02-11 08:01:38 -05:00
${1:${VISUAL}}
except ${2:Exception} as ${3:e}:
2012-08-16 23:41:25 -04:00
${4:raise $3}
else:
2013-07-17 19:06:05 -04:00
${5}
2012-08-16 23:41:25 -04:00
finally:
${0}
2022-08-08 09:45:56 -04:00
2012-08-16 23:41:25 -04:00
# if __name__ == '__main__':
snippet ifmain
if __name__ == '__main__':
${0:main()}
2012-08-16 23:41:25 -04:00
# __magic__
snippet _
__${1:init}__
2022-08-08 09:45:56 -04:00
# debugger breakpoint
snippet br
breakpoint()
2012-08-16 23:41:25 -04:00
# python debugger (pdb)
snippet pdb
__import__('pdb').set_trace()
2016-02-20 08:13:10 -05:00
# bpython debugger (bpdb)
snippet bpdb
__import__('bpdb').set_trace()
2012-08-16 23:41:25 -04:00
# ipython debugger (ipdb)
snippet ipdb
__import__('ipdb').set_trace()
2015-01-18 07:58:28 -05:00
# embed ipython itself
snippet iem
__import__('IPython').embed()
2015-02-04 05:43:54 -05:00
# remote python debugger (rpdb)
snippet rpdb
__import__('rpdb').set_trace()
2017-05-02 08:42:08 -04:00
# web python debugger (wdb)
snippet wdb
__import__('wdb').set_trace()
2015-07-13 06:22:46 -04:00
# ptpython
snippet ptpython
__import__('ptpython.repl', fromlist=('repl')).embed(globals(), locals(), vi_mode=${1:False}, history_filename=${2:None})
2013-07-17 19:06:05 -04:00
# python console debugger (pudb)
snippet pudb
__import__('pudb').set_trace()
2020-04-25 21:56:16 -04:00
# python console debugger remote (pudb)
snippet pudbr
from pudb.remote import set_trace
set_trace()
2015-12-08 08:20:04 -05:00
# pdb in nosetests
snippet nosetrace
__import__('nose').tools.set_trace()
2012-08-16 23:41:25 -04:00
snippet pprint
__import__('pprint').pprint(${1})
2022-08-08 09:45:56 -04:00
2012-08-16 23:41:25 -04:00
snippet "
2015-07-13 06:22:46 -04:00
"""${0:doc}
2012-08-16 23:41:25 -04:00
"""
2022-08-08 09:45:56 -04:00
# assertions
snippet a=
self.assertEqual(${0}, ${1})
2012-08-16 23:41:25 -04:00
# test function/method
snippet test
def test_${1:description}(${2:`indent('.') ? 'self' : ''`}):
${0}
2012-08-16 23:41:25 -04:00
# test case
snippet testcase
class ${1:ExampleCase}(unittest.TestCase):
2013-07-17 19:06:05 -04:00
2012-08-16 23:41:25 -04:00
def test_${2:description}(self):
${0}
# test given when then
snippet tgwt
# given: ${1}
# when: ${2}
# then: ${3}
2012-08-16 23:41:25 -04:00
snippet fut
from __future__ import ${0}
2022-08-08 09:45:56 -04:00
#getopt
snippet getopt
try:
# Short option syntax: "hv:"
# Long option syntax: "help" or "verbose="
opts, args = getopt.getopt(sys.argv[1:], "${1:short_options}", [${2:long_options}])
2013-07-17 19:06:05 -04:00
except getopt.GetoptError, err:
# Print debug info
print str(err)
${3:error_action}
for option, argument in opts:
if option in ("-h", "--help"):
${0}
elif option in ("-v", "--verbose"):
verbose = argument
2022-08-08 09:45:56 -04:00
2018-02-04 06:35:08 -05:00
# argparse
snippet addp
parser = ${VISUAL:argparse.}ArgumentParser()
snippet addsp
${0:sub_parser} = parser.add_subparsers().add_parser("${1:name}")
snippet addarg
parser.add_argument("${0:short_arg}", "${1:long_arg}", default=${2:None}, help="${3:Help text}")
snippet addnarg
2021-06-23 05:57:12 -04:00
parser.add_argument("${0:arg}", nargs="${1:*}", default=${2:None}, help="${3:Help text}")
2018-02-04 06:35:08 -05:00
snippet addaarg
parser.add_argument("${0:arg}", "${1:long_arg}", action="${2:store_true}", default=${3:False}, help="${4:Help text}")
snippet pargs
"${VISUAL:return }"parser.parse_args()
2019-03-08 06:04:56 -05:00
2013-05-25 20:31:29 -04:00
# logging
# glog = get log
snippet glog
import logging
LOGGER = logging.getLogger(${0:__name__})
2013-05-25 20:31:29 -04:00
snippet le
LOGGER.error(${0:msg})
2013-07-17 19:06:05 -04:00
# conflict with lambda=ld, therefor we change into Logger.debuG
snippet lg
LOGGER.debug(${0:msg})
2013-05-25 20:31:29 -04:00
snippet lw
LOGGER.warning(${0:msg})
2013-05-25 20:31:29 -04:00
snippet lc
LOGGER.critical(${0:msg})
2013-05-25 20:31:29 -04:00
snippet li
LOGGER.info(${0:msg})
2013-08-03 08:50:12 -04:00
snippet epydoc
2015-07-13 06:22:46 -04:00
"""${1:Description}
2013-08-03 08:50:12 -04:00
@param ${2:param}: ${3: Description}
@type $2: ${4: Type}
@return: ${5: Description}
@rtype : ${6: Type}
@raise e: ${0: Description}
2013-08-03 08:50:12 -04:00
"""
2014-04-18 08:58:02 -04:00
snippet dol
def ${1:__init__}(self, *args, **kwargs):
2022-08-08 09:45:56 -04:00
super(${0:ClassName}, self).$1(*args, **kwargs)
2014-04-18 08:58:02 -04:00
snippet kwg
self.${1:var_name} = kwargs.get('$1', ${2:None})
snippet lkwg
${1:var_name} = kwargs.get('$1', ${2:None})
2015-01-18 07:58:28 -05:00
snippet args
*args${1:,}${0}
snippet kwargs
**kwargs${1:,}${0}
snippet akw
*args, **kwargs${1:,}${0}
# comprehensions
snippet lcp list comprehension
[${1} for ${2} in ${3:${VISUAL}}]${0}
snippet dcp dict comprehension
{${1}: ${2} for ${3} in ${4:${VISUAL}}}${0}
snippet scp set comprehension
{${1} for ${2} in ${3:${VISUAL}}}${0}
2018-02-04 06:35:08 -05:00
snippet contain "methods for emulating a container type" b
def __len__(self):
${1:pass}
def __getitem__(self, key):
${2:pass}
def __setitem__(self, key, value):
${3:pass}
def __delitem__(self, key):
${4:pass}
def __iter__(self):
${5:pass}
def __reversed__(self):
${6:pass}
def __contains__(self, item):
${7:pass}
2019-03-08 06:04:56 -05:00
2018-02-04 06:35:08 -05:00
snippet context "context manager methods" b
def __enter__(self):
${1:pass}
def __exit__(self, exc_type, exc_value, traceback):
${2:pass}
2019-03-08 06:04:56 -05:00
2018-02-04 06:35:08 -05:00
snippet attr "methods for customizing attribute access" b
def __getattr__(self, name):
${1:pass}
def __setattr__(self, name, value):
${2:pass}
def __delattr__(self, name):
${3:pass}
2019-03-08 06:04:56 -05:00
2018-02-04 06:35:08 -05:00
snippet desc "methods implementing descriptors" b
def __get__(self, instance, owner):
${1:pass}
def __set__(self, instance, value):
${2:pass}
def __delete__(self, instance):
${3:pass}
2019-03-08 06:04:56 -05:00
2018-02-04 06:35:08 -05:00
snippet cmp "methods implementing rich comparison"
def __eq__(self, other):
${1:pass}
def __ne__(self, other):
${2:pass}
def __lt__(self, other):
${3:pass}
def __le__(self, other):
${4:pass}
def __gt__(self, other):
${5:pass}
def __ge__(self, other):
${6:pass}
def __cmp__(self, other):
${7:pass}
2019-03-08 06:04:56 -05:00
2018-02-04 06:35:08 -05:00
snippet repr "methods implementing string representation"
def __repr__(self):
${1:pass}
def __str__(self):
${2:pass}
def __unicode__(self):
${3:pass}
2019-03-08 06:04:56 -05:00
2018-02-04 06:35:08 -05:00
# note: reflected operands and augmented arithmeitc assignements have been
# intentionally ommited to reduce verbosity.
snippet numeric "methods for emulating a numeric type" b
def __add__(self, other):
${1:pass}
def __sub__(self, other):
${2:pass}
def __mul__(self, other):
${3:pass}
def __div__(self, other):
${4:pass}
def __truediv__(self, other):
${5:pass}
def __floordiv__(self, other):
${6:pass}
def __mod__(self, other):
${7:pass}
def __divmod__(self, other):
${8:pass}
def __pow__(self, other):
${9:pass}
def __lshift__(self, other):
${10:pass}
def __rshift__(self, other):
${11:pass}
def __and__(self, other):
${12:pass}
def __xor__(self, other):
${13:pass}
def __or__(self, other):
${14:pass}
def __neg__(self):
${15:pass}
def __pos__(self):
${16:pass}
def __abs__(self):
${17:pass}
def __invert__(self):
${18:pass}
def __complex__(self):
${19:pass}
def __int__(self):
${20:pass}
def __long__(self):
${21:pass}
def __float__(self):
${22:pass}
def __oct__(self):
${22:pass}
def __hex__(self):
${23:pass}
def __index__(self):
${24:pass}
def __coerce__(self, other):
${25:pass}
2019-03-08 06:04:56 -05:00
# Printing
snippet pr
print($0)
snippet prs
print("$0")
snippet prf
print(f"$0")
snippet fpr
print($0, file=${1:sys.stderr})
snippet fprs
print("$0", file=${1:sys.stderr})
snippet fprf
print(f"$0", file=${1:sys.stderr})