Repository navigation
Expand file tree
/
Copy pathpython.snip
More file actions
273 lines (229 loc) · 5.48 KB
/
Copy pathpython.snip
File metadata and controls
273 lines (229 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
snippet py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division, print_function, absolute_import, unicode_literals
${1}
snippet imp
import ${1:module}
snippet from
from ${1:package} import ${2:module}
# Module Docstring
snippet docs
"""
File: ${1:`Filename('$1.py', 'foo.py')`}
Author: `g:snips_author`
Email: `g:snips_email`
Github: `g:snips_github`
Description: ${2}
"""
snippet class
abbr class Class(...): ...
prev_word '^'
class ${1:name}(${2:object}):
"""${3:class documentation}"""
def __init__(self, ${4}):
"""${5:__init__ documentation}"""
${6:pass}
snippet init
abbr def __init__(...): ...
prev_word '^'
def __init__(self, ${1}):
"""${2:__init__ documentation}"""
${3:pass}
snippet init_inheritance
abbr def __init__(...): ...
prev_word '^'
def __init__(self, ${1}):
"""${2:__init__ documentation}"""
${3:SuperClass}.__init__(self)
${4:pass}
snippet def
abbr def function(...): ...
prev_word '^'
def ${1:name}(${2}):
"""${3:function documentation}"""
${4:pass}
snippet defm
abbr def method(self, ...): ...
prev_word '^'
def ${1:name}(self, ${2}):
"""${3:method documentation}"""
${4:pass}
snippet elif
abbr elif ...: ...
prev_word '^'
elif ${1:condition}:
${2:pass}
snippet else
abbr else: ...
prev_word '^'
else:
${1:pass}
snippet fileidiom
abbr f = None try: f = open(...) finally: ...
prev_word '^'
${1:f} = None
try:
$1 = open(${2})
${3}
finally:
if $1:
$1.close()
snippet for
abbr for ... in ...: ...
options indent
prev_word '^'
for ${1:value} in ${2:list}:
${3:pass}
snippet if
abbr if ...: ...
prev_word '^'
if ${1:condition}:
${2:pass}
snippet ife
abbr ife ...: ...
options indent
prev_word '^'
if ${1:condition}:
${2:pass}
else:
${3:pass}
snippet ifee
abbr ife ...: ...
prev_word '^'
if ${1:condition}:
${2:pass}
elif ${3:condition}:
${4:pass}
else:
${5:pass}
snippet ifmain
abbr if __name__ == '__main__': ...
prev_word '^'
if __name__ == '__main__':
${1:pass}
snippet try
options indent
try:
${1:# TODO: write code...}
except ${2:Exception} as ${3:e}:
${4:raise $3}
snippet tryee
try:
${1:# TODO: write code...}
except ${2:Exception} as ${3:e}:
${4:raise $3}
else:
${5:# TODO: write code...}
snippet tryef
options indent
try:
${1:# TODO: write code...}
except ${2:Exception} as ${3:e}:
${4:raise $3}
finally:
${5:# TODO: write code...}
snippet tryeef
options indent
try:
${1:# TODO: write code...}
except ${2:Exception} as ${3:e}:
${4:raise $3}
else:
${5:# TODO: write code...}
finally:
${6:# TODO: write code...}
snippet while
abbr while ...: ...
prev_word '^'
while ${1:condition}:
${2:pass}
snippet python
prev_word '^'
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
${1}
snippet with_cd
with cd('${1:/path/to}'):
${2}
snippet op
with open('${1:/path/to}', '${2:r}') as ${3:file}:
$3.${4:read}
snippet readstdin
for line in fileinput.input():
${1}
snippet readline
with open('${1:/path/to}') as ${2:file}::
for ${3:line} in $2:
${4:print(line)}
# Lambda
snippet ld
${1:var} = lambda ${2:vars} : ${3:action}
# python debugger (pdb)
snippet pdb
import pdb; pdb.set_trace()
# ipython debugger (ipdb)
snippet ipdb
import ipdb; ipdb.set_trace()
# ipython debugger (pdbbb)
snippet pdbbb
import pdbpp; pdbpp.set_trace()
snippet pprint
import pprint; pprint.pprint(${1})${2}
#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}])
except getopt.GetoptError, err:
# Print debug info
print(str(err))
${3:error_action}
for option, argument in opts:
if option in ("-h", "--help"):
${4}
elif option in ("-v", "--verbose"):
verbose = argument
# logging
# glog = get log
snippet logimport
from logging import getLogger, StreamHandler, Formatter , DEBUG
snippet logimport_rainbow
import sys
from logging import getLogger, Formatter, DEBUG
from rainbow_logging_handler import RainbowLoggingHandler
snippet getlogger
logger = getLogger(${1:__name__})
formatter = Formatter( '%(asctime)s %(pathname)s [%(module)s] - %(funcName)s:L%(lineno)d : %(message)s')
handler = StreamHandler()
handler.setFormatter(formatter)
handler.setLevel(DEBUG)
logger.setLevel(DEBUG)
logger.addHandler(handler)
logger.propagate = False
snippet getlogger_rainbow
logger = getLogger(${1:__name__})
formatter = Formatter( '%(asctime)s %(pathname)s [%(module)s] - %(funcName)s:L%(lineno)d : %(message)s')
handler = RainbowLoggingHandler(sys.stderr)
handler.setFormatter(formatter)
logger.setLevel(DEBUG)
logger.propagate = False
logger.addHandler(handler)
snippet argparse
import argparse
parser = argparse.ArgumentParser( description='${1:description}')
parser.add_argument( '${2: -a}', '${3: --longoption}', dest='${4:valiable}', type=str, help='${5:helpmessage}', required=${6:option})
parser.add_argument( '${6: -a}', '${7: --longoption}', dest='${8:valiable}', action='store_true', help='${9:helpmessage}', required=${10:False})
parser.set_defaults(quiet=${11:False})
snippet le
logger.error(${1:msg})
# conflict with lambda=ld, therefor we change into Logger.debuG
snippet lg
logger.debug(${1:msg})
snippet lw
logger.warning(${1:msg})
snippet lc
logger.critical(${1:msg})
snippet li
logger.info(${1:msg})