473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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 te**@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
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()

Apr 13 '06 #1
17 1958
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 te**@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.


I think you could just use getopt or optparse, passing in each line
after doing a simple .split() on it. The resulting handling of the
arguments would be much simpler and cleaner than what you have.

That won't work perfectly well if you can have quoted arguments with
spaces in them, however, but the example above does not.

-Peter

Apr 13 '06 #2
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 te**@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
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()


I hope no typos:

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 enumerate(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" % (settings[opt_map(flag)], option)
afile.close()

James

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

http://www.jamesstroud.com/
Apr 13 '06 #3
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 te**@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
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/
Apr 13 '06 #4
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 te**@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, ...
Apr 13 '06 #5
John Machin wrote:
On 13/04/2006 11:23 AM, James Stroud wrote:

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.


Who said anything about attribute names? I did it this way to show the
OP that not every value has to be "select variables which would
eventually be included in a class object". Using a dict would be far
more attractive than a bunch of "setattr" & "getattr"s later.
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.


Who said the "-" was optional?
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


Typos.
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, ...


Peter's post was up when I responded, but it does not give the OP any
ideas about how to write better python code. Note the 4 page if...elif
construct. optparse might be logical for vet pythonistas, but if someone
is writing code like to OP, it might be better to give him some coding
hints at the expense of sophistication.

James

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

http://www.jamesstroud.com/
Apr 13 '06 #6
On 13/04/2006 12:26 PM, James Stroud wrote:
John Machin wrote:
On 13/04/2006 11:23 AM, James Stroud wrote:
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.


Who said anything about attribute names? I did it this way to show the
OP that not every value has to be "select variables which would
eventually be included in a class object".


Sorry, I don't understand that sentence.
Using a dict would be far
more attractive than a bunch of "setattr" & "getattr"s later.
Who said anything about a bunch of *attrs later?

Instead of
settings[opt_map[flag]] = option
you'd have
setattr(anobj, opt_map[flag], option)
with the __init__ for the class setting up the default values.
And no getattrs, you'd use the names like qmgr and port as per the OP:
anobj.qmgr, anobj.port, etc ...
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.


Who said the "-" was optional?


Nobody. Who said the user data was going to be syntactically correct?
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


Typos.
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, ...


Peter's post was up when I responded, but it does not give the OP any
ideas about how to write better python code.


Yes it does. It gives the OP the idea that if there's a Python standard
module that does the job, the best Python code that the OP could write
would be calls to that module.
Note the 4 page if...elif
construct.
I did. I read *all* of it.
optparse might be logical for vet pythonistas, but if someone
is writing code like to OP, it might be better to give him some coding
hints at the expense of sophistication.


The "sophistication" of optparse appears to match what the OP appears to
want to do; IMO preferable to the sophistry of something like

flags, options = splitline[::2], splitline[1::2]

which is sure to cause the OP some headscratching and needs to be thrown
away immediately he starts to consider (a) variable number of parameters
(b) error detection.
Apr 13 '06 #7
Peter Hansen wrote:
I think you could just use getopt or optparse, passing in each line
after doing a simple .split() on it.**The*resulting*handling*of*the
arguments would be much simpler and cleaner than what you have.

That won't work perfectly well if you can have quoted arguments with
spaces in them, however, but the example above does not.


shlex.split() may help with the splitting, e. g.:
shlex.split("alpha beta --gamma 'delta epsilon' zeta\\ eta\n")

['alpha', 'beta', '--gamma', 'delta epsilon', 'zeta eta']

Peter
Apr 13 '06 #8
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 te**@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.


I suggest you use getopt or optpase to perform this task
those modules are designed to perform what you want, with error checking

import getopt
inp = open("const.txt","r")
for line in inp:
try:
opt, args = getopt.getopt(iline.split(), 'c:k:m:o:p:s:t:i')
print opt
except getopt.GetoptError, msg:
# handle the error as you need

Eric
--
Je voudrais savoir s'il existe un compteur de vitesse (?) pour savoir
à quelle vitesse on est réellement connecté au FAI et surtout si une
telle bête existe... où la trouver.
-+- RJ in: Guide du Neuneu d'Usenet - Baisse la tête et pédale -+-
Apr 13 '06 #9
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 te**@email.com


Have you looked at optparse ?

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 13 '06 #10
bruno at modulix 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 te**@email.com


Have you looked at optparse ?

I have.

In the interactive version of the code, I use:

#
# Parse command line options and automatically build help/usage
#
parser = OptionParser()

parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=1,
help="don't print status messages to stdout")
parser.add_option("-m", dest="qmanager",
help="Queue Manager to inquire against")
parser.add_option("-s", dest="host",
help="Host the que manager resides on")
parser.add_option("-p", dest="port",
help="Port queue manager listens on"),
parser.add_option("-o", dest="object",
help="Queue object being inquired on"),
parser.add_option("-k", dest="key",
help="object attribute to be inquired about"),
parser.add_option("-t", type="string",dest="mto",
help="e-mail address the report will go to"),
parser.add_option("-d", action="store_false",dest="report",
help="optional switch - enabling this sends output
to e-mail")
(options, args) = parser.parse_args()
The module optparse seemed to be aimed at reading from commandline
versus pulling attributes from a read line.

Apr 13 '06 #11
News wrote:
bruno at modulix wrote:

Have you looked at optparse ?

I have.

The module optparse seemed to be aimed at reading from commandline
versus pulling attributes from a read line.

hummmm ....

lets see
optparse seems to be reading from commandline. that's not false but not
really true ;-)

point 1:
remember that commandline reading is done thru sys.argv wich provide a a
list of strings

point 2
now consider pulling datas from readline, wich return a string.
if you split this string you'll have a list of string.

point 3
optparse is designed for reading from a list of strings

I let the conclusion to your imagination ;-))
Eric
--
Salut,Je m'appele sed.je suis etudiant en communication, j'ai lu votre
message.je viens vous dire un petiit bonjour,et vous laisser mon
mél: vous pouvez me repondre maintenant si vous étez conecter.
-+-Guide du Neuneu d'Usenet - La com', elle ne passera pas par moi -+-
Apr 13 '06 #12
News wrote:
bruno at modulix 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 te**@email.com
Have you looked at optparse ?


I have.

In the interactive version of the code, I use:

#
# Parse command line options and automatically build help/usage
#
parser = OptionParser()

parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=1,
help="don't print status messages to stdout")
parser.add_option("-m", dest="qmanager",
help="Queue Manager to inquire against")
parser.add_option("-s", dest="host",
help="Host the que manager resides on")
parser.add_option("-p", dest="port",
help="Port queue manager listens on"),
parser.add_option("-o", dest="object",
help="Queue object being inquired on"),
parser.add_option("-k", dest="key",
help="object attribute to be inquired about"),
parser.add_option("-t", type="string",dest="mto",
help="e-mail address the report will go to"),
parser.add_option("-d", action="store_false",dest="report",
help="optional switch - enabling this sends output
to e-mail")
(options, args) = parser.parse_args()


So why do you inflict yourself the pain of rewriting all the parsing etc???
The module optparse seemed to be aimed at reading from commandline
versus pulling attributes from a read line.


http://www.python.org/doc/2.4.2/lib/...rguments.html:
"""
The whole point of creating and populating an OptionParser is to call
its parse_args() method:

(options, args) = parser.parse_args(args=None, options=None)

where the input parameters are

args
the list of arguments to process (sys.argv[1:] by default)
"""

what about something like :

line = myfile.readline()
options = parser.parse_args(line.split())

But what, if you prefer to rewrite (and maintain) a custom parser doing
exactly the same thing, please do !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 13 '06 #13
bruno at modulix wrote:
News wrote:
bruno at modulix 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 te**@email.com
Have you looked at optparse ?

I have.

In the interactive version of the code, I use:

#
# Parse command line options and automatically build help/usage
#
parser = OptionParser()

parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=1,
help="don't print status messages to stdout")
parser.add_option("-m", dest="qmanager",
help="Queue Manager to inquire against")
parser.add_option("-s", dest="host",
help="Host the que manager resides on")
parser.add_option("-p", dest="port",
help="Port queue manager listens on"),
parser.add_option("-o", dest="object",
help="Queue object being inquired on"),
parser.add_option("-k", dest="key",
help="object attribute to be inquired about"),
parser.add_option("-t", type="string",dest="mto",
help="e-mail address the report will go to"),
parser.add_option("-d", action="store_false",dest="report",
help="optional switch - enabling this sends output
to e-mail")
(options, args) = parser.parse_args()


So why do you inflict yourself the pain of rewriting all the parsing etc???
The module optparse seemed to be aimed at reading from commandline
versus pulling attributes from a read line.


http://www.python.org/doc/2.4.2/lib/...rguments.html:
"""
The whole point of creating and populating an OptionParser is to call
its parse_args() method:

(options, args) = parser.parse_args(args=None, options=None)

where the input parameters are

args
the list of arguments to process (sys.argv[1:] by default)
"""

what about something like :

line = myfile.readline()
options = parser.parse_args(line.split())

But what, if you prefer to rewrite (and maintain) a custom parser doing
exactly the same thing, please do !-)


sometimes you can't see the forest .. trees and all that :)

I really appreciate everyones insight on this.

It was very helpful.

OT: I saw several references to "OP". What does this mean?
Apr 13 '06 #14
News wrote:
OT: I saw several references to "OP". What does this mean?


It's a TOOTKA :-)

You - The Original Post/Poster.

Gerard

Apr 13 '06 #15
Hi everyone,

Just to be complete, my final solution was:

parser = OptionParser()

parser.add_option("-m","--qmanager", dest="qmanager",
help="\t\tQueue Manager to inquire against"),

parser.add_option("-s","--server", dest="host",
help="\t\tHost the que manager resides on"),

parser.add_option("-c","--check", dest="check",
help="\t\tTest object if it is less/equal/greater"),

parser.add_option("-p","--port", type="string", dest="port",
help="\t\tPort queue manager listens on"),

parser.add_option("-o","--object", dest="object",
help="\t\tQueue object being inquired on"),

parser.add_option("-k", "--key",dest="key",
help="\t\tobject attribute to be inquired about"),

parser.add_option("-t","--to", type="string",dest="mto",
help="\t\te-mail address the report will go to"),

parser.add_option("-q","--quiet", action="store_false",dest="quiet",
help="\t\toptional - just returns the value"),

parser.add_option("-f", "--file",
action="store", type="string", dest="filename",
help="Pull command strings from FILE",
metavar="FILE")

parser.add_option("-d","--display",
action="store_false",dest="report",
help="\t\toptional - use sends output to e-mail")

(options, args) = parser.parse_args()

if options.filename is not None:
line = use_file()
(options,args) = parser.parse_args(line.split())
Apr 13 '06 #16
News wrote:
(snip)
sometimes you can't see the forest .. trees and all that :)
Yes, been here too... I wrote a CSV parser before realising there was a
pretty good one in the standard lib :(

(snip) OT: I saw several references to "OP". What does this mean?


Original Poster

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 13 '06 #17
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 te**@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
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()


Others have addressed your specific question. I'm going to make
a different suggestion. Change your thinking so that you can use
ConfigParser to get your values from a .INI/.CONF file instead.
The file would look something like:

[section001]
m=queuemanager
server=myserver
port=1080
key=somekey
object=someobject
c=20
emailtolist=te**@email.com

[section002]
..
.. If you have many of these just put things in a loop
.. and process each section
..

You can then read with (not tested):

import ConfigParser
inifilename='program.ini'
INI=ConfigParser.ConfigParser()
INI.read(inifilename)
section='section001'
option='m'
try: m=INI.get(section, option)
except: m=None
option='server'
try: server=INI.get(section, option)
except: server=None
option='port'
try: port=INI.getint(section, option)
except: port=None # Or set to default port
option='key'
try: key=INI.get(section, option)
except: key=None
option='object'
try: object=INI.get(section, option)
except: object=None
option='c'
try: c=INI.getint(section, option)
except: c=None
option='emailtolist'
try: emailtolist=INI.get(section, option).split(';')
except: emailtolist=None
Just a suggestion.

-Larry Bates
Apr 14 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Adam Parkin | last post by:
Hi all, what I want to do is write a function to toggle the enabled field of all controls within a frame, but what I can't figure out is how does one iterate through all controls in a frame object?...
2
by: Hans Deragon | last post by:
Greetings. I am performing: commands.getstatusoutput("rsync <params>"); rsync takes a long time and prints out a steady stream of lines showing which file it is currently working on.
0
by: Rolf Hemmerling | last post by:
Hello ! How to let GPP/GCC read command line switches from a file, **for Windows** ? I was told a Linux solution: "Your options in options.txt Then: gcc $(cat options.txt) or: gcc `cat...
7
by: Dave Hansen | last post by:
OK, first, I don't often have the time to read this group, so apologies if this is a FAQ, though I couldn't find anything at python.org. Second, this isn't my code. I wouldn't do this. But a...
2
by: Smile_7 | last post by:
Hello: I am using some 3rd party assemblies that require a license file. If I create a new project inside visual studios and compile the application, I have no problems but my project requires...
9
by: David Helgason | last post by:
I'm calling one stored procedure with a prepared statement on the server with 6 arrays of around 1200 elements each as parameters. The parameters are around 220K in total. This is taking a...
34
by: electrician | last post by:
Perl has it, Basic has it, Fortran has it. What is so difficult about creating a goto command for JavaScript. Just set up a label and say go to it.
8
by: 4.spam | last post by:
Hello. WinXP, db2 v8.2.7 When I use UPDATE COMMAND OPTION USING <any_switchON(or OFF) it seems that this command has absolutely no effect on these switches since command LIST COMMAND...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.