Printing defaulted values


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Printing defaulted values
# 1  
Old 11-27-2010
Printing defaulted values

I have written a phyton script that accepts command line arguments. I am setting defaults for some of them if the user does not specify them. However I want to print the user values and the defaulted values seperately. However, once I set the default values, then I cannot use

Code:
if (isinstance(dtau,types.NoneType)):

as I have now set dtau to a default value.

Any help appreciated.


Code:
if __name__ == "__main__":

    Version="V01"
    tpath = None;
    fbase = None
    fdata = None;
    fstmod = None;
    frestore = None;
    ftag = None;

    nlay = None;
    param = None;
    intp = None;
    std = None;
    tol = None;

    nxz = None;
    nxp = None;
    nzp = None;
    npop = None;
    varp = None;
    varpint = None;
    varpfrc = None;
    sigma = None;
    maxiter = None;
    dtau = None;
    mdacc = None;
    mindist = None;
    maxitertp = None;
    vrb = None;
    tpf = None;
    tee = None;

    long_opts= [
    "tpath=",
    "fbase=",
    "fdata=",
    "fstmod=",
    "frestore=",
    "ftag=",
    "tee=",
    "nxz=",
    "npop=",
    "varp=",
    "sigma=",
    "maxiter=",
    "dtau=",
    "mdacc=",
    "mindist=",
    "maxitertp=",
    "tpf=",
    "vrb=",
    "usage",
    "example",
    "help"]

    try:
        optlist, args = getopt.getopt(sys.argv[1:], "", long_opts)

    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        print_usage()
        sys.exit(2)

    for opt, val in optlist:
        if opt == "--usage":
            print_usage()
            sys.exit()
        elif opt == "--example":
            print_example()
            sys.exit()
        elif opt == "--help":
            print_help()
            sys.exit()
        elif opt == "--tpath":
            tpath = val
        elif opt == "--fbase":
            fbase = val.split('.base')[0];
        elif opt == "--fdata":
            fdata = val.split('.dat')[0];
        elif opt == "--fstmod":
            fstmod = val.split('.cmod')[0];
        elif opt == "--frestore":
            frestore = val
        elif opt == "--ftag":
            ftag = val
        elif opt == "--nxz":
            nxp = val.split('x')[0];
            nzp = val.split('x')[1];
        elif opt == "--npop":
            npop = val
        elif opt == "--varp":
             varpint = val.split('.')[0]
             varpfrc = val.split('.')[1]
        elif opt == "--sigma":
            sigma = val
        elif opt == "--maxiter":
            maxiter = val
        elif opt == "--dtau":
            dtau = val
        elif opt == "--mdacc":
            mdacc = val
        elif opt == "--mindist":
            mindist = val
        elif opt == "--maxitertp":
            maxitertp = val
        elif opt == "--vrb":
            vrb = val
        elif opt == "--tpf":
            tpf = val
        elif opt == "--tee":
            tee = val
        else:
            print "\n  ERROR: Unrecognised argument,",opt
            print ""
            sys.exit(-1)

    # -------------------------------------------------------------------
    # Check for any errors that might occur
    # -------------------------------------------------------------------

    if npop == 0:
        print "\n ERROR: npop not set"
        print "          Set npop using the --npop directive\n"
        sys.exit(-1)

    if not isinstance(fstmod,types.NoneType) and not isinstance(frestore,types.NoneType):
        print "\n ERROR: Cannot have both --fstmod and --frestore directives"
        print "          Set only one of them\n"
        sys.exit(-1)

    if not isinstance(frestore,types.NoneType) and isinstance(ftag,types.NoneType):
        print "\n ERROR: The --frestore directive requires --ftag"
        print "          Set ftag using the --ftag directive\n"
        sys.exit(-1)

    # ---------------------------------------------------------------
    # SET DEFAULT VALUES IF DIRECTIVES NOT SET
    # ---------------------------------------------------------------

    print "\nSETTING DEFAULT ARGUMENTS ...\n"

    if isinstance(fbase,types.NoneType):
        basefile_count = 0
        file_name = ""
        for root, subFolders, files in os.walk(os.getcwd()):
            for file in files:
                fbase = file
                if file.find('.base') != -1 :
                    file_name = file
                    basefile_count = basefile_count + 1

    if basefile_count > 1:
        print "\nERROR: %s base files exist" %(basefile_count)
        print "         Set the base file using the --fbase directive\n"
        sys.exit(-1)

    fbase = file_name.split('.base')[0];

# -------------------------------------------------------------------

    if isinstance(fdata,types.NoneType):
        basefile_count = 0
        file_name = ""
        for root, subFolders, files in os.walk(os.getcwd()):
            for file in files:
                fdata = file
                if file.find('.dat') != -1 :
                    file_name = file
                    basefile_count = basefile_count + 1

    if basefile_count > 1:
      print "\nERROR: %s data files exist"%(basefile_count)
      print "       Set the data file using the --fdata directive\n"
      sys.exit(-1)

    fbase = file_name.split('.')[0];

    # ---------------------------------------------------------------

    if (isinstance(tpath,types.NoneType)):
        tpath = "/nethome/chrisd/"

    if (isinstance(sigma,types.NoneType)):
        sigma = 1.0

    if (isinstance(maxiter,types.NoneType)):
        maxiter = 200

    if (isinstance(dtau,types.NoneType)):
        dtau = 0.03

    if (isinstance(mdacc,types.NoneType)):
        mdacc = 0.3

    if (isinstance(mindist,types.NoneType)):
        mindist = 0.05

    if (isinstance(maxitertp,types.NoneType)):
        maxitertp = 25

    if (isinstance(vrb,types.NoneType)):
        vrb = "medium"

    if (isinstance(nlay,types.NoneType)):
        nlay = 1

    if (isinstance(param,types.NoneType)):
        param = "P"

    if (isinstance(intp,types.NoneType)):
        intp = "LIN"

    if (isinstance(std,types.NoneType)):
        std = "on"

    if (isinstance(tol,types.NoneType)):
        tol = 0.0

# -------------------------------------------------------------------
# PRINT COMMAND LINE ARGUMENTS
# -------------------------------------------------------------------

    print "\nCOMMAND LINE ARGUMENTS:\n"

    if (not isinstance(tpath,types.NoneType)):
        print " -tpath = %s\n"%(tpath)

    if (not isinstance(fbase,types.NoneType)):
        print " -fbase = %s\n"%(fbase)

    if (not isinstance(fdata,types.NoneType)):
        print " -fdata = %s\n"%(fdata)

    if (not isinstance(fstmod,types.NoneType)):
        print " -fstmod = %s\n"%(fstmod)

    if (not isinstance(frestore,types.NoneType)):
        print " -frestore = %s\n"%(frestore)

    if (not isinstance(ftag,types.NoneType)):
        print " -ftag = %s\n"%(ftag)

    if (not isinstance(nxz,types.NoneType)):
        print " -nxz = %s\n"%(nxz)

    if (not isinstance(npop,types.NoneType)):
        print " -npop = %s\n"%(npop)

    if (not isinstance(varp,types.NoneType)):
        print " -varp = %s\n"%(varp)

    if (not isinstance(sigma,types.NoneType)):
        print " -sigma = %s\n"%(sigma)

    if (not isinstance(maxiter,types.NoneType)):
        print " -maxiter = %s\n"%(maxiter)

    if (not isinstance(dtau,types.NoneType)):
        print " -dtau = %s\n"%(dtau)

    if (not isinstance(mdacc,types.NoneType)):
        print " -mdacc = %s\n"%(mdacc)

    if (not isinstance(mindist,types.NoneType)):
        print " -mindist = %s\n"%(mindist)

    if (not isinstance(maxitertp,types.NoneType)):
        print " -maxitertp = %s\n"%(maxitertp)

    if (not isinstance(vrb,types.NoneType)):
        print " -vrb = %s\n"%(vrb)

# -------------------------------------------------------------------
# PRINT DEFAULTED VALUES
# -------------------------------------------------------------------

    print "\nDEFAULTED ARGUMENTS:\n"

    if (isinstance(fbase,types.NoneType)):
        print " -fbase = %s\n"%(fbase)

    if (isinstance(fdata,types.NoneType)):
        print " -fdata = %s\n"%(fdata)

    if (isinstance(sigma,types.NoneType)):
        print " -sigma = %s\n"%(sigma)

    if (isinstance(maxiter,types.NoneType)):
        print " -maxiter = %s\n"%(maxiter)

    if (isinstance(vrb,types.NoneType)):
        print " -vrb = %s\n"%(vrb)

    if (isinstance(dtau,types.NoneType)):
        print " -dtau = %s\n"%(dtau)

    if (isinstance(mdacc,types.NoneType)):
        print " -mdacc = %s\n"%(mdacc)

    if (isinstance(mindist,types.NoneType)):
        print " -mindist = %s\n"%(mindist)

    if (isinstance(maxitertp,types.NoneType)):
        print " -maxitertp = %s\n"%(maxitertp)

    if (isinstance(nlay,types.NoneType)):
        print " -nlay = %s\n"%(nlay)

    if (isinstance(param,types.NoneType)):
        print " -param = %s\n"%(param)

    if (isinstance(intp,types.NoneType)):
        print " -intp = %s\n"%(intp)

    if (isinstance(std,types.NoneType)):
        print " -std = %s\n"%(std)

    if (isinstance(tol,types.NoneType)):
        print " -tol = %s\n"%(tol)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Array not printing values if used in a loop

Hello! I'm making an English to Morse Code translator and I was able to mostly get it all working by looking through older posts here; however, I have one small problem. When I run it it's just printing spaces for where the characters should be. It runs the right amount of times, and if I try... (3 Replies)
Discussion started by: arcoleman10
3 Replies

2. Shell Programming and Scripting

Printing null values in awk

Hi, I have a csv file with given details abc.txt 123,ra,point,,there 232,ba,points,home,pheer I want to get those values and store them in different variables: Code: while read line do echo $line |awk -F"," '{print $1" "$2" "$3" "$4" "$5"}'|read dbt_acct val_dt crncy AMT... (11 Replies)
Discussion started by: rahulsk
11 Replies

3. Shell Programming and Scripting

Printing $values using awk

Hi All I had requirement where I need to re-order columns in a file by using a control file. here is the ctrl file c1 c2 c3 source file c3 | c1 | c2 a | b| c I should create output file based on the ctrl file columns o/p should look like this c1 | c2 | c3 b| c|a I wrote some... (9 Replies)
Discussion started by: gvkumar25
9 Replies

4. UNIX for Dummies Questions & Answers

Printing the intermediate integer values

Dear Help, I have an input file which looks like - 121 300 122 345 124 567 127 234 $1 has 125 and 126 missing. How can I output those missing values? Thanks (6 Replies)
Discussion started by: Indra2011
6 Replies

5. Programming

Printing values from a class

I have a class and want to print values in MOD using L = new Layer* ; How can I print the values in MOD using this object L??? class Layer { public : Model* MODP; Model* MODS; (1 Reply)
Discussion started by: kristinu
1 Replies

6. Shell Programming and Scripting

Shell : eliminating zero values and printing

I have a log file containing the below data and should have the output file as below. and the output file should not contain any 0 values. Eg. It should not contain 0000000:0000000 in it. input.txt Media200.5.5.1 00010003:065D1202 Media100.5.5.2 7,588,666,067,931,543... (6 Replies)
Discussion started by: scriptscript
6 Replies

7. UNIX for Dummies Questions & Answers

Printing all the values in the middle of two columns

Hi, I have a tab delimited text file with three columns: Input: 1 25734 25737 1 32719 32724 1 59339 59342 1 59512 59513 1 621740 621745 For each row of the text file I want to print out all the values between the second and third columns, including them. The... (3 Replies)
Discussion started by: evelibertine
3 Replies

8. Programming

Printing float values in C

Hi, I have small problem to print float value in the fallowing code float Cx, W,f=250000, Cr=92.00,pi=3.14; W=2*pi*f; Cx = 1/W.Cr; //Cx value will be come around like 7.07E-9. printf("capacitance value: %.10f",Cx); I am trying to print Cx value using above code but it was not... (3 Replies)
Discussion started by: veerubiji
3 Replies

9. Shell Programming and Scripting

printing two values with TAB in between

Dear friends, I want to print variables' values in certain format where space between two values of variables is "a tab" I tried where I provided "tab" between two varibales. But when it print values on screen its giving me output without spaces in two values. Request you to help me in... (7 Replies)
Discussion started by: anushree.a
7 Replies

10. Programming

printing all array values using dbx 7.2.1

how do we print the entire contents of arrays in dbx ? Ive tried using print x to print values 1 to 5 of the array x, however dbx complains and doesnt allow this help is much appreciated (1 Reply)
Discussion started by: JamesGoh
1 Replies
Login or Register to Ask a Question