-3

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

""")
4

2 回答 2

10
if option.startswith('template'):
    hf = staticFile("static/{}/header".format(option))
    ...
于 2013-10-19T14:08:16.027 回答
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")
...
于 2013-10-19T14:08:34.033 回答