473,405 Members | 2,287 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,405 software developers and data experts.

Call script which accepts com. line par. from another script anderror control

Hi,

I would really appreciate help on this. I have a script (s1.py) which I
would like to call from another script (s2.py). No problem there.

The first issue is that s1.py has a command line parser which I would like
to keep but instead of fetching the information from the command line, I
would like to direct the parser to get the information from a variable so i
can call sp1.py from sp2.py and give it an expression to parse that normally
would go on the command line. To make things clearer:

s1.py (currently) --------------------------------------------

def ...
def ...

cmdl_parser = optparse.OptionParser..
cmdl_parser.add_option..

(cmdl_opts, cmdl_args) = cmdl_parser.parse_args()

-----------------------------------------------------------------

sp1.py (the one I would like)
---------------------------------------------------------------------------------------------

def ...
def ...

def MAIN(expression2parse) <----- add a main so can call
from other script
cmdl_parser = optparse.OptionParser..
cmdl_parser.add_option..

(cmdl_opts, cmdl_args) = cmdl_parser.parse_args()
<---------------------------do this but on "expression2parse"

-----------------------------------------------------------------------------------------------------------------------------------
The second issue is error control. In case of an error in sp1.py I want the
error handling to happen at the level of sp2.py so I can better manage
things. Does anyone know how this could be done. Essentially return control
to sp2.py?

I am new to Python (5 hours) but have extensive programming in C++. I would
really appreciate your input.

Thanks!

Kakeez

__________________________________________________ _______________
Fight Allergies With Live Search
http://search.live.com/results.aspx?...ca&FORM=SERNEP

May 24 '07 #1
1 1213
Karim Ali wrote:
def MAIN(expression2parse) <----- add a main so can
call from other script
Of course you don't mean you want another python interpreter to fire up
and run the other script?

Here is an example of the way to do what you are suggesting:

# mod1.py

def doit(param):
if not param % 37:
raise ValueError, 'Why u want to do dat?'
else:
print 'All right!'
return 'Value is: %s' % param

# end of mod1.py
# mod2.py

import mod1

print mod1.doit(20)
print
print mod1.doit(30)
print
try:
print mod1.doit(37)
except ValueError:
print 'Gracefully handling 37....'
print
print mod1.doit(37)

# end of mod2
James
May 24 '07 #2

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

Similar topics

1
by: sam | last post by:
I have a function writen in Perl that takes a name-value list (array) as argument and returns a name-value list (array). My question is: How to call this function from PHP and get the returned...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
10
by: bienwell | last post by:
Hi, I have a question about file included in ASP.NET. I have a file that includes all the Sub functions (e.g FileFunct.vb). One of the functions in this file is : Sub TestFunct(ByVal...
7
by: J-T | last post by:
I have a user control called "Test1" with two button on it ,one is input and the other one is a server control as follow: <input type="button"...
1
by: drewmania001 | last post by:
i've read various info on the web including the following http://php.mirrors.ilisys.com.au/manual/en/ref.mysqli.php mySQL Version 4.1.16 PHP Version 5.1.2 with Zend Engine v2.1.0 OS Windows...
13
by: wattersmt | last post by:
Hello, I am trying to write a python cgi that calls a script over ssh, the problem is the script takes a very long time to execute so Apache makes the CGI time out and I never see any output. ...
3
by: Angus | last post by:
I have a web page with a toolbar containing a Save button. The Save button can change contextually to be a Search button in some cases. Hence the button name searchsavechanges. The snippet of...
3
by: computer_guy | last post by:
Hi Everyone, I run into a problem. I am trying to write an aspx that can dynamically generate an image based on some input parameters. Things are very simple if the size of the parameters is...
5
by: inetquestion | last post by:
I am looking for a web interface for shell commands or shell scripts. Does anyone know of any exexisting php scripts which would solve this requirement? PHP form accepts input from a user, then...
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: 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?
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
agi2029
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 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.