pycharm custom code snippet
content
(1) General stage
0. New .py file template: 2
0. Add custom code snippets in pycharm: one picture knows all: 3
1. Define class: classin Description: class init function4
2. Define function: de Description: def func14
3. For loop: forin Description: for i in range5
4. Default encoding format: u8 Description: # coding: utf-85
5. Producer-consumer model: shengxiao Description: Producer and consumer from Thread5
6. Hash encryption: sha1 Description: sha1 code encrypt6
7. Handling exceptions: try Description: try except finally6
8. Handling exceptions: tee Description: try... except... else7
9. Handling exceptions: tef Description: try... except... finally7
10. Add double quotes: str Description: tostring7
11. Return true: rt Description: return Ture7
12. Return false: rf Description: return False8
(2) Django stage
13. Rendering template: reren Description: return render()
14. Prepare data: context description: context = {}
15. Prepare data: rehtt Description: return HttpResponse()
16. Prepare data: rejson Description: return JsonResponse()
(1) General stage
0. Create a new .py file template:
ctrl + alt + S shortcut key: Quickly bring up the settings window.
Setting-"Editor-"File and Code Templates-"Files-"Python Script enters the code snippet editing interface.
# -*- coding: utf-8 -*-
# Created by $USER on $DATE
# Copyright (c) $YEAR $USER. All rights reserved.
if __name__ == '__main__':
pass
This ends here: the code
0. Add custom code snippets in pycharm: you know everything in one picture:
ctrl + alt + S shortcut key: Quickly bring up the settings window.
Or: Enter the code snippet editing interface from File->Setting->Editor->Live Templates->Python.
summary:
$ITERABLE$ indicates the initial position of the cursor
$end$ indicates the position where the cursor stopped
$END$ indicates the last position of the cursor (tab switching)
$SELECTION$ represents the selected code
$class$ represents the current class name
$method$ represents the current method name
$NAME$ Name position marker (custom), initial cursor stay. Generally, multiple $NAME$ are used to name at the same time.
$var$ variable position marker (custom), initial cursor stay. Generally, multiple $var$ are used to name at the same time.
$var1$ variable 1, when the tab is switched, the cursor will stay there
$var2$ variable 2, when the tab is switched, the cursor will stay there
$var3$ variable 3, when the tab is switched, the cursor will stay there
Summary of commonly used custom code snippets in pycharm:
It is recommended to use Enter for position switching.
1. Define class: classin Description: class init function
class $NAME$($var1$):
"""这是$NAME$"""
def __init__(self$var2$):
$END$
pass
This is the end of it: Code Tip: super initializes the initial value in the parent class, there is no need to customize the code snippet.
Set variable properties:
Note: You must use double quotes, single quotes or no quotes.
2. Define function: de Description: def func1
def $NAME$($arg$):
$END$
pass
It ends here
hint:
It is very convenient to use d to define methods in a class. Self is automatically generated without defining it yourself.
3. For loop: forin Description: for i in range
for $INDEX$ in range($num$):
$END$
pass
This ends here: the code
Set variable properties:
4. Default encoding format: u8 Description: # coding:utf-8
# coding:utf-8
$END$
This ends here: the code
5. Producer-consumer model: shengxiao Description: Producer and consumer from Thread
from threading import Thread
# producer
class Producer(Thread):
def __init__(self):
super().__init__()
pass
def run(self):
pass
# consumers
class Consumer(Thread):
def __init__(self):
super().__init__()
pass
def run(self):
pass
# main function
def main():
$ var $
pass
if __name__ == '__main__':
main()
This ends here: the code
6. Hash encryption: sha1 Description: sha1 encode hexdigest
from hashlib import sha1
s = sha1()
s.update($password$.encode())
$password$_sha1 = s.hexdigest()
It ends here
Tip: The default value of $password$ is set to "password" alone.
7. Handling exceptions: try Description: try except finally
try:
$pass$
except Exception as e:
print (s)
finally:
$clo$
pass
$END$
It ends here
Tip: The default value of $pass$ is set to "pass" alone; the default value of $clo is set to "conn.close()"
8. Handling exceptions: tee Description: try... except... else
try:
$SELECTION$$END$
except Exception as e:
print (s)
else:
pass
It ends here
Hint: $SELECTION$ represents the selected code.
When using it, select the code to be put into try, then press ctrl+alt+T to select tee.
9. Handling exceptions: tef Description: try... except... finally
try:
$SELECTION$$END$
except Exception as e:
print (s)
finally:
pass
It ends here
Hint: $SELECTION$ represents the selected code.
When using, select the code to be put in try, then press ctrl+alt+T, select tef.
10. Add double quotes: str Description: tostring
"$SELECTION$"
It ends here
Hint: $SELECTION$ represents the selected code.
When using, select the code to be placed in "" double quotation marks, then press ctrl+alt+T to select str.
11. Return true: rt Description: return True
return True$END$
It ends here
12. Return false: rf Description: return False
return False$END$
It ends here
(2) Django stage
13. Rendering template: reren Description: return render()
context={$keyvalue$}
return render(request,'$template$.html',context)
It ends here
Hint: order and initial value $template$="booktest/index", $keyvalue$
14. Prepare data: context description: context = {}
context={
'$uname$':$uname$,
'$ upwd $': $ upwd $,
'$var3$':$var3$,
'$var4$':$var4$,
'$ var5 $': $ var5 $,
'$var6$': $var6$,
}
It ends here
Hint: initial value $uname$="uname", $upwd$="upwd"
15. Prepare data: rehtt Description: return HttpResponse()
return HttpResponse("$ok$")
It ends here
Hint: initial value $ok$="ok"
16. Prepare data: rejson Description: return JsonResponse()
return JsonResponse({'$count$': $count$})
It ends here
Hint: Initial value $count$="count"