News wrote:
> Hi everyone,
> 
> My goal is to pull command switches/options from a file and then assign
> the values to select variables which would eventually be included in a
> class object.
> 
> The data file looks something like this but the switches could be in any
> order and not all may be used.
> 
> -m quemanager -s server -p port -k key -o object -c 20 -t [EMAIL PROTECTED]
> 
> Also, please keep in mind that the source code will have more than one
> line in it and each has to be treaded separately.
> 
> 
> In a first pass, I wrote the following code which works but there is
> probably a better way of doing it.
> 
> Any ideas to make it more efficient/stream-lined would be greatly
> appreciated.
> 
> #!/usr/bin/python
> 
> import string
> inp = open("const.txt","r")
> #
> # Read File
> #
> 
> while True:
>       
>         #
>         # Get line from file
>         #
>       line=inp.readline()
> 
>         #
>         # Check for EOF or break line up to extract relevant pieces
>         #
>         if len(line) == 0:
>               break
>       else:
>               split_line=line.split()
>               length=len(split_line)
>               count=0
>       
>         #
>         # Evaluate list item and assign variable based on its contents
>         # Print statements are for debugging purposes only
>         #
>       for i in range(length):
>               if split_line[count] == "-m":
>                       qmgr=split_line[count+1]
>                       print "Queue Manager",qmgr;
>               elif split_line[count] == "-s":
>                       server=split_line[count+1]
>                       print "Server",server;
>               elif split_line[count] == "-p":
>                       port=split_line[count+1]
>                       print "Port",port;
>               elif split_line[count] == "-o":
>                       object=split_line[count+1]
>                       print "Object",object;
>               elif split_line[count] == "-k":
>                       key=split_line[count+1]
>                       print "Key",key;
>               elif split_line[count] == "-t":
>                       mto=split_line[count+1]
>                       print "To",mto; 
>               elif split_line[count] == "-c":
>                       check=split_line[count+1]       
>                       print "Check",check;
>               elif split_line[count] == "-d":
>                       report=""
>                       print "Report",report;
>               elif split_line[count] == "-q":
>                       display=False
>                       print "Display",display;
>               else:
>                       continue
>                       
>               count=count+1
> 
> # Close input file
> #
> inp.close()
> 

Anyone who got the previous message, there was a typo. This looks better

opt_map = {'m': 'Queue Manager',  's': 'Server',  'p': 'Port',
            'o': 'Object',         'k': 'Key',     't': 'To',
            'c': 'Check',          'd': 'Report',  'q': 'Display}

afile = open("filename")

settings = {}
for aline in afile:
   splitline = aline.split()
   flags, options = splitline[::2], splitline[1::2]
   flags = [f[1] for f in flags]  # get rid of pesky "-"s
   for flag, option in zip(flags, options):
     settings[opt_map(flag)] = option
     print "%s = %s" % (opt_map(flag), option)

afile.close()



-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to