コード例 #1
0
ファイル: example.py プロジェクト: fons/dyn_options
def main(argv) :
    option = dyn_options.create_option(argv, option_defaults())
    print "using defaults :", option

    option = dyn_options.create_option(argv)
    print "no defaults :", option


    if option.opt4 :
        print "opt4 is set :", option.opt4
    else :
        print "opt4 is not set"
コード例 #2
0
ファイル: dyn_options_test.py プロジェクト: fons/dyn_options
def test1() :

    """
    Test of general use.
    - use default if option is not supplied on the command line.
    - option by itself becomes a boolean. 
    """

    L=['./dyn_options.py', '--opt2', 'opt2_value', '--opt3']
    option = dyn_options.create_option(L, option_defaults())
    try :
        assert option.opt1 == "opt1_default"
        assert option.opt2 == "opt2_value"
        assert option.opt3 == True
    except AssertionError :
        traceback.print_exc()
        
        print "Failed test1 : parsing ", str(L)
        print "generated : ", option
        print "internals : ", option.__repr__()
        return -1

    print "pass test1"

    return 0
コード例 #3
0
ファイル: dyn_options_test.py プロジェクト: fons/dyn_options
def test5() :

    """
    Test to see if option is set...
    """

    L=['./dyn_options.py', '--opt1', 'opt1_value', '-opt2', 'opt2_value', '-opt3']
    option = dyn_options.create_option(L, option_defaults())
    try :
        assert option.opt1 == "opt1_value"
        assert option.opt2 == "opt2_value"
        assert option.opt3 == True
        assert option.help == False

        is_set = False
        if option.opt1 :
            is_set = True
        assert is_set == True

        is_set = True
        if not option.opt1 :
            is_set = False
        assert is_set == True

        is_set = False
        if option.opt1000 :
            is_set = True
        assert is_set == False

        is_set = True
        if not option.opt1000 :
            is_set = False
        assert is_set == False


    except AssertionError :
        traceback.print_exc()
        
        print "Failed test5 : parsing ", str(L)
        print "generated : ", option
        print "internals : ", option.__repr__()
        return -1

    print "pass test5"
    return 0
コード例 #4
0
ファイル: dyn_options_test.py プロジェクト: fons/dyn_options
def test2() :

    """
    Same as test1, but now start with a single -
    """

    L=['./dyn_options.py', '-opt2', 'opt2_value', '-opt3']
    option = dyn_options.create_option(L, option_defaults())
    try :
        assert option.opt1 == "opt1_default"
        assert option.opt2 == "opt2_value"
        assert option.opt3 == True
    except AssertionError :
        traceback.print_exc()
        
        print "Failed test2 : parsing ", str(L)
        print "generated : ", option
        print "internals : ", option.__repr__()
        return -1

    print "pass test2"

    return 0
コード例 #5
0
ファイル: dyn_options_test.py プロジェクト: fons/dyn_options
def test6() :

    """
    Test to see if option prints...
    """

    L=['./dyn_options.py', '--opt1', 'opt1_value', '-opt2', 'opt2_value', '-opt3']
    option = dyn_options.create_option(L, option_defaults())
    try :
        print "The command line is : ", L, " with defaults :", option_defaults()
        print "You should see the option print it's 'public' members:" 
        print option
        print "You should see the option print all its members:" 
        option.__repr__()

    except :
        traceback.print_exc()
        
        print "Failed test : parsing ", str(L)
        print "this is the print test; so I can't print out the option object"
        return -1
    print "pass test6"
    return 0
コード例 #6
0
ファイル: dyn_options_test.py プロジェクト: fons/dyn_options
def test4() :

    """
    option is immutable
    """

    L=['./dyn_options.py', '--opt1', 'opt1_value', '-opt2', 'opt2_value', '-opt3']
    option = dyn_options.create_option(L, option_defaults())
    try :
        assert option.opt1 == "opt1_value"
        assert option.opt2 == "opt2_value"
        assert option.opt3 == True
        assert option.help == False

        #Try to override...
        option.help = True
        assert option.help == False

        option.opt2 == "new_opt2_value"
        assert option.opt2 == "opt2_value"


        #Try to add new attribute
        option.opt55 = "opt55_value"
        assert option.opt55 == False
        
    except AssertionError :
        traceback.print_exc()
        
        print "Failed test4 : parsing ", str(L)
        print "generated : ", option
        print "internals : ", option.__repr__()
        return -1

    print "pass test4"
    return 0
コード例 #7
0
ファイル: dyn_options_test.py プロジェクト: fons/dyn_options
def test3() :

    """
    Override default on the command line
    """

    L=['./dyn_options.py', '--opt1', 'opt1_value', '-opt2', 'opt2_value', '-opt3', '--help']
    option = dyn_options.create_option(L, option_defaults())
    try :
        assert option.opt1 == "opt1_value"
        assert option.opt2 == "opt2_value"
        assert option.opt3 == True
        assert option.help == True
    except AssertionError :
        traceback.print_exc()
        
        print "Failed test3 : parsing ", str(L)
        print "generated : ", option
        print "internals : ", option.__repr__()
        return -1

    print "pass test3"

    return 0
コード例 #8
0
ファイル: prime_combine.py プロジェクト: fons/blog-code
def main(argv) :
    option = dyn_options.create_option(argv, defaults())
    print option 
    if option.help :
        help()
        return 0

    args = (option.limit, option.nthreads, option.repeat)

    if option.nthread_range :
        timing_test(option)
        return

    routines = map(lambda s : s.strip(), option.routines.split(","))

    if option.all or "orig" in routines :
        if option.time_it :
            print time_routine("main_orig",   args)
        else :
            cnt = main_orig(option.limit, option.nthreads)
            print "main_orig : ", cnt , " primes found"

    if option.all or "nolocks" in routines :
        if option.time_it :
            print time_routine("main_nolocks",   args)
        else :
            cnt = main_nolocks(option.limit, option.nthreads)
            print "main_nolocks : ", cnt , " primes found"

    if option.all or "nolocks_alt" in routines :
        if option.time_it :
            print time_routine("main_nolocks_alt",   args)
        else :
            cnt = main_nolocks_alt(option.limit, option.nthreads)
            print "main_nolocks_alt : ", cnt , " primes found"

    if option.all or "nolocks_alt2" in routines :
        if option.time_it :
            print time_routine("main_nolocks_alt2",   args)
        else :
            cnt = main_nolocks_alt2(option.limit, option.nthreads)
            print "main_nolocks_alt2 : ", cnt , " primes found"

    if option.all or "smp" in routines :
        if option.time_it :
            print time_routine("main_smp",   args)
        else :
            cnt = main_smp(option.limit, option.nthreads)
            print "main_smp : ", cnt , " primes found"

    if option.all or "smp_alt" in routines :
        if option.time_it :
            print time_routine("main_smp_alt",   args)
        else :
            cnt = main_smp_alt(option.limit, option.nthreads)
            print "main_smp_alt : ", cnt , " primes found"

    if option.all or "smp_shared" in routines :
        if option.time_it :
            print time_routine("main_smp_shared",   args)
        else :
            cnt = main_smp_shared(option.limit, option.nthreads)
            print "main_smp_shared : ", cnt , " primes found"

        
    if option.all or "smp_shared_2" in routines :
        if option.time_it :
            print time_routine("main_smp_shared_2",   args)
        else :
            cnt = main_smp_shared_2(option.limit, option.nthreads)
            print "main_smp_shared_2 : ", cnt , " primes found"

    sys.exit(0)