如何缩短很多?

时间:2013-10-19 14:04:02

标签: python

是否可以使用字典缩短项目代码,因为一旦用户使用命令行参数“--template1”或1-4范围内的其他数字执行程序,它会将当前页眉,页脚分配给根据给定的命令行参数号在各自路径中预定义的文件。我想知道是否可以在字典中添加这些文件或以某种方式缩短这100行?

if len(sys.argv) == 2:
    all_templates = ('--template1', '--template2', '--template3', '--template4')
    if not sys.argv[1].startswith(all_templates):
        sys.exit("""Usage:  python build.py --template1

""")
    if sys.argv[1].startswith('--'):
        option = sys.argv[1][2:]
        if option == 'template1':
            hf = staticFile("static/template-1/header")
            header = hf()
            header = string.replace(header, "site_description", Settings.site_description)
            fh = staticFile("static/template-1/header_forum")
            forum_header = fh()
            forum_header = string.replace(forum_header, "site_description", Settings.site_description)
            sh = staticFile("static/template-1/header_search")
            search_header = sh()
            search_header = string.replace(search_header, "site_description", Settings.site_description)
            ah = staticFile("static/template-1/header_archive")
            archive_header = ah()
            archive_header = string.replace(archive_header, "site_description", Settings.site_description)
            fp = staticFile("static/template-1/footer_post")
            footer_p = fp()
            footer_p = string.replace(footer_p, "footer_text", Settings.footer_text)
            fi = staticFile("static/template-1/footer")
            footer_i = fi()
            footer_i = string.replace(footer_i, "footer_text", Settings.footer_text)
        if option == 'template2':
            hf = staticFile("static/template-2/header")
            header = hf()
            header = string.replace(header, "site_description", Settings.site_description)
            fh = staticFile("static/template-2/header_forum")
            forum_header = fh()
            forum_header = string.replace(forum_header, "site_description", Settings.site_description)
            sh = staticFile("static/template-2/header_search")
            search_header = sh()
            search_header = string.replace(search_header, "site_description", Settings.site_description)
            ah = staticFile("static/template-2/header_archive")
            archive_header = ah()
            archive_header = string.replace(archive_header, "site_description", Settings.site_description)
            fp = staticFile("static/template-2/footer_post")
            footer_p = fp()
            footer_p = string.replace(footer_p, "footer_text", Settings.footer_text)
            fi = staticFile("static/template-2/footer")
            footer_i = fi()
            footer_i = string.replace(footer_i, "footer_text", Settings.footer_text)
        if option == 'template3':
            hf = staticFile("static/template-3/header")
            header = hf()
            header = string.replace(header, "site_description", Settings.site_description)
            fh = staticFile("static/template-3/header_forum")
            forum_header = fh()
            forum_header = string.replace(forum_header, "site_description", Settings.site_description)
            sh = staticFile("static/template-3/header_search")
            search_header = sh()
            search_header = string.replace(search_header, "site_description", Settings.site_description)
            ah = staticFile("static/template-3/header_archive")
            archive_header = ah()
            archive_header = string.replace(archive_header, "site_description", Settings.site_description)
            fp = staticFile("static/template-3/footer_post")
            footer_p = fp()
            footer_p = string.replace(footer_p, "footer_text", Settings.footer_text)
            fi = staticFile("static/template-3/footer")
            footer_i = fi()
            footer_i = string.replace(footer_i, "footer_text", Settings.footer_text)
        if option == 'template4':
            hf = staticFile("static/template-4/header")
            header = hf()
            header = string.replace(header, "site_description", Settings.site_description)
            fh = staticFile("static/template-4/header_forum")
            forum_header = fh()
            forum_header = string.replace(forum_header, "site_description", Settings.site_description)
            sh = staticFile("static/template-4/header_search")
            search_header = sh()
            search_header = string.replace(search_header, "site_description", Settings.site_description)
            ah = staticFile("static/template-4/header_archive")
            archive_header = ah()
            archive_header = string.replace(archive_header, "site_description", Settings.site_description)
            fp = staticFile("static/template-4/footer_post")
            footer_p = fp()
            footer_p = string.replace(footer_p, "footer_text", Settings.footer_text)
            fi = staticFile("static/template-4/footer")
            footer_i = fi()
            footer_i = string.replace(footer_i, "footer_text", Settings.footer_text)
elif len(sys.argv) < 2:
    sys.exit("""Usage:  python build.py --template1

""")
elif len(sys.argv) > 2:
    sys.exit("""Usage:  python build.py --template1

""")
else:
    sys.exit("""Usage:  python build.py --template1

""")

2 个答案:

答案 0 :(得分:10)

if option.startswith('template'):
    hf = staticFile("static/{}/header".format(option))
    ...

答案 1 :(得分:0)

使用 var 选项代替template-n

hf = staticFile("static/" + option + "/header")
header = hf()
header = string.replace(header, "site_description", Settings.site_description)
fh = staticFile("static/" + option + "/header_forum")
...