Skip to main content
 首页 » 编程设计

python之缩短这些行的正确方法是什么

2024年10月01日7luoye11

pylint 一直对我大喊大叫,我试图让 PEP8 兼容。我尝试了几种不同的方法,但不是那种足够禅宗的方法 对于 pylint。

def arg_handler(): 
    ''' Process command-line args ''' 
 
    parser = argparse.ArgumentParser(description='PoC for the TCP/32764 backdoor.\n'\ 
        'see https://github.com/elvanderb/TCP-32764 for more details') 
 
    parser.add_argument('--ip', type=str, nargs='?', help='routers IP', default='192.168.1.1') 
    parser.add_argument('--port', type=int, nargs='?', help='port to use', default=32764) 
    command_group = parser.add_mutually_exclusive_group() 
    command_group.add_argument('--is_vuln',help='tells you if the router is vulnerable or not (default)', action="store_true") 
    command_group.add_argument('--shell', help='gives you a root shell on the router', action="store_true") 
    command_group.add_argument('--execute', type=str, nargs='?', help='run a command and dump straight to stdout', default='') 
    command_group.add_argument('--print_conf', help='pretty print router\'s configuration', action="store_true") 
    command_group.add_argument('--get_credentials', help='gets credentials', action="store_true") 
    command_group.add_argument('--get_var', type=str, nargs='?', metavar='var_name', help='get router\'s configuration variable') 
    command_group.add_argument('--set_var', type=str, nargs='?', metavar='var_name=val', help='set router\'s configuration variable') 
    command_group.add_argument('--message', type=int, nargs='?', help='message to send', choices=range(1, 14)) 
    command_group.add_argument('--send_file', type=str, nargs='?', help='file to send') 
    command_group.add_argument('--send_file2', type=str, nargs='?', help='file to send, using echo -n -e') 
    parser.add_argument('--payload', type=str, nargs='?', help='message\'s payload', default='') 
    parser.add_argument('--timeout', type=int, nargs='?', help='connexion timeout in seconds', default=1) 
    parser.add_argument('--remote-filename', type=str, nargs='?', help='remote filename in /tmp when copying', default="upload") 
    return parser.parse_args() 

请您参考如下方法:

作为您已实现的示例:

command_group.add_argument( 
    '--is_vuln', 
    help='tells you if the router is vulnerable or not (default)', 
    action="store_true" 
) 

如果我没记错的话,每行 79 个字符以内的任何内容都符合 PEP8。

另外,当一个字符串超过79个字符时,你可以这样格式化:

long_string = ("thisisanextremelylongstring" 
               "thatissolongiamputtingiton" 
               "anotherline")