“This is the 11th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

Abstract

In a common business development scenario, there is often a lot of repetitive code to develop, which is time-consuming but necessary, just like when we write an analysis report, and we have to work on a fixed format each time. We can summarize some common template code from our daily development experience to help us achieve five lines of code development efficiency per second.

Business development scenario

I use the Flask framework to develop back-end API services. The following is the rough code for developing two apis. You need to register the routing connection and handling request classes in urls.py and implement the specific execution logic for handling request classes in views.py.

# 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 the code

You can see that code like registering routes can be abstracted into a template

bp.add_url_rule(
    $routing link $, view_func=$.as_view(Another name "$$"),
    methods=[$Support request type $. ] )Copy the code

The code for the handler implementation can be abstracted into two templates (because a handler class can handle multiple requests, such as GET and POST)

class$Handler class name $View(MethodView) :
    
Copy the code
@swag_from_yml_file($API file path $)
@permission_required()
def$request method $(Self, the $argument $) :$Business logic $return Response()
Copy the code

IDE development tool PyCharm’s Live Template

  1. Use shortcut keyscommand+.(MAC) orCtrl+Alt+s(Windows)

2. Go to the Settings page. inEditorBar searchLive Templates 3. NewTemplate GroupName,Python Flask 4. Create a template under the new GroupregisbpThis 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 the code

Notice, click here

  1. Create a new template under a new GroupviewClass

This template is used to declare the handler class

"""
Abbreviation: viewClass
Description: create ViewClass
Template text:
"""
class $ViewName$View(MethodView) :


Copy the code
  1. Create a new template under a new GroupviewMethod
"""
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 the code

Done, and later development needs, just enterregisbp,viewClass,viewMethodafterenterCan.