#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    13
    Rep Power
    0

    How to shorten many if ?


    Is it possible to shorten my project's code with a dictionary, because once the user executes the program with command line argument of "--template1" or other number in a range 1-4, it assigns the current header, footer to the pre-defined files in their respective paths according to the given command line argument number. I'm wondering is it possible to add those files in a dictionary or to shorten those 100 lines somehow ?

    Code:
    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. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    This code looks equivalent to me. Carefully prepared, but untested.
    Code:
    all_templates = ('--template1', '--template2', '--template3', '--template4')
    
    # let's escape soon
    if not ((len(sys.argv) == 2) and sys.argv[1].startswith(all_templates)):
        sys.exit('Use:  python build.py --template[1-4]\n\n')
    
    option = sys.argv[1][-1]  # option must be a string digit 1 through 4.
    
    hf = staticFile("static/template-{}/header".format(option))
    header = hf()
    header = string.replace(header, "site_description", Settings.site_description)
    fh = staticFile("static/template-{}/header_forum".format(option))
    forum_header = fh()
    forum_header = string.replace(forum_header, "site_description", Settings.site_description)
    sh = staticFile("static/template-{}/header_search".format(option))
    search_header = sh()
    search_header = string.replace(search_header, "site_description", Settings.site_description)
    ah = staticFile("static/template-{}/header_archive".format(option))
    archive_header = ah()
    archive_header = string.replace(archive_header, "site_description", Settings.site_description)
    fp = staticFile("static/template-{}/footer_post".format(option))
    footer_p = fp()
    footer_p = string.replace(footer_p, "footer_text", Settings.footer_text)
    fi = staticFile("static/template-{}/footer".format(option))
    footer_i = fi()
    footer_i = string.replace(footer_i, "footer_text", Settings.footer_text)
    Restructuring the program removed most of the horrid indentation levels. The first substitution I noticed was that this test
    if sys.argv[1].startswith('--'):
    is useless because you already had that information. emacs prepares most of the regular expression for me:
    Code:
    hf = staticfile("static/template-[1234]/header")
     header = hf()
     header = string\.replace(header, "site_description", settings\.site_description)
     fh = staticfile("static/template-[1234]/header_forum")
     forum_header = fh()
     forum_header = string\.replace(forum_header, "site_description", settings\.site_description)
     sh = staticfile("static/template-[1-4]/header_search")
     search_header = sh()
     search_header = string\.replace(search_header, "site_description", settings\.site_description)
     ah = staticfile("static/template-[1-4]/header_archive")
     archive_header = ah()
     archive_header = string\.replace(archive_header, "site_description", settings\.site_description)
     fp = staticfile("static/template-[1-4]/footer_post")
     footer_p = fp()
     footer_p = string\.replace(footer_p, "footer_text", settings\.footer_text)
     fi = staticfile("static/template-[1-4]/footer")
     footer_i = fi()
     footer_i = string\.replace(footer_i, "footer_text", settings\.footer_text)
    so I easily assured myself I was working with deplorable duplicated code. I used j (www.jsoftware.com) to confirm
    Code:
    if not ((len(sys.argv) == 2) and sys.argv[1].startswith(all_templates)):
    is the same as
    Code:
    if  ((len(sys.argv) != 2) or not sys.argv[1].startswith(all_templates))
    Here's the j sentence with confirming result on second line:
    Code:
       (P nand Q) -: ((not P) or (not Q))
    1
    Last edited by b49P23TIvg; October 20th, 2013 at 06:29 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    13
    Rep Power
    0
    Thank you again b49P23TIvg

    Originally Posted by b49P23TIvg
    This code looks equivalent to me. Carefully prepared, but untested.
    Code:
    all_templates = ('--template1', '--template2', '--template3', '--template4')
    
    # let's escape soon
    if not ((len(sys.argv) == 2) and sys.argv[1].startswith(all_templates)):
        sys.exit('Use:  python build.py --template[1-4]\n\n')
    
    option = sys.argv[1][-1]  # option must be a string digit 1 through 4.
    
    hf = staticFile("static/template-{}/header".format(option))
    header = hf()
    header = string.replace(header, "site_description", Settings.site_description)
    fh = staticFile("static/template-{}/header_forum".format(option))
    forum_header = fh()
    forum_header = string.replace(forum_header, "site_description", Settings.site_description)
    sh = staticFile("static/template-{}/header_search".format(option))
    search_header = sh()
    search_header = string.replace(search_header, "site_description", Settings.site_description)
    ah = staticFile("static/template-{}/header_archive".format(option))
    archive_header = ah()
    archive_header = string.replace(archive_header, "site_description", Settings.site_description)
    fp = staticFile("static/template-{}/footer_post".format(option))
    footer_p = fp()
    footer_p = string.replace(footer_p, "footer_text", Settings.footer_text)
    fi = staticFile("static/template-{}/footer".format(option))
    footer_i = fi()
    footer_i = string.replace(footer_i, "footer_text", Settings.footer_text)
    Restructuring the program removed most of the horrid indentation levels. The first substitution I noticed was that this test
    if sys.argv[1].startswith('--'):
    is useless because you already had that information. emacs prepares most of the regular expression for me:
    Code:
    hf = staticfile("static/template-[1234]/header")
     header = hf()
     header = string\.replace(header, "site_description", settings\.site_description)
     fh = staticfile("static/template-[1234]/header_forum")
     forum_header = fh()
     forum_header = string\.replace(forum_header, "site_description", settings\.site_description)
     sh = staticfile("static/template-[1-4]/header_search")
     search_header = sh()
     search_header = string\.replace(search_header, "site_description", settings\.site_description)
     ah = staticfile("static/template-[1-4]/header_archive")
     archive_header = ah()
     archive_header = string\.replace(archive_header, "site_description", settings\.site_description)
     fp = staticfile("static/template-[1-4]/footer_post")
     footer_p = fp()
     footer_p = string\.replace(footer_p, "footer_text", settings\.footer_text)
     fi = staticfile("static/template-[1-4]/footer")
     footer_i = fi()
     footer_i = string\.replace(footer_i, "footer_text", settings\.footer_text)
    so I easily assured myself I was working with deplorable duplicated code. I used j (www.jsoftware.com) to confirm
    Code:
    if not ((len(sys.argv) == 2) and sys.argv[1].startswith(all_templates)):
    is the same as
    Code:
    if  ((len(sys.argv) != 2) or not sys.argv[1].startswith(all_templates))
    Here's the j sentence with confirming result on second line:
    Code:
       (P nand Q) -: ((not P) or (not Q))
    1

IMN logo majestic logo threadwatch logo seochat tools logo