473,837 Members | 1,535 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 15039
"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
2370
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, 1)
16
4759
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 interpreter within our C++ aplication that we can feed our custom scripts. The interpreter is primitive...
5
4626
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 but not have it kill the python interpreter when I run it interactively? I suspect I may need to...
6
5259
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
4410
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
1408
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
1075
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 = "/forum-replies.cfm?t="+threadNum+"&p="+pageNum +"#r"+anchorNum to:
4
34314
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
9683
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10631
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10276
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9409
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7006
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5670
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5851
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4477
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
2
4049
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.