473,383 Members | 1,762 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,383 software developers and data experts.

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 15006
"Alan Isaac" <ai****@american.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(sys.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****@american.eduescribió en el mensaje
news:wWQuh.1259$SE6.1215@trnddc03...
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
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...
16
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...
5
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...
6
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...
8
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
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
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...
4
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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 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.