vscode configures the yapf formatting plugin

introduction

  • yapfautopep8It is more flexible and powerful than , you can consider starting

configuration steps

  1. install yapf:
    pip install yapf
    
  2. vscode configurationsetting.json
        "python.formatting.provider": "yapf",
        "python.formatting.yapfArgs": [
            "--style={based_on_style=pep8, arithmetic_precedence_indication=True, column_limit=79, coalesce_brackets=True, dedent_closing_brackets=True}"
        ],
        "editor.formatOnSave": true,
        "editor.defaultFormatter": "ms-python.python",
    
  3. custom formatting rules
    • Run on the command line terminal: yapf --style-help, you can see all the default configurations, and use the examples to determine whether you use them.
    • In the second step yapfArgs, it is to determine what you need by looking at each switch function. You can configure it yourself.
    • The new configuration can be added directly later.
      "--style={based_on_style=pep8, arithmetic_precedence_indication=True, column_limit=79, coalesce_brackets=True, dedent_closing_brackets=True}"
      
  4. Open one demo.py, let's say the following:
    a = 1 * 2 + 3 / 4
    b = 1/2 - 3*4
    c = (1 + 2) * (3 - 4)
    d = (1 - 2) / (3 + 4)
    e = 1 * 2 - 3
    f = 1 + 2 + 3 + 4
    
    call_func_that_takes_a_dict({
          
          
        'key1': 'value1',
        'key2': 'value2',})
    
    
    config = {
          
          
        'key1': 'value1',
        'key2': 'value2',}
    
    • After using the above styles, it will become the following format:
      a = 1*2 + 3/4
      b = 1/2 - 3*4
      c = (1+2) * (3-4)
      d = (1-2) / (3+4)
      e = 1*2 - 3
      f = 1 + 2 + 3 + 4
      
      call_func_that_takes_a_dict({
              
              
          'key1': 'value1',
          'key2': 'value2',
      })
      
      config = {
              
              
          'key1': 'value1',
          'key2': 'value2',
      }
      
      

Guess you like

Origin blog.csdn.net/shiwanghualuo/article/details/131282319