473,587 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stop script w/o exiting interpreter

I'm fairly new to Python and I've lately been running a script at
the interpreter while working on it. Sometimes I only want to
run the first quarter or half etc. What is the "good" way to do this?

Possible ugly hacks include:

- stick an undefined name at the desired stop point
- comment out the last half

I do not like these and assume that I have overlooked the obvious.

Thanks,
Alan Isaac
Jan 25 '07 #1
6 15025
"Alan Isaac" <ai****@america n.eduwrites:
I'm fairly new to Python and I've lately been running a script at
the interpreter while working on it. Sometimes I only want to
run the first quarter or half etc. What is the "good" way to do this?
If it's single threaded then just call sys.exit(). If you don't know
what single threaded means, your program is definitely single threaded.
Jan 25 '07 #2
Hello Alan,
I'm fairly new to Python and I've lately been running a script at
the interpreter while working on it. Sometimes I only want to
run the first quarter or half etc. What is the "good" way to do this?
If you want to exit from the program then "raise SystemExit" is what
you want.
If you want to enter the debugger, you can do:

from pdb import set_trace

....
set_trace() # Stop and execute debugger here.
....

HTH,
--
Miki
http://pythonwise.blogspot.com

Jan 26 '07 #3
Alan Isaac a écrit :
I'm fairly new to Python and I've lately been running a script at
the interpreter while working on it. Sometimes I only want to
run the first quarter or half etc. What is the "good" way to do this?
If the point is to debug your script, then import pdb; pdb.set_trace()
Possible ugly hacks include:

- stick an undefined name at the desired stop point
- comment out the last half

I do not like these and assume that I have overlooked the obvious.
If you have much of your code in functions/classes etc, and the bare
minimum[1] at the top level, then you can launch a Python shell, import
your module, and test functions as you wish...

[1]:

import XXX
import YYY

# lots of functions/classes etc

def main(argv):
# what would have been at the top level

if __name__ == __main__:
import sys
sys.exit(main(s ys.argv))
Jan 26 '07 #4
Alan Isaac wrote:
I'm fairly new to Python and I've lately been running a script at
the interpreter while working on it. Sometimes I only want to
run the first quarter or half etc. What is the "good" way to do this?

Possible ugly hacks include:

- stick an undefined name at the desired stop point
- comment out the last half

I do not like these and assume that I have overlooked the obvious.

Thanks,
Alan Isaac

Alan,

If you are using Windows, you might consider PyScripter.
http://mmm-experts.com/Products.aspx?ProductId=4

Colin W.

Jan 26 '07 #5
Please note that this post has subject
"stop script w/o exiting interpreter".

The object is to work at the *interactive* interpreter,
without leaving it.

Here is an example goal:
start a Python shell,
execfile a script,
exit the script at line 25,
and return to the Python shell.

E.g., some languages include a ``stop`` statement that you can put on line
25.
Ideally, I would like the equivalent of this.

Solutions suggested in this thread included:
- raise SystemExit
but this will exit the interpreter
- sys.exit()
but this will exit the interpreter
- use pdb's set_trace()
but I think that answers a different question.
(However it does work to raise BdbQuit, but I'd like something less
messy.)
- wrap all code in functions and test the functions
but this does not apply to my current use case
- use PyScripter
but this is overkill for my very simple goal

Note that I can just put the undefined name ``stop`` on any line
I want, and the script will stop execucting at that line and will
return to the interactive interpreter, as I wish. It is just that it
returns with an error message, and I'd like to avoid that.

Thanks,
Alan Isaac
Jan 27 '07 #6
"Alan Isaac" <ai****@america n.eduescribió en el mensaje
news:wWQuh.1259 $SE6.1215@trndd c03...
Please note that this post has subject
"stop script w/o exiting interpreter".
Note that I can just put the undefined name ``stop`` on any line
I want, and the script will stop execucting at that line and will
return to the interactive interpreter, as I wish. It is just that it
returns with an error message, and I'd like to avoid that.
If this is just for playing inside the interpreter, just ignore the
exception.
Or comment out all lines from 25 to end of script, some editors (including
IDLE) have support for that.
Or use a giant """
string
""" if you can.
If you invoke your script with `python -i your_script.py` Python will show
the interpreter prompt when your script finishes (either normally or raising
an exception).

--
Gabriel Genellina
Jan 28 '07 #7

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

Similar topics

2
2363
by: yawnmoth | last post by:
i've written a php script to test proxy servers, but when i run it from the command line, it doesn't terminate. i have to hit Ctrl+C to terminate it. so, any ideas as to what i'm doing wrong, and how i could fix it? here's the code: <? $address = 'whatever'; $port = 80; $proxy = fsockopen("tcp://$address", (int) $port, $errno, $errstr,...
16
4736
by: fernandez.dan | last post by:
Hey I'm sorry if this is not the appropriate news group for this question. I was wondering if anyone has any recommendation for embbedding a script engine in a c++ application. I want to feed my C++ application scripts which based on the script would create C++ objects and call the appropriate methods. At the moment I created a simple...
5
4614
by: Russ | last post by:
I wrote a simple little function for exiting with an error message: def error ( message ): print_stack(); exit ("\nERROR: " + message + "\n") It works fine for executing as a script, but when I run it interactively in the python interpreter it kills the interpreter. That's not what I want. Is there a simple way to have a script terminate...
6
5239
by: John (Z R) L | last post by:
Hi all, I am very new to programming, and I chose to study the Python language before C++. I am currently using the Wikibooks "Non-Programmer's Tutorial for Python", and am up to the section "Who goes there"? http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python/Who_Goes_There%3F But after clicking "run module" for " a = 1
8
4398
by: Harati | last post by:
I prepared my own player using php For this ,i want code for play(),pause(),stop(). I tried a lot with player.controls.play() but no use
2
1402
by: mag_dex | last post by:
I have a question. Can you do in JavaScript sth like that: * cmd1 statement_to_stop_script_for_e.g._2seconds, hmm.. stop(2) or sth like that... cmd2 and so on. * Best wishes!
1
1060
by: Yansky | last post by:
I'm having some problems debugging a python script on my server. I'm getting a "500 Internal Server Error". What the script does is take a link and then scrape a page and store some info from the page into a database. It seemed to be working fine until I changed one line of code from: shortenLink =...
4
34289
by: devi thapa | last post by:
Hi, I am executing a python script in a shell script. The python script actually returns a value. So, can I get the return value in a shell script? If yes, then help me out. Regards, Devi
0
7918
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6621
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.