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: -
import sys
-
-
sys.argv[1,2,3,4]
-
-
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
4 2154 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: -
import sys
-
-
sys.argv[1,2,3,4]
-
-
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 - ...function definition....
-
-
if __name__ == '__main__':
-
import sys
-
find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
-
At the python command line: - from find_root import find_root
-
find_root('x**2-4', 3, 0, 0.0001)
Is there another way?
This works at the windows command prompt:
C:\>python find_root.py x**2-4 3 0 0.0001 - ...function definition....
-
-
if __name__ == '__main__':
-
import sys
-
find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
-
At the python command line: - from find_root import find_root
-
find_root('x**2-4', 3, 0, 0.0001)
Is there another way?
- ...function definition....
-
-
if __name__ == '__main__':
-
import sys
-
find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
-
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: - ...function definition....
-
-
if __name__ == '__main__':
-
import sys
-
try:
-
find_root(sys.argv[1], float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4]))
-
except (IndexError, TypeError):
-
print "Usage: Too few arguments"
-
It's a little nicer than showing a python traceback.
Thank you! Good information.
TMS
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: - def find_root(f, xLo, xHi, eps=0.00001):
-
..................
-
..................
Then the function call could be: - find_root('5*x**3+2*x**2-x+1', -3, 4)
-
# if you need a value for eps different from the default
-
find_root('5*x**3+2*x**2-x+1', -3, 4, .001)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Hari |
last post by:
can i have command line arguments in VS.NET applicatio?
if yes how?
Can i have some code snippets of the above functionality?
I know we can acjieve this in console application form
command...
|
by: Jon Hewer |
last post by:
hi
i am writing a little script and currently implementing command line
arguments following the guide by mark pilgrim from dive into python;
...
|
by: Steve M |
last post by:
I'm trying to invoke a Java command-line program from my Python program
on Windows XP. I cannot get the paths in one of the arguments to work
right.
The instructions for the program describe the...
|
by: SunRise |
last post by:
Hi
I am creating a C Program , to extract
only-Printable-characters from a file ( any type of file) and display
them.
OS: Windows-XP
Ple help me to fix the Errors & Warnings and explain...
|
by: amirmira |
last post by:
I would like to set command line arguments to a service at install time. I
need to do this because I need to get information from different registry
locations depending on my command line argument....
|
by: Rune Jacobsen |
last post by:
Hi,
I've been trying to figure this one out, but my experience just doesn't
have what it takes... :|
I am writing an application that reads an XML file and displays the
contents in various...
|
by: Roland |
last post by:
Hi,
I am developing a C++ project and want to pass some command line
arguments in VS .NET 2003. I am in debug mode, the configuration is set
to Debug and I entered my argument list in Project ->...
|
by: raphfrk |
last post by:
I have a program which reads in 3 filenames from the command line
prog filename1 filename2 filename3
However, it doesn't work when one of the filenames has spaces in it
(due to a directory...
|
by: Milan |
last post by:
Hi,
Please guide me how to set command line argument and
how to retrive command line argument.
Senario: vb.net application should be able to execute from command
prompt by passing login and...
|
by: Jwe |
last post by:
Hi,
I've written a program which has both a command line interface
and Windows form interface, however it isn't quite working
correctly.
When run from command line with no arguments it should...
|
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
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |