471,123 Members | 916 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

command line arguments

TMS
119 100+
Let's say I want to call one of my modules from the command line, for instance I have this find_root(f,xLo, xHi, eps) assignment I've been working on.

I have the header that looks like this:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. sys.argv[1,2,3,4]
  4.  
  5.  
Wouldn't that be enough that when I go to a terminal screen (Ubuntu) or console (windows) I could simply type:

find_root.py find_root('x**2 - 2, 0, 3, .0001) and it would work? Assuming that my arguments and the function itself is correct. I'm only looking at the command line arguments right now.

Thank you
Jan 24 '07 #1
4 1954
bvdet
2,851 Expert Mod 2GB
Let's say I want to call one of my modules from the command line, for instance I have this find_root(f,xLo, xHi, eps) assignment I've been working on.

I have the header that looks like this:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. sys.argv[1,2,3,4]
  4.  
  5.  
Wouldn't that be enough that when I go to a terminal screen (Ubuntu) or console (windows) I could simply type:

find_root.py find_root('x**2 - 2, 0, 3, .0001) and it would work? Assuming that my arguments and the function itself is correct. I'm only looking at the command line arguments right now.

Thank you
This works at the windows command prompt:

C:\>python find_root.py x**2-4 3 0 0.0001

Expand|Select|Wrap|Line Numbers
  1. ...function definition....
  2.  
  3. if __name__ == '__main__':
  4.     import sys
  5.     find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
  6.  
At the python command line:
Expand|Select|Wrap|Line Numbers
  1. from find_root import find_root
  2. find_root('x**2-4', 3, 0, 0.0001)
Is there another way?
Jan 24 '07 #2
bartonc
6,596 Expert 4TB
This works at the windows command prompt:

C:\>python find_root.py x**2-4 3 0 0.0001

Expand|Select|Wrap|Line Numbers
  1. ...function definition....
  2.  
  3. if __name__ == '__main__':
  4.     import sys
  5.     find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
  6.  
At the python command line:
Expand|Select|Wrap|Line Numbers
  1. from find_root import find_root
  2. find_root('x**2-4', 3, 0, 0.0001)
Is there another way?
Expand|Select|Wrap|Line Numbers
  1. ...function definition....
  2.  
  3. if __name__ == '__main__':
  4.     import sys
  5.     find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
  6.  
is not quite complete because errors will be thrown when too few arguments a sent from the command line or if those arguments can't be converted to float. Most command line scripts will provide help when this happens. Elaborate on this idea:

Expand|Select|Wrap|Line Numbers
  1. ...function definition....
  2.  
  3. if __name__ == '__main__':
  4.     import sys
  5.     try:
  6.         find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
  7.     except (IndexError, TypeError):
  8.         print "Usage: Too few arguments"
  9.  
It's a little nicer than showing a python traceback.
Jan 25 '07 #3
TMS
119 100+
Thank you! Good information.

TMS
Jan 25 '07 #4
bvdet
2,851 Expert Mod 2GB
I have a suggestion for a slight improvement. If you find that you are entering in the same value for 'eps' consistently, add a default to the function argument list:
Expand|Select|Wrap|Line Numbers
  1. def find_root(f, xLo, xHi, eps=0.00001):
  2. ..................
  3. ..................
Then the function call could be:
Expand|Select|Wrap|Line Numbers
  1. find_root('5*x**3+2*x**2-x+1', -3, 4)
  2. # if you need a value for eps different from the default
  3. find_root('5*x**3+2*x**2-x+1', -3, 4, .001)
Jan 25 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Hari | last post: by
6 posts views Thread by Jon Hewer | last post: by
1 post views Thread by Rune Jacobsen | last post: by
4 posts views Thread by Roland | last post: by
40 posts views Thread by raphfrk | last post: by
2 posts views Thread by Milan | last post: by

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.