代码之家  ›  专栏  ›  技术社区  ›  F.Lira

删除帮助功能中的冗余

  •  -1
  • F.Lira  · 技术社区  · 6 年前

    我可以编写参数,但如果命令没有提供正确的参数,我就无法避免编写函数“help”来打印它。

    任何建议都将不胜感激。

    def get_arguments():
        parser = argparse.ArgumentParser(prog = 'contig2genbank.py', usage= '\n\t%(prog)s [contig ID/ or list of contigs] [output prefix]' , description = "Split a .gbk (.gbff) file and retrieve the information of the contig of interest.",formatter_class=RawTextHelpFormatter)
        parser.add_argument('-s', required = False, type = str, help='Name of input file (mandatory with -g)', metavar='') # used to extract single contigs
        parser.add_argument('-l', required = False, type = str, help='Name of input file  when in list (mandatory)', metavar='') # when giving a list of genomes and contigs to be extracted; see above the list example
        parser.add_argument('-g', required = False, type = str, help='Name of genome file (mandatory with -i)', metavar='') # when using a single genome, -g is the name of genome file in .gbk (.gbff) format
        return parser.parse_args()
    
    
    def help():
        parser = argparse.ArgumentParser(prog = 'contig2genbank.py', usage= '\n\t%(prog)s [contig ID/ or list of contigs] [output prefix]' , description = "Split a .gbk (.gbff) file and retrieve the information of the contig of interest.",formatter_class=RawTextHelpFormatter)
        parser.add_argument('-s', required = False, type = str, help='Name of input file (mandatory with -g)', metavar='') # used to extract single contigs
        parser.add_argument('-l', required = False, type = str, help='Name of input file  when in list (mandatory)', metavar='') # when giving a list of genomes and contigs to be extracted; see above the list example
        parser.add_argument('-g', required = False, type = str, help='Name of genome file (mandatory with -i)', metavar='') # when using a single genome, -g is the name of genome file in .gbk (.gbff) format 
        parser.print_help()
    
    
    def main():
        args = get_arguments()  # check arguments
    
        if args.s and args.g:
            with open(args.g, "rU") as genome: #open(args.c + '.gbk','w') as outgbk:
                for record in SeqIO.parse(genome, "genbank"):
                    if args.s == record.id:
                        #with open(args.c + '.gbk','w') as outgbk:
                        SeqIO.write([record], open(record.id + ".gbk", "w"), "genbank")
            print record.id + ".gbk created."
    
        else:
            help()
    

    我期望的是避免get_参数和帮助函数中的行重复。

    2 回复  |  直到 5 年前
        1
  •  2
  •   Dani Mesejo    6 年前

    编写函数 get_parser 这样地:

    def get_parser():
        parser = argparse.ArgumentParser(prog = 'contig2genbank.py', usage= '\n\t%(prog)s [contig ID/ or list of contigs] [output prefix]' , description = "Split a .gbk (.gbff) file and retrieve the information of the contig of interest.",formatter_class=RawTextHelpFormatter)
        parser.add_argument('-s', required = False, type = str, help='Name of input file (mandatory with -g)', metavar='') # used to extract single contigs
        parser.add_argument('-l', required = False, type = str, help='Name of input file  when in list (mandatory)', metavar='') # when giving a list of genomes and contigs to be extracted; see above the list example
        parser.add_argument('-g', required = False, type = str, help='Name of genome file (mandatory with -i)', metavar='') # when using a single genome, -g is the name of genome file in .gbk (.gbff) format
        return parser
    

    main 功能如下:

    def main():
        parser = get_parser()
        args = parser.parse_args()  # check arguments
    
        if args.s and args.g:
            with open(args.g, "rU") as genome: #open(args.c + '.gbk','w') as outgbk:
                for record in SeqIO.parse(genome, "genbank"):
                    if args.s == record.id:
                        #with open(args.c + '.gbk','w') as outgbk:
                        SeqIO.write([record], open(record.id + ".gbk", "w"), "genbank")
            print record.id + ".gbk created."
    
        else:
            parser.print_help()
    
        2
  •  1
  •   Kings85    6 年前

    我不是专家,但如果您只需在两个原始函数中添加第三个函数,名为: get_parser

    def get_parser():
    parser = argparse.ArgumentParser(prog = 'contig2genbank.py', usage= '\n\t%(prog)s [contig ID/ or list of contigs] [output prefix]' , description = "Split a .gbk (.gbff) file and retrieve the information of the contig of interest.",formatter_class=RawTextHelpFormatter)
    parser.add_argument('-s', required = False, type = str, help='Name of input file (mandatory with -g)', metavar='') # used to extract single contigs
    parser.add_argument('-l', required = False, type = str, help='Name of input file  when in list (mandatory)', metavar='') # when giving a list of genomes and contigs to be extracted; see above the list example
    parser.add_argument('-g', required = False, type = str, help='Name of genome file (mandatory with -i)', metavar='') # when using a single genome, -g is the name of genome file in .gbk (.gbff) format
    return parser
    

    然后,原来的两个函数只有两行: 获取分析器 得到它,然后解析/打印