abstract
In common business development scenarios , A lot of repetitive code is often developed , The code here is time-consuming but necessary , Just like we write analysis reports , Every time you have to spend energy on a fixed format . We can learn more from our daily development experience and summarize some commonly used template code to help us achieve it
Code development efficiency of five lines per second .
Business development scenario
I use Flask Framework to develop the backend api service , Here are two examples of development api Approximate code to be implemented , Need in urls.py Register the routing connection and processing request classes in the file , stay views.py
The file implements the specific execution logic of the processing request class .
# urls.py:: bp = Blueprint("api", __name__) bp.add_url_rule(
"/courses/<string:course_id>",
view_func=v.CourseDetailView.as_view("course_detail"), methods=["GET"], )
bp.add_url_rule( "/courses/<string:course_id>/instruction",
view_func=v.CourseInstructionView.as_view("course_instruction"),
methods=["GET"], ) # views.py:: class CourseDetailView(MethodView):
@swag_from_yml_file("users/course_detail_get.yml") @permission_required() def
get(self, course_id): ... return Response() class
CourseInstrauView(MethodView):
@swag_from_yml_file("users/course_instruction_get.yml") @permission_required()
def get(self, course_id): ... return Response() Copy code
You can see that the code for registering routes can be abstracted as a template
bp.add_url_rule( "$ Routing link $", view_func=$ Processing method class $.as_view("$ alias $"),
methods=["$ Support request type $", ...] ) Copy code
The code implemented by the processing method can be abstracted into two templates ( Because a processing method class can process multiple requests , as :GET,POST)
class $ Processing method class name $View(MethodView): Copy code @swag_from_yml_file("$api Document path $")
@permission_required() def $ Request method $(self, $ parameter $): $ Business logic $ return Response() Copy code
IDE development tool pycharm of Live Template
* Use shortcut keys command+,(mac) or Ctrl+Alt+s(Windows)
2. Enter the setup page . stay Editor Column search Live Templates
3. newly build Template Group, Name is Python Flask
4. In the new Group New template under regisbp This template is used to register routing codes
""" Abbreviation: regisbp Description: register blueprint Template text: """
bp.add_url_rule( "$url$",
view_func=$ViewName$View.as_view("$viewIdentify$"),methods=[$Methods$] ) Copy code
Need attention , Remember to point here
* In the new Group New template under viewClass
This template is used to declare the processing method class
""" Abbreviation: viewClass Description: create ViewClass Template text: """
class $ViewName$View(MethodView): Copy code
* In the new Group New template under viewMethod """ Abbreviation: viewMethod Description: view
method Template text: """ @swag_from_yml_file("$doc_path$")
@permission_required() def $method$(self, $args$): $code$ return
encoder.json_response($rv$) Copy code
complete , When needed for later development , Just enter regisbp,viewClass,viewMethod Then press enter .
Technology