mirror of
https://github.com/amix/vimrc
synced 2025-07-18 17:44:59 +08:00
merge
This commit is contained in:
@ -7,18 +7,24 @@ priority -50
|
||||
#! header
|
||||
snippet #! "Shebang header for python scripts" b
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
# -*- coding: utf-8 -*-
|
||||
$0
|
||||
endsnippet
|
||||
|
||||
snippet ifmain "ifmain" b
|
||||
if __name__ == '__main__':
|
||||
${1:main()}$0
|
||||
if __name__ == `!p snip.rv = get_quoting_style(snip)`__main__`!p snip.rv = get_quoting_style(snip)`:
|
||||
${1:${VISUAL:main()}}
|
||||
endsnippet
|
||||
|
||||
snippet with "with" b
|
||||
with ${1:expr}`!p snip.rv = " as " if t[2] else ""`${2:var}:
|
||||
${3:${VISUAL:pass}}
|
||||
$0
|
||||
endsnippet
|
||||
|
||||
snippet for "for loop" b
|
||||
for ${1:item} in ${2:iterable}:
|
||||
${3:pass}
|
||||
${3:${VISUAL:pass}}
|
||||
endsnippet
|
||||
|
||||
##########
|
||||
@ -35,6 +41,8 @@ NORMAL = 0x1
|
||||
DOXYGEN = 0x2
|
||||
SPHINX = 0x3
|
||||
GOOGLE = 0x4
|
||||
NUMPY = 0x5
|
||||
JEDI = 0x6
|
||||
|
||||
SINGLE_QUOTES = "'"
|
||||
DOUBLE_QUOTES = '"'
|
||||
@ -69,7 +77,10 @@ def get_quoting_style(snip):
|
||||
return DOUBLE_QUOTES
|
||||
|
||||
def triple_quotes(snip):
|
||||
return get_quoting_style(snip) * 3
|
||||
style = snip.opt("g:ultisnips_python_triple_quoting_style")
|
||||
if not style:
|
||||
return get_quoting_style(snip) * 3
|
||||
return (SINGLE_QUOTES if style == 'single' else DOUBLE_QUOTES) * 3
|
||||
|
||||
def triple_quotes_handle_trailing(snip, quoting_style):
|
||||
"""
|
||||
@ -105,6 +116,8 @@ def get_style(snip):
|
||||
if style == "doxygen": return DOXYGEN
|
||||
elif style == "sphinx": return SPHINX
|
||||
elif style == "google": return GOOGLE
|
||||
elif style == "numpy": return NUMPY
|
||||
elif style == "jedi": return JEDI
|
||||
else: return NORMAL
|
||||
|
||||
|
||||
@ -117,12 +130,16 @@ def format_arg(arg, style):
|
||||
return ":%s: TODO" % arg
|
||||
elif style == GOOGLE:
|
||||
return "%s (TODO): TODO" % arg
|
||||
elif style == JEDI:
|
||||
return ":type %s: TODO" % arg
|
||||
elif style == NUMPY:
|
||||
return "%s : TODO" % arg
|
||||
|
||||
|
||||
def format_return(style):
|
||||
if style == DOXYGEN:
|
||||
return "@return: TODO"
|
||||
elif style in (NORMAL, SPHINX):
|
||||
elif style in (NORMAL, SPHINX, JEDI):
|
||||
return ":returns: TODO"
|
||||
elif style == GOOGLE:
|
||||
return "Returns: TODO"
|
||||
@ -139,6 +156,8 @@ def write_docstring_args(args, snip):
|
||||
|
||||
if style == GOOGLE:
|
||||
write_google_docstring_args(args, snip)
|
||||
elif style == NUMPY:
|
||||
write_numpy_docstring_args(args, snip)
|
||||
else:
|
||||
for arg in args:
|
||||
snip += format_arg(arg, style)
|
||||
@ -165,6 +184,23 @@ def write_google_docstring_args(args, snip):
|
||||
snip.rv += '\n' + snip.mkline('', indent='')
|
||||
|
||||
|
||||
def write_numpy_docstring_args(args, snip):
|
||||
if args:
|
||||
snip += "Parameters"
|
||||
snip += "----------"
|
||||
|
||||
kwargs = [arg for arg in args if arg.is_kwarg()]
|
||||
args = [arg for arg in args if not arg.is_kwarg()]
|
||||
|
||||
if args:
|
||||
for arg in args:
|
||||
snip += format_arg(arg, NUMPY)
|
||||
if kwargs:
|
||||
for kwarg in kwargs:
|
||||
snip += format_arg(kwarg, NUMPY) + ', optional'
|
||||
snip.rv += '\n' + snip.mkline('', indent='')
|
||||
|
||||
|
||||
def write_init_body(args, parents, snip):
|
||||
parents = [p.strip() for p in parents.split(",")]
|
||||
parents = [p for p in parents if p != 'object']
|
||||
@ -199,10 +235,19 @@ def write_function_docstring(t, snip):
|
||||
write_docstring_args(args, snip)
|
||||
|
||||
style = get_style(snip)
|
||||
snip += format_return(style)
|
||||
|
||||
if style == NUMPY:
|
||||
snip += 'Returns'
|
||||
snip += '-------'
|
||||
snip += 'TODO'
|
||||
else:
|
||||
snip += format_return(style)
|
||||
snip.rv += '\n' + snip.mkline('', indent='')
|
||||
snip += triple_quotes(snip)
|
||||
|
||||
def get_dir_and_file_name(snip):
|
||||
return os.getcwd().split(os.sep)[-1] + '.' + snip.basename
|
||||
|
||||
endglobal
|
||||
|
||||
########################################
|
||||
@ -441,13 +486,18 @@ def __coerce__(self, other):
|
||||
${25:pass}
|
||||
endsnippet
|
||||
|
||||
snippet deff
|
||||
def ${1:fname}(`!p snip.rv = vim.eval('indent(".") ? "self" : ""')`$2):
|
||||
$0
|
||||
endsnippet
|
||||
|
||||
snippet def "function with docstrings" b
|
||||
def ${1:function}(`!p
|
||||
if snip.indent:
|
||||
snip.rv = 'self' + (", " if len(t[2]) else "")`${2:arg1}):
|
||||
`!p snip.rv = triple_quotes(snip)`${4:TODO: Docstring for $1.}`!p
|
||||
write_function_docstring(t, snip) `
|
||||
${0:pass}
|
||||
${5:${VISUAL:pass}}
|
||||
endsnippet
|
||||
|
||||
|
||||
@ -458,7 +508,7 @@ if snip.indent:
|
||||
snip.rv = 'cls' + (", " if len(t[2]) else "")`${2:arg1}):
|
||||
`!p snip.rv = triple_quotes(snip)`${4:TODO: Docstring for $1.}`!p
|
||||
write_function_docstring(t, snip) `
|
||||
${0:pass}
|
||||
${5:${VISUAL:pass}}
|
||||
endsnippet
|
||||
|
||||
|
||||
@ -467,7 +517,7 @@ snippet defs "static method with docstrings" b
|
||||
def ${1:function}(${2:arg1}):
|
||||
`!p snip.rv = triple_quotes(snip)`${4:TODO: Docstring for $1.}`!p
|
||||
write_function_docstring(t, snip) `
|
||||
${0:pass}
|
||||
${5:${VISUAL:pass}}
|
||||
endsnippet
|
||||
|
||||
|
||||
@ -520,19 +570,19 @@ endsnippet
|
||||
####################
|
||||
snippet if "If" b
|
||||
if ${1:condition}:
|
||||
${2:pass}
|
||||
${2:${VISUAL:pass}}
|
||||
endsnippet
|
||||
|
||||
snippet ife "If / Else" b
|
||||
if ${1:condition}:
|
||||
${2:pass}
|
||||
${2:${VISUAL:pass}}
|
||||
else:
|
||||
${3:pass}
|
||||
endsnippet
|
||||
|
||||
snippet ifee "If / Elif / Else" b
|
||||
if ${1:condition}:
|
||||
${2:pass}
|
||||
${2:${VISUAL:pass}}
|
||||
elif ${3:condition}:
|
||||
${4:pass}
|
||||
else:
|
||||
@ -545,33 +595,33 @@ endsnippet
|
||||
##########################
|
||||
snippet try "Try / Except" b
|
||||
try:
|
||||
${1:pass}
|
||||
except ${2:Exception}, ${3:e}:
|
||||
${1:${VISUAL:pass}}
|
||||
except ${2:Exception} as ${3:e}:
|
||||
${4:raise $3}
|
||||
endsnippet
|
||||
|
||||
snippet try "Try / Except / Else" b
|
||||
snippet trye "Try / Except / Else" b
|
||||
try:
|
||||
${1:pass}
|
||||
except ${2:Exception}, ${3:e}:
|
||||
${1:${VISUAL:pass}}
|
||||
except ${2:Exception} as ${3:e}:
|
||||
${4:raise $3}
|
||||
else:
|
||||
${5:pass}
|
||||
endsnippet
|
||||
|
||||
snippet try "Try / Except / Finally" b
|
||||
snippet tryf "Try / Except / Finally" b
|
||||
try:
|
||||
${1:pass}
|
||||
except ${2:Exception}, ${3:e}:
|
||||
${1:${VISUAL:pass}}
|
||||
except ${2:Exception} as ${3:e}:
|
||||
${4:raise $3}
|
||||
finally:
|
||||
${5:pass}
|
||||
endsnippet
|
||||
|
||||
snippet try "Try / Except / Else / Finally" b
|
||||
snippet tryef "Try / Except / Else / Finally" b
|
||||
try:
|
||||
${1:pass}
|
||||
except${2: ${3:Exception}, ${4:e}}:
|
||||
${1:${VISUAL:pass}}
|
||||
except${2: ${3:Exception} as ${4:e}}:
|
||||
${5:raise}
|
||||
else:
|
||||
${6:pass}
|
||||
@ -580,48 +630,36 @@ finally:
|
||||
endsnippet
|
||||
|
||||
|
||||
#####################
|
||||
######################
|
||||
# Assertions & Tests #
|
||||
#####################
|
||||
|
||||
snippet pdb "Set PDB breakpoint" b
|
||||
import pdb; pdb.set_trace()
|
||||
endsnippet
|
||||
|
||||
snippet ipdb "Set IPDB breakpoint" b
|
||||
import ipdb; ipdb.set_trace()
|
||||
endsnippet
|
||||
|
||||
snippet pudb "Set PUDB breakpoint" b
|
||||
import pudb; pudb.set_trace()
|
||||
endsnippet
|
||||
######################
|
||||
|
||||
snippet ae "Assert equal" b
|
||||
self.assertEqual(${1:first},${2:second})
|
||||
self.assertEqual(${1:${VISUAL:first}},${2:second})
|
||||
endsnippet
|
||||
|
||||
snippet at "Assert True" b
|
||||
self.assertTrue(${0:exp})
|
||||
self.assertTrue(${1:${VISUAL:expression}})
|
||||
endsnippet
|
||||
|
||||
snippet af "Assert False" b
|
||||
self.assertFalse(${1:expression})
|
||||
self.assertFalse(${1:${VISUAL:expression}})
|
||||
endsnippet
|
||||
|
||||
snippet aae "Assert almost equal" b
|
||||
self.assertAlmostEqual(${1:first},${2:second})
|
||||
self.assertAlmostEqual(${1:${VISUAL:first}},${2:second})
|
||||
endsnippet
|
||||
|
||||
snippet ar "Assert raises" b
|
||||
self.assertRaises(${1:exception}, ${2:func}${3/.+/, /}${3:arguments})
|
||||
self.assertRaises(${1:exception}, ${2:${VISUAL:func}}${3/.+/, /}${3:arguments})
|
||||
endsnippet
|
||||
|
||||
snippet an "Assert is None" b
|
||||
self.assertIsNone(${0:expression})
|
||||
self.assertIsNone(${1:${VISUAL:expression}})
|
||||
endsnippet
|
||||
|
||||
snippet ann "Assert is not None" b
|
||||
self.assertIsNotNone(${0:expression})
|
||||
self.assertIsNotNone(${1:${VISUAL:expression}})
|
||||
endsnippet
|
||||
|
||||
snippet testcase "pyunit testcase" b
|
||||
@ -636,25 +674,39 @@ class Test${1:Class}(${2:unittest.TestCase}):
|
||||
${5:pass}
|
||||
|
||||
def test_${6:name}(self):
|
||||
${7:pass}
|
||||
${7:${VISUAL:pass}}
|
||||
endsnippet
|
||||
|
||||
snippet " "triple quoted string (double quotes)" b
|
||||
"""
|
||||
${1:doc}
|
||||
${1:${VISUAL:doc}}
|
||||
`!p triple_quotes_handle_trailing(snip, '"')`
|
||||
endsnippet
|
||||
|
||||
snippet ' "triple quoted string (single quotes)" b
|
||||
'''
|
||||
${1:doc}
|
||||
${1:${VISUAL:doc}}
|
||||
`!p triple_quotes_handle_trailing(snip, "'")`
|
||||
endsnippet
|
||||
|
||||
snippet doc "doc block (triple quotes)"
|
||||
`!p snip.rv = triple_quotes(snip)`
|
||||
${1:doc}
|
||||
${1:${VISUAL:doc}}
|
||||
`!p snip.rv = triple_quotes(snip)`
|
||||
endsnippet
|
||||
|
||||
snippet pmdoc "pocoo style module doc string" b
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
`!p snip.rv = get_dir_and_file_name(snip)`
|
||||
`!p snip.rv = '~' * len(get_dir_and_file_name(snip))`
|
||||
|
||||
${1:DESCRIPTION}
|
||||
|
||||
:copyright: (c) `date +%Y` by ${2:YOUR_NAME}.
|
||||
:license: ${3:LICENSE_NAME}, see LICENSE for more details.
|
||||
"""
|
||||
$0
|
||||
endsnippet
|
||||
|
||||
# vim:ft=snippets:
|
||||
|
Reference in New Issue
Block a user