Iterating command switches from a data file - have a working solution but it seems inefficient

John Machin sjmachin at lexicon.net
Wed Apr 12 22:09:55 EDT 2006


On 13/04/2006 11:23 AM, James Stroud wrote:
> 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 test at email.com
>> 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

Which version of Python is the OP using??

>> 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;
[snip]
>>         elif split_line[count] == "-c":
>>             check=split_line[count+1]   
>>             print "Check",check;
>>         elif split_line[count] == "-d":
>>             report=""

Uh-oh ... the plot just changed. Looks like -d and -q are NOT meant to 
be followed by an operand.


>>             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}
> 

'Queue Manager' is not suitable for later use as an attribute name.

> 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

Actually that's getting rid of the first character irrespective of 
whether it's "-" or not -- except when there's only one character in 
which case it will die in a hole.


>   for flag, option in zip(flags, options):
>     settings[opt_map(flag)] = option
>     print "%s = %s" % (opt_map(flag), option)

opt_map is a dict; should be opt_map[flag] in above two lines

> 
> afile.close()
> 

Like Peter said, use optparse -- it handles the no-argument flags, has 
error detection, defaults, can be told the type of the flag, already 
returns the goodies as attributes of an object, ...



More information about the Python-list mailing list